Search in sources :

Example 1 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class ExecutablePreparedStatementBase method execute.

@Override
public void execute(PreparedStatementFactory factory) throws DatabaseException {
    // build the sql statement
    List<ColumnConfig> cols = new ArrayList<>(getColumns().size());
    String sql = generateSql(cols);
    for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
        listener.writeSqlWillRun(sql);
    }
    Scope.getCurrentScope().getLog(getClass()).fine("Number of columns = " + cols.size());
    // create prepared statement
    PreparedStatement stmt = factory.create(sql);
    try {
        attachParams(cols, stmt);
        // trigger execution
        executePreparedStatement(stmt);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        for (Closeable closeable : closeables) {
            try {
                closeable.close();
            } catch (IOException ignore) {
            }
        }
        JdbcUtil.closeStatement(stmt);
    }
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SqlListener(liquibase.listener.SqlListener) DatabaseException(liquibase.exception.DatabaseException)

Example 2 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class AbstractExecuteTest method test.

private void test(String[] expectedSql, Class<? extends Database>[] includeDatabases, Class<? extends Database>[] excludeDatabases) throws Exception {
    if (expectedSql != null) {
        for (Database database : TestContext.getInstance().getAllDatabases()) {
            if (shouldTestDatabase(database, includeDatabases, excludeDatabases)) {
                testedDatabases.add(database.getClass());
                if (database.getConnection() != null) {
                    ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).init();
                    LockServiceFactory.getInstance().getLockService(database).init();
                }
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, database);
                assertNotNull("Null SQL for " + database, sql);
                assertEquals("Unexpected number of  SQL statements for " + database, expectedSql.length, sql.length);
                int index = 0;
                for (String convertedSql : expectedSql) {
                    convertedSql = replaceEscaping(convertedSql, database);
                    convertedSql = replaceDatabaseClauses(convertedSql, database);
                    convertedSql = replaceStandardTypes(convertedSql, database);
                    assertEquals("Incorrect SQL for " + database.getClass().getName(), convertedSql.toLowerCase().trim(), sql[index].toSql().toLowerCase());
                    index++;
                }
            }
        }
    }
    resetAvailableDatabases();
    for (DatabaseTestSystem testSystem : Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getAvailable(DatabaseTestSystem.class)) {
        testSystem.start();
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(testSystem.getConnection()));
        Statement statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
        if (shouldTestDatabase(database, includeDatabases, excludeDatabases)) {
            String sqlToRun = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, database)[0].toSql();
            try {
                for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                    listener.writeSqlWillRun(sqlToRun);
                }
                statement.execute(sqlToRun);
            } catch (Exception e) {
                System.out.println("Failed to execute against " + database.getShortName() + ": " + sqlToRun);
                throw e;
            }
        }
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) MockDatabase(liquibase.database.core.MockDatabase) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) JdbcConnection(liquibase.database.jvm.JdbcConnection) TestSystemFactory(liquibase.extension.testing.testsystem.TestSystemFactory) DatabaseTestSystem(liquibase.extension.testing.testsystem.DatabaseTestSystem) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql)

Example 3 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class LockServiceExecuteTest method fixupLockTables.

private void fixupLockTables() throws DatabaseException, LockException {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database.getConnection() != null) {
            Statement statement = null;
            try {
                statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
                try {
                    String sql = "DROP TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
                    for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                        listener.writeSqlWillRun(sql);
                    }
                    statement.execute(sql);
                } catch (Exception e) {
                // ok
                }
                try {
                    String sql = "DROP TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName());
                    for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                        listener.writeSqlWillRun(sql);
                    }
                    statement.execute(sql);
                } catch (Exception e) {
                // ok
                }
                statement.close();
                database.commit();
            } catch (SQLException e) {
                throw new DatabaseException(e);
            }
        }
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) LockException(liquibase.exception.LockException) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException)

Example 4 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class HsqlConnection method commit.

@Override
public void commit() throws DatabaseException {
    super.commit();
    Statement st = null;
    try {
        st = createStatement();
        final String sql = "CHECKPOINT";
        for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
            listener.writeSqlWillRun(sql);
        }
        st.execute(sql);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        JdbcUtil.closeStatement(st);
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseException(liquibase.exception.DatabaseException)

Example 5 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class HsqlConnection method rollback.

@Override
public void rollback() throws DatabaseException {
    super.rollback();
    Statement st = null;
    try {
        st = createStatement();
        final String sql = "CHECKPOINT";
        for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
            listener.writeSqlWillRun(sql);
        }
        st.execute(sql);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        JdbcUtil.closeStatement(st);
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

SqlListener (liquibase.listener.SqlListener)9 DatabaseException (liquibase.exception.DatabaseException)8 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)6 JdbcConnection (liquibase.database.jvm.JdbcConnection)5 Database (liquibase.database.Database)2 ChangeLogParseException (liquibase.exception.ChangeLogParseException)2 LiquibaseException (liquibase.exception.LiquibaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 SqlStatement (liquibase.statement.SqlStatement)2 Connection (java.sql.Connection)1 ColumnConfig (liquibase.change.ColumnConfig)1 DatabaseConnection (liquibase.database.DatabaseConnection)1 MockDatabase (liquibase.database.core.MockDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 UnsupportedDatabase (liquibase.database.core.UnsupportedDatabase)1 ExampleCustomDatabase (liquibase.database.example.ExampleCustomDatabase)1 LockException (liquibase.exception.LockException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 DatabaseTestSystem (liquibase.extension.testing.testsystem.DatabaseTestSystem)1