Search in sources :

Example 41 with SqlStatement

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

the class AddPrimaryKeyChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    AddPrimaryKeyStatement statement = new AddPrimaryKeyStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnNames(), getConstraintName());
    statement.setTablespace(getTablespace());
    statement.setClustered(getClustered());
    statement.setForIndexName(getForIndexName());
    statement.setForIndexSchemaName(getForIndexSchemaName());
    statement.setForIndexCatalogName(getForIndexCatalogName());
    if (database instanceof DB2Database) {
        return new SqlStatement[] { statement, new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()) };
    //todo        } else if (database instanceof SQLiteDatabase) {
    //            // return special statements for SQLite databases
    //            return generateStatementsForSQLiteDatabase(database);
    }
    return new SqlStatement[] { statement };
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) AddPrimaryKeyStatement(liquibase.statement.core.AddPrimaryKeyStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement)

Example 42 with SqlStatement

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

the class CreateProcedureChange method generateStatements.

protected SqlStatement[] generateStatements(String logicText, String endDelimiter, Database database) {
    CreateProcedureStatement statement = new CreateProcedureStatement(getCatalogName(), getSchemaName(), getProcedureName(), logicText, endDelimiter);
    statement.setReplaceIfExists(getReplaceIfExists());
    return new SqlStatement[] { statement };
}
Also used : SqlStatement(liquibase.statement.SqlStatement) CreateProcedureStatement(liquibase.statement.core.CreateProcedureStatement)

Example 43 with SqlStatement

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

the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithStatementsOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor.

@Test
public void executeRollbackStatements_WithStatementsOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor() throws Exception {
    Database database = getDatabase();
    final MockExecutor mockExecutor = new MockExecutor();
    ExecutorService.getInstance().setExecutor(database, mockExecutor);
    final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
    final SqlStatement dropTableStatement = new DropTableStatement(null, null, "test_table", false);
    final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
    appendSqlVisitor.setApplyToRollback(false);
    appendSqlVisitor.setValue(" SHOULD NOT BE APPENDED");
    sqlVisitors.add(appendSqlVisitor);
    database.executeRollbackStatements(new SqlStatement[] { dropTableStatement }, sqlVisitors);
    assertEquals("DROP TABLE test_table;", mockExecutor.getRanSql().trim());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) SqlStatement(liquibase.statement.SqlStatement) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) DropTableStatement(liquibase.statement.core.DropTableStatement) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Example 44 with SqlStatement

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

the class AbstractSQLChangeTest method generateStatements_convertsEndingsOnSqlServer.

@Test
public void generateStatements_convertsEndingsOnSqlServer() {
    ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("LINE 1;\n--a comment\nLINE 2;\nLINE 3;");
    change.setSplitStatements(false);
    change.setStripComments(true);
    SqlStatement[] statements = change.generateStatements(new MSSQLDatabase());
    assertEquals(1, statements.length);
    assertEquals("LINE 1;\r\n\r\nLINE 2;\r\nLINE 3;", ((RawSqlStatement) statements[0]).getSql());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Test(org.junit.Test)

Example 45 with SqlStatement

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

the class AddColumnChangeTest method generateStatements_DB2_multipleColumns_single_reorg.

@Test
public void generateStatements_DB2_multipleColumns_single_reorg() {
    AddColumnChange change = new AddColumnChange();
    AddColumnConfig column1 = new AddColumnConfig();
    column1.setName("column1");
    column1.setType("INT");
    change.addColumn(column1);
    AddColumnConfig column2 = new AddColumnConfig();
    column2.setName("column2");
    column2.setType("INT");
    change.addColumn(column2);
    SqlStatement[] statements = change.generateStatements(new DB2Database());
    Assert.assertEquals(2, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertTrue(stmt.isMultiple());
    Assert.assertEquals(2, stmt.getColumns().size());
    Assert.assertTrue(statements[1] instanceof ReorganizeTableStatement);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

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