Search in sources :

Example 6 with Sql

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

the class CreateViewGenerator method generateSql.

@Override
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    if (database instanceof InformixDatabase) {
        return new CreateViewGeneratorInformix().generateSql(statement, database, sqlGeneratorChain);
    }
    List<Sql> sql = new ArrayList<Sql>();
    StringClauses viewDefinition = SqlParser.parse(statement.getSelectQuery(), true, true);
    if (!statement.isFullDefinition()) {
        viewDefinition.prepend(" ").prepend("AS").prepend(" ").prepend(database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName())).prepend(" ").prepend("VIEW").prepend(" ").prepend("CREATE");
    }
    if (statement.isReplaceIfExists()) {
        if (database instanceof FirebirdDatabase) {
            viewDefinition.replaceIfExists("CREATE", "RECREATE");
        } else if (database instanceof SybaseASADatabase && statement.getSelectQuery().toLowerCase().startsWith("create view")) {
        // Sybase ASA saves view definitions with header.
        } else if (database instanceof MSSQLDatabase) {
            //from http://stackoverflow.com/questions/163246/sql-server-equivalent-to-oracles-create-or-replace-view
            CatalogAndSchema schema = new CatalogAndSchema(statement.getCatalogName(), statement.getSchemaName()).customize(database);
            sql.add(new UnparsedSql("IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[" + schema.getSchemaName() + "].[" + statement.getViewName() + "]'))\n" + "    EXEC sp_executesql N'CREATE VIEW [" + schema.getSchemaName() + "].[" + statement.getViewName() + "] AS SELECT ''This is a code stub which will be replaced by an Alter Statement'' as [code_stub]'"));
            viewDefinition.replaceIfExists("CREATE", "ALTER");
        } else if (database instanceof PostgresDatabase) {
            sql.add(new UnparsedSql("DROP VIEW IF EXISTS " + database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName())));
        } else {
            if (!viewDefinition.contains("replace")) {
                viewDefinition.replace("CREATE", "CREATE OR REPLACE");
            }
        }
    }
    sql.add(new UnparsedSql(viewDefinition.toString(), getAffectedView(statement)));
    return sql.toArray(new Sql[sql.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) CatalogAndSchema(liquibase.CatalogAndSchema) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) StringClauses(liquibase.util.StringClauses)

Example 7 with Sql

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

the class CreateViewGeneratorInformix method generateSql.

@Override
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String viewName = database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName());
    String createClause = "CREATE VIEW  " + viewName + " AS SELECT * FROM (" + statement.getSelectQuery() + ") AS v";
    if (statement.isReplaceIfExists()) {
        return new Sql[] { new UnparsedSql("DROP VIEW IF EXISTS " + viewName), new UnparsedSql(createClause, new View().setName(viewName).setSchema(statement.getCatalogName(), statement.getViewName())) };
    }
    return new Sql[] { new UnparsedSql(createClause) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) View(liquibase.structure.core.View) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 8 with Sql

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

the class DropColumnGenerator method generateMultipleColumnSql.

private Sql[] generateMultipleColumnSql(List<DropColumnStatement> columns, Database database) {
    List<Sql> result = new ArrayList<Sql>();
    if (database instanceof MySQLDatabase) {
        String alterTable = "ALTER TABLE " + database.escapeTableName(columns.get(0).getCatalogName(), columns.get(0).getSchemaName(), columns.get(0).getTableName());
        for (int i = 0; i < columns.size(); i++) {
            alterTable += " DROP " + database.escapeColumnName(columns.get(i).getCatalogName(), columns.get(i).getSchemaName(), columns.get(i).getTableName(), columns.get(i).getColumnName());
            if (i < columns.size() - 1) {
                alterTable += ",";
            }
        }
        result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
    } else {
        for (DropColumnStatement column : columns) {
            result.add(generateSingleColumnSql(column, database)[0]);
        }
    }
    return result.toArray(new Sql[result.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) DropColumnStatement(liquibase.statement.core.DropColumnStatement) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 9 with Sql

use of liquibase.sql.Sql 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 10 with Sql

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

Aggregations

Sql (liquibase.sql.Sql)95 Test (org.junit.Test)55 Database (liquibase.database.Database)42 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)40 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)37 OracleDatabase (liquibase.database.core.OracleDatabase)36 PostgresDatabase (liquibase.database.core.PostgresDatabase)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DB2Database (liquibase.database.core.DB2Database)33 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)32 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)32 CreateTableStatement (liquibase.statement.core.CreateTableStatement)32 DerbyDatabase (liquibase.database.core.DerbyDatabase)31 H2Database (liquibase.database.core.H2Database)31 UnparsedSql (liquibase.sql.UnparsedSql)31 ArrayList (java.util.ArrayList)11 CatalogAndSchema (liquibase.CatalogAndSchema)8