KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DatabaseMetadata.java,v 1.12 2005/06/26 06:36:03 oneovthafew Exp $
2
package org.hibernate.tool.hbm2ddl;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.DatabaseMetaData JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Statement JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
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 /**
23  * JDBC database metadata
24  * @author Christoph Sturm, Teodor Danciu
25  */

26 public class DatabaseMetadata {
27     
28     private static final Log log = LogFactory.getLog(DatabaseMetadata.class);
29     
30     private final Map JavaDoc tables = new HashMap JavaDoc();
31     private final Set JavaDoc sequences = new HashSet JavaDoc();
32
33     private DatabaseMetaData JavaDoc meta;
34     private SQLExceptionConverter sqlExceptionConverter;
35
36     public DatabaseMetadata(Connection JavaDoc connection, Dialect dialect) throws SQLException JavaDoc {
37         sqlExceptionConverter = dialect.buildSQLExceptionConverter();
38         meta = connection.getMetaData();
39         initSequences(connection, dialect);
40     }
41
42     private static final String JavaDoc[] TYPES = {"TABLE"};
43
44     public TableMetadata getTableMetadata(String JavaDoc name, String JavaDoc schema, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc connection, Dialect dialect) throws SQLException JavaDoc {
105         if ( dialect.supportsSequences() ) {
106             String JavaDoc sql = dialect.getQuerySequencesString();
107             if (sql!=null) {
108     
109                 Statement JavaDoc statement = null;
110                 ResultSet JavaDoc 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 JavaDoc key) {
129         return key instanceof String JavaDoc && sequences.contains( ( (String JavaDoc) key ).toLowerCase() );
130     }
131
132     public boolean isTable(Object JavaDoc key) throws HibernateException {
133         return key instanceof String JavaDoc && ( getTableMetadata( (String JavaDoc) key, null, null ) != null );
134     }
135     
136     public String JavaDoc toString() {
137         return "DatabaseMetadata" + tables.keySet().toString() + sequences.toString();
138     }
139 }
140
141
142
143
144
145
Popular Tags