use of liquibase.statement.SqlStatement 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()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AddAutoIncrementChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
if (database instanceof PostgresDatabase) {
String sequenceName = (getTableName() + "_" + getColumnName() + "_seq");
String escapedTableName = database.escapeObjectName(getTableName(), Table.class);
String escapedColumnName = database.escapeObjectName(getColumnName(), Table.class);
if (escapedTableName != null && escapedColumnName != null && !escapedTableName.startsWith("\"") && !escapedColumnName.startsWith("\"")) {
sequenceName = sequenceName.toLowerCase();
}
String schemaPrefix;
if (this.schemaName == null) {
schemaPrefix = database.getDefaultSchemaName();
} else {
schemaPrefix = this.schemaName;
}
if (schemaPrefix == null) {
schemaPrefix = "";
} else {
schemaPrefix = schemaPrefix + ".";
}
return new SqlStatement[] { new CreateSequenceStatement(catalogName, this.schemaName, sequenceName), new SetNullableStatement(catalogName, this.schemaName, getTableName(), getColumnName(), null, false), new AddDefaultValueStatement(catalogName, this.schemaName, getTableName(), getColumnName(), getColumnDataType(), new SequenceNextValueFunction(schemaPrefix + sequenceName)) };
}
return new SqlStatement[] { new AddAutoIncrementStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), getStartWith(), getIncrementBy()) };
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AddNotNullConstraintChange 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>();
if (defaultNullValue != null) {
statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), getDefaultNullValue()).setWhereClause(getColumnName() + " IS NULL"));
}
// // ... test if column contains NULL values
// if (defaultNullValue == null) {
// List<Map> null_rows = null;
// try {
// null_rows = database.getExecutor().
// queryForList(new RawSqlStatement(
// "SELECT * FROM `"+
// database.escapeTableName(getSchemaName(), getTableName())+
// "` WHERE `"+getColumnName()+"` IS NULL;"));
// } catch (DatabaseException e) {
// e.printStackTrace();
// }
// if (null_rows.size()>0) {
// throw new UnsupportedChangeException(
// "Failed to add a Not-Null-Constraint because " +
// "some values are null. Use the " +
// "defaultNullValue attribute to define default " +
// "values for the existing null values.");
// }
// }
// 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(getColumnName())) {
column.getConstraints().setNullable(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 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()]);
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AddUniqueConstraintChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
//todo if (database instanceof SQLiteDatabase) {
// // return special statements for SQLite databases
// return generateStatementsForSQLiteDatabase(database);
// }
boolean deferrable = false;
if (getDeferrable() != null) {
deferrable = getDeferrable();
}
boolean initiallyDeferred = false;
if (getInitiallyDeferred() != null) {
initiallyDeferred = getInitiallyDeferred();
}
boolean disabled = false;
if (getDisabled() != null) {
disabled = getDisabled();
}
boolean clustered = false;
if (getClustered() != null) {
clustered = getClustered();
}
AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement(getCatalogName(), getSchemaName(), getTableName(), ColumnConfig.arrayFromNames(getColumnNames()), getConstraintName());
statement.setTablespace(getTablespace()).setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setDisabled(disabled).setClustered(clustered);
statement.setForIndexName(getForIndexName());
statement.setForIndexSchemaName(getForIndexSchemaName());
statement.setForIndexCatalogName(getForIndexCatalogName());
return new SqlStatement[] { statement };
}
Aggregations