Search in sources :

Example 11 with DatabaseConnection

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();
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) SQLException(java.sql.SQLException) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) MockDatabase(liquibase.database.core.MockDatabase) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseConnection(liquibase.database.DatabaseConnection) TestSystemFactory(liquibase.extension.testing.testsystem.TestSystemFactory) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseTestSystem(liquibase.extension.testing.testsystem.DatabaseTestSystem) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with DatabaseConnection

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);
}
Also used : DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 13 with DatabaseConnection

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);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseException(liquibase.exception.DatabaseException)

Example 14 with DatabaseConnection

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);
}
Also used : Matcher(java.util.regex.Matcher) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Method(java.lang.reflect.Method) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 15 with DatabaseConnection

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

Aggregations

DatabaseConnection (liquibase.database.DatabaseConnection)36 JdbcConnection (liquibase.database.jvm.JdbcConnection)20 DatabaseException (liquibase.exception.DatabaseException)15 OfflineConnection (liquibase.database.OfflineConnection)12 Database (liquibase.database.Database)11 SQLException (java.sql.SQLException)10 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 Test (org.junit.Test)5 Statement (java.sql.Statement)4 DatabaseFactory (liquibase.database.DatabaseFactory)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 CallableStatement (java.sql.CallableStatement)3 Connection (java.sql.Connection)3 Liquibase (liquibase.Liquibase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 SqlStatement (liquibase.statement.SqlStatement)3 Method (java.lang.reflect.Method)2 BigInteger (java.math.BigInteger)2 DatabaseMetaData (java.sql.DatabaseMetaData)2