use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.
the class UpdateDataChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
boolean needsPreparedStatement = false;
for (ColumnConfig column : getColumns()) {
if (column.getValueBlobFile() != null) {
needsPreparedStatement = true;
}
if (column.getValueClobFile() != null) {
needsPreparedStatement = true;
}
if (database instanceof OracleDatabase && column.getType() != null && column.getType().equalsIgnoreCase("CLOB") && column.getValue() != null && column.getValue().length() >= 4000) {
needsPreparedStatement = true;
}
}
if (needsPreparedStatement) {
UpdateExecutablePreparedStatement statement = new UpdateExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns, getChangeSet(), this.getResourceAccessor());
statement.setWhereClause(where);
for (ColumnConfig whereParam : whereParams) {
if (whereParam.getName() != null) {
statement.addWhereColumnName(whereParam.getName());
}
statement.addWhereParameter(whereParam.getValueObject());
}
return new SqlStatement[] { statement };
}
UpdateStatement statement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
for (ColumnConfig column : getColumns()) {
statement.addNewColumnValue(column.getName(), column.getValueObject());
}
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.core.UpdateStatement 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()]);
}
use of liquibase.statement.core.UpdateStatement 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.core.UpdateStatement 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.core.UpdateStatement in project liquibase by liquibase.
the class LockDatabaseChangeLogGenerator method generateSql.
@Override
public Sql[] generateSql(LockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String liquibaseSchema = database.getLiquibaseSchemaName();
String liquibaseCatalog = database.getLiquibaseCatalogName();
UpdateStatement updateStatement = new UpdateStatement(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogLockTableName());
updateStatement.addNewColumnValue("LOCKED", true);
updateStatement.addNewColumnValue("LOCKGRANTED", new Timestamp(new java.util.Date().getTime()));
updateStatement.addNewColumnValue("LOCKEDBY", hostname + hostDescription + " (" + hostaddress + ")");
updateStatement.setWhereClause(database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "ID") + " = 1 AND " + database.escapeColumnName(liquibaseCatalog, liquibaseSchema, database.getDatabaseChangeLogTableName(), "LOCKED") + " = " + DataTypeFactory.getInstance().fromDescription("boolean", database).objectToSql(false, database));
return SqlGeneratorFactory.getInstance().generateSql(updateStatement, database);
}
Aggregations