use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.
the class ModifyDataTypeGenerator method generateSql.
@Override
public Sql[] generateSql(ModifyDataTypeStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String alterTable = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
// add "MODIFY"
alterTable += " " + getModifyString(database) + " ";
// add column name
String columnName = database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName());
alterTable += columnName;
// adds a space if nothing else
alterTable += getPreDataTypeString(database);
// add column type
DatabaseDataType newDataType = DataTypeFactory.getInstance().fromDescription(statement.getNewDataType(), database).toDatabaseDataType(database);
alterTable += newDataType;
if (database instanceof PostgresDatabase) {
alterTable += " USING (" + columnName + "::" + newDataType + ")";
}
return new Sql[] { new UnparsedSql(alterTable, getAffectedTable(statement)) };
}
use of liquibase.datatype.DatabaseDataType in project liquibase by liquibase.
the class AddColumnGenerator method generateSingleColumnSQL.
protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) {
DatabaseDataType columnType = DataTypeFactory.getInstance().fromDescription(statement.getColumnType() + (statement.isAutoIncrement() ? "{autoIncrement:true}" : ""), database).toDatabaseDataType(database);
String alterTable = " ADD " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + columnType;
if (statement.isAutoIncrement() && database.supportsAutoIncrement()) {
AutoIncrementConstraint autoIncrementConstraint = statement.getAutoIncrementConstraint();
alterTable += " " + database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy());
}
alterTable += getDefaultClause(statement, database);
if (!statement.isNullable()) {
alterTable += " NOT NULL";
} else {
if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase || (database instanceof MSSQLDatabase && columnType.toString().equalsIgnoreCase("timestamp"))) {
alterTable += " NULL";
}
}
if (statement.isPrimaryKey()) {
alterTable += " PRIMARY KEY";
}
if (database instanceof MySQLDatabase && statement.getRemarks() != null) {
alterTable += " COMMENT '" + statement.getRemarks() + "' ";
}
if (statement.getAddAfterColumn() != null && !statement.getAddAfterColumn().isEmpty()) {
alterTable += " AFTER `" + statement.getAddAfterColumn() + "` ";
}
return alterTable;
}
Aggregations