Package net.sf.basedb.core.dbengine
Class TableInfo
java.lang.Object
net.sf.basedb.core.dbengine.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.
- Version:
- 2.0
- Author:
- nicklas
- Last modified
- $Date: 2021-06-07 11:22:59 +0200 (Mon, 07 Jun 2021) $
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Holds information about a single column in a table.static class
Holds information about a foreign key of a table.static class
Holds information about an index in a table.static class
Holds information about the primary key of a table. -
Field Summary
Modifier and TypeFieldDescriptionprivate long
private Set<TableInfo.ColumnInfo>
private Dialect
private boolean
private Set<TableInfo.ForeignKeyInfo>
private Set<TableInfo.IndexInfo>
private long
private TableInfo.PrimaryKeyInfo
private final Table
-
Constructor Summary
ModifierConstructorDescriptionprivate
TableInfo
(Table table, DatabaseMetaData metaData) Deprecated.TableInfo
(Table table, DatabaseMetaData metaData, String defaultCatalog, String defaultSchema) Create table info by reading JDBC metadata from the current databaseCreate table info by reading the Hibernate mappings. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Check if the table exists in the database or not.Find all indexes with the given set of columns.findColumn
(String column) Find information about a column with the given name.findForeignKeyName
(String name, Set<String> columns, String refName) Check if there is a foreign key with the same name or exactly the same set of columns referencing the same table.findIndexName
(String name, Set<String> columns) Check if there is an index with the same name or exactly the same set of columns.Get information about the columns in the table.Get information about the foreign keys in the table.getFullQuotedTableName
(DbEngine engine) Get the fully quialified and quoted table name using the quoting rules from the given DBEngineGet information about the indexes in the table.Get information about the primary key of the tablelong
getRowCount
(DbEngine engine, Connection connection, boolean approximate) Count the number of rows in the table.getTable()
Get the table this information is representing.listTables
(DatabaseMetaData metaData, String catalog, String schema) List all tables that are present in the given catalog and/or schema.private String
Sometime the database put quotes around names.boolean
It is safe to drop an index if it is not the primary key.
-
Field Details
-
table
-
dialect
-
existsInDb
private boolean existsInDb -
columns
-
pkInfo
-
foreignKeys
-
indexes
-
numberOfRows
private long numberOfRows -
approximateNumberOfRows
private long approximateNumberOfRows -
STRING_TYPE
-
-
Constructor Details
-
TableInfo
-
TableInfo
Deprecated.In 3.14, useTableInfo(Table, DatabaseMetaData, String, String)
insteadCreate table info by reading JDBC metadata from the current database- Parameters:
table
- The Hibernate table objectmetaData
- The JDBC metadata connected to the current database- Throws:
SQLException
- If there is an error
-
TableInfo
public TableInfo(Table table, DatabaseMetaData metaData, String defaultCatalog, String defaultSchema) throws SQLException Create table info by reading JDBC metadata from the current database- Parameters:
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 catalog- Throws:
SQLException
- If there is an error- Since:
- 3.4
-
TableInfo
Create table info by reading the Hibernate mappings.- Parameters:
table
- The Hibernate table objectdialect
- The current database dialect
-
-
Method Details
-
listTables
public static List<Table> listTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException List all tables that are present in the given catalog and/or schema.- Parameters:
metaData
- A metadata object that is connected to a databasecatalog
- The name of the catalog, or nullschema
- The name of the schema, or null- Returns:
- A list of tables
- Throws:
SQLException
- Since:
- 3.1
-
getTable
Get the table this information is representing.- Returns:
- The table object
- Since:
- 3.1
-
existsInDb
public boolean existsInDb()Check if the table exists in the database or not. This value is only meaningful if the TableInfo object was created from JDBC metadata usingTableInfo(Table, DatabaseMetaData)
.- Since:
- 3.1
-
makeSafe
Sometime the database put quotes around names. This method strips all extra characters.- Parameters:
name
- The name to make safe, or null- Returns:
- The safe name or null
- Since:
- 2.1.1
-
getColumns
Get information about the columns in the table.- Returns:
- A set containing information about each column
-
getPrimaryKey
Get information about the primary key of the table- Returns:
- A PrimaryKeyInfo object, or null if the table doesn't have a primary key
-
getForeignKeys
Get information about the foreign keys in the table.- Returns:
- A set containing information about each foreign key
-
findForeignKeyName
Check if there is a foreign key with the same name or exactly the same set of columns referencing the same table.- Parameters:
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 table- Returns:
- The name of the foreign key, or null if no foreign key was found
- Since:
- 3.8
-
getIndexes
Get information about the indexes in the table. Note! Some databases ie. MySQL, uses indexes to keep track of foreign and primary keys. This means that the information in this set includes one index for each primary/foreign key in addition to those indexes created for other reasons.- Returns:
- A set containing information about each index
-
safeToDrop
It is safe to drop an index if it is not the primary key.- Parameters:
ii
- The IndexInfo object we want to check if it is safe to drop- Returns:
- TRUE if is safe to drop the index, FALSE otherwise
-
findIndexName
Check if there is an index with the same name or exactly the same set of 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.
- Parameters:
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- Returns:
- The name of the index, or null if no index was found
-
findAll
Find all indexes with the given set of columns. Used for getting rid of duplicates.- Parameters:
columns
- A set containing the column names that should be present in the index- Returns:
- A list with the index information or an empty list of no index is found
- Since:
- 3.4
-
findColumn
Find information about a column with the given name.- Parameters:
column
- The name of the column- Returns:
- The column information or null if no column is found
- Since:
- 3.1
-
getFullQuotedTableName
Get the fully quialified and quoted table name using the quoting rules from the given DBEngine- Parameters:
engine
- A DbEngine- Returns:
- The fully quoted table name
- Since:
- 3.1
-
getRowCount
public long getRowCount(DbEngine engine, Connection connection, boolean approximate) throws SQLException Count the number of rows in the table.- Parameters:
connection
- The connection to user for counting- Throws:
SQLException
- Since:
- 3.1
-
TableInfo(Table, DatabaseMetaData, String, String)
instead