2.17.2: 2011-06-17

net.sf.basedb.core.dbengine
Class TableInfo

java.lang.Object
  extended by net.sf.basedb.core.dbengine.TableInfo

public class TableInfo
extends Object

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: 2010-08-13 10:50:27 +0200 (Fri, 13 Aug 2010) $

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  Set<TableInfo.ColumnInfo> columns
           
private  org.hibernate.dialect.Dialect dialect
           
private  Set<TableInfo.ForeignKeyInfo> foreignKeys
           
private  Set<TableInfo.IndexInfo> indexes
           
private  TableInfo.PrimaryKeyInfo pkInfo
           
private static Set<Integer> STRING_TYPE
           
private  org.hibernate.mapping.Table table
           
 
Constructor Summary
private TableInfo(org.hibernate.mapping.Table table)
           
  TableInfo(org.hibernate.mapping.Table table, DatabaseMetaData metaData)
          Create table info by reading JDBC metadata from the current database
  TableInfo(org.hibernate.mapping.Table table, org.hibernate.dialect.Dialect dialect)
          Create table info by reading the Hibernate mappings.
 
Method Summary
 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.
 Set<TableInfo.IndexInfo> getIndexes()
          Get information about the indexes in the table.
 TableInfo.PrimaryKeyInfo getPrimaryKey()
          Get information about the primary key of the table
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

table

private org.hibernate.mapping.Table table

dialect

private org.hibernate.dialect.Dialect dialect

columns

private Set<TableInfo.ColumnInfo> columns

pkInfo

private TableInfo.PrimaryKeyInfo pkInfo

foreignKeys

private Set<TableInfo.ForeignKeyInfo> foreignKeys

indexes

private Set<TableInfo.IndexInfo> indexes

STRING_TYPE

private static final Set<Integer> STRING_TYPE
Constructor Detail

TableInfo

private TableInfo(org.hibernate.mapping.Table table)

TableInfo

public TableInfo(org.hibernate.mapping.Table table,
                 DatabaseMetaData metaData)
          throws SQLException
Create table info by reading JDBC metadata from the current database

Parameters:
table - The Hibernate table object
metaData - The JDBC metadata connected to the current database
Throws:
SQLException - If there is an error

TableInfo

public TableInfo(org.hibernate.mapping.Table table,
                 org.hibernate.dialect.Dialect dialect)
Create table info by reading the Hibernate mappings.

Parameters:
table - The Hibernate table object
dialect - The current database dialect
Method Detail

makeSafe

private String makeSafe(String name)
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

public Set<TableInfo.ColumnInfo> getColumns()
Get information about the columns in the table.

Returns:
A set containing information about each column

getPrimaryKey

public TableInfo.PrimaryKeyInfo 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

public Set<TableInfo.ForeignKeyInfo> getForeignKeys()
Get information about the foreign keys in the table.

Returns:
A set containing information about each foreign key

getIndexes

public Set<TableInfo.IndexInfo> 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

public 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.

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.

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

public String findIndexName(String name,
                            Set<String> columns)
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 columns
columns - 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

2.17.2: 2011-06-17