Class JdbcModelReader
java.lang.Object
edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.JdbcModelReader
- Direct Known Subclasses:
AxionModelReader
,Db2ModelReader
,DerbyModelReader
,FirebirdModelReader
,HsqlDbModelReader
,InterbaseModelReader
,MaxDbModelReader
,MckoiModelReader
,MSSqlModelReader
,MySqlModelReader
,Oracle8ModelReader
,PostgreSqlModelReader
,SapDbModelReader
,SybaseModelReader
An utility class to create a Database model from a live database.
- Version:
- $Revision: 543392 $
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
determineAutoIncrementFromResultSetMetaData
(Table table, Column[] columnsToCheck) Helper method that determines the auto increment status for the given columns via theResultSetMetaData.isAutoIncrement(int)
method.determineSchemaOf
(Connection connection, String schemaPattern, Table table) Deprecated.Will be removed once full schema support is in placeprotected List
Returns the descriptors for the columns to be read from the column meta data result set.protected List
Returns the descriptors for the columns to be read from the foreign key meta data result set.protected List
Returns the descriptors for the columns to be read from the index meta data result set.protected List
Returns the descriptors for the columns to be read from the primary key meta data result set.protected List
Returns the descriptors for the columns to be read from the table meta data result set.protected Connection
Returns the active connection.getDatabase
(Connection connection, String name) Reads the database model from the given connection.getDatabase
(Connection connection, String name, String catalog, String schema, String[] tableTypes) Reads the database model from the given connection.Returns the catalog(s) in the database to read per default.Returns the default pattern to read the relevant columns from the database.Returns the schema(s) in the database to read per default.Returns the default pattern to read the relevant tables from the database.String[]
Returns the table types to recognize per default.Returns the platform that this model reader belongs to.Returns the platform specific settings.protected List
Returns descriptors for the columns that shall be read from the result set when reading the meta data for table columns.protected List
Returns descriptors for the columns that shall be read from the result set when reading the meta data for foreign keys originating from a table.protected List
Returns descriptors for the columns that shall be read from the result set when reading the meta data for indices.protected List
Returns descriptors for the columns that shall be read from the result set when reading the meta data for primary keys.protected List
Returns descriptors for the columns that shall be read from the result set when reading the meta data for a table.protected boolean
isInternalForeignKeyIndex
(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk, Index index) Tries to determine whether the index is the internal database-generated index for the given foreign key.protected boolean
isInternalPrimaryKeyIndex
(DatabaseMetaDataWrapper metaData, Table table, Index index) Tries to determine whether the index is the internal database-generated index for the given table's primary key.protected boolean
Checks whether the given index matches the column list.protected Column
readColumn
(DatabaseMetaDataWrapper metaData, Map values) Extracts a column definition from the result set.protected Collection
readColumns
(DatabaseMetaDataWrapper metaData, String tableName) Reads the column definitions for the indicated table.protected Map
readColumns
(ResultSet resultSet, List columnDescriptors) Reads the indicated columns from the result set.protected void
readForeignKey
(DatabaseMetaDataWrapper metaData, Map values, Map knownFks) Reads the next foreign key spec from the result set.protected Collection
readForeignKeys
(DatabaseMetaDataWrapper metaData, String tableName) Retrieves the foreign keys of the indicated table.protected void
readIndex
(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) Reads the next index spec from the result set.protected Collection
readIndices
(DatabaseMetaDataWrapper metaData, String tableName) Determines the indices for the indicated table.protected String
readPrimaryKeyName
(DatabaseMetaDataWrapper metaData, Map values) Extracts a primary key name from the result set.protected Collection
readPrimaryKeyNames
(DatabaseMetaDataWrapper metaData, String tableName) Retrieves the names of the columns that make up the primary key for a given table.protected Table
readTable
(DatabaseMetaDataWrapper metaData, Map values) Reads the next table from the meta data.protected Collection
readTables
(String catalog, String schemaPattern, String[] tableTypes) Reads the tables from the database metadata.protected void
removeInternalForeignKeyIndex
(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk) Tries to remove the internal index for the given foreign key.protected void
removeInternalPrimaryKeyIndex
(DatabaseMetaDataWrapper metaData, Table table) Tries to remove the internal index for the table's primary key.protected void
removeSystemIndices
(DatabaseMetaDataWrapper metaData, Table table) Removes system indices (generated by the database for primary and foreign keys) from the table.void
setDefaultCatalogPattern
(String catalogPattern) Sets the catalog(s) in the database to read per default.void
setDefaultColumnPattern
(String columnPattern) Sets the default pattern to read the relevant columns from the database.void
setDefaultSchemaPattern
(String schemaPattern) Sets the schema(s) in the database to read per default.void
setDefaultTablePattern
(String tablePattern) Sets the default pattern to read the relevant tables from the database.void
setDefaultTableTypes
(String[] types) Sets the table types to recognize per default.protected void
sortForeignKeys
(Database model) Sorts the foreign keys in the tables of the model.protected String
Replaces a specific character sequence in the given text with the character sequence whose escaped version it is.
-
Constructor Details
-
JdbcModelReader
Creates a new model reader instance.- Parameters:
platform
- The plaftform this builder belongs to
-
-
Method Details
-
getPlatform
Returns the platform that this model reader belongs to.- Returns:
- The platform
-
getPlatformInfo
Returns the platform specific settings.- Returns:
- The platform settings
-
initColumnsForTable
Returns descriptors for the columns that shall be read from the result set when reading the meta data for a table. Note that the columns are read in the order defined by this list.
Redefine this method if you want more columns or a different order.- Returns:
- The descriptors for the result set columns
-
initColumnsForColumn
Returns descriptors for the columns that shall be read from the result set when reading the meta data for table columns. Note that the columns are read in the order defined by this list.
Redefine this method if you want more columns or a different order.- Returns:
- The map column name -> descriptor for the result set columns
-
initColumnsForPK
Returns descriptors for the columns that shall be read from the result set when reading the meta data for primary keys. Note that the columns are read in the order defined by this list.
Redefine this method if you want more columns or a different order.- Returns:
- The map column name -> descriptor for the result set columns
-
initColumnsForFK
Returns descriptors for the columns that shall be read from the result set when reading the meta data for foreign keys originating from a table. Note that the columns are read in the order defined by this list.
Redefine this method if you want more columns or a different order.- Returns:
- The map column name -> descriptor for the result set columns
-
initColumnsForIndex
Returns descriptors for the columns that shall be read from the result set when reading the meta data for indices. Note that the columns are read in the order defined by this list.
Redefine this method if you want more columns or a different order.- Returns:
- The map column name -> descriptor for the result set columns
-
getDefaultCatalogPattern
Returns the catalog(s) in the database to read per default.- Returns:
- The default catalog(s)
-
setDefaultCatalogPattern
Sets the catalog(s) in the database to read per default.- Parameters:
catalogPattern
- The catalog(s)
-
getDefaultSchemaPattern
Returns the schema(s) in the database to read per default.- Returns:
- The default schema(s)
-
setDefaultSchemaPattern
Sets the schema(s) in the database to read per default.- Parameters:
schemaPattern
- The schema(s)
-
getDefaultTablePattern
Returns the default pattern to read the relevant tables from the database.- Returns:
- The table pattern
-
setDefaultTablePattern
Sets the default pattern to read the relevant tables from the database.- Parameters:
tablePattern
- The table pattern
-
getDefaultColumnPattern
Returns the default pattern to read the relevant columns from the database.- Returns:
- The column pattern
-
setDefaultColumnPattern
Sets the default pattern to read the relevant columns from the database.- Parameters:
columnPattern
- The column pattern
-
getDefaultTableTypes
Returns the table types to recognize per default.- Returns:
- The default table types
-
setDefaultTableTypes
Sets the table types to recognize per default. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".- Parameters:
types
- The table types
-
getColumnsForTable
Returns the descriptors for the columns to be read from the table meta data result set.- Returns:
- The column descriptors
-
getColumnsForColumn
Returns the descriptors for the columns to be read from the column meta data result set.- Returns:
- The column descriptors
-
getColumnsForPK
Returns the descriptors for the columns to be read from the primary key meta data result set.- Returns:
- The column descriptors
-
getColumnsForFK
Returns the descriptors for the columns to be read from the foreign key meta data result set.- Returns:
- The column descriptors
-
getColumnsForIndex
Returns the descriptors for the columns to be read from the index meta data result set.- Returns:
- The column descriptors
-
getConnection
Returns the active connection. Note that this is only set during a call toreadTables(String, String, String[])
.- Returns:
- The connection or
null
if there is no active connection
-
getDatabase
Reads the database model from the given connection.- Parameters:
connection
- The connectionname
- The name of the resulting database;null
when the default name (the catalog) is desired which might benull
itself though- Returns:
- The database model
- Throws:
SQLException
-
getDatabase
public Database getDatabase(Connection connection, String name, String catalog, String schema, String[] tableTypes) throws SQLException Reads the database model from the given connection.- Parameters:
connection
- The connectionname
- The name of the resulting database;null
when the default name (the catalog) is desired which might benull
itself thoughcatalog
- The catalog to acess in the database; usenull
for the default valueschema
- The schema to acess in the database; usenull
for the default valuetableTypes
- The table types to process; usenull
or an empty list for the default ones- Returns:
- The database model
- Throws:
SQLException
-
readTables
protected Collection readTables(String catalog, String schemaPattern, String[] tableTypes) throws SQLException Reads the tables from the database metadata.- Parameters:
catalog
- The catalog to acess in the database; usenull
for the default valueschemaPattern
- The schema(s) to acess in the database; usenull
for the default valuetableTypes
- The table types to process; usenull
or an empty list for the default ones- Returns:
- The tables
- Throws:
SQLException
-
readTable
Reads the next table from the meta data.- Parameters:
metaData
- The database meta datavalues
- The table metadata values as defined bygetColumnsForTable()
- Returns:
- The table or
null
if the result set row did not contain a valid table - Throws:
SQLException
-
removeSystemIndices
protected void removeSystemIndices(DatabaseMetaDataWrapper metaData, Table table) throws SQLException Removes system indices (generated by the database for primary and foreign keys) from the table.- Parameters:
metaData
- The database meta datatable
- The table- Throws:
SQLException
-
removeInternalPrimaryKeyIndex
protected void removeInternalPrimaryKeyIndex(DatabaseMetaDataWrapper metaData, Table table) throws SQLException Tries to remove the internal index for the table's primary key.- Parameters:
metaData
- The database meta datatable
- The table- Throws:
SQLException
-
removeInternalForeignKeyIndex
protected void removeInternalForeignKeyIndex(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk) throws SQLException Tries to remove the internal index for the given foreign key.- Parameters:
metaData
- The database meta datatable
- The table where the table is definedfk
- The foreign key- Throws:
SQLException
-
matches
Checks whether the given index matches the column list.- Parameters:
index
- The indexcolumnsToSearchFor
- The names of the columns that the index should be for- Returns:
true
if the index matches the columns
-
isInternalPrimaryKeyIndex
protected boolean isInternalPrimaryKeyIndex(DatabaseMetaDataWrapper metaData, Table table, Index index) throws SQLException Tries to determine whether the index is the internal database-generated index for the given table's primary key. Note that only unique indices with the correct columns are fed to this method. Redefine this method for specific platforms if there are better ways to determine internal indices.- Parameters:
metaData
- The database meta datatable
- The table owning the indexindex
- The index to check- Returns:
true
if the index seems to be an internal primary key one- Throws:
SQLException
-
isInternalForeignKeyIndex
protected boolean isInternalForeignKeyIndex(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk, Index index) throws SQLException Tries to determine whether the index is the internal database-generated index for the given foreign key. Note that only non-unique indices with the correct columns are fed to this method. Redefine this method for specific platforms if there are better ways to determine internal indices.- Parameters:
metaData
- The database meta datatable
- The table owning the index and foreign keyfk
- The foreign keyindex
- The index to check- Returns:
true
if the index seems to be an internal primary key one- Throws:
SQLException
-
readColumns
protected Collection readColumns(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException Reads the column definitions for the indicated table.- Parameters:
metaData
- The database meta datatableName
- The name of the table- Returns:
- The columns
- Throws:
SQLException
-
readColumn
Extracts a column definition from the result set.- Parameters:
metaData
- The database meta datavalues
- The column meta data values as defined bygetColumnsForColumn()
- Returns:
- The column
- Throws:
SQLException
-
readPrimaryKeyNames
protected Collection readPrimaryKeyNames(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException Retrieves the names of the columns that make up the primary key for a given table.- Parameters:
metaData
- The database meta datatableName
- The name of the table from which to retrieve PK information- Returns:
- The primary key column names
- Throws:
SQLException
-
readPrimaryKeyName
protected String readPrimaryKeyName(DatabaseMetaDataWrapper metaData, Map values) throws SQLException Extracts a primary key name from the result set.- Parameters:
metaData
- The database meta datavalues
- The primary key meta data values as defined bygetColumnsForPK()
- Returns:
- The primary key name
- Throws:
SQLException
-
readForeignKeys
protected Collection readForeignKeys(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException Retrieves the foreign keys of the indicated table.- Parameters:
metaData
- The database meta datatableName
- The name of the table from which to retrieve FK information- Returns:
- The foreign keys
- Throws:
SQLException
-
readForeignKey
protected void readForeignKey(DatabaseMetaDataWrapper metaData, Map values, Map knownFks) throws SQLException Reads the next foreign key spec from the result set.- Parameters:
metaData
- The database meta datavalues
- The foreign key meta data as defined bygetColumnsForFK()
knownFks
- The already read foreign keys for the current table- Throws:
SQLException
-
readIndices
protected Collection readIndices(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException Determines the indices for the indicated table.- Parameters:
metaData
- The database meta datatableName
- The name of the table- Returns:
- The list of indices
- Throws:
SQLException
-
readIndex
protected void readIndex(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) throws SQLException Reads the next index spec from the result set.- Parameters:
metaData
- The database meta datavalues
- The index meta data as defined bygetColumnsForIndex()
knownIndices
- The already read indices for the current table- Throws:
SQLException
-
readColumns
Reads the indicated columns from the result set.- Parameters:
resultSet
- The result setcolumnDescriptors
- The dscriptors of the columns to read- Returns:
- The read values keyed by the column name
- Throws:
SQLException
-
determineAutoIncrementFromResultSetMetaData
protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck) throws SQLException Helper method that determines the auto increment status for the given columns via theResultSetMetaData.isAutoIncrement(int)
method.- Parameters:
table
- The tablecolumnsToCheck
- The columns to check (e.g. the primary key columns)- Throws:
SQLException
-
sortForeignKeys
Sorts the foreign keys in the tables of the model.- Parameters:
model
- The model
-
unescape
Replaces a specific character sequence in the given text with the character sequence whose escaped version it is.- Parameters:
text
- The textunescaped
- The unescaped string, e.g. "'"escaped
- The escaped version, e.g. "''"- Returns:
- The resulting text
-
determineSchemaOf
public String determineSchemaOf(Connection connection, String schemaPattern, Table table) throws SQLException Deprecated.Will be removed once full schema support is in placeTries to find the schema to which the given table belongs.- Parameters:
connection
- The database connectionschemaPattern
- The schema pattern to limit the schemas to search intable
- The table to search for- Returns:
- The schema name or
null
if the schema of the table could not be found - Throws:
SQLException
-