View Javadoc

1   /*
2    * OracleDdlDAO.java
3    *
4    * Created on 16 de Abril de 2005, 09:19
5    */
6   
7   package org.thema.scriptware.dao;
8   
9   import java.sql.CallableStatement;
10  import java.sql.Clob;
11  import java.sql.Connection;
12  import java.sql.ParameterMetaData;
13  import java.sql.PreparedStatement;
14  import java.sql.ResultSet;
15  import java.sql.SQLException;
16  import java.sql.Types;
17  
18  import java.util.Collection;
19  import java.util.LinkedList;
20  
21  
22  /***
23   *
24   * @author Eduardo M . Sasso
25   */
26  public class OracleDdlDAO implements DdlDAO {
27      
28      Connection conn;
29      /*** Creates a new instance of OracleDdlDAO */
30      public OracleDdlDAO(Connection conn) {
31          this.conn = conn;
32      }
33      
34      public String getScript(String object,String type) throws SQLException{
35          String retorno = null;
36          try {
37              CallableStatement cs = conn.prepareCall("begin ? := DBMS_METADATA.GET_DDL(?,?); end;");
38              cs.registerOutParameter(1,Types.CLOB);
39              cs.setString(2, type.toUpperCase());
40              cs.setString(3,object.toUpperCase());
41              cs.executeUpdate();
42              Clob clob = cs.getClob(1);
43              retorno = clob.getSubString(Long.parseLong("1"), Integer.parseInt(Long.toString(clob.length())));
44          } catch(SQLException e) {
45              throw new SQLException(e.getMessage());
46          }
47          
48          return retorno + "/";
49      }
50      public Collection getScriptSQL(String sql) throws SQLException{
51          try{
52              PreparedStatement pstmt = conn.prepareStatement(sql);
53              /*
54              ParameterMetaData metaData = pstmt.getParameterMetaData();
55              for(int i = 1; i <= metaData.getParameterCount(); i++){
56                  System.out.println(metaData.getParameterTypeName(i));
57              }
58              */
59              ResultSet rs =  pstmt.executeQuery();
60              
61              String tipo;
62              String nome;
63              String resultado;
64              LinkedList lista = new LinkedList();
65              while (rs.next()) {
66                  nome = rs.getString(1);
67                  tipo = rs.getString(2);
68                  resultado = getScript(nome,tipo);
69                  lista.add(resultado);
70              }
71              return lista;
72          } catch (SQLException e) {
73              throw new SQLException(e.getMessage());
74          }
75      }
76      
77      
78      public Collection getScript(String object) throws SQLException{
79          /*
80           * TODO tentar retornar um map de chave e valores
81           * onde o tipo seja chave, para futuramente mostrar objetos
82           * por tipo e permitir ordenacao
83           */
84          try {
85              ResultSet rs = getObjectType(object);
86              String tipo;
87              String nome;
88              String resultado;
89              LinkedList lista = new LinkedList();
90              while (rs.next()) {
91                  tipo = rs.getString("object_type");
92                  nome = rs.getString("object_name");
93                  resultado = getScript(nome,tipo);
94                  lista.add(resultado);
95              }
96              return lista;
97          } catch (SQLException e) {
98              throw new SQLException(e.getMessage());
99          }
100     }
101     
102     private ResultSet getObjectType(String object) throws SQLException {
103         /*
104          * TODO - sql deve ser mais dinamica, arquivo de propriedades
105          */
106         String sql = "select object_type,object_name from user_objects where not object_type = 'PACKAGE BODY' and object_name like ?";
107         PreparedStatement pstmt = conn.prepareStatement(sql);
108         pstmt.setString(1, object.toUpperCase());
109         return pstmt.executeQuery();
110         
111     }
112     
113     
114     
115 }