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 };
}
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()]);
}
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()]);
}
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()]);
}
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()]);
}
Aggregations