001: //$Id: TableMetadata.java 10725 2006-11-06 14:35:20Z max.andersen@jboss.com $
002: package org.hibernate.tool.hbm2ddl;
003:
004: import java.sql.DatabaseMetaData;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: /**
014: * JDBC table metadata
015: * @author Christoph Sturm, Max Rydahl Andersen
016: */
017: public class TableMetadata {
018:
019: private static final Log log = LogFactory
020: .getLog(TableMetadata.class);
021:
022: private final String catalog;
023: private final String schema;
024: private final String name;
025: private final Map columns = new HashMap();
026: private final Map foreignKeys = new HashMap();
027: private final Map indexes = new HashMap();
028:
029: TableMetadata(ResultSet rs, DatabaseMetaData meta, boolean extras)
030: throws SQLException {
031: catalog = rs.getString("TABLE_CAT");
032: schema = rs.getString("TABLE_SCHEM");
033: name = rs.getString("TABLE_NAME");
034: initColumns(meta);
035: if (extras) {
036: initForeignKeys(meta);
037: initIndexes(meta);
038: }
039: String cat = catalog == null ? "" : catalog + '.';
040: String schem = schema == null ? "" : schema + '.';
041: log.info("table found: " + cat + schem + name);
042: log.info("columns: " + columns.keySet());
043: if (extras) {
044: log.info("foreign keys: " + foreignKeys.keySet());
045: log.info("indexes: " + indexes.keySet());
046: }
047: }
048:
049: public String getName() {
050: return name;
051: }
052:
053: public String getCatalog() {
054: return catalog;
055: }
056:
057: public String getSchema() {
058: return schema;
059: }
060:
061: public String toString() {
062: return "TableMetadata(" + name + ')';
063: }
064:
065: public ColumnMetadata getColumnMetadata(String columnName) {
066: return (ColumnMetadata) columns.get(columnName.toLowerCase());
067: }
068:
069: public ForeignKeyMetadata getForeignKeyMetadata(String keyName) {
070: return (ForeignKeyMetadata) foreignKeys.get(keyName
071: .toLowerCase());
072: }
073:
074: public IndexMetadata getIndexMetadata(String indexName) {
075: return (IndexMetadata) indexes.get(indexName.toLowerCase());
076: }
077:
078: private void addForeignKey(ResultSet rs) throws SQLException {
079: String fk = rs.getString("FK_NAME");
080:
081: if (fk == null)
082: return;
083:
084: ForeignKeyMetadata info = getForeignKeyMetadata(fk);
085: if (info == null) {
086: info = new ForeignKeyMetadata(rs);
087: foreignKeys.put(info.getName().toLowerCase(), info);
088: }
089:
090: info
091: .addColumn(getColumnMetadata(rs
092: .getString("FKCOLUMN_NAME")));
093: }
094:
095: private void addIndex(ResultSet rs) throws SQLException {
096: String index = rs.getString("INDEX_NAME");
097:
098: if (index == null)
099: return;
100:
101: IndexMetadata info = getIndexMetadata(index);
102: if (info == null) {
103: info = new IndexMetadata(rs);
104: indexes.put(info.getName().toLowerCase(), info);
105: }
106:
107: info.addColumn(getColumnMetadata(rs.getString("COLUMN_NAME")));
108: }
109:
110: public void addColumn(ResultSet rs) throws SQLException {
111: String column = rs.getString("COLUMN_NAME");
112:
113: if (column == null)
114: return;
115:
116: if (getColumnMetadata(column) == null) {
117: ColumnMetadata info = new ColumnMetadata(rs);
118: columns.put(info.getName().toLowerCase(), info);
119: }
120: }
121:
122: private void initForeignKeys(DatabaseMetaData meta)
123: throws SQLException {
124: ResultSet rs = null;
125:
126: try {
127: rs = meta.getImportedKeys(catalog, schema, name);
128: while (rs.next())
129: addForeignKey(rs);
130: } finally {
131: if (rs != null)
132: rs.close();
133: }
134: }
135:
136: private void initIndexes(DatabaseMetaData meta) throws SQLException {
137: ResultSet rs = null;
138:
139: try {
140: rs = meta.getIndexInfo(catalog, schema, name, false, true);
141:
142: while (rs.next()) {
143: if (rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic)
144: continue;
145: addIndex(rs);
146: }
147: } finally {
148: if (rs != null)
149: rs.close();
150: }
151: }
152:
153: private void initColumns(DatabaseMetaData meta) throws SQLException {
154: ResultSet rs = null;
155:
156: try {
157: rs = meta.getColumns(catalog, schema, name, "%");
158: while (rs.next())
159: addColumn(rs);
160: } finally {
161: if (rs != null)
162: rs.close();
163: }
164: }
165:
166: }
|