use of liquibase.statement.core.SetColumnRemarksStatement in project liquibase by liquibase.
the class AddColumnChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> sql = new ArrayList<>();
List<AddColumnStatement> addColumnStatements = new ArrayList<>();
List<UpdateStatement> addColumnUpdateStatements = new ArrayList<>();
List<SqlStatement> addNotNullConstraintStatements = new ArrayList<>();
if (getColumns().isEmpty()) {
return new SqlStatement[] { new AddColumnStatement(catalogName, schemaName, tableName, null, null, null) };
}
for (AddColumnConfig column : getColumns()) {
Set<ColumnConstraint> constraints = new HashSet<>();
ConstraintsConfig constraintsConfig = column.getConstraints();
if (constraintsConfig != null) {
if ((constraintsConfig.isNullable() != null) && !constraintsConfig.isNullable()) {
if (column.getValueObject() != null) {
List<SqlStatement> sqlStatements = generateAddNotNullConstraintStatements(column, database);
addNotNullConstraintStatements.addAll(sqlStatements);
} else {
NotNullConstraint notNullConstraint = createNotNullConstraint(constraintsConfig);
constraints.add(notNullConstraint);
}
}
if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint(constraintsConfig.getUniqueConstraintName());
if (constraintsConfig.getValidateUnique() != null && !constraintsConfig.getValidateUnique()) {
uniqueConstraint.setValidateUnique(false);
}
constraints.add(uniqueConstraint);
}
if ((constraintsConfig.isPrimaryKey() != null) && constraintsConfig.isPrimaryKey()) {
PrimaryKeyConstraint primaryKeyConstraint = new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName());
if (constraintsConfig.getValidatePrimaryKey() != null && !constraintsConfig.getValidatePrimaryKey()) {
primaryKeyConstraint.setValidatePrimaryKey(false);
}
constraints.add(primaryKeyConstraint);
}
if ((constraintsConfig.getReferences() != null) || ((constraintsConfig.getReferencedColumnNames() != null) && (constraintsConfig.getReferencedTableName() != null))) {
ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(), constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(), constraintsConfig.getReferencedColumnNames());
if (constraintsConfig.getValidateForeignKey() != null && !constraintsConfig.getValidateForeignKey()) {
foreignKeyConstraint.setValidateForeignKey(false);
}
if (constraintsConfig.isDeleteCascade() != null) {
foreignKeyConstraint.setDeleteCascade(constraintsConfig.isDeleteCascade());
}
if (constraintsConfig.isDeferrable() != null) {
foreignKeyConstraint.setDeferrable(constraintsConfig.isDeferrable());
}
if (constraintsConfig.isInitiallyDeferred() != null) {
foreignKeyConstraint.setInitiallyDeferred(constraintsConfig.isInitiallyDeferred());
}
constraints.add(foreignKeyConstraint);
}
}
if ((column.isAutoIncrement() != null) && column.isAutoIncrement()) {
constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy(), column.getGenerationType(), column.getDefaultOnNull()));
}
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());
addColumnStatement.setComputed(column.getComputed());
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));
}
sql.addAll(addNotNullConstraintStatements);
for (ColumnConfig column : getColumns()) {
String columnRemarks = StringUtil.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks, column.getType());
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
if (!(database instanceof MySQLDatabase)) {
// don't re-add the comments with mysql because mysql messes with the column definition
sql.add(remarksStatement);
}
}
}
}
return sql.toArray(new SqlStatement[sql.size()]);
}
use of liquibase.statement.core.SetColumnRemarksStatement in project liquibase by liquibase.
the class CreateTableChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
CreateTableStatement statement = generateCreateTableStatement();
for (ColumnConfig column : getColumns()) {
ConstraintsConfig constraints = column.getConstraints();
boolean isAutoIncrement = (column.isAutoIncrement() != null) && column.isAutoIncrement();
Object defaultValue = column.getDefaultValueObject();
LiquibaseDataType columnType = null;
if (column.getType() != null) {
columnType = DataTypeFactory.getInstance().fromDescription(column.getType() + (isAutoIncrement ? "{autoIncrement:true}" : ""), database);
isAutoIncrement |= columnType.isAutoIncrement();
}
if ((constraints != null) && (constraints.isPrimaryKey() != null) && constraints.isPrimaryKey()) {
statement.addPrimaryKeyColumn(column.getName(), columnType, defaultValue, constraints.getValidatePrimaryKey(), constraints.isDeferrable() != null && constraints.isDeferrable(), constraints.isInitiallyDeferred() != null && constraints.isInitiallyDeferred(), constraints.getPrimaryKeyName(), constraints.getPrimaryKeyTablespace());
} else {
statement.addColumn(column.getName(), columnType, column.getDefaultValueConstraintName(), defaultValue, column.getRemarks());
}
if (constraints != null) {
if (constraints.isNullable() != null && !constraints.isNullable()) {
NotNullConstraint notNullConstraint = new NotNullConstraint(column.getName()).setConstraintName(constraints.getNotNullConstraintName()).setValidateNullable(constraints.getValidateNullable() == null ? true : constraints.getValidateNullable());
statement.addColumnConstraint(notNullConstraint);
}
if ((constraints.getReferences() != null) || ((constraints.getReferencedTableName() != null) && (constraints.getReferencedColumnNames() != null))) {
if (StringUtil.trimToNull(constraints.getForeignKeyName()) == null) {
throw new UnexpectedLiquibaseException("createTable with references requires foreignKeyName");
}
ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(constraints.getForeignKeyName(), constraints.getReferences(), constraints.getReferencedTableName(), constraints.getReferencedColumnNames());
fkConstraint.setReferencedTableCatalogName(constraints.getReferencedTableCatalogName());
fkConstraint.setReferencedTableSchemaName(constraints.getReferencedTableSchemaName());
fkConstraint.setColumn(column.getName());
fkConstraint.setDeleteCascade((constraints.isDeleteCascade() != null) && constraints.isDeleteCascade());
fkConstraint.setInitiallyDeferred((constraints.isInitiallyDeferred() != null) && constraints.isInitiallyDeferred());
fkConstraint.setDeferrable((constraints.isDeferrable() != null) && constraints.isDeferrable());
Boolean validate = constraints.getValidateForeignKey();
if (validate != null) {
fkConstraint.setValidateForeignKey(constraints.getValidateForeignKey());
}
statement.addColumnConstraint(fkConstraint);
}
if ((constraints.isUnique() != null) && constraints.isUnique()) {
statement.addColumnConstraint(new UniqueConstraint(constraints.getUniqueConstraintName(), constraints.getValidateUnique() == null ? true : constraints.getValidateUnique()).addColumns(column.getName()));
}
}
if (isAutoIncrement) {
statement.addColumnConstraint(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy(), column.getGenerationType(), column.getDefaultOnNull()));
}
}
statement.setTablespace(StringUtil.trimToNull(getTablespace()));
List<SqlStatement> statements = new ArrayList<>();
statements.add(statement);
if (StringUtil.trimToNull(remarks) != null) {
SetTableRemarksStatement remarksStatement = new SetTableRemarksStatement(catalogName, schemaName, tableName, remarks);
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
statements.add(remarksStatement);
}
}
for (ColumnConfig column : getColumns()) {
String columnRemarks = StringUtil.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks, column.getType());
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
statements.add(remarksStatement);
}
}
final Boolean computed = column.getComputed();
if (computed != null && computed) {
statement.setComputed(column.getName());
}
}
return statements.toArray(new SqlStatement[statements.size()]);
}
Aggregations