Search in sources :

Example 46 with Statement

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);
}
Also used : SQLException(java.sql.SQLException) Matcher(java.util.regex.Matcher) RawCallStatement(liquibase.statement.core.RawCallStatement) RawSqlStatement(liquibase.statement.core.RawSqlStatement) 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) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 47 with Statement

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);
        }
    }
}
Also used : RawCallStatement(liquibase.statement.core.RawCallStatement) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) Logger(liquibase.logging.Logger) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException)

Example 48 with 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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) LockException(liquibase.exception.LockException) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException)

Example 49 with Statement

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;
            }
        }
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) MockDatabase(liquibase.sdk.database.MockDatabase) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql)

Example 50 with Statement

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();
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) SQLException(java.sql.SQLException) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) MockDatabase(liquibase.sdk.database.MockDatabase) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) DatabaseConnection(liquibase.database.DatabaseConnection) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

Statement (java.sql.Statement)3054 Connection (java.sql.Connection)1634 ResultSet (java.sql.ResultSet)1631 SQLException (java.sql.SQLException)1529 PreparedStatement (java.sql.PreparedStatement)1329 Test (org.junit.Test)570 ArrayList (java.util.ArrayList)323 CallableStatement (java.sql.CallableStatement)135 ResultSetMetaData (java.sql.ResultSetMetaData)127 IOException (java.io.IOException)121 Properties (java.util.Properties)114 HashMap (java.util.HashMap)83 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 StringPlus (mom.trd.opentheso.bdd.tools.StringPlus)63 DataSource (javax.sql.DataSource)62 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)61 Vector (java.util.Vector)61 DatabaseMetaData (java.sql.DatabaseMetaData)53 KeyValue (org.vcell.util.document.KeyValue)49