use of liquibase.statement.core.SetTableRemarksStatement 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 = DataTypeFactory.getInstance().fromDescription(column.getType() + (isAutoIncrement ? "{autoIncrement:true}" : ""), database);
if (constraints != null && constraints.isPrimaryKey() != null && constraints.isPrimaryKey()) {
statement.addPrimaryKeyColumn(column.getName(), columnType, defaultValue, constraints.getPrimaryKeyName(), constraints.getPrimaryKeyTablespace());
} else {
statement.addColumn(column.getName(), columnType, column.getDefaultValueConstraintName(), defaultValue, column.getRemarks());
}
if (constraints != null) {
if (constraints.isNullable() != null && !constraints.isNullable()) {
statement.addColumnConstraint(new NotNullConstraint(column.getName()));
}
if (constraints.getReferences() != null || (constraints.getReferencedTableName() != null && constraints.getReferencedColumnNames() != null)) {
if (StringUtils.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());
statement.addColumnConstraint(fkConstraint);
}
if (constraints.isUnique() != null && constraints.isUnique()) {
statement.addColumnConstraint(new UniqueConstraint(constraints.getUniqueConstraintName()).addColumns(column.getName()));
}
}
if (isAutoIncrement) {
statement.addColumnConstraint(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy()));
}
}
statement.setTablespace(StringUtils.trimToNull(getTablespace()));
List<SqlStatement> statements = new ArrayList<SqlStatement>();
statements.add(statement);
if (StringUtils.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 = StringUtils.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks);
if (!(database instanceof MySQLDatabase) && SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
statements.add(remarksStatement);
}
}
}
return statements.toArray(new SqlStatement[statements.size()]);
}
Aggregations