1 package org.hibernate.tool.hbm2ddl; 3 4 import java.sql.DatabaseMetaData ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.hibernate.util.StringHelper; 13 14 18 public class TableMetadata { 19 20 private static final Log log = LogFactory.getLog(TableMetadata.class); 21 22 private final String catalog; 23 private final String schema; 24 private final String name; 25 private final Map columns = new HashMap (); 26 private final Map foreignKeys = new HashMap (); 27 private final Map indexes = new HashMap (); 28 29 TableMetadata(ResultSet rs, DatabaseMetaData meta) throws SQLException { 30 catalog = rs.getString("TABLE_CAT"); 31 schema = rs.getString("TABLE_SCHEM"); 32 name = rs.getString("TABLE_NAME"); 33 initColumns(meta); 34 initForeignKeys(meta); 35 initIndexes(meta); 36 String cat = catalog==null ? "" : catalog + '.'; 37 String schem = schema==null ? "" : schema + '.'; 38 log.info( "table found: " + cat + schem + name ); 39 log.info( "columns: " + columns.keySet() ); 40 log.info( "foreign keys: " + foreignKeys.keySet() ); 41 log.info( "indexes: " + indexes.keySet() ); 42 } 43 44 public String getName() { 45 return name; 46 } 47 48 public String toString() { 49 return "TableMetadata(" + name + ')'; 50 } 51 52 public ColumnMetadata getColumnMetadata(String columnName) { 53 return (ColumnMetadata) columns.get( columnName.toLowerCase() ); 54 } 55 56 public ForeignKeyMetadata getForeignKeyMetadata(String keyName) { 57 return (ForeignKeyMetadata) foreignKeys.get( keyName.toLowerCase() ); 58 } 59 60 public IndexMetadata getIndexMetadata(String indexName) { 61 return (IndexMetadata) indexes.get( indexName.toLowerCase() ); 62 } 63 64 private void addForeignKey(ResultSet rs) throws SQLException { 65 String fk = rs.getString("FK_NAME"); 66 67 if (fk == null) return; 68 69 ForeignKeyMetadata info = getForeignKeyMetadata(fk); 70 if (info == null) { 71 info = new ForeignKeyMetadata(rs); 72 foreignKeys.put( info.getName().toLowerCase(), info ); 73 } 74 75 info.addColumn( getColumnMetadata( rs.getString("FKCOLUMN_NAME") ) ); 76 } 77 78 private void addIndex(ResultSet rs) throws SQLException { 79 String index = rs.getString("INDEX_NAME"); 80 81 if (index == null) return; 82 83 IndexMetadata info = getIndexMetadata(index); 84 if (info == null) { 85 info = new IndexMetadata(rs); 86 indexes.put( info.getName().toLowerCase(), info ); 87 } 88 89 info.addColumn( getColumnMetadata( rs.getString("COLUMN_NAME") ) ); 90 } 91 92 public void addColumn(ResultSet rs) throws SQLException { 93 String column = rs.getString("COLUMN_NAME"); 94 95 if (column==null) return; 96 97 if ( getColumnMetadata(column) == null ) { 98 ColumnMetadata info = new ColumnMetadata(rs); 99 columns.put( info.getName().toLowerCase(), info ); 100 } 101 } 102 103 private void initForeignKeys(DatabaseMetaData meta) throws SQLException { 104 ResultSet rs = null; 105 106 try { 107 if ( meta.storesUpperCaseIdentifiers() ) { 108 rs = meta.getImportedKeys( 109 StringHelper.toUpperCase(catalog), 110 StringHelper.toUpperCase(schema), 111 StringHelper.toUpperCase(name) 112 ); 113 } 114 else if ( meta.storesLowerCaseIdentifiers() ) { 115 rs = meta.getImportedKeys( 116 StringHelper.toLowerCase(catalog), 117 StringHelper.toLowerCase(schema), 118 StringHelper.toLowerCase(name) 119 ); 120 } 121 else { 122 rs = meta.getImportedKeys(catalog, schema, name); 123 } 124 125 while ( rs.next() ) addForeignKey(rs); 126 } 127 finally { 128 if (rs != null) rs.close(); 129 } 130 } 131 132 private void initIndexes(DatabaseMetaData meta) throws SQLException { 133 ResultSet rs = null; 134 135 try { 136 if ( meta.storesUpperCaseIdentifiers() ) { 137 rs = meta.getIndexInfo( 138 StringHelper.toUpperCase(catalog), 139 StringHelper.toUpperCase(schema), 140 StringHelper.toUpperCase(name), 141 false, 142 true 143 ); 144 } 145 else if ( meta.storesLowerCaseIdentifiers() ) { 146 rs = meta.getIndexInfo( 147 StringHelper.toLowerCase(catalog), 148 StringHelper.toLowerCase(schema), 149 StringHelper.toLowerCase(name), 150 false, 151 true 152 ); 153 } 154 else { 155 rs = meta.getIndexInfo(catalog, schema, name, false, true); 156 } 157 158 while ( rs.next() ) { 159 if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic ) continue; 160 addIndex(rs); 161 } 162 } 163 finally { 164 if (rs != null) rs.close(); 165 } 166 } 167 168 private void initColumns(DatabaseMetaData meta) throws SQLException { 169 ResultSet rs = null; 170 171 try { 172 if ( meta.storesUpperCaseIdentifiers() ) { 173 rs = meta.getColumns( 174 StringHelper.toUpperCase(catalog), 175 StringHelper.toUpperCase(schema), 176 StringHelper.toUpperCase(name), 177 "%" 178 ); 179 } 180 else if ( meta.storesLowerCaseIdentifiers() ) { 181 rs = meta.getColumns( 182 StringHelper.toLowerCase(catalog), 183 StringHelper.toLowerCase(schema), 184 StringHelper.toLowerCase(name), 185 "%" 186 ); 187 } 188 else { 189 rs = meta.getColumns(catalog, schema, name, "%"); 190 } 191 192 while ( rs.next() ) addColumn(rs); 193 } 194 finally { 195 if (rs != null) rs.close(); 196 } 197 } 198 199 } 200 201 202 203 204 205 206 | Popular Tags |