Public Member Functions | |
DatabaseSQLMetaData (Trace logger, Connection connection, int dynamicPrecision, boolean gatherSystemTables, String schemaPattern) | |
final DatabaseSchema | createDatabaseSchema () throws SQLException |
Package Attributes | |
Trace | logger |
Connection | connection |
int | dynamicPrecision |
boolean | gatherSystemTables |
String | schemaPattern |
Definition at line 43 of file DatabaseSQLMetaData.java.
|
Creates a new
Definition at line 59 of file DatabaseSQLMetaData.java. 00061 { 00062 super(); 00063 this.logger = logger; 00064 this.connection = connection; 00065 this.dynamicPrecision = dynamicPrecision; 00066 this.gatherSystemTables = gatherSystemTables; 00067 this.schemaPattern = schemaPattern; 00068 }
|
|
Create a database schema from the given connection
Definition at line 77 of file DatabaseSQLMetaData.java. References org.objectweb.cjdbc.common.sql.schema.DatabaseSchema.addTable(). 00078 { 00079 ResultSet rs = null; 00080 00081 connection.setAutoCommit(false); // Needed 00082 // for 00083 // Derby 00084 // Get DatabaseMetaData 00085 DatabaseMetaData metaData = connection.getMetaData(); 00086 if (metaData == null) 00087 { 00088 logger.warn(Translate.get("backend.meta.received.null")); 00089 return null; 00090 } 00091 00092 DatabaseSchema databaseSchema = new DatabaseSchema(); 00093 00094 // Check if we should get system tables or not 00095 String[] types; 00096 if (gatherSystemTables) 00097 { 00098 schemaPattern = null; 00099 types = new String[]{"TABLE", "VIEW", "SYSTEM TABLE", "SYSTEM VIEW"}; 00100 } 00101 else 00102 types = new String[]{"TABLE", "VIEW"}; 00103 00104 // Get tables meta data 00105 // getTables() gets a description of tables matching the catalog, schema, 00106 // table name pattern and type. Sending in null for catalog and schema 00107 // drops them from the selection criteria. The table name pattern "%" 00108 // means match any substring of 0 or more characters. 00109 // Last argument allows to obtain only database tables 00110 try 00111 { 00112 rs = metaData.getTables(null, schemaPattern, "%", types); 00113 } 00114 catch (Exception e) 00115 { 00116 // VIEWS cannot be retrieved with this backend 00117 logger.error(Translate.get("backend.meta.view.not.supported"), e); 00118 if (gatherSystemTables) 00119 types = new String[]{"TABLE", "SYSTEM TABLE",}; 00120 else 00121 types = new String[]{"TABLE"}; 00122 rs = metaData.getTables(null, schemaPattern, "%", types); 00123 } 00124 00125 if (rs == null) 00126 { 00127 logger.warn(Translate.get("backend.meta.received.null")); 00128 connection.commit(); 00129 return null; 00130 } 00131 00132 String tableName; 00133 DatabaseTable table = null; 00134 while (rs.next()) 00135 { 00136 // 1 is table catalog, 2 is table schema, 3 is table name, 4 is type 00137 tableName = rs.getString(3); 00138 if (logger.isDebugEnabled()) 00139 logger.debug(Translate.get("backend.meta.found.table", tableName)); 00140 00141 // Create a new table and add it to the database schema 00142 table = new DatabaseTable(tableName); 00143 databaseSchema.addTable(table); 00144 00145 if (dynamicPrecision >= DatabaseBackendSchemaConstants.DynamicPrecisionColumn) 00146 { 00147 // Get information about this table columns 00148 getColumns(metaData, table); 00149 // Get information about this table primary keys 00150 getPrimaryKeys(metaData, table); 00151 } 00152 } 00153 00154 // Get Procedures for this database 00155 if (dynamicPrecision >= DatabaseBackendSchemaConstants.DynamicPrecisionProcedures) 00156 getProcedures(metaData, databaseSchema); 00157 00158 try 00159 { 00160 rs.close(); 00161 } 00162 catch (Exception ignore) 00163 { 00164 } 00165 00166 try 00167 { 00168 connection.commit(); 00169 } 00170 catch (Exception ignore) 00171 { 00172 // This was a read-only transaction 00173 } 00174 00175 try 00176 { 00177 // restore connection 00178 connection.setAutoCommit(true); 00179 } 00180 catch (SQLException e1) 00181 { 00182 // ignore, transaction is no more valid 00183 } 00184 00185 return databaseSchema; 00186 }
|