de.fesenmeyer.dbnormalizer.core.algorithms
Class NormalizationScriptBuilder

java.lang.Object
  extended by de.fesenmeyer.dbnormalizer.core.algorithms.NormalizationScriptBuilder

public class NormalizationScriptBuilder
extends Object

This class creates a SQL script which can be used to apply the normalization proposed by a NormalizationProposal object to a DB.

NOTE: The current implementation has some limitations:

Because of this limitations, first check the generated SQL script before applying it to the DB.

Author:
DF

Field Summary
private  NormalizationProposal normProposal
          The normalization proposal for which the sql script should be generated.
 
Constructor Summary
NormalizationScriptBuilder(NormalizationProposal normProposal)
          Class constructor specifying the normalization proposal for which the sql script should be generated.
 
Method Summary
private  StringBuffer buildAddFKConstraint(FK fk)
          Builds a SQL statement which creates the foreign key (FK) constraint specified by the fk argument
private  StringBuffer buildAddFKConstraints(Collection<FK> fks)
          Builds SQL statements which create the foreign key (FK) constraints specified by the fks argument
private  StringBuffer buildCreateTableScript(Table table)
          Builds a SQL statement to create the table (without foreign keys) given by the table argument
private  StringBuffer buildCreateTablesScript(Collection<Table> createdTables)
          Builds SQL statements to create the tables (without foreign keys) given by the createdTables argument.
private  StringBuffer buildCreateView(Collection<Table> createdTables, Table oldTable)
          Builds a CREATE VIEW statement for creating a view which constructs the old table's data from the new (created) tables.
private  StringBuffer buildDataMigrationScript(Collection<Table> createdTables, Table oldTable)
          Builds a data migration script which copies the data from the old table to the new tables.
private  StringBuffer buildDataMigrationScript(Table createdTable, Table oldTable)
          Builds a data migration script which copies the appropriate data from the old table to one of the new (created) tables.
private  StringBuffer buildDropOldTableScript(Table oldTable)
          Builds a statement which drops the old table with the CASCADE CONSTRAINTS option.
private  StringBuffer buildPrimaryKeyDef(PK pk)
          Builds a primary key (PK) definition for the PK specified by argument pk
private  StringBuffer buildTableColumnDef(Table table, TableColumn col)
          Builds a table column definition for the column given by argument col
private  StringBuffer buildUniqueConstraintDef(Collection<String> attributes)
          Builds a unique constraint definition for the specified attributes
 String createScript()
          Creates a SQL script based on the normalization proposal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

normProposal

private NormalizationProposal normProposal
The normalization proposal for which the sql script should be generated.

Constructor Detail

NormalizationScriptBuilder

public NormalizationScriptBuilder(NormalizationProposal normProposal)
Class constructor specifying the normalization proposal for which the sql script should be generated.

Parameters:
normProposal - a normalization proposal
Method Detail

createScript

public String createScript()
Creates a SQL script based on the normalization proposal.

Returns:
the SQL script as a string
See Also:
NormalizationScriptBuilder

buildCreateTablesScript

private StringBuffer buildCreateTablesScript(Collection<Table> createdTables)
Builds SQL statements to create the tables (without foreign keys) given by the createdTables argument.

Parameters:
createdTables - the tables for which CREATE TABLE-statements should be build
Returns:
a StringBuffer containing SQL statements

buildCreateTableScript

private StringBuffer buildCreateTableScript(Table table)
Builds a SQL statement to create the table (without foreign keys) given by the table argument

Parameters:
table - the table for which a CREATE TABLE-statement should be build
Returns:
a StringBuffer containing the SQL statement

buildPrimaryKeyDef

private StringBuffer buildPrimaryKeyDef(PK pk)
Builds a primary key (PK) definition for the PK specified by argument pk

Parameters:
pk - a PK
Returns:
a StringBuffer containing the PK definition

buildUniqueConstraintDef

private StringBuffer buildUniqueConstraintDef(Collection<String> attributes)
Builds a unique constraint definition for the specified attributes

Parameters:
attributes - a collection containing the attribute names
Returns:
a StringBuffer containing the unique constraint definition

buildTableColumnDef

private StringBuffer buildTableColumnDef(Table table,
                                         TableColumn col)
Builds a table column definition for the column given by argument col

Parameters:
table - the table which contains the column
col - the column
Returns:
a StringBuffer containing the table column definition

buildDataMigrationScript

private StringBuffer buildDataMigrationScript(Collection<Table> createdTables,
                                              Table oldTable)
Builds a data migration script which copies the data from the old table to the new tables.

Parameters:
createdTables - the created (new) tables
oldTable - the old table
Returns:
a StringBuffer containing the data migration script

buildDataMigrationScript

private StringBuffer buildDataMigrationScript(Table createdTable,
                                              Table oldTable)
Builds a data migration script which copies the appropriate data from the old table to one of the new (created) tables.

Parameters:
createdTable - one of the new (created) tables
oldTable - the old table
Returns:
a StringBuffer containing the data migration script

buildDropOldTableScript

private StringBuffer buildDropOldTableScript(Table oldTable)
Builds a statement which drops the old table with the CASCADE CONSTRAINTS option.

Parameters:
oldTable - the old table
Returns:
a StringBuffer containing the DROP TABLE-statement

buildAddFKConstraints

private StringBuffer buildAddFKConstraints(Collection<FK> fks)
Builds SQL statements which create the foreign key (FK) constraints specified by the fks argument

Parameters:
fks - the FKs to be defined
Returns:
a StringBuffer containing the SQL statements

buildAddFKConstraint

private StringBuffer buildAddFKConstraint(FK fk)
Builds a SQL statement which creates the foreign key (FK) constraint specified by the fk argument

Parameters:
fk - the FK to be defined
Returns:
a StringBuffer containing the SQL statement

buildCreateView

private StringBuffer buildCreateView(Collection<Table> createdTables,
                                     Table oldTable)
Builds a CREATE VIEW statement for creating a view which constructs the old table's data from the new (created) tables. For this purpose, the NATURAL JOIN operator has been used.

Parameters:
createdTables - the new (created) tables
oldTable - the old table
Returns:
a StringBuffer containing the CREATE VIEW statement