1 24 25 package org.objectweb.cjdbc.common.sql.schema; 26 27 import java.sql.Connection ; 28 import java.sql.DatabaseMetaData ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 32 import org.objectweb.cjdbc.common.i18n.Translate; 33 import org.objectweb.cjdbc.common.log.Trace; 34 import org.objectweb.cjdbc.controller.backend.DatabaseBackendSchemaConstants; 35 36 43 public class DatabaseSQLMetaData 44 { 45 Trace logger; 46 Connection connection; 47 int dynamicPrecision; 48 boolean gatherSystemTables; 49 String schemaPattern; 50 51 61 public DatabaseSQLMetaData(Trace logger, Connection connection, 62 int dynamicPrecision, boolean gatherSystemTables, String schemaPattern) 63 { 64 super(); 65 this.logger = logger; 66 this.connection = connection; 67 this.dynamicPrecision = dynamicPrecision; 68 this.gatherSystemTables = gatherSystemTables; 69 this.schemaPattern = schemaPattern; 70 } 71 72 79 public final DatabaseSchema createDatabaseSchema() throws SQLException 80 { 81 ResultSet rs = null; 82 83 connection.setAutoCommit(false); DatabaseMetaData metaData = connection.getMetaData(); 85 if (metaData == null) 86 { 87 logger.warn(Translate.get("backend.meta.received.null")); 88 return null; 89 } 90 91 DatabaseSchema databaseSchema = new DatabaseSchema(); 92 93 String [] types; 95 if (gatherSystemTables) 96 { 97 schemaPattern = null; 98 types = new String []{"TABLE", "VIEW", "SYSTEM TABLE", "SYSTEM VIEW"}; 99 } 100 else 101 types = new String []{"TABLE", "VIEW"}; 102 103 try 110 { 111 rs = metaData.getTables(null, schemaPattern, "%", types); 112 } 113 catch (Exception e) 114 { 115 logger.error(Translate.get("backend.meta.view.not.supported"), e); 117 if (gatherSystemTables) 118 types = new String []{"TABLE", "SYSTEM TABLE",}; 119 else 120 types = new String []{"TABLE"}; 121 rs = metaData.getTables(null, schemaPattern, "%", types); 122 } 123 124 if (rs == null) 125 { 126 logger.warn(Translate.get("backend.meta.received.null")); 127 connection.commit(); 128 return null; 129 } 130 131 String tableName; 132 DatabaseTable table = null; 133 while (rs.next()) 134 { 135 tableName = rs.getString(3); 137 if (logger.isDebugEnabled()) 138 logger.debug(Translate.get("backend.meta.found.table", tableName)); 139 140 table = new DatabaseTable(tableName); 142 databaseSchema.addTable(table); 143 144 if (dynamicPrecision >= DatabaseBackendSchemaConstants.DynamicPrecisionColumn) 145 { 146 getColumns(metaData, table); 148 getPrimaryKeys(metaData, table); 150 } 151 } 152 153 if (dynamicPrecision >= DatabaseBackendSchemaConstants.DynamicPrecisionProcedures) 155 getProcedures(metaData, databaseSchema); 156 157 try 158 { 159 rs.close(); 160 } 161 catch (Exception ignore) 162 { 163 } 164 165 try 166 { 167 connection.commit(); 168 } 169 catch (Exception ignore) 170 { 171 } 173 174 try 175 { 176 connection.setAutoCommit(true); 178 } 179 catch (SQLException e1) 180 { 181 } 183 184 return databaseSchema; 185 } 186 187 191 private void getProcedures(DatabaseMetaData metaData, DatabaseSchema schema) 192 { 193 if (logger.isDebugEnabled()) 194 logger.debug(Translate.get("backend.meta.get.procedures")); 195 ResultSet rs = null; 196 ResultSet rs2 = null; 197 try 198 { 199 rs = metaData.getProcedures(null, null, "%"); 201 202 if (rs == null) 203 { 204 logger.warn(Translate.get("backend.meta.get.procedures.failed", 205 metaData.getConnection().getCatalog())); 206 return; 207 } 208 209 while (rs.next()) 210 { 211 DatabaseProcedure procedure = new DatabaseProcedure(rs.getString(3), rs 216 .getString(7), rs.getShort(8)); 217 218 if (schema.getProcedure(procedure.getName()) != null) 219 { 220 if (logger.isDebugEnabled()) 221 logger.debug(Translate 222 .get("backend.meta.procedure.already.in.schema", procedure 223 .getName())); 224 continue; 225 } 226 else 227 { 228 if (logger.isDebugEnabled()) 229 logger.debug(Translate.get("backend.meta.found.procedure", 230 procedure.getName())); 231 } 232 233 236 if (dynamicPrecision < DatabaseBackendSchemaConstants.DynamicPrecisionProcedures) 237 continue; 238 rs2 = metaData 240 .getProcedureColumns(null, null, procedure.getName(), "%"); 241 if (rs2 == null) 242 logger.warn(Translate.get("backend.meta.get.procedure.params.failed", 243 procedure.getName())); 244 else 245 { 246 while (rs2.next()) 247 { 248 DatabaseProcedureParameter param = new DatabaseProcedureParameter( 260 rs2.getString(4), rs2.getInt(5), rs2.getInt(6), rs2 261 .getString(7), rs2.getFloat(8), rs2.getInt(9), rs2 262 .getInt(10), rs2.getInt(11), rs2.getInt(12), rs2 263 .getString(13)); 264 procedure.addParameter(param); 281 } 282 rs2.close(); 283 } 284 285 schema.addProcedure(procedure); 286 } 287 } 288 catch (Exception e) 289 { 290 logger.error(Translate.get("backend.meta.get.procedures.failed", e 291 .getMessage()), e); 292 } 293 finally 294 { 295 try 296 { 297 rs.close(); 298 } 299 catch (Exception ignore) 300 { 301 } 302 try 303 { 304 rs2.close(); 305 } 306 catch (Exception ignoreAsWell) 307 { 308 } 309 } 310 } 311 312 320 private void getColumns(DatabaseMetaData metaData, DatabaseTable table) 321 throws SQLException 322 { 323 ResultSet rs = null; 324 try 325 { 326 rs = metaData.getColumns(null, null, table.getName(), "%"); 332 333 if (rs == null) 334 { 335 logger.warn(Translate.get("backend.meta.get.columns.failed", table 336 .getName())); 337 return; 338 } 339 340 DatabaseColumn column = null; 341 int type; 342 while (rs.next()) 343 { 344 type = rs.getShort(5); 347 column = new DatabaseColumn(rs.getString(4), false, type); 348 table.addColumn(column); 349 350 if (logger.isDebugEnabled()) 351 logger.debug(Translate.get("backend.meta.found.column", rs 352 .getString(4))); 353 } 354 } 355 catch (SQLException e) 356 { 357 throw new SQLException (Translate.get("backend.meta.get.columns.failed", 358 table.getName())); 359 } 360 finally 361 { 362 try 363 { 364 rs.close(); 365 } 366 catch (Exception ignore) 367 { 368 } 369 } 370 } 371 372 380 private void getPrimaryKeys(DatabaseMetaData metaData, DatabaseTable table) 381 throws SQLException 382 { 383 ResultSet rs = null; 384 try 385 { 386 391 rs = metaData.getPrimaryKeys(null, null, table.getName()); 392 393 if (rs == null) 394 { 395 logger.warn(Translate.get("backend.meta.get.primary.keys.failed", table 396 .getName())); 397 return; 398 } 399 400 String columnName = null; 401 while (rs.next()) 402 { 403 404 columnName = rs.getString(4); 407 if (columnName == null) 408 continue; 409 if (logger.isDebugEnabled()) 410 logger.debug(Translate.get("backend.meta.found.primary.key", 411 columnName)); 412 413 table.getColumn(columnName).setIsUnique(true); 415 } 416 } 417 catch (SQLException e) 418 { 419 throw new SQLException (Translate.get( 420 "backend.meta.get.primary.keys.failed", table.getName())); 421 } 422 finally 423 { 424 try 425 { 426 rs.close(); 427 } 428 catch (Exception ignore) 429 { 430 } 431 } 432 } 433 434 } 435 | Popular Tags |