FileDocCategorySizeDatePackage
DatabaseMetadata.javaAPI DocHibernate 3.2.55200Mon Nov 06 07:35:20 GMT 2006org.hibernate.tool.hbm2ddl

DatabaseMetadata

public class DatabaseMetadata extends Object
JDBC database metadata
author
Christoph Sturm, Teodor Danciu

Fields Summary
private static final Log
log
private final Map
tables
private final Set
sequences
private final boolean
extras
private DatabaseMetaData
meta
private org.hibernate.exception.SQLExceptionConverter
sqlExceptionConverter
private static final String[]
TYPES
Constructors Summary
public DatabaseMetadata(Connection connection, org.hibernate.dialect.Dialect dialect)


	       
		this(connection, dialect, true);
	
public DatabaseMetadata(Connection connection, org.hibernate.dialect.Dialect dialect, boolean extras)

		sqlExceptionConverter = dialect.buildSQLExceptionConverter();
		meta = connection.getMetaData();
		this.extras = extras;
		initSequences(connection, dialect);
	
Methods Summary
public TableMetadatagetTableMetadata(java.lang.String name, java.lang.String schema, java.lang.String catalog, boolean isQuoted)


	            

		Object identifier = identifier(catalog, schema, name);
		TableMetadata table = (TableMetadata) tables.get(identifier);
		if (table!=null) {
			return table;
		}
		else {
			
			try {
				ResultSet rs = null;
				try {
					if ( (isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
						rs = meta.getTables(catalog, schema, name, TYPES);
					} else if ( (isQuoted && meta.storesUpperCaseQuotedIdentifiers()) 
						|| (!isQuoted && meta.storesUpperCaseIdentifiers() )) {
						rs = meta.getTables( 
								StringHelper.toUpperCase(catalog), 
								StringHelper.toUpperCase(schema), 
								StringHelper.toUpperCase(name), 
								TYPES 
							);
					}
					else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
							|| (!isQuoted && meta.storesLowerCaseIdentifiers() )) {
						rs = meta.getTables( 
								StringHelper.toLowerCase(catalog), 
								StringHelper.toLowerCase(schema), 
								StringHelper.toLowerCase(name), 
								TYPES 
							);
					}
					else {
						rs = meta.getTables(catalog, schema, name, TYPES);
					}
					
					while ( rs.next() ) {
						String tableName = rs.getString("TABLE_NAME");
						if ( name.equalsIgnoreCase(tableName) ) {
							table = new TableMetadata(rs, meta, extras);
							tables.put(identifier, table);
							return table;
						}
					}
					
					log.info("table not found: " + name);
					return null;

				}
				finally {
					if (rs!=null) rs.close();
				}
			}
			catch (SQLException sqle) {
				throw JDBCExceptionHelper.convert(
                        sqlExceptionConverter,
				        sqle,
				        "could not get table metadata: " + name
					);
			}
		}

	
private java.lang.Objectidentifier(java.lang.String catalog, java.lang.String schema, java.lang.String name)

		return Table.qualify(catalog,schema,name);
	
private voidinitSequences(java.sql.Connection connection, org.hibernate.dialect.Dialect dialect)

		if ( dialect.supportsSequences() ) {
			String sql = dialect.getQuerySequencesString();
			if (sql!=null) {
	
				Statement statement = null;
				ResultSet rs = null;
				try {
					statement = connection.createStatement();
					rs = statement.executeQuery(sql);
		
					while ( rs.next() ) {
						sequences.add( rs.getString(1).toLowerCase().trim() );
					}
				}
				finally {
					if (rs!=null) rs.close();
					if (statement!=null) statement.close();
				}
				
			}
		}
	
public booleanisSequence(java.lang.Object key)

		if (key instanceof String){
			String[] strings = StringHelper.split(".", (String) key);
			return sequences.contains( strings[strings.length-1].toLowerCase());
		}
		return false;
	
public booleanisTable(java.lang.Object key)

 		if(key instanceof String) {
			Table tbl = new Table((String)key);
			if ( getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null ) {
 				return true;
 			} else {
 				String[] strings = StringHelper.split(".", (String) key);
 				if(strings.length==3) {
					tbl = new Table(strings[2]);
					tbl.setCatalog(strings[0]);
					tbl.setSchema(strings[1]);
					return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null;
 				} else if (strings.length==2) {
					tbl = new Table(strings[1]);
					tbl.setSchema(strings[0]);
					return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null;
 				}
 			}
 		}
 		return false;
 	
public java.lang.StringtoString()

		return "DatabaseMetadata" + tables.keySet().toString() + sequences.toString();