use of liquibase.database.OfflineConnection 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;
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class DatabaseUtils method initializeDatabase.
/**
* Executes RawSqlStatements particular to each database engine to set the default schema for the given Database
*
* @param defaultCatalogName Catalog name and schema name are similar concepts.
* Used if defaultCatalogName is null.
* @param defaultSchemaName Catalog name and schema name are similar concepts.
* Catalog is used with Oracle, DB2 and MySQL, and takes
* precedence over the schema name.
* @param database Which Database object is affected by the initialization.
* @throws DatabaseException
*/
public static void initializeDatabase(String defaultCatalogName, String defaultSchemaName, Database database) throws DatabaseException {
if (((defaultCatalogName != null) || (defaultSchemaName != null)) && !(database.getConnection() instanceof OfflineConnection)) {
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
if (database instanceof OracleDatabase) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(new RawSqlStatement("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schema, Schema.class)));
} else if (database instanceof PostgresDatabase && defaultSchemaName != null) {
final String searchPath = executor.queryForObject(new RawSqlStatement("SHOW SEARCH_PATH"), String.class);
if (!searchPath.equals(defaultCatalogName) && !searchPath.startsWith(defaultSchemaName + ",") && !searchPath.startsWith("\"" + defaultSchemaName + "\",")) {
executor.execute(new RawSqlStatement("SET SEARCH_PATH TO " + database.escapeObjectName(defaultSchemaName, Schema.class) + ", " + searchPath));
}
} else if (database instanceof AbstractDb2Database) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(new RawSqlStatement("SET CURRENT SCHEMA " + schema));
} else if (database instanceof MySQLDatabase) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(new RawSqlStatement("USE " + schema));
}
}
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class H2Database method setConnection.
@Override
public void setConnection(DatabaseConnection conn) {
Connection sqlConn = null;
if (!(conn instanceof OfflineConnection)) {
try {
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) {
Statement statement = null;
ResultSet resultSet = null;
try {
statement = sqlConn.createStatement();
resultSet = statement.executeQuery("SELECT SCHEMA()");
String schemaName = null;
if (resultSet.next()) {
schemaName = resultSet.getString(1);
}
if (schemaName != null) {
this.connectionSchemaName = schemaName;
}
} catch (SQLException e) {
Scope.getCurrentScope().getLog(getClass()).info("Could not read current schema name: " + e.getMessage());
} finally {
JdbcUtil.close(resultSet, statement);
}
}
}
super.setConnection(conn);
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class OfflineChangeLogHistoryServiceTest method createService.
/**
* Create OfflineChangeLogHistoryService and register LoggingExecutor
*/
private OfflineChangeLogHistoryService createService(Writer writer, String outputLiquibaseSql) {
HsqlDatabase database = new HsqlDatabase();
File changeLogCsvFile = new File(temporaryFolder.getRoot(), CHANGE_LOG_CSV);
OfflineConnection connection = new OfflineConnection("offline:hsqldb?changeLogFile=" + changeLogCsvFile.getAbsolutePath() + "&outputLiquibaseSql=" + outputLiquibaseSql, new ClassLoaderResourceAccessor());
database.setConnection(connection);
connection.attached(database);
OfflineChangeLogHistoryService changeLogHistoryService = (OfflineChangeLogHistoryService) ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
//
// Create the new LoggingExecutor and give it the original Executor as a delegator
// We also set the LoggingExecutor as the JDBC Executor
//
LoggingExecutor loggingExecutor = new LoggingExecutor(Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database), writer, database);
Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("logging", database, loggingExecutor);
Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, loggingExecutor);
return changeLogHistoryService;
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class OracleDatabaseTest method saveNlsEnvironment.
@Test
public void saveNlsEnvironment() throws Exception {
Database database = getDatabase();
ResourceAccessor junitResourceAccessor = new JUnitResourceAccessor();
OfflineConnection offlineConnection = new OfflineConnection("offline:oracle", junitResourceAccessor);
database.setConnection(offlineConnection);
}
Aggregations