use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.
the class AddLookupTableChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
String newTableCatalogName = getNewTableCatalogName();
String newTableSchemaName = getNewTableSchemaName();
String existingTableCatalogName = getExistingTableCatalogName();
String existingTableSchemaName = getExistingTableSchemaName();
SqlStatement[] createTablesSQL = { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
if (database instanceof MSSQLDatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof SybaseASADatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof DB2Database) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " AS (SELECT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " AS " + database.escapeObjectName(getNewColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + ") WITH NO DATA"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
} else if (database instanceof InformixDatabase) {
createTablesSQL = new SqlStatement[] { new RawSqlStatement("CREATE TABLE " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " " + getNewColumnDataType() + " )"), new RawSqlStatement("INSERT INTO " + database.escapeTableName(newTableCatalogName, newTableSchemaName, getNewTableName()) + " ( " + database.escapeObjectName(getNewColumnName(), Column.class) + " ) SELECT DISTINCT " + database.escapeObjectName(getExistingColumnName(), Column.class) + " FROM " + database.escapeTableName(existingTableCatalogName, existingTableSchemaName, getExistingTableName()) + " WHERE " + database.escapeObjectName(getExistingColumnName(), Column.class) + " IS NOT NULL") };
}
statements.addAll(Arrays.asList(createTablesSQL));
if (!(database instanceof OracleDatabase)) {
AddNotNullConstraintChange addNotNullChange = new AddNotNullConstraintChange();
addNotNullChange.setSchemaName(newTableSchemaName);
addNotNullChange.setTableName(getNewTableName());
addNotNullChange.setColumnName(getNewColumnName());
addNotNullChange.setColumnDataType(getNewColumnDataType());
statements.addAll(Arrays.asList(addNotNullChange.generateStatements(database)));
}
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
}
AddPrimaryKeyChange addPKChange = new AddPrimaryKeyChange();
addPKChange.setSchemaName(newTableSchemaName);
addPKChange.setTableName(getNewTableName());
addPKChange.setColumnNames(getNewColumnName());
statements.addAll(Arrays.asList(addPKChange.generateStatements(database)));
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(newTableCatalogName, newTableSchemaName, getNewTableName()));
}
AddForeignKeyConstraintChange addFKChange = new AddForeignKeyConstraintChange();
addFKChange.setBaseTableSchemaName(existingTableSchemaName);
addFKChange.setBaseTableName(getExistingTableName());
addFKChange.setBaseColumnNames(getExistingColumnName());
addFKChange.setReferencedTableSchemaName(newTableSchemaName);
addFKChange.setReferencedTableName(getNewTableName());
addFKChange.setReferencedColumnNames(getNewColumnName());
addFKChange.setConstraintName(getFinalConstraintName());
statements.addAll(Arrays.asList(addFKChange.generateStatements(database)));
return statements.toArray(new SqlStatement[statements.size()]);
}
use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.
the class DropColumnChange method generateMultipeColumns.
private SqlStatement[] generateMultipeColumns(Database database) {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
List<DropColumnStatement> dropStatements = new ArrayList<DropColumnStatement>();
for (ColumnConfig column : columns) {
if (database instanceof SQLiteDatabase) {
statements.addAll(Arrays.asList(generateStatementsForSQLiteDatabase(database, column.getName())));
} else {
dropStatements.add(new DropColumnStatement(getCatalogName(), getSchemaName(), getTableName(), column.getName()));
}
}
if (dropStatements.size() == 1) {
statements.add(dropStatements.get(0));
} else {
statements.add(new DropColumnStatement(dropStatements));
}
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
}
return statements.toArray(new SqlStatement[statements.size()]);
}
use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.
the class AddPrimaryKeyChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
AddPrimaryKeyStatement statement = new AddPrimaryKeyStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnNames(), getConstraintName());
statement.setTablespace(getTablespace());
statement.setClustered(getClustered());
statement.setForIndexName(getForIndexName());
statement.setForIndexSchemaName(getForIndexSchemaName());
statement.setForIndexCatalogName(getForIndexCatalogName());
if (database instanceof DB2Database) {
return new SqlStatement[] { statement, new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()) };
//todo } else if (database instanceof SQLiteDatabase) {
// // return special statements for SQLite databases
// return generateStatementsForSQLiteDatabase(database);
}
return new SqlStatement[] { statement };
}
use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.
the class AddColumnChangeTest method generateStatements_DB2_multipleColumns_single_reorg.
@Test
public void generateStatements_DB2_multipleColumns_single_reorg() {
AddColumnChange change = new AddColumnChange();
AddColumnConfig column1 = new AddColumnConfig();
column1.setName("column1");
column1.setType("INT");
change.addColumn(column1);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("INT");
change.addColumn(column2);
SqlStatement[] statements = change.generateStatements(new DB2Database());
Assert.assertEquals(2, statements.length);
Assert.assertTrue(statements[0] instanceof AddColumnStatement);
AddColumnStatement stmt = (AddColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
Assert.assertTrue(statements[1] instanceof ReorganizeTableStatement);
}
Aggregations