Search in sources :

Example 16 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class CreateViewChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    boolean replaceIfExists = false;
    if (getReplaceIfExists() != null && getReplaceIfExists()) {
        replaceIfExists = true;
    }
    boolean fullDefinition = false;
    if (this.fullDefinition != null) {
        fullDefinition = this.fullDefinition;
    }
    if (!supportsReplaceIfExistsOption(database) && replaceIfExists) {
        statements.add(new DropViewStatement(getCatalogName(), getSchemaName(), getViewName()));
        statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), getSelectQuery(), false).setFullDefinition(fullDefinition));
    } else {
        statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), getSelectQuery(), replaceIfExists).setFullDefinition(fullDefinition));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) ArrayList(java.util.ArrayList) DropViewStatement(liquibase.statement.core.DropViewStatement)

Example 17 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AbstractSQLChange method generateStatements.

/**
     * Generates one or more SqlStatements depending on how the SQL should be parsed.
     * If split statements is set to true then the SQL is split and each command is made into a separate SqlStatement.
     * <p></p>
     * If stripping comments is true then any comments are removed before the splitting is executed.
     * The set SQL is passed through the {@link java.sql.Connection#nativeSQL} method if a connection is available.
     */
@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> returnStatements = new ArrayList<SqlStatement>();
    String sql = StringUtils.trimToNull(getSql());
    if (sql == null) {
        return new SqlStatement[0];
    }
    String processedSQL = normalizeLineEndings(sql);
    for (String statement : StringUtils.processMutliLineSQL(processedSQL, isStripComments(), isSplitStatements(), getEndDelimiter())) {
        if (database instanceof MSSQLDatabase) {
            statement = statement.replaceAll("\\n", "\r\n");
        }
        String escapedStatement = statement;
        try {
            if (database.getConnection() != null) {
                escapedStatement = database.getConnection().nativeSQL(statement);
            }
        } catch (DatabaseException e) {
            escapedStatement = statement;
        }
        returnStatements.add(new RawSqlStatement(escapedStatement, getEndDelimiter()));
    }
    return returnStatements.toArray(new SqlStatement[returnStatements.size()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 18 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddColumnGeneratorSQLite method generateSql.

@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<Sql> sql = new ArrayList<Sql>();
    // define alter table logic
    SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {

        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[] { new ColumnConfig().setName(statement.getColumnName()).setType(statement.getColumnType()).setAutoIncrement(statement.isAutoIncrement()) };
        }

        public boolean copyThisColumn(ColumnConfig column) {
            return !column.getName().equals(statement.getColumnName());
        }

        public boolean createThisColumn(ColumnConfig column) {
            return true;
        }

        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        List<SqlStatement> alterTableStatements = SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
        sql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(alterTableStatements.toArray(new SqlStatement[alterTableStatements.size()]), database)));
    } catch (DatabaseException e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return sql.toArray(new Sql[sql.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 19 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class SqlGeneratorFactory method generateSql.

public Sql[] generateSql(SqlStatement[] statements, Database database) {
    List<Sql> returnList = new ArrayList<Sql>();
    SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance();
    for (SqlStatement statement : statements) {
        Sql[] sqlArray = factory.generateSql(statement, database);
        if (sqlArray != null && sqlArray.length > 0) {
            List<Sql> sqlList = Arrays.asList(sqlArray);
            returnList.addAll(sqlList);
        }
    }
    return returnList.toArray(new Sql[returnList.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Sql(liquibase.sql.Sql)

Example 20 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class MarkChangeSetRanGenerator method generateSql.

@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String dateValue = database.getCurrentDateTimeFunction();
    ChangeSet changeSet = statement.getChangeSet();
    SqlStatement runStatement;
    try {
        if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
            //don't mark
            return new Sql[0];
        }
        String tag = null;
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }
        if (statement.getExecType().ranBefore) {
            runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
            if (tag != null) {
                ((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
            }
        } else {
            runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue("COMMENTS", limitSize(StringUtils.trimToEmpty(changeSet.getComments()))).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue("CONTEXTS", changeSet.getContexts() == null || changeSet.getContexts().isEmpty() ? null : changeSet.getContexts().toString()).addColumnValue("LABELS", changeSet.getLabels() == null || changeSet.getLabels().isEmpty() ? null : changeSet.getLabels().toString()).addColumnValue("LIQUIBASE", LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP")).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
            if (tag != null) {
                ((InsertStatement) runStatement).addColumnValue("TAG", tag);
            }
        }
    } catch (LiquibaseException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql)

Aggregations

SqlStatement (liquibase.statement.SqlStatement)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)9 Column (liquibase.structure.core.Column)8 DB2Database (liquibase.database.core.DB2Database)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 DatabaseException (liquibase.exception.DatabaseException)6 Database (liquibase.database.Database)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 Sql (liquibase.sql.Sql)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UpdateStatement (liquibase.statement.core.UpdateStatement)5 Index (liquibase.structure.core.Index)5 Change (liquibase.change.Change)4 ColumnConfig (liquibase.change.ColumnConfig)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)3