Search in sources :

Example 6 with UnparsedSql

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

the class CreateIndexGenerator method generateSql.

@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    if (database instanceof OracleDatabase) {
        // Oracle don't create index when creates foreignKey
        // It means that all indexes associated with foreignKey should be created manualy
        List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
        if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT))) {
            return new Sql[0];
        }
    } else {
        // Default filter of index creation:
        // creation of all indexes with associations are switched off.
        List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
        if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
            return new Sql[0];
        }
    }
    StringBuffer buffer = new StringBuffer();
    buffer.append("CREATE ");
    if (statement.isUnique() != null && statement.isUnique()) {
        buffer.append("UNIQUE ");
    }
    if (database instanceof MSSQLDatabase) {
        if (statement.isClustered() != null) {
            if (statement.isClustered()) {
                buffer.append("CLUSTERED ");
            } else {
                buffer.append("NONCLUSTERED ");
            }
        }
    }
    buffer.append("INDEX ");
    if (statement.getIndexName() != null) {
        String indexSchema = statement.getTableSchemaName();
        buffer.append(database.escapeIndexName(statement.getTableCatalogName(), indexSchema, statement.getIndexName())).append(" ");
    }
    buffer.append("ON ");
    if (database instanceof OracleDatabase && statement.isClustered() != null && statement.isClustered()) {
        buffer.append("CLUSTER ");
    }
    buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
    Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
    while (iterator.hasNext()) {
        AddColumnConfig column = iterator.next();
        if (column.getComputed() == null) {
            buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
        } else {
            if (column.getComputed()) {
                buffer.append(column.getName());
            } else {
                buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
            }
        }
        if (column.getDescending() != null && column.getDescending()) {
            buffer.append(" DESC");
        }
        if (iterator.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(")");
    if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
        if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
            buffer.append(" ON ").append(statement.getTablespace());
        } else if (database instanceof DB2Database || database instanceof InformixDatabase) {
            buffer.append(" IN ").append(statement.getTablespace());
        } else {
            buffer.append(" TABLESPACE ").append(statement.getTablespace());
        }
    }
    if (database instanceof DB2Database && statement.isClustered() != null && statement.isClustered()) {
        buffer.append(" CLUSTER");
    }
    return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) AddColumnConfig(liquibase.change.AddColumnConfig) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 7 with UnparsedSql

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

the class CreateIndexGeneratorPostgres method generateSql.

@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // Default filter of index creation:
    // creation of all indexes with associations are switched off.
    List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
    if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
        return new Sql[0];
    }
    StringBuilder buffer = new StringBuilder();
    buffer.append("CREATE ");
    if (statement.isUnique() != null && statement.isUnique()) {
        buffer.append("UNIQUE ");
    }
    buffer.append("INDEX ");
    if (statement.getIndexName() != null) {
        // for postgres setting the schema name for the index name is invalid
        buffer.append(database.escapeObjectName(statement.getIndexName(), Index.class)).append(" ");
    }
    buffer.append("ON ");
    buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
    Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
    while (iterator.hasNext()) {
        AddColumnConfig column = iterator.next();
        if (column.getComputed() == null) {
            buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
        } else {
            if (column.getComputed()) {
                buffer.append(column.getName());
            } else {
                buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
            }
        }
        if (iterator.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(")");
    if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
        if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
            buffer.append(" ON ").append(statement.getTablespace());
        } else if (database instanceof DB2Database || database instanceof InformixDatabase) {
            buffer.append(" IN ").append(statement.getTablespace());
        } else {
            buffer.append(" TABLESPACE ").append(statement.getTablespace());
        }
    }
    if (statement.isClustered() != null && statement.isClustered()) {
        return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)), new UnparsedSql("CLUSTER " + database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName()) + " USING " + database.escapeObjectName(statement.getIndexName(), Index.class)) };
    } else {
        return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) DB2Database(liquibase.database.core.DB2Database) InformixDatabase(liquibase.database.core.InformixDatabase) UnparsedSql(liquibase.sql.UnparsedSql) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 8 with UnparsedSql

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

the class CreateProcedureGenerator method surroundWithSchemaSets.

/**
     * Convenience method for when the schemaName is set but we don't want to parse the body
     */
public static void surroundWithSchemaSets(List<Sql> sql, String schemaName, Database database) {
    if ((StringUtils.trimToNull(schemaName) != null) && !LiquibaseConfiguration.getInstance().getProperty(ChangeLogParserCofiguration.class, ChangeLogParserCofiguration.USE_PROCEDURE_SCHEMA).getValue(Boolean.class)) {
        String defaultSchema = database.getDefaultSchemaName();
        if (database instanceof OracleDatabase) {
            sql.add(0, new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schemaName, Schema.class)));
            sql.add(new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(defaultSchema, Schema.class)));
        } else if (database instanceof DB2Database) {
            sql.add(0, new UnparsedSql("SET CURRENT SCHEMA " + schemaName));
            sql.add(new UnparsedSql("SET CURRENT SCHEMA " + defaultSchema));
        }
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DB2Database(liquibase.database.core.DB2Database) UnparsedSql(liquibase.sql.UnparsedSql) Schema(liquibase.structure.core.Schema) ChangeLogParserCofiguration(liquibase.parser.ChangeLogParserCofiguration)

Example 9 with UnparsedSql

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

the class AddDefaultValueGeneratorPostgres method generateSql.

@Override
public Sql[] generateSql(final AddDefaultValueStatement statement, final Database database, final SqlGeneratorChain sqlGeneratorChain) {
    if (!(statement.getDefaultValue() instanceof SequenceNextValueFunction)) {
        return super.generateSql(statement, database, sqlGeneratorChain);
    }
    List<Sql> commands = new ArrayList<Sql>(Arrays.asList(super.generateSql(statement, database, sqlGeneratorChain)));
    // for postgres, we need to also set the sequence to be owned by this table for true serial like functionality.
    // this will allow a drop table cascade to remove the sequence as well.
    SequenceNextValueFunction sequenceFunction = (SequenceNextValueFunction) statement.getDefaultValue();
    String sequenceName = sequenceFunction.getValue();
    String sequenceSchemaName = sequenceFunction.getSequenceSchemaName();
    String sequence = database.escapeObjectName(null, sequenceSchemaName, sequenceName, Sequence.class);
    Sql alterSequenceOwner = new UnparsedSql("ALTER SEQUENCE " + sequence + " OWNED BY " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + "." + database.escapeObjectName(statement.getColumnName(), Column.class), getAffectedColumn(statement), getAffectedSequence(sequenceFunction));
    commands.add(alterSequenceOwner);
    return commands.toArray(new Sql[commands.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 10 with UnparsedSql

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

the class AddAutoIncrementGeneratorMySQL method generateSql.

@Override
public Sql[] generateSql(final AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    Sql[] sql = super.generateSql(statement, database, sqlGeneratorChain);
    if (statement.getStartWith() != null) {
        MySQLDatabase mysqlDatabase = (MySQLDatabase) database;
        String alterTableSql = "ALTER TABLE " + mysqlDatabase.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " " + mysqlDatabase.getTableOptionAutoIncrementStartWithClause(statement.getStartWith());
        sql = concact(sql, new UnparsedSql(alterTableSql, getAffectedTable(statement)));
    }
    return sql;
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) MySQLDatabase(liquibase.database.core.MySQLDatabase) 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