org.clazzes.jdbc2xml.schema
Class SchemaEngine

java.lang.Object
  extended by org.clazzes.jdbc2xml.schema.SchemaEngine
Direct Known Subclasses:
SchemaEngineImpl

public abstract class SchemaEngine
extends java.lang.Object

Author:
lech

Constructor Summary
protected SchemaEngine()
          Default constructor, which sets the default time zone.
 
Method Summary
abstract  TableInfo addColumn(TableInfo ti, ColumnInfo columnInfo)
          Add a column to an existing table.
abstract  TableInfo addForeignKey(TableInfo ti, ForeignKeyInfo fkInfo)
          Add a foreign key to an existing table.
abstract  TableInfo addIndex(TableInfo tableinfo, IndexInfo indexInfo)
          Add an index to an existing table.
abstract  TableInfo changeColumn(TableInfo ti, java.lang.String oldColumnName, ColumnInfo columnInfo)
          Rename an existing column of an existing table and possibly change it's type.
abstract  void commit()
          Perform all pending schema modifications and discard the rollback history.
abstract  void createForeignKeys(TableInfo ti)
          Create foreign keys of an existing table in the database.
abstract  java.sql.PreparedStatement createInsertStatement(TableInfo ti)
          Create an insert statement for all column in the given table.
abstract  void createTable(TableInfo ti, boolean addForeignKeys)
          Create a table in the database, including primary key and indices (which must have been added already).
abstract  TableInfo dropColumn(TableInfo ti, java.lang.String columnName, boolean force)
          Drop an existing column of an existing table.
abstract  TableInfo dropForeignKey(TableInfo ti, java.lang.String fkName)
          Drop an existing foreign key of an existing table.
abstract  TableInfo dropForeignKeys(TableInfo ti)
          Drop all existing foreign keys of an existing table.
abstract  TableInfo dropIndex(TableInfo tableInfo, java.lang.String indexName)
          Drop an index from an existing table.
abstract  void dropStaleBackupTables()
          Some operations like drop table or drop column create backup tables for performing rollback operations.
abstract  void dropTable(TableInfo ti, boolean force)
          Drop an existing table.
abstract  void dropTables(java.util.List<TableInfo> tables, boolean force)
          Drop a list of existing table.
abstract  TableInfo fetchTableInfo(java.lang.String tableName, IndexFilter filter)
          Retrieve the current layout of the given table in the database.
abstract  java.util.List<TableInfo> fetchTableInfos(TableFilter filter)
          Return the layout of multiple table in the database.
 java.sql.Connection getConnection()
           
 Dialect getDialect()
           
 java.lang.String getSchema()
           
 java.util.TimeZone getTimeZone()
           
abstract  TableInfo modifyColumn(TableInfo ti, ColumnInfo columnInfo)
          Change an existing column of an existing table.
static SchemaEngine newInstance()
          After generating a new SchemaEngine instance you usually have to call setConnection(Connection) in order to set up the database on which to operate.
abstract  void rollback()
          Roll back all schema operations undertaken since the last call to commit() or rollback().
 void setConnection(java.sql.Connection connection)
          This method initializes the database dialect too, if the the dialect has not been set through setDialect(Dialect) before.
 void setDialect(Dialect dialect)
          Normally you need not call this method, because the dialect is usually initialized by setConnection(Connection).
 void setSchema(java.lang.String schema)
           
 void setTimeZone(java.util.TimeZone timeZone)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SchemaEngine

protected SchemaEngine()
Default constructor, which sets the default time zone.

Method Detail

newInstance

public static SchemaEngine newInstance()
After generating a new SchemaEngine instance you usually have to call setConnection(Connection) in order to set up the database on which to operate. Additionally, it is recommended, that you configure the default time zone using {setTimeZone(TimeZone).

Returns:
The first implementation registered under the resource path META-INF/services/org.clazzes.jdbc2xml.schema.SchemaEngine.
See Also:
ServiceRegistry.lookupProviders(Class)

getConnection

public java.sql.Connection getConnection()
Returns:
The database connection.

setConnection

public void setConnection(java.sql.Connection connection)
                   throws java.sql.SQLException
This method initializes the database dialect too, if the the dialect has not been set through setDialect(Dialect) before. The dialect is retrieved through a call to DialectFactory.newDialect(java.sql.DatabaseMetaData) on the instance returned by DialectFactory.newInstance().

Parameters:
connection - The database connection to set.
Throws:
java.sql.SQLException - If the metadata retrieval on the database failed during the dialect initialization.

getTimeZone

public java.util.TimeZone getTimeZone()
Returns:
The default time zone used for transforming date values, if the database engine does not store a time zone information.

setTimeZone

public void setTimeZone(java.util.TimeZone timeZone)
Parameters:
timeZone - The time zone to set.

getDialect

public Dialect getDialect()
Returns:
The dialect used by this engine.

setDialect

public void setDialect(Dialect dialect)
Normally you need not call this method, because the dialect is usually initialized by setConnection(Connection).

Parameters:
dialect - The database dialect to set

getSchema

public java.lang.String getSchema()
Returns:
The schema name used for retrieving schema metadata. The schema is most often null, because most databases use a single namespace for tables, if the connection is opened with a valid catalog.
See Also:
DatabaseMetaData.getTables(String, String, String, String[]), DatabaseMetaData.getColumns(String, String, String, String), DatabaseMetaData.getImportedKeys(String, String, String), DatabaseMetaData.getExportedKeys(String, String, String), DatabaseMetaData.getPrimaryKeys(String, String, String)

setSchema

public void setSchema(java.lang.String schema)
Parameters:
schema - The schema to set

fetchTableInfo

public abstract TableInfo fetchTableInfo(java.lang.String tableName,
                                         IndexFilter filter)
                                  throws java.sql.SQLException
Retrieve the current layout of the given table in the database.

Parameters:
tableName - The name of the table to search.
filter - The filter retrieval configuration. If a null value is passed, a default filter will be used, which skips internal indices.
Returns:
The complete table information.
Throws:
java.sql.SQLException - Upon database errors.
See Also:
IndexFilter.isKeepInternalIndices()

fetchTableInfos

public abstract java.util.List<TableInfo> fetchTableInfos(TableFilter filter)
                                                   throws java.sql.SQLException
Return the layout of multiple table in the database.

Parameters:
filter - If null, return all tables of the configured schema. Otherwise return only those tables, for which TableFilter.processTable(String) returns true. Additionally, a null value incurs default table filtering, which means to fetch all table infos.
Returns:
The list of table informations.
Throws:
java.sql.SQLException - Upon database errors.

createInsertStatement

public abstract java.sql.PreparedStatement createInsertStatement(TableInfo ti)
                                                          throws java.sql.SQLException
Create an insert statement for all column in the given table. The placeholders in the insert statement are in the order of the ColumnInfo object inside the TableInfo's list of column

Parameters:
ti - The table metadata.
Returns:
A Prepared statement that may be used to insert data into the database.
Throws:
java.sql.SQLException - Upon database errors.

createTable

public abstract void createTable(TableInfo ti,
                                 boolean addForeignKeys)
                          throws java.sql.SQLException
Create a table in the database, including primary key and indices (which must have been added already).

Parameters:
ti - The table metadata.
addForeignKeys - If this parameter is true, all foreign keys are generated together with the table structure. If set to false, you may create the foreign keys later on using createForeignKeys(TableInfo).
Throws:
java.sql.SQLException - upon database errors.

createForeignKeys

public abstract void createForeignKeys(TableInfo ti)
                                throws java.sql.SQLException
Create foreign keys of an existing table in the database.

Parameters:
ti - The table metadata.
Throws:
java.sql.SQLException - Upon database errors.

dropForeignKeys

public abstract TableInfo dropForeignKeys(TableInfo ti)
                                   throws java.sql.SQLException
Drop all existing foreign keys of an existing table.

Parameters:
ti - TableInfo describing the table containing the foreign keys to drop.
Returns:
The updated TableInfo without foreign keys.
Throws:
java.sql.SQLException - Upon database errors.

dropTable

public abstract void dropTable(TableInfo ti,
                               boolean force)
                        throws java.sql.SQLException
Drop an existing table.

Parameters:
ti - TableInfo describing the table to be dropped.
force - If set to true, the data is not backed up before dropping the table. Consequently, rollback is not supported, if force is set to true.
Throws:
java.sql.SQLException - Upon database errors.

dropTables

public abstract void dropTables(java.util.List<TableInfo> tables,
                                boolean force)
                         throws java.sql.SQLException
Drop a list of existing table.

Parameters:
tables - A list of TableInfo objects describing the tables to be dropped.
force - If set to true, the data is not backed up before dropping the table. Consequently, rollback is not supported, if force is set to true.
Throws:
java.sql.SQLException - Upon database errors.

addColumn

public abstract TableInfo addColumn(TableInfo ti,
                                    ColumnInfo columnInfo)
                             throws java.sql.SQLException
Add a column to an existing table.

Parameters:
ti - TableInfo describing the table to add column to.
columnInfo - ColumnInfo holding data for the column to be added.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

modifyColumn

public abstract TableInfo modifyColumn(TableInfo ti,
                                       ColumnInfo columnInfo)
                                throws java.sql.SQLException
Change an existing column of an existing table.

Parameters:
ti - TableInfo describing the table containing the column to change.
columnInfo - ColumnInfo holding new definition data for the column.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

changeColumn

public abstract TableInfo changeColumn(TableInfo ti,
                                       java.lang.String oldColumnName,
                                       ColumnInfo columnInfo)
                                throws java.sql.SQLException
Rename an existing column of an existing table and possibly change it's type.

Parameters:
ti - TableInfo describing the table containing the column to change.
oldColumnName - The original name of the column.
columnInfo - ColumnInfo holding the new name and the new definition data for the column.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

dropColumn

public abstract TableInfo dropColumn(TableInfo ti,
                                     java.lang.String columnName,
                                     boolean force)
                              throws java.sql.SQLException
Drop an existing column of an existing table.

Parameters:
ti - TableInfo describing the table containing the column to drop.
columnName - Name of the column to drop.
force - If set to true, the data is not backed up before dropping the column. Consequently, rollback is not supported, if force is set to true.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

addForeignKey

public abstract TableInfo addForeignKey(TableInfo ti,
                                        ForeignKeyInfo fkInfo)
                                 throws java.sql.SQLException
Add a foreign key to an existing table.

Parameters:
ti - TableInfo describing the table to add the foreign key to.
fkInfo - The ForeignKeyInfo to add.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

dropForeignKey

public abstract TableInfo dropForeignKey(TableInfo ti,
                                         java.lang.String fkName)
                                  throws java.sql.SQLException
Drop an existing foreign key of an existing table.

Parameters:
ti - TableInfo describing the table to drop the foreign key from.
fkName - The ForeignKeyInfo to drop.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

addIndex

public abstract TableInfo addIndex(TableInfo tableinfo,
                                   IndexInfo indexInfo)
                            throws java.sql.SQLException
Add an index to an existing table.

Parameters:
tableinfo - TableInfo describing the table to add the index.
indexInfo - The IndexInfo to add.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

dropIndex

public abstract TableInfo dropIndex(TableInfo tableInfo,
                                    java.lang.String indexName)
                             throws java.sql.SQLException
Drop an index from an existing table.

Parameters:
tableInfo - TableInfo describing the table to drop the index from.
indexName - The name of the IndexInfo to drop.
Returns:
The updated TableInfo.
Throws:
java.sql.SQLException - Upon database errors.

dropStaleBackupTables

public abstract void dropStaleBackupTables()
                                    throws java.sql.SQLException
Some operations like drop table or drop column create backup tables for performing rollback operations. If a rollback of such an operation fails or a user does not commit() after such operations, these backup tables remain in the target database. This call searches these tables, which are named like JDBC2XML__* and drops them, if necessary.

Throws:
java.sql.SQLException

commit

public abstract void commit()
                     throws java.sql.SQLException
Perform all pending schema modifications and discard the rollback history.

Throws:
java.sql.SQLException - Upon database errors.

rollback

public abstract void rollback()
                       throws java.sql.SQLException
Roll back all schema operations undertaken since the last call to commit() or rollback().

Throws:
java.sql.SQLException - Upon database errors or when a rollback of a schema operation is not supported.


Copyright © 2008. All Rights Reserved.