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);
}
}
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;
}
}
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations