|
3.1.2: 2012-07-31 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.basedb.core.dbengine.TableInfo
public class TableInfo
Holds minimal but useful information about a table in the database. The information can be populated either from Hibernate mappings or from JDBC metadata about the database. This makes it possible to compare the structure of the current database with the information in the Hibernate mapping documents.
The main reason for this class to exists is that we want to solve the problem Hibernate has with creating indexes when doing an update. The information in this class makes it possible to find which indexes are missing. As a bonus we also have code to drop and re-create all indexes.
Nested Class Summary | |
---|---|
static class |
TableInfo.ColumnInfo
Holds information about a single column in a table. |
static class |
TableInfo.ForeignKeyInfo
Holds information about a foreign key of a table. |
static class |
TableInfo.IndexInfo
Holds information about an index in a table. |
static class |
TableInfo.PrimaryKeyInfo
Holds information about the primary key of a table. |
Field Summary | |
---|---|
private long |
approximateNumberOfRows
|
private Set<TableInfo.ColumnInfo> |
columns
|
private Dialect |
dialect
|
private boolean |
existsInDb
|
private Set<TableInfo.ForeignKeyInfo> |
foreignKeys
|
private Set<TableInfo.IndexInfo> |
indexes
|
private long |
numberOfRows
|
private TableInfo.PrimaryKeyInfo |
pkInfo
|
private static Set<Integer> |
STRING_TYPE
|
private Table |
table
|
Constructor Summary | |
---|---|
private |
TableInfo(Table table)
|
|
TableInfo(Table table,
DatabaseMetaData metaData)
Create table info by reading JDBC metadata from the current database |
|
TableInfo(Table table,
Dialect dialect)
Create table info by reading the Hibernate mappings. |
Method Summary | |
---|---|
boolean |
existsInDb()
Check if the table exists in the database or not. |
TableInfo.ColumnInfo |
findColumn(String column)
Find information about a column with the given name. |
String |
findIndexName(String name,
Set<String> columns)
Check if there is an index with the same name or exactly the same set of columns. |
Set<TableInfo.ColumnInfo> |
getColumns()
Get information about the columns in the table. |
Set<TableInfo.ForeignKeyInfo> |
getForeignKeys()
Get information about the foreign keys in the table. |
String |
getFullQuotedTableName(DbEngine engine)
Get the fully quialified and quoted table name using the quoting rules from the given DBEngine |
Set<TableInfo.IndexInfo> |
getIndexes()
Get information about the indexes in the table. |
TableInfo.PrimaryKeyInfo |
getPrimaryKey()
Get information about the primary key of the table |
long |
getRowCount(DbEngine engine,
Connection connection,
boolean approximate)
Count the number of rows in the table. |
Table |
getTable()
Get the table this information is representing. |
static List<Table> |
listTables(DatabaseMetaData metaData,
String catalog,
String schema)
List all tables that are present in the given catalog and/or schema. |
private String |
makeSafe(String name)
Sometime the database put quotes around names. |
boolean |
safeToDrop(TableInfo.IndexInfo ii)
It is safe to drop an index if it is not a foreign key, a primary key index or a unique column. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final Table table
private Dialect dialect
private boolean existsInDb
private Set<TableInfo.ColumnInfo> columns
private TableInfo.PrimaryKeyInfo pkInfo
private Set<TableInfo.ForeignKeyInfo> foreignKeys
private Set<TableInfo.IndexInfo> indexes
private long numberOfRows
private long approximateNumberOfRows
private static final Set<Integer> STRING_TYPE
Constructor Detail |
---|
private TableInfo(Table table)
public TableInfo(Table table, DatabaseMetaData metaData) throws SQLException
table
- The Hibernate table objectmetaData
- The JDBC metadata connected to the current database
SQLException
- If there is an errorpublic TableInfo(Table table, Dialect dialect)
table
- The Hibernate table objectdialect
- The current database dialectMethod Detail |
---|
public static List<Table> listTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException
metaData
- A metadata object that is connected to a databasecatalog
- The name of the catalog, or nullschema
- The name of the schema, or null
SQLException
public Table getTable()
public boolean existsInDb()
TableInfo(Table, DatabaseMetaData)
.
private String makeSafe(String name)
name
- The name to make safe, or null
public Set<TableInfo.ColumnInfo> getColumns()
public TableInfo.PrimaryKeyInfo getPrimaryKey()
public Set<TableInfo.ForeignKeyInfo> getForeignKeys()
public Set<TableInfo.IndexInfo> getIndexes()
public boolean safeToDrop(TableInfo.IndexInfo ii)
In any case, we don't want to mess with primary or foreign key indexes, and Hibernate seems to be able to manage indexes for unique columns (single) so we don't mess with that either.
ii
- The IndexInfo object we want to check if it is safe to drop
public String findIndexName(String name, Set<String> columns)
Hibernate doesn't generate a name for multi-column indexes or unique constraints, which means that we have to find any index matching the column names. In case, Hibernate starts generating the indexes with names, we check against the name as well.
name
- The name of the index to look for, or null to only use the
columnscolumns
- A set containing the column names that should be present in
the index
public TableInfo.ColumnInfo findColumn(String column)
column
- The name of the column
public String getFullQuotedTableName(DbEngine engine)
engine
- A DbEngine
public long getRowCount(DbEngine engine, Connection connection, boolean approximate) throws SQLException
connection
- The connection to user for counting
SQLException
|
3.1.2: 2012-07-31 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |