Search in sources :

Example 16 with DatabaseDataType

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)) };
}
Also used : DatabaseDataType(liquibase.datatype.DatabaseDataType) UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 17 with DatabaseDataType

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;
}
Also used : AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) DatabaseDataType(liquibase.datatype.DatabaseDataType)

Aggregations

DatabaseDataType (liquibase.datatype.DatabaseDataType)17 BigInteger (java.math.BigInteger)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DatabaseException (liquibase.exception.DatabaseException)4 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 PostgresDatabase (liquibase.database.core.PostgresDatabase)2 Sql (liquibase.sql.Sql)2 UnparsedSql (liquibase.sql.UnparsedSql)2 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Change (liquibase.change.Change)1 ColumnConfig (liquibase.change.ColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 CreateTableChange (liquibase.change.core.CreateTableChange)1 HsqlDatabase (liquibase.database.core.HsqlDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)1