use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class AbstractExecuteTest method resetAvailableDatabases.
public void resetAvailableDatabases() throws Exception {
for (DatabaseTestSystem testSystem : Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getAvailable(DatabaseTestSystem.class)) {
testSystem.start();
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(testSystem.getConnection()));
DatabaseConnection connection = database.getConnection();
Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
connection.commit();
try {
database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
CatalogAndSchema alt = new CatalogAndSchema(testSystem.getAltCatalog(), testSystem.getAltSchema());
database.dropDatabaseObjects(alt);
} catch (Exception 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(null, testSystem.getAltSchema()));
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(testSystem.getAltCatalog(), testSystem.getAltSchema(), database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
// ok
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(testSystem.getAltCatalog(), testSystem.getAltSchema(), database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
// ok
}
connection.commit();
}
List<? extends SqlStatement> setupStatements = setupStatements(database);
if (setupStatements != null) {
for (SqlStatement statement : setupStatements) {
Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).execute(statement);
}
}
connectionStatement.close();
}
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class AbstractIntegrationTest method openConnection.
private void openConnection() throws Exception {
DatabaseConnection connection = new JdbcConnection(testSystem.getConnection());
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class ForeignKeySnapshotGenerator method driverUsesSpFkeys.
/*
* Sql server JDBC drivers prior to 6.3.3 used sp_fkeys to determine the delete/cascade metadata.
* The sp_fkeys stored procedure spec says that returned integer values of 0, 1, 2, or 4
* translate to cascade, noAction, SetNull, or SetDefault which are not the values in the JDBC
* standard.
*
* If this method returns true, the sp_fkeys values should be used. Otherwise use the standard jdbc logic
*
* The change in logic went in with https://github.com/Microsoft/mssql-jdbc/pull/490
*/
private boolean driverUsesSpFkeys(Database database) throws DatabaseException {
if (!(database instanceof MSSQLDatabase)) {
return false;
}
DatabaseConnection connection = database.getConnection();
if (!(connection instanceof JdbcConnection)) {
return false;
}
try {
DatabaseMetaData metaData = ((JdbcConnection) connection).getMetaData();
int driverMajorVersion = metaData.getDriverMajorVersion();
int driverMinorVersion = metaData.getDriverMinorVersion();
String driverName = metaData.getDriverName();
if (!driverName.startsWith("Microsoft")) {
return false;
}
if (driverMajorVersion > 6 || (driverMajorVersion == 6 && driverMinorVersion >= 3)) {
return false;
}
return true;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class OracleDatabase method setConnection.
@Override
public void setConnection(DatabaseConnection conn) {
// noinspection HardCodedStringLiteral,HardCodedStringLiteral,HardCodedStringLiteral,HardCodedStringLiteral,
// HardCodedStringLiteral
// 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 https://liquibase.jira.com/browse/CORE-2192
*/
if (conn instanceof JdbcConnection) {
sqlConn = ((JdbcConnection) conn).getWrappedConnection();
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
if (sqlConn != null) {
tryProxySession(conn.getURL(), sqlConn);
try {
// noinspection HardCodedStringLiteral
reservedWords.addAll(Arrays.asList(sqlConn.getMetaData().getSQLKeywords().toUpperCase().split(",\\s*")));
} catch (SQLException e) {
// noinspection HardCodedStringLiteral
Scope.getCurrentScope().getLog(getClass()).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) {
// noinspection HardCodedStringLiteral
Scope.getCurrentScope().getLog(getClass()).info("Could not set remarks reporting on OracleDatabase: " + e.getMessage());
// cannot set it. That is OK
}
CallableStatement statement = null;
try {
// noinspection HardCodedStringLiteral
statement = sqlConn.prepareCall("{call DBMS_UTILITY.DB_VERSION(?,?)}");
statement.registerOutParameter(1, Types.VARCHAR);
statement.registerOutParameter(2, Types.VARCHAR);
statement.execute();
String compatibleVersion = statement.getString(2);
if (compatibleVersion != null) {
Matcher majorVersionMatcher = Pattern.compile("(\\d+)\\.(\\d+)\\..*").matcher(compatibleVersion);
if (majorVersionMatcher.matches()) {
this.databaseMajorVersion = Integer.valueOf(majorVersionMatcher.group(1));
this.databaseMinorVersion = Integer.valueOf(majorVersionMatcher.group(2));
}
}
} catch (SQLException e) {
@SuppressWarnings("HardCodedStringLiteral") String message = "Cannot read from DBMS_UTILITY.DB_VERSION: " + e.getMessage();
// noinspection HardCodedStringLiteral
Scope.getCurrentScope().getLog(getClass()).info("Could not set check compatibility mode on OracleDatabase, assuming not running in any sort of compatibility mode: " + message);
} finally {
JdbcUtil.closeStatement(statement);
}
if (GlobalConfiguration.DDL_LOCK_TIMEOUT.getCurrentValue() != null) {
int timeoutValue = GlobalConfiguration.DDL_LOCK_TIMEOUT.getCurrentValue();
Scope.getCurrentScope().getLog(getClass()).fine("Setting DDL_LOCK_TIMEOUT value to " + timeoutValue);
String sql = "ALTER SESSION SET DDL_LOCK_TIMEOUT=" + timeoutValue;
PreparedStatement ddlLockTimeoutStatement = null;
try {
ddlLockTimeoutStatement = sqlConn.prepareStatement(sql);
ddlLockTimeoutStatement.execute();
} catch (SQLException sqle) {
Scope.getCurrentScope().getUI().sendErrorMessage("Unable to set the DDL_LOCK_TIMEOUT_VALUE: " + sqle.getMessage(), sqle);
Scope.getCurrentScope().getLog(getClass()).warning("Unable to set the DDL_LOCK_TIMEOUT_VALUE: " + sqle.getMessage(), sqle);
} finally {
JdbcUtil.closeStatement(ddlLockTimeoutStatement);
}
}
}
}
super.setConnection(conn);
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class OracleDatabase method canAccessDbaRecycleBin.
public boolean canAccessDbaRecycleBin() {
if (canAccessDbaRecycleBin == null) {
DatabaseConnection connection = getConnection();
if ((connection == null) || (connection instanceof OfflineConnection)) {
return false;
}
Statement statement = null;
try {
statement = ((JdbcConnection) connection).createStatement();
@SuppressWarnings("HardCodedStringLiteral") ResultSet resultSet = statement.executeQuery("select 1 from dba_recyclebin where 0=1");
// don't need to do anything with the result set, just make sure statement ran.
resultSet.close();
this.canAccessDbaRecycleBin = true;
} catch (Exception e) {
// noinspection HardCodedStringLiteral
if ((e instanceof SQLException) && e.getMessage().startsWith("ORA-00942")) {
// ORA-00942: table or view does not exist
this.canAccessDbaRecycleBin = false;
} else {
// noinspection HardCodedStringLiteral
Scope.getCurrentScope().getLog(getClass()).warning("Cannot check dba_recyclebin access", e);
this.canAccessDbaRecycleBin = false;
}
} finally {
JdbcUtil.close(null, statement);
}
}
return canAccessDbaRecycleBin;
}
Aggregations