Search in sources :

Example 1 with ReorganizeTableStatement

use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.

the class DropColumnChange method generateSingleColumn.

private SqlStatement[] generateSingleColumn(Database database) {
    if (database instanceof SQLiteDatabase) {
        // return special statements for SQLite databases
        return generateStatementsForSQLiteDatabase(database, getColumnName());
    }
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    statements.add(new DropColumnStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName()));
    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 2 with ReorganizeTableStatement

use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.

the class RenameTableChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    statements.add(new RenameTableStatement(getCatalogName(), getSchemaName(), getOldTableName(), getNewTableName()));
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getNewTableName()));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) RenameTableStatement(liquibase.statement.core.RenameTableStatement)

Example 3 with ReorganizeTableStatement

use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.

the class AddColumnChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> sql = new ArrayList<SqlStatement>();
    List<AddColumnStatement> addColumnStatements = new ArrayList<AddColumnStatement>();
    List<UpdateStatement> addColumnUpdateStatements = new ArrayList<UpdateStatement>();
    if (getColumns().size() == 0) {
        return new SqlStatement[] { new AddColumnStatement(catalogName, schemaName, tableName, null, null, null) };
    }
    for (AddColumnConfig column : getColumns()) {
        Set<ColumnConstraint> constraints = new HashSet<ColumnConstraint>();
        ConstraintsConfig constraintsConfig = column.getConstraints();
        if (constraintsConfig != null) {
            if (constraintsConfig.isNullable() != null && !constraintsConfig.isNullable()) {
                constraints.add(new NotNullConstraint());
            }
            if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
                constraints.add(new UniqueConstraint());
            }
            if (constraintsConfig.isPrimaryKey() != null && constraintsConfig.isPrimaryKey()) {
                constraints.add(new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName()));
            }
            if (constraintsConfig.getReferences() != null || (constraintsConfig.getReferencedColumnNames() != null && constraintsConfig.getReferencedTableName() != null)) {
                constraints.add(new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(), constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(), constraintsConfig.getReferencedColumnNames()));
            }
        }
        if (column.isAutoIncrement() != null && column.isAutoIncrement()) {
            constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy()));
        }
        AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(), getTableName(), column.getName(), column.getType(), column.getDefaultValueObject(), column.getRemarks(), constraints.toArray(new ColumnConstraint[constraints.size()]));
        addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
        if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
            addColumnStatement.setAddAfterColumn(column.getAfterColumn());
        } else if (((database instanceof HsqlDatabase) || (database instanceof H2Database)) && (column.getBeforeColumn() != null)) {
            addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
        } else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
            addColumnStatement.setAddAtPosition(column.getPosition());
        }
        addColumnStatements.add(addColumnStatement);
        if (column.getValueObject() != null) {
            UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
            updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
            if (database instanceof DB2Database) {
                // Cannot update until table is reorganized in DB2
                addColumnUpdateStatements.add(updateStatement);
            } else {
                sql.add(updateStatement);
            }
        }
    }
    if (database instanceof DB2Database) {
        sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
        // Add all the update statements after the reorg table in DB2
        sql.addAll(addColumnUpdateStatements);
    }
    if (addColumnStatements.size() == 1) {
        sql.add(0, addColumnStatements.get(0));
    } else {
        sql.add(0, new AddColumnStatement(addColumnStatements));
    }
    for (ColumnConfig column : getColumns()) {
        String columnRemarks = StringUtils.trimToNull(column.getRemarks());
        if (columnRemarks != null) {
            SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks);
            if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
                sql.add(remarksStatement);
            }
        }
    }
    return sql.toArray(new SqlStatement[sql.size()]);
}
Also used : ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) SetColumnRemarksStatement(liquibase.statement.core.SetColumnRemarksStatement) HashSet(java.util.HashSet) UpdateStatement(liquibase.statement.core.UpdateStatement) AddColumnStatement(liquibase.statement.core.AddColumnStatement)

Example 4 with ReorganizeTableStatement

use of liquibase.statement.core.ReorganizeTableStatement in project liquibase by liquibase.

the class AddNotNullConstraintChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    ////        if (database instanceof SQLiteDatabase) {
    //    		// return special statements for SQLite databases
    //    		return generateStatementsForSQLiteDatabase(database);
    //        }
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), defaultNullValue).setWhereClause(database.escapeObjectName(getColumnName(), Column.class) + " IS NULL"));
    }
    statements.add(new SetNullableStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), false, getConstraintName()));
    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) UpdateStatement(liquibase.statement.core.UpdateStatement) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Column(liquibase.structure.core.Column) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList)

Example 5 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)

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