de.fesenmeyer.dbnormalizer.core.dbaccess
Class DatabaseMetadataUtil

java.lang.Object
  extended by de.fesenmeyer.dbnormalizer.core.dbaccess.DatabaseMetadataUtil

public final class DatabaseMetadataUtil
extends Object

Utility class for retrieving database information. Acts as a facade to SQuirreL and DatabaseMetaData code.

Author:
DF
See Also:
DatabaseMetaData

Field Summary
private static DBNormalizerLogger logger
          The logger for this class.
 
Constructor Summary
DatabaseMetadataUtil()
           
 
Method Summary
private static Collection<FK> buildFKsFromResultSet(ResultSet rs)
          Helper method creating a collection of FKs from the ResultSet returned from the DatabaseMetaData.getImportedKeys(String, String, String) or DatabaseMetaData.getExportedKeys(String, String, String) method.
private static String createFKKey(FK fk)
          Helper method which creates a string which can be used as a FK's key in a map.
private static Table createTableFromTableInfo(net.sourceforge.squirrel_sql.fw.sql.ITableInfo tableInfo)
          Helper method which converts from SQuirreL's ITableInfo object to DBNormalizer's Table object.
static Collection<String> getCatalogs(DBNormalizerSession session)
          Gets the catalogs for the specified DBNormalizer session.
static Collection<FK> getExportedKeys(DatabaseMetaData dbmd, String catalog, String schema, String tableName)
          Gets the exported keys for a table specified by catalog, schema and name.
static Collection<FK> getImportedKeys(DatabaseMetaData dbmd, String catalog, String schema, String tableName)
          Gets the imported keys for a table specified by catalog, schema and name.
static PK getPK(DBNormalizerSession session, String catalog, String schema, String tableName)
          Gets the PK for a table specified by catalog, schema and name.
static Collection<String> getSchemas(DBNormalizerSession session)
          Gets the schemas for the specified DBNormalizer session.
static Collection<TableColumn> getTableColumnInfos(DBNormalizerSession session, String catalogName, String schemaName, String tableName)
          Gets TableColumn objects for a table specified by catalog, schema and name.
static Collection<TableContainer> getTableContainers(DBNormalizerSession session)
          Gets all TableContainers (catalogs or schemas) for the specified DBNormalizer session.
static Table getTableInfo(DBNormalizerSession session, String catalogName, String schemaName, String tableName, boolean complete)
          Gets the table with the specified name, catalog and schema.
static Collection<Table> getTableInfos(DBNormalizerSession session, String catalogName, String schemaName)
          Gets all tables from the specified catalog or schema.
static Collection<Table> getTableInfos(DBNormalizerSession session, String catalogName, String schemaName, boolean complete)
          Gets all tables from the specified catalog or schema.
static Collection<Table> getTableInfos(DBNormalizerSession session, TableContainer tableContainer, boolean complete)
          Gets all tables from the specified TableContainer.
private static void retrieveCompleteTableInfo(DBNormalizerSession session, Table table)
          Helper method which fills the given table object with further information (Column, PK and FK information).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static DBNormalizerLogger logger
The logger for this class.

Constructor Detail

DatabaseMetadataUtil

public DatabaseMetadataUtil()
Method Detail

getTableInfos

public static Collection<Table> getTableInfos(DBNormalizerSession session,
                                              String catalogName,
                                              String schemaName)
                                       throws SQLException
Gets all tables from the specified catalog or schema.

NOTE: The returned table objects are not completely filled due to performance issues, but only contain the table's name, catalog, schema and TableContainer. To get all available information, use the getTableInfos(DBNormalizerSession, String, String, boolean) method or get further information by other methods of this class (e.g. getTableColumnInfos(DBNormalizerSession, String, String, String).

Parameters:
session - the DBNormalizer session which is connected to the DB
catalogName - the catalog's name, may be null
schemaName - the schema's name, may be null
Returns:
a collection of Table objects
Throws:
SQLException

getTableInfos

public static Collection<Table> getTableInfos(DBNormalizerSession session,
                                              String catalogName,
                                              String schemaName,
                                              boolean complete)
                                       throws SQLException
Gets all tables from the specified catalog or schema.

NOTE: If you set the argument complete to false, the returned table objects will not be completely filled, but only contain the table's name, catalog, schema and TableContainer. To get all available information, which is not high-performance, set the argument complete to true.

Parameters:
session - the DBNormalizer session which is connected to the DB
catalogName - the catalog's name, may be null
schemaName - the schema's name, may be null
complete - true, to read all available information; false, otherwise
Returns:
a collection of Table objects
Throws:
SQLException

getTableInfos

public static Collection<Table> getTableInfos(DBNormalizerSession session,
                                              TableContainer tableContainer,
                                              boolean complete)
                                       throws SQLException
Gets all tables from the specified TableContainer.

NOTE: If you set the argument complete to false, the returned table objects will not be completely filled, but only contain the table's name, catalog, schema and TableContainer. To get all available information, which is not high-performance, set the argument complete to true.

Parameters:
session - the DBNormalizer session which is connected to the DB
tableContainer - the TableContainer
complete - true, to read all available information; false, otherwise
Returns:
a collection of Table objects
Throws:
SQLException

createTableFromTableInfo

private static Table createTableFromTableInfo(net.sourceforge.squirrel_sql.fw.sql.ITableInfo tableInfo)
Helper method which converts from SQuirreL's ITableInfo object to DBNormalizer's Table object.

Parameters:
tableInfo - an ITableInfo object
Returns:
a Table object

getTableContainers

public static Collection<TableContainer> getTableContainers(DBNormalizerSession session)
                                                     throws SQLException
Gets all TableContainers (catalogs or schemas) for the specified DBNormalizer session.

Parameters:
session - the DBNormalizer-Session
Returns:
a collection of TableContainers
Throws:
SQLException

getTableInfo

public static Table getTableInfo(DBNormalizerSession session,
                                 String catalogName,
                                 String schemaName,
                                 String tableName,
                                 boolean complete)
                          throws SQLException
Gets the table with the specified name, catalog and schema.

NOTE: If you set the argument complete to false, the returned table object will not be completely filled, but only contain the table's name, catalog, schema and TableContainer. To get all available information, which is not high-performance, set the argument complete to true.

Parameters:
session - the DBNormalizer session which is connected to the DB
catalogName - the catalog's name, may be null
schemaName - the schema's name, may be null
tableName - the table's (simple) name
complete - true, to read all available information; false, otherwise
Returns:
a table object, if such a table could be found in the DB; null, otherwise
Throws:
SQLException

retrieveCompleteTableInfo

private static void retrieveCompleteTableInfo(DBNormalizerSession session,
                                              Table table)
                                       throws SQLException
Helper method which fills the given table object with further information (Column, PK and FK information).

Parameters:
session - the DBNormalizer session which is connected to the DB
table - a table object
Throws:
SQLException

getCatalogs

public static Collection<String> getCatalogs(DBNormalizerSession session)
                                      throws SQLException
Gets the catalogs for the specified DBNormalizer session.

Parameters:
session - the DBNormalizer session which is connected to the DB
Returns:
the catalogs as a collection of strings
Throws:
SQLException

getSchemas

public static Collection<String> getSchemas(DBNormalizerSession session)
                                     throws SQLException
Gets the schemas for the specified DBNormalizer session.

Parameters:
session - the DBNormalizer session which is connected to the DB
Returns:
the schemas as a collection of strings
Throws:
SQLException

getImportedKeys

public static Collection<FK> getImportedKeys(DatabaseMetaData dbmd,
                                             String catalog,
                                             String schema,
                                             String tableName)
                                      throws SQLException
Gets the imported keys for a table specified by catalog, schema and name.

Parameters:
dbmd - a SQL-DatabaseMetaData-object
catalog - the table's catalog, may be null
schema - the table's schema, may be null
tableName - the table's (simple) name
Returns:
a collection of FKs
Throws:
SQLException
See Also:
DatabaseMetaData.getImportedKeys(String, String, String)

getExportedKeys

public static Collection<FK> getExportedKeys(DatabaseMetaData dbmd,
                                             String catalog,
                                             String schema,
                                             String tableName)
                                      throws SQLException
Gets the exported keys for a table specified by catalog, schema and name.

Parameters:
dbmd - a SQL-DatabaseMetaData-object
catalog - the table's catalog, may be null
schema - the table's schema, may be null
tableName - the table's (simple) name
Returns:
a collection of FKs
Throws:
SQLException
See Also:
DatabaseMetaData.getExportedKeys(String, String, String)

buildFKsFromResultSet

private static Collection<FK> buildFKsFromResultSet(ResultSet rs)
                                             throws SQLException
Helper method creating a collection of FKs from the ResultSet returned from the DatabaseMetaData.getImportedKeys(String, String, String) or DatabaseMetaData.getExportedKeys(String, String, String) method.

Parameters:
rs - the ResultSet
Returns:
a collection of FKs
Throws:
SQLException

createFKKey

private static String createFKKey(FK fk)
Helper method which creates a string which can be used as a FK's key in a map.

Parameters:
fk - a FK
Returns:
the string key

getPK

public static PK getPK(DBNormalizerSession session,
                       String catalog,
                       String schema,
                       String tableName)
                throws SQLException
Gets the PK for a table specified by catalog, schema and name.

Parameters:
session - the DBNormalizer session which is connected to the DB
catalog - the table's catalog, may be null
schema - the table's schema, may be null
tableName - the table's (simple) name
Returns:
the table's PK, if such a table exists and it contains a PK;null, otherwise
Throws:
SQLException

getTableColumnInfos

public static Collection<TableColumn> getTableColumnInfos(DBNormalizerSession session,
                                                          String catalogName,
                                                          String schemaName,
                                                          String tableName)
                                                   throws SQLException
Gets TableColumn objects for a table specified by catalog, schema and name.

Parameters:
session - the DBNormalizer session which is connected to the DB
catalogName - the table's catalog, may be null
schemaName - the table's schema, may be null
tableName - the table's (simple) name
Returns:
a collection of TableColumn objects
Throws:
SQLException