Search in sources :

Example 21 with UnparsedSql

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

the class MockSqlGenerator method generateSql.

@Override
public Sql[] generateSql(SqlStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<Sql> sql = new ArrayList<Sql>();
    for (String returnSql : this.returnSql) {
        sql.add(new UnparsedSql(returnSql));
    }
    sql.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
    return sql.toArray(new Sql[sql.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 22 with UnparsedSql

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

the class AddDefaultValueGeneratorInformix method generateSql.

@Override
public Sql[] generateSql(AddDefaultValueStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    Object defaultValue = statement.getDefaultValue();
    StringBuffer sql = new StringBuffer("ALTER TABLE ");
    sql.append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()));
    sql.append(" MODIFY (");
    sql.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()));
    sql.append(" ");
    sql.append(DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database));
    sql.append(" DEFAULT ");
    sql.append(DataTypeFactory.getInstance().fromObject(defaultValue, database).objectToSql(defaultValue, database));
    sql.append(")");
    UnparsedSql unparsedSql = new UnparsedSql(sql.toString(), getAffectedColumn(statement));
    return new Sql[] { unparsedSql };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 23 with UnparsedSql

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

the class CopyRowsGenerator method generateSql.

@Override
public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    StringBuffer sql = new StringBuffer();
    if (statement.getCopyColumns().size() == 0) {
        return new Sql[] {};
    }
    if (database instanceof SQLiteDatabase) {
        sql.append("INSERT INTO `").append(statement.getTargetTable()).append("` (");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append("`").append(column.getName()).append("`");
        }
        sql.append(") SELECT ");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append("`").append(column.getName()).append("`");
        }
        sql.append(" FROM `").append(statement.getSourceTable()).append("`");
    }
    return new Sql[] { new UnparsedSql(sql.toString(), getAffectedTable(statement)) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 24 with UnparsedSql

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

the class CreateProcedureGenerator method generateSql.

@Override
public Sql[] generateSql(CreateProcedureStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<Sql> sql = new ArrayList<Sql>();
    String schemaName = statement.getSchemaName();
    if (schemaName == null && LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getAlwaysOverrideStoredLogicSchema()) {
        schemaName = database.getDefaultSchemaName();
    }
    String procedureText = addSchemaToText(statement.getProcedureText(), schemaName, "PROCEDURE", database);
    if (statement.getReplaceIfExists() != null && statement.getReplaceIfExists()) {
        String fullyQualifiedName = database.escapeObjectName(statement.getProcedureName(), StoredProcedure.class);
        if (schemaName != null) {
            fullyQualifiedName = database.escapeObjectName(schemaName, Schema.class) + "." + fullyQualifiedName;
        }
        sql.add(new UnparsedSql("if object_id('" + fullyQualifiedName + "', 'p') is null exec ('create procedure " + fullyQualifiedName + " as select 1 a')"));
        StringClauses parsedSql = SqlParser.parse(procedureText, true, true);
        StringClauses.ClauseIterator clauseIterator = parsedSql.getClauseIterator();
        Object next = "START";
        while (next != null && !(next.toString().equalsIgnoreCase("create") || next.toString().equalsIgnoreCase("alter")) && clauseIterator.hasNext()) {
            next = clauseIterator.nextNonWhitespace();
        }
        clauseIterator.replace("ALTER");
        procedureText = parsedSql.toString();
    }
    procedureText = removeTrailingDelimiter(procedureText, statement.getEndDelimiter());
    if (database instanceof MSSQLDatabase && procedureText.toLowerCase().contains("merge") && !procedureText.endsWith(";")) {
        //mssql "AS MERGE" procedures need a trailing ; (regardless of the end delimiter)
        StringClauses parsed = SqlParser.parse(procedureText);
        StringClauses.ClauseIterator clauseIterator = parsed.getClauseIterator();
        boolean reallyMerge = false;
        while (clauseIterator.hasNext()) {
            Object clause = clauseIterator.nextNonWhitespace();
            if (((String) clause).equalsIgnoreCase("merge")) {
                reallyMerge = true;
            }
        }
        if (reallyMerge) {
            procedureText = procedureText + ";";
        }
    }
    sql.add(new UnparsedSql(procedureText, statement.getEndDelimiter()));
    surroundWithSchemaSets(sql, statement.getSchemaName(), database);
    return sql.toArray(new Sql[sql.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) Schema(liquibase.structure.core.Schema) ArrayList(java.util.ArrayList) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) StringClauses(liquibase.util.StringClauses)

Example 25 with UnparsedSql

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

the class AddColumnGenerator method generateSingleColumn.

protected Sql[] generateSingleColumn(AddColumnStatement statement, Database database) {
    String alterTable = generateSingleColumBaseSQL(statement, database);
    alterTable += generateSingleColumnSQL(statement, database);
    List<Sql> returnSql = new ArrayList<Sql>();
    returnSql.add(new UnparsedSql(alterTable, getAffectedColumn(statement)));
    addUniqueConstrantStatements(statement, database, returnSql);
    addForeignKeyStatements(statement, database, returnSql);
    return returnSql.toArray(new Sql[returnSql.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

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