use of java.sql.Statement in project liquibase by liquibase.
the class OracleDatabase method setConnection.
@Override
public void setConnection(DatabaseConnection conn) {
//more reserved words not returned by driver
reservedWords.addAll(Arrays.asList("GROUP", "USER", "SESSION", "PASSWORD", "RESOURCE", "START", "SIZE", "UID", "DESC", "ORDER"));
Connection sqlConn = null;
if (!(conn instanceof OfflineConnection)) {
try {
/**
* Don't try to call getWrappedConnection if the conn instance is
* is not a JdbcConnection. This happens for OfflineConnection.
* @see <a href="https://liquibase.jira.com/browse/CORE-2192">CORE-2192</a>
**/
if (conn instanceof JdbcConnection) {
Method wrappedConn = conn.getClass().getMethod("getWrappedConnection");
wrappedConn.setAccessible(true);
sqlConn = (Connection) wrappedConn.invoke(conn);
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
if (sqlConn != null) {
try {
reservedWords.addAll(Arrays.asList(sqlConn.getMetaData().getSQLKeywords().toUpperCase().split(",\\s*")));
} catch (SQLException e) {
LogFactory.getLogger().info("Could get sql keywords on OracleDatabase: " + e.getMessage());
//can not get keywords. Continue on
}
try {
Method method = sqlConn.getClass().getMethod("setRemarksReporting", Boolean.TYPE);
method.setAccessible(true);
method.invoke(sqlConn, true);
} catch (Exception e) {
LogFactory.getLogger().info("Could not set remarks reporting on OracleDatabase: " + e.getMessage());
//cannot set it. That is OK
;
}
Statement statement = null;
ResultSet resultSet = null;
try {
statement = sqlConn.createStatement();
resultSet = statement.executeQuery("SELECT value FROM v$parameter WHERE name = 'compatible'");
String compatibleVersion = null;
if (resultSet.next()) {
compatibleVersion = resultSet.getString("value");
}
if (compatibleVersion != null) {
Matcher majorVersionMatcher = Pattern.compile("(\\d+)\\..*").matcher(compatibleVersion);
if (majorVersionMatcher.matches()) {
this.databaseMajorVersion = Integer.valueOf(majorVersionMatcher.group(1));
}
}
} catch (SQLException e) {
String message = "Cannot read from v$parameter: " + e.getMessage();
LogFactory.getLogger().info("Could not set check compatibility mode on OracleDatabase, assuming not running in any sort of compatibility mode: " + message);
} finally {
JdbcUtils.close(resultSet, statement);
}
}
}
super.setConnection(conn);
}
use of java.sql.Statement in project liquibase by liquibase.
the class PostgresDatabase method setConnection.
@Override
public void setConnection(DatabaseConnection conn) {
super.setConnection(conn);
Logger log = LogFactory.getInstance().getLog();
if (conn instanceof JdbcConnection) {
Statement statement = null;
ResultSet resultSet = null;
try {
statement = ((JdbcConnection) conn).createStatement();
resultSet = statement.executeQuery("select setting from pg_settings where name = 'edb_redwood_date'");
if (resultSet.next()) {
String setting = resultSet.getString(1);
if (setting != null && setting.equals("on")) {
log.warning("EnterpriseDB " + conn.getURL() + " does not store DATE columns. Auto-converts them to TIMESTAMPs. (edb_redwood_date=true)");
}
}
} catch (Exception e) {
log.info("Cannot check pg_settings", e);
} finally {
JdbcUtils.close(resultSet, statement);
}
}
}
use of java.sql.Statement 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 {
statement.execute("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
} catch (Exception e) {
//ok
}
try {
statement.execute("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
} catch (Exception e) {
//ok
}
statement.close();
database.commit();
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
}
use of java.sql.Statement 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 (Database availableDatabase : DatabaseTestContext.getInstance().getAvailableDatabases()) {
Statement statement = ((JdbcConnection) availableDatabase.getConnection()).getUnderlyingConnection().createStatement();
if (shouldTestDatabase(availableDatabase, includeDatabases, excludeDatabases)) {
String sqlToRun = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, availableDatabase)[0].toSql();
try {
statement.execute(sqlToRun);
} catch (Exception e) {
System.out.println("Failed to execute against " + availableDatabase.getShortName() + ": " + sqlToRun);
throw e;
}
}
}
}
use of java.sql.Statement in project liquibase by liquibase.
the class AbstractExecuteTest method resetAvailableDatabases.
public void resetAvailableDatabases() throws Exception {
for (Database database : DatabaseTestContext.getInstance().getAvailableDatabases()) {
DatabaseConnection connection = database.getConnection();
Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
try {
database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
} catch (Throwable e) {
throw new UnexpectedLiquibaseException("Error dropping objects for database " + database.getShortName(), e);
}
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
;
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
;
}
connection.commit();
if (database.supportsSchemas()) {
database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
}
List<? extends SqlStatement> setupStatements = setupStatements(database);
if (setupStatements != null) {
for (SqlStatement statement : setupStatements) {
ExecutorService.getInstance().getExecutor(database).execute(statement);
}
}
connectionStatement.close();
}
}
Aggregations