Search in sources :

Example 26 with UnparsedSql

use of liquibase.sql.UnparsedSql in project liquibase by liquibase.

the class SetNullableGenerator method generateSql.

@Override
public Sql[] generateSql(SetNullableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String sql;
    String nullableString;
    if (statement.isNullable()) {
        nullableString = " NULL";
    } else {
        nullableString = " NOT NULL";
    }
    if (database instanceof OracleDatabase && statement.getConstraintName() != null) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " CONSTRAINT " + statement.getConstraintName() + nullableString;
    } else if (database instanceof OracleDatabase || database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + nullableString;
    } else if (database instanceof MSSQLDatabase) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString;
    } else if (database instanceof MySQLDatabase) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString;
    } else if (database instanceof DerbyDatabase) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN  " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + nullableString;
    } else if (database instanceof HsqlDatabase || database instanceof H2Database) {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " SET" + nullableString;
    } else if (database instanceof InformixDatabase) {
        // Informix simply omits the null for nullables
        if (statement.isNullable()) {
            nullableString = "";
        }
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY (" + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString + ")";
    } else {
        sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN  " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + (statement.isNullable() ? " DROP NOT NULL" : " SET NOT NULL");
    }
    List<Sql> returnList = new ArrayList<Sql>();
    returnList.add(new UnparsedSql(sql, getAffectedColumn(statement)));
    if (database instanceof DB2Database) {
        Sql[] a = SqlGeneratorFactory.getInstance().generateSql(new ReorganizeTableStatement(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()), database);
        if (a != null) {
            returnList.addAll(Arrays.asList(a));
        }
    }
    return returnList.toArray(new Sql[returnList.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 27 with UnparsedSql

use of liquibase.sql.UnparsedSql in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorMySQL method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append("RC.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
    sb.append("KCU.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
    sb.append("RC.REFERENCED_TABLE_NAME ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
    sb.append("KCU.REFERENCED_COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", ");
    sb.append("RC.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
    sb.append("FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,");
    sb.append("     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ");
    sb.append("WHERE RC.TABLE_NAME = KCU.TABLE_NAME ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA ");
    sb.append("AND RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME ");
    sb.append("AND RC.TABLE_NAME = '").append(statement.getBaseTableName()).append("' ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = '").append(schema.getCatalogName()).append("'");
    sb.append("AND KCU.TABLE_SCHEMA = '").append(schema.getCatalogName()).append("'");
    return new Sql[] { new UnparsedSql(sb.toString()) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 28 with UnparsedSql

use of liquibase.sql.UnparsedSql in project liquibase by liquibase.

the class DropIndexGenerator method generateSql.

@Override
public Sql[] generateSql(DropIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
    if (associatedWith != null) {
        if (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT)) {
            return new Sql[0];
        } else if (associatedWith.contains(Index.MARK_FOREIGN_KEY)) {
            if (!(database instanceof OracleDatabase || database instanceof MSSQLDatabase)) {
                return new Sql[0];
            }
        }
    }
    String schemaName = statement.getTableSchemaName();
    if (database instanceof MySQLDatabase) {
        return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(null, null, statement.getIndexName()) + " ON " + database.escapeTableName(statement.getTableCatalogName(), schemaName, statement.getTableName()), getAffectedIndex(statement)) };
    } else if (database instanceof MSSQLDatabase) {
        return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(null, null, statement.getIndexName()) + " ON " + database.escapeTableName(null, schemaName, statement.getTableName()), getAffectedIndex(statement)) };
    } else if (database instanceof SybaseDatabase) {
        return new Sql[] { new UnparsedSql("DROP INDEX " + statement.getTableName() + "." + statement.getIndexName(), getAffectedIndex(statement)) };
    } else if (database instanceof PostgresDatabase) {
        return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(statement.getTableCatalogName(), schemaName, statement.getIndexName()), getAffectedIndex(statement)) };
    }
    return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(statement.getTableCatalogName(), schemaName, statement.getIndexName()), getAffectedIndex(statement)) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 29 with UnparsedSql

use of liquibase.sql.UnparsedSql 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)

Aggregations

UnparsedSql (liquibase.sql.UnparsedSql)29 Sql (liquibase.sql.Sql)28 ArrayList (java.util.ArrayList)10 CatalogAndSchema (liquibase.CatalogAndSchema)8 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)3 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)3 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)3 Schema (liquibase.structure.core.Schema)3 LinkedList (java.util.LinkedList)2 AddColumnConfig (liquibase.change.AddColumnConfig)2 ColumnConfig (liquibase.change.ColumnConfig)2 DB2Database (liquibase.database.core.DB2Database)2 MySQLDatabase (liquibase.database.core.MySQLDatabase)2 DatabaseDataType (liquibase.datatype.DatabaseDataType)2 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)2 UniqueConstraint (liquibase.statement.UniqueConstraint)2 Table (liquibase.structure.core.Table)2 StringClauses (liquibase.util.StringClauses)2 BigInteger (java.math.BigInteger)1 InformixDatabase (liquibase.database.core.InformixDatabase)1