KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tool > hbm2ddl > TableMetadata


1 //$Id: TableMetadata.java,v 1.4 2004/09/01 08:17:37 oneovthafew Exp $
2
package org.hibernate.tool.hbm2ddl;
3
4 import java.sql.DatabaseMetaData JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.hibernate.util.StringHelper;
13
14 /**
15  * JDBC table metadata
16  * @author Christoph Sturm
17  */

18 public class TableMetadata {
19     
20     private static final Log log = LogFactory.getLog(TableMetadata.class);
21     
22     private final String JavaDoc catalog;
23     private final String JavaDoc schema;
24     private final String JavaDoc name;
25     private final Map JavaDoc columns = new HashMap JavaDoc();
26     private final Map JavaDoc foreignKeys = new HashMap JavaDoc();
27     private final Map JavaDoc indexes = new HashMap JavaDoc();
28
29     TableMetadata(ResultSet JavaDoc rs, DatabaseMetaData JavaDoc meta) throws SQLException JavaDoc {
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 JavaDoc cat = catalog==null ? "" : catalog + '.';
37         String JavaDoc 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 JavaDoc getName() {
45         return name;
46     }
47
48     public String JavaDoc toString() {
49         return "TableMetadata(" + name + ')';
50     }
51
52     public ColumnMetadata getColumnMetadata(String JavaDoc columnName) {
53         return (ColumnMetadata) columns.get( columnName.toLowerCase() );
54     }
55
56     public ForeignKeyMetadata getForeignKeyMetadata(String JavaDoc keyName) {
57         return (ForeignKeyMetadata) foreignKeys.get( keyName.toLowerCase() );
58     }
59
60     public IndexMetadata getIndexMetadata(String JavaDoc indexName) {
61         return (IndexMetadata) indexes.get( indexName.toLowerCase() );
62     }
63
64     private void addForeignKey(ResultSet JavaDoc rs) throws SQLException JavaDoc {
65         String JavaDoc 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 JavaDoc rs) throws SQLException JavaDoc {
79         String JavaDoc 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 JavaDoc rs) throws SQLException JavaDoc {
93         String JavaDoc 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 JavaDoc meta) throws SQLException JavaDoc {
104         ResultSet JavaDoc 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 JavaDoc meta) throws SQLException JavaDoc {
133         ResultSet JavaDoc 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 JavaDoc meta) throws SQLException JavaDoc {
169         ResultSet JavaDoc 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