List<SqlStatement> sql = new ArrayList<SqlStatement>();
List<AddColumnStatement> addColumnStatements = new ArrayList<AddColumnStatement>();
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()]));
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 (database instanceof DB2Database) {
sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
}
if (column.getValueObject() != null) {
UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
sql.add(updateStatement);
}
}
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) {