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