Search in sources :

Example 36 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class DeleteDataChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    DeleteStatement statement = new DeleteStatement(getCatalogName(), getSchemaName(), getTableName());
    statement.setWhereClause(where);
    for (ColumnConfig whereParam : whereParams) {
        if (whereParam.getName() != null) {
            statement.addWhereColumnName(whereParam.getName());
        }
        statement.addWhereParameter(whereParam.getValueObject());
    }
    return new SqlStatement[] { statement };
}
Also used : SqlStatement(liquibase.statement.SqlStatement) DeleteStatement(liquibase.statement.core.DeleteStatement)

Example 37 with SqlStatement

use of liquibase.statement.SqlStatement 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 38 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class DropPrimaryKeyChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    // Note: The attribute "constraintName" is used to pass the column 
    // name instead of the constraint name.
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getConstraintName())) {
                column.getConstraints().setPrimaryKey(false);
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 39 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class MergeColumnChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    AddColumnChange addNewColumnChange = new AddColumnChange();
    addNewColumnChange.setSchemaName(schemaName);
    addNewColumnChange.setTableName(getTableName());
    AddColumnConfig columnConfig = new AddColumnConfig();
    columnConfig.setName(getFinalColumnName());
    columnConfig.setType(getFinalColumnType());
    addNewColumnChange.addColumn(columnConfig);
    statements.addAll(Arrays.asList(addNewColumnChange.generateStatements(database)));
    String updateStatement = "UPDATE " + database.escapeTableName(getCatalogName(), getSchemaName(), getTableName()) + " SET " + database.escapeObjectName(getFinalColumnName(), Column.class) + " = " + database.getConcatSql(database.escapeObjectName(getColumn1Name(), Column.class), "'" + getJoinString() + "'", database.escapeObjectName(getColumn2Name(), Column.class));
    statements.add(new RawSqlStatement(updateStatement));
    if (database instanceof SQLiteDatabase) {
        // SQLite does not support this ALTER TABLE operation until now.
        // For more information see: http://www.sqlite.org/omitted.html
        // This is a small work around...
        // define alter table logic
        AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

            @Override
            public ColumnConfig[] getColumnsToAdd() {
                ColumnConfig[] new_columns = new ColumnConfig[1];
                ColumnConfig new_column = new ColumnConfig();
                new_column.setName(getFinalColumnName());
                new_column.setType(getFinalColumnType());
                new_columns[0] = new_column;
                return new_columns;
            }

            @Override
            public boolean copyThisColumn(ColumnConfig column) {
                return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name()));
            }

            @Override
            public boolean createThisColumn(ColumnConfig column) {
                return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name()));
            }

            @Override
            public boolean createThisIndex(Index index) {
                return !(index.getColumnNames().contains(getColumn1Name()) || index.getColumnNames().contains(getColumn2Name()));
            }
        };
        try {
            // alter table
            statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // ...if it is not a SQLite database 
        DropColumnChange dropColumn1Change = new DropColumnChange();
        dropColumn1Change.setSchemaName(schemaName);
        dropColumn1Change.setTableName(getTableName());
        dropColumn1Change.setColumnName(getColumn1Name());
        statements.addAll(Arrays.asList(dropColumn1Change.generateStatements(database)));
        DropColumnChange dropColumn2Change = new DropColumnChange();
        dropColumn2Change.setSchemaName(schemaName);
        dropColumn2Change.setTableName(getTableName());
        dropColumn2Change.setColumnName(getColumn2Name());
        statements.addAll(Arrays.asList(dropColumn2Change.generateStatements(database)));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor)

Example 40 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class RenameColumnChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getOldColumnName())) {
                column.setName(getNewColumnName());
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            if (index.getColumnNames().contains(getOldColumnName())) {
                Iterator<Column> columnIterator = index.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column column = columnIterator.next();
                    if (column.getName().equals(getOldColumnName())) {
                        columnIterator.remove();
                        break;
                    }
                }
                index.addColumn(new Column(getNewColumnName()).setRelation(index.getTable()));
            }
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Aggregations

SqlStatement (liquibase.statement.SqlStatement)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)9 Column (liquibase.structure.core.Column)8 DB2Database (liquibase.database.core.DB2Database)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 DatabaseException (liquibase.exception.DatabaseException)6 Database (liquibase.database.Database)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 Sql (liquibase.sql.Sql)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UpdateStatement (liquibase.statement.core.UpdateStatement)5 Index (liquibase.structure.core.Index)5 Change (liquibase.change.Change)4 ColumnConfig (liquibase.change.ColumnConfig)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)3