1 package org.hibernate.tool.hbm2ddl; 3 4 import java.sql.Connection ; 5 import java.sql.DatabaseMetaData ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.sql.Statement ; 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.hibernate.HibernateException; 17 import org.hibernate.exception.JDBCExceptionHelper; 18 import org.hibernate.exception.SQLExceptionConverter; 19 import org.hibernate.dialect.Dialect; 20 import org.hibernate.util.StringHelper; 21 22 26 public class DatabaseMetadata { 27 28 private static final Log log = LogFactory.getLog(DatabaseMetadata.class); 29 30 private final Map tables = new HashMap (); 31 private final Set sequences = new HashSet (); 32 33 private DatabaseMetaData meta; 34 private SQLExceptionConverter sqlExceptionConverter; 35 36 public DatabaseMetadata(Connection connection, Dialect dialect) throws SQLException { 37 sqlExceptionConverter = dialect.buildSQLExceptionConverter(); 38 meta = connection.getMetaData(); 39 initSequences(connection, dialect); 40 } 41 42 private static final String [] TYPES = {"TABLE"}; 43 44 public TableMetadata getTableMetadata(String name, String schema, String catalog) throws HibernateException { 45 46 TableMetadata table = (TableMetadata) tables.get(name); 47 if (table!=null) { 48 return table; 49 } 50 else { 51 52 try { 53 ResultSet rs = null; 54 try { 55 56 if ( meta.storesUpperCaseIdentifiers() ) { 57 rs = meta.getTables( 58 StringHelper.toUpperCase(catalog), 59 StringHelper.toUpperCase(schema), 60 StringHelper.toUpperCase(name), 61 TYPES 62 ); 63 } 64 else if ( meta.storesLowerCaseIdentifiers() ) { 65 rs = meta.getTables( 66 StringHelper.toLowerCase(catalog), 67 StringHelper.toLowerCase(schema), 68 StringHelper.toLowerCase(name), 69 TYPES 70 ); 71 } 72 else { 73 rs = meta.getTables(catalog, schema, name, TYPES); 74 } 75 76 while ( rs.next() ) { 77 String tableName = rs.getString("TABLE_NAME"); 78 if ( name.equalsIgnoreCase(tableName) ) { 79 table = new TableMetadata(rs, meta); 80 tables.put(name, table); 81 return table; 82 } 83 } 84 85 log.info("table not found: " + name); 86 return null; 87 88 } 89 finally { 90 if (rs!=null) rs.close(); 91 } 92 } 93 catch (SQLException sqle) { 94 throw JDBCExceptionHelper.convert( 95 sqlExceptionConverter, 96 sqle, 97 "could not get table metadata: " + name 98 ); 99 } 100 } 101 102 } 103 104 private void initSequences(Connection connection, Dialect dialect) throws SQLException { 105 if ( dialect.supportsSequences() ) { 106 String sql = dialect.getQuerySequencesString(); 107 if (sql!=null) { 108 109 Statement statement = null; 110 ResultSet rs = null; 111 try { 112 statement = connection.createStatement(); 113 rs = statement.executeQuery(sql); 114 115 while ( rs.next() ) { 116 sequences.add( rs.getString(1).toLowerCase().trim() ); 117 } 118 } 119 finally { 120 if (rs!=null) rs.close(); 121 if (statement!=null) statement.close(); 122 } 123 124 } 125 } 126 } 127 128 public boolean isSequence(Object key) { 129 return key instanceof String && sequences.contains( ( (String ) key ).toLowerCase() ); 130 } 131 132 public boolean isTable(Object key) throws HibernateException { 133 return key instanceof String && ( getTableMetadata( (String ) key, null, null ) != null ); 134 } 135 136 public String toString() { 137 return "DatabaseMetadata" + tables.keySet().toString() + sequences.toString(); 138 } 139 } 140 141 142 143 144 145 | Popular Tags |