Search in sources :

Example 6 with ReorganizeTableStatement

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()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column)

Example 7 with ReorganizeTableStatement

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()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) DropColumnStatement(liquibase.statement.core.DropColumnStatement)

Example 8 with ReorganizeTableStatement

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 };
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) AddPrimaryKeyStatement(liquibase.statement.core.AddPrimaryKeyStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement)

Example 9 with ReorganizeTableStatement

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);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Aggregations

ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)9 ArrayList (java.util.ArrayList)7 SqlStatement (liquibase.statement.SqlStatement)7 DB2Database (liquibase.database.core.DB2Database)6 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)2 AddColumnStatement (liquibase.statement.core.AddColumnStatement)2 DropColumnStatement (liquibase.statement.core.DropColumnStatement)2 UpdateStatement (liquibase.statement.core.UpdateStatement)2 Column (liquibase.structure.core.Column)2 HashSet (java.util.HashSet)1 AddColumnChange (liquibase.change.core.AddColumnChange)1 Sql (liquibase.sql.Sql)1 UnparsedSql (liquibase.sql.UnparsedSql)1 AddPrimaryKeyStatement (liquibase.statement.core.AddPrimaryKeyStatement)1 RawSqlStatement (liquibase.statement.core.RawSqlStatement)1 RenameTableStatement (liquibase.statement.core.RenameTableStatement)1 SetColumnRemarksStatement (liquibase.statement.core.SetColumnRemarksStatement)1 SetNullableStatement (liquibase.statement.core.SetNullableStatement)1 Test (org.junit.Test)1