com.partnersoft.sql
Class DatabaseMigrationTool

java.lang.Object
  extended by com.partnersoft.sql.DatabaseMigrationTool

public class DatabaseMigrationTool
extends java.lang.Object

Copyright 2008 Partner Software, Inc.

Version:
$Id$
Author:
Rich Stepanski

Constructor Summary
DatabaseMigrationTool(SqlConnection connection)
          Creates a new DatabaseMigrationTool connected to the database the passed configuration referes to.
DatabaseMigrationTool(SqlDatabase config)
          Creates a new DatabaseMigrationTool connected to the database the passed configuration referes to.
 
Method Summary
 void addColumnToTable(java.lang.String tableName, java.lang.String columnName, java.lang.String columnType)
          Add a new column to the connected database.
 void addColumnToTable(java.lang.String tableName, java.lang.String columnName, java.lang.String columnType, java.lang.Object defValue)
          Adds a column to the specified table in the connected database.
 void changeColumnName(java.lang.String tableName, java.lang.String origName, java.lang.String newName, java.lang.String sqlType)
          Renames a column.
 void changeColumnType(java.lang.String tableName, java.lang.String columnName, java.lang.String sqlType)
          Attempts to change a column to specified data type and convert all data to the new type.
 void closeConnection()
          Closes DatabaseConnection.
 void copyColumnDataToColumn(java.lang.String tableName, java.lang.String targetColumn, java.lang.String destColumn)
          Copies all data from column matching targetColumn name to destColumn in the passed table.
 void createTable(java.lang.String tableName, java.lang.String... colNameThenType)
          Creates a table using Sql of format: CREATE TABLE ( , ...
 void deleteColumnFromTable(java.lang.String tableName, java.lang.String columnName)
          Deletes the specified column from the table.
 void executeSql(java.lang.String sql)
          Execute the passed sql.
 void finalize()
           
 SqlConnection getConnection()
          Returns fire to play with.
 java.lang.String getTableName()
          Returns the stored table name.
 void setColumnInTableToValue(java.lang.String tableName, java.lang.String columnName, java.lang.Object defValue)
          Sets all column entries of the passed name in the passed table to the passed value.
 void setTableName(java.lang.String table)
          Sets a table name to be stored.
 void simpleChangeType(java.lang.String tableName, java.lang.String columnName, java.lang.String sqlType)
          Attempts to change a column type through SQL.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseMigrationTool

public DatabaseMigrationTool(SqlDatabase config)
                      throws java.sql.SQLException
Creates a new DatabaseMigrationTool connected to the database the passed configuration referes to. Opens a connection to database after creation.

Throws:
java.sql.SQLException

DatabaseMigrationTool

public DatabaseMigrationTool(SqlConnection connection)
                      throws java.sql.SQLException
Creates a new DatabaseMigrationTool connected to the database the passed configuration referes to. Opens a connection to database after creation.

Throws:
java.sql.SQLException
Method Detail

executeSql

public void executeSql(java.lang.String sql)
                throws java.sql.SQLException
Execute the passed sql. Needed?

Throws:
java.sql.SQLException

getConnection

public SqlConnection getConnection()
Returns fire to play with.

Returns:

closeConnection

public void closeConnection()
                     throws java.sql.SQLException
Closes DatabaseConnection. Also called by class's finalize.

Throws:
java.sql.SQLException

getTableName

public java.lang.String getTableName()
Returns the stored table name.

Returns:

setTableName

public void setTableName(java.lang.String table)
Sets a table name to be stored.

Parameters:
table -

addColumnToTable

public void addColumnToTable(java.lang.String tableName,
                             java.lang.String columnName,
                             java.lang.String columnType)
                      throws java.sql.SQLException
Add a new column to the connected database.

Parameters:
tableName - - Name of table to add column to.
columnName - - Name of column to create.
columnType - - Sql type of column to create, used directly in SQL statement.
Throws:
java.sql.SQLException

addColumnToTable

public void addColumnToTable(java.lang.String tableName,
                             java.lang.String columnName,
                             java.lang.String columnType,
                             java.lang.Object defValue)
                      throws java.sql.SQLException
Adds a column to the specified table in the connected database. Every entry in the table is set to the passed default value.

Parameters:
tableName - - Name of table to add column to.
columnName - - Name of column to create.
columnType - - SQL type of column to create, used directly in SQL.
defValue - - Default value to set each entry to.
Throws:
java.sql.SQLException

setColumnInTableToValue

public void setColumnInTableToValue(java.lang.String tableName,
                                    java.lang.String columnName,
                                    java.lang.Object defValue)
                             throws java.sql.SQLException
Sets all column entries of the passed name in the passed table to the passed value.

Parameters:
tableName - - Name of table to add column to.
columnName - - Name of column to create.
defValue - - Default value to set each entry to.
Throws:
java.sql.SQLException

deleteColumnFromTable

public void deleteColumnFromTable(java.lang.String tableName,
                                  java.lang.String columnName)
                           throws java.sql.SQLException
Deletes the specified column from the table.

Parameters:
tableName -
columnName -
Throws:
java.sql.SQLException

copyColumnDataToColumn

public void copyColumnDataToColumn(java.lang.String tableName,
                                   java.lang.String targetColumn,
                                   java.lang.String destColumn)
                            throws java.sql.SQLException
Copies all data from column matching targetColumn name to destColumn in the passed table.

Parameters:
tableName - - Table to copy data.
targetColumn - - Column to copy from.
destColumn - - Column to copy to.
Throws:
java.sql.SQLException

changeColumnName

public void changeColumnName(java.lang.String tableName,
                             java.lang.String origName,
                             java.lang.String newName,
                             java.lang.String sqlType)
                      throws java.sql.SQLException
Renames a column. Does not change the type or handle changing data. Simply renames.

Parameters:
tableName - - Table name to rename column.
origName - - Original column name.
newName - - New Column name.
sqlType - - Current SQL type of column.
Throws:
java.sql.SQLException

changeColumnType

public void changeColumnType(java.lang.String tableName,
                             java.lang.String columnName,
                             java.lang.String sqlType)
                      throws java.sql.SQLException
Attempts to change a column to specified data type and convert all data to the new type.

Note: currently only setup to handle converts between numbers and strings. String is the default.

Parameters:
tableName - - Table name to alter column type.
columnName - - Name of column to change type.
sqlType - - SQL type to convert column and column data to.
Throws:
java.sql.SQLException

simpleChangeType

public void simpleChangeType(java.lang.String tableName,
                             java.lang.String columnName,
                             java.lang.String sqlType)
                      throws java.sql.SQLException
Attempts to change a column type through SQL. Conversion is handled by the database. Note: Never has worked with HSQLDBs.

Parameters:
tableName -
columnName -
sqlType -
Throws:
java.sql.SQLException

createTable

public void createTable(java.lang.String tableName,
                        java.lang.String... colNameThenType)
                 throws java.sql.SQLException
Creates a table using Sql of format: CREATE TABLE ( , ... )

Parameters:
tableName - - Name of table to create
colNameThenType - - Array of Column names followed by Sql types.
Throws:
java.sql.SQLException

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object