public class TableInfo
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
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.
|
Modifier and Type | Field and Description |
---|---|
private long |
approximateNumberOfRows |
private java.util.Set<TableInfo.ColumnInfo> |
columns |
private Dialect |
dialect |
private boolean |
existsInDb |
private java.util.Set<TableInfo.ForeignKeyInfo> |
foreignKeys |
private java.util.Set<TableInfo.IndexInfo> |
indexes |
private long |
numberOfRows |
private TableInfo.PrimaryKeyInfo |
pkInfo |
private static java.util.Set<java.lang.Integer> |
STRING_TYPE |
private Table |
table |
Modifier | Constructor and Description |
---|---|
private |
TableInfo(Table table) |
|
TableInfo(Table table,
java.sql.DatabaseMetaData metaData)
Deprecated.
In 3.14, use
TableInfo(Table, DatabaseMetaData, String, String) instead |
|
TableInfo(Table table,
java.sql.DatabaseMetaData metaData,
java.lang.String defaultCatalog,
java.lang.String defaultSchema)
Create table info by reading JDBC metadata from the current database
|
|
TableInfo(Table table,
Dialect dialect)
Create table info by reading the Hibernate mappings.
|
Modifier and Type | Method and Description |
---|---|
boolean |
existsInDb()
Check if the table exists in the database or not.
|
java.util.List<TableInfo.IndexInfo> |
findAll(java.util.Set<java.lang.String> columns)
Find all indexes with the given set of columns.
|
TableInfo.ColumnInfo |
findColumn(java.lang.String column)
Find information about a column with the given name.
|
java.lang.String |
findForeignKeyName(java.lang.String name,
java.util.Set<java.lang.String> columns,
java.lang.String refName)
Check if there is a foreign key with the same name or exactly the same
set of columns referencing the same table.
|
java.lang.String |
findIndexName(java.lang.String name,
java.util.Set<java.lang.String> columns)
Check if there is an index with the same name or exactly the same
set of columns.
|
java.util.Set<TableInfo.ColumnInfo> |
getColumns()
Get information about the columns in the table.
|
java.util.Set<TableInfo.ForeignKeyInfo> |
getForeignKeys()
Get information about the foreign keys in the table.
|
java.lang.String |
getFullQuotedTableName(DbEngine engine)
Get the fully quialified and quoted table name using
the quoting rules from the given DBEngine
|
java.util.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,
java.sql.Connection connection,
boolean approximate)
Count the number of rows in the table.
|
Table |
getTable()
Get the table this information is representing.
|
static java.util.List<Table> |
listTables(java.sql.DatabaseMetaData metaData,
java.lang.String catalog,
java.lang.String schema)
List all tables that are present in the given catalog and/or schema.
|
private java.lang.String |
makeSafe(java.lang.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 the primary key.
|
private final Table table
private Dialect dialect
private boolean existsInDb
private java.util.Set<TableInfo.ColumnInfo> columns
private TableInfo.PrimaryKeyInfo pkInfo
private java.util.Set<TableInfo.ForeignKeyInfo> foreignKeys
private java.util.Set<TableInfo.IndexInfo> indexes
private long numberOfRows
private long approximateNumberOfRows
private static final java.util.Set<java.lang.Integer> STRING_TYPE
private TableInfo(Table table)
@Deprecated public TableInfo(Table table, java.sql.DatabaseMetaData metaData) throws java.sql.SQLException
TableInfo(Table, DatabaseMetaData, String, String)
insteadtable
- The Hibernate table objectmetaData
- The JDBC metadata connected to the current databasejava.sql.SQLException
- If there is an errorpublic TableInfo(Table table, java.sql.DatabaseMetaData metaData, java.lang.String defaultCatalog, java.lang.String defaultSchema) throws java.sql.SQLException
table
- The Hibernate table objectmetaData
- The JDBC metadata connected to the current databasedefaultCatalog
- Catalog to use if the table doesn't specify a catalogdefaultSchema
- Schema to use if the table doesn't specify a catalogjava.sql.SQLException
- If there is an errorpublic static java.util.List<Table> listTables(java.sql.DatabaseMetaData metaData, java.lang.String catalog, java.lang.String schema) throws java.sql.SQLException
metaData
- A metadata object that is connected to a databasecatalog
- The name of the catalog, or nullschema
- The name of the schema, or nulljava.sql.SQLException
public Table getTable()
public boolean existsInDb()
TableInfo(Table, DatabaseMetaData)
.private java.lang.String makeSafe(java.lang.String name)
name
- The name to make safe, or nullpublic java.util.Set<TableInfo.ColumnInfo> getColumns()
public TableInfo.PrimaryKeyInfo getPrimaryKey()
public java.util.Set<TableInfo.ForeignKeyInfo> getForeignKeys()
public java.lang.String findForeignKeyName(java.lang.String name, java.util.Set<java.lang.String> columns, java.lang.String refName)
name
- The name of the foreign key to look for, or null to only use the
columnscolumns
- A set containing the column names that should be present in
the foreign keyrefName
- The referenced tablepublic java.util.Set<TableInfo.IndexInfo> getIndexes()
public boolean safeToDrop(TableInfo.IndexInfo ii)
ii
- The IndexInfo object we want to check if it is safe to droppublic java.lang.String findIndexName(java.lang.String name, java.util.Set<java.lang.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 indexpublic java.util.List<TableInfo.IndexInfo> findAll(java.util.Set<java.lang.String> columns)
columns
- A set containing the column names that should be present in
the indexpublic TableInfo.ColumnInfo findColumn(java.lang.String column)
column
- The name of the columnpublic java.lang.String getFullQuotedTableName(DbEngine engine)
engine
- A DbEnginepublic long getRowCount(DbEngine engine, java.sql.Connection connection, boolean approximate) throws java.sql.SQLException
connection
- The connection to user for countingjava.sql.SQLException