Search in sources :

Example 6 with OfflineConnection

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;
}
Also used : RawCallStatement(liquibase.statement.core.RawCallStatement) RawSqlStatement(liquibase.statement.core.RawSqlStatement) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 7 with OfflineConnection

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));
        }
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) Schema(liquibase.structure.core.Schema) ExecutorService(liquibase.executor.ExecutorService) OfflineConnection(liquibase.database.OfflineConnection)

Example 8 with OfflineConnection

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);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Method(java.lang.reflect.Method) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) DateParseException(liquibase.exception.DateParseException) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParseException(java.text.ParseException)

Example 9 with OfflineConnection

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;
}
Also used : HsqlDatabase(liquibase.database.core.HsqlDatabase) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) OfflineConnection(liquibase.database.OfflineConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 10 with OfflineConnection

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);
}
Also used : JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) Database(liquibase.database.Database) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) OfflineConnection(liquibase.database.OfflineConnection) Test(org.junit.Test) AbstractJdbcDatabaseTest(liquibase.database.AbstractJdbcDatabaseTest)

Aggregations

OfflineConnection (liquibase.database.OfflineConnection)23 DatabaseException (liquibase.exception.DatabaseException)14 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 Database (liquibase.database.Database)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CallableStatement (java.sql.CallableStatement)3 ParseException (java.text.ParseException)3 DateParseException (liquibase.exception.DateParseException)3 ExecutorService (liquibase.executor.ExecutorService)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 DatabaseFactory (liquibase.database.DatabaseFactory)2 Executor (liquibase.executor.Executor)2 ParsedNode (liquibase.parser.core.ParsedNode)2