Search in sources :

Example 16 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class MSSQLDatabase method isCaseSensitive.

@Override
public boolean isCaseSensitive() {
    if (caseSensitive == null) {
        try {
            if (getConnection() instanceof JdbcConnection) {
                String catalog = getConnection().getCatalog();
                String sql = "SELECT CONVERT([sysname], DATABASEPROPERTYEX(N'" + escapeStringForDatabase(catalog) + "', 'Collation'))";
                String collation = ExecutorService.getInstance().getExecutor(this).queryForObject(new RawSqlStatement(sql), String.class);
                caseSensitive = collation != null && !collation.contains("_CI_");
            } else if (getConnection() instanceof OfflineConnection) {
                caseSensitive = ((OfflineConnection) getConnection()).isCaseSensitive();
            }
        } catch (Exception e) {
            LogFactory.getLogger().warning("Cannot determine case sensitivity from MSSQL", e);
        }
    }
    return caseSensitive != null && caseSensitive;
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 17 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class MSSQLDatabase method sendsStringParametersAsUnicode.

public boolean sendsStringParametersAsUnicode() {
    if (sendsStringParametersAsUnicode == null) {
        try {
            if (getConnection() instanceof JdbcConnection) {
                PreparedStatement ps = null;
                ResultSet rs = null;
                try {
                    String sql = "SELECT CONVERT([sysname], SQL_VARIANT_PROPERTY(?, 'BaseType'))";
                    ps = ((JdbcConnection) getConnection()).prepareStatement(sql);
                    ps.setString(1, "Liquibase");
                    rs = ps.executeQuery();
                    String baseType = null;
                    if (rs.next()) {
                        baseType = rs.getString(1);
                    }
                    // i.e. nvarchar (or nchar)
                    sendsStringParametersAsUnicode = baseType == null || baseType.startsWith("n");
                } finally {
                    JdbcUtils.close(rs, ps);
                }
            } else if (getConnection() instanceof OfflineConnection) {
                sendsStringParametersAsUnicode = ((OfflineConnection) getConnection()).getSendsStringParametersAsUnicode();
            }
        } catch (Exception e) {
            LogFactory.getLogger().warning("Cannot determine whether String parameters are sent as Unicode for MSSQL", e);
        }
    }
    return sendsStringParametersAsUnicode == null ? true : sendsStringParametersAsUnicode;
}
Also used : ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 18 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class JdbcExecutor method execute.

public Object execute(CallableStatementCallback action, List<SqlVisitor> sqlVisitors) throws DatabaseException {
    DatabaseConnection con = database.getConnection();
    if (con instanceof OfflineConnection) {
        throw new DatabaseException("Cannot execute commands against an offline database");
    }
    CallableStatement stmt = null;
    try {
        String sql = applyVisitors(action.getStatement(), sqlVisitors)[0];
        stmt = ((JdbcConnection) con).getUnderlyingConnection().prepareCall(sql);
        return action.doInCallableStatement(stmt);
    } catch (SQLException ex) {
        // Release Connection early, to avoid potential connection pool deadlock
        // in the case when the exception translator hasn't been initialized yet.
        JdbcUtils.closeStatement(stmt);
        stmt = null;
        throw new DatabaseException("Error executing SQL " + StringUtils.join(applyVisitors(action.getStatement(), sqlVisitors), "; on " + con.getURL()) + ": " + ex.getMessage(), ex);
    } finally {
        JdbcUtils.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException)

Example 19 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class AbstractIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    openConnection(url);
    if (database != null) {
        if (!database.getConnection().getAutoCommit()) {
            database.rollback();
        }
        SnapshotGeneratorFactory.resetAll();
        ExecutorService.getInstance().reset();
        LockServiceFactory.getInstance().resetAll();
        LockServiceFactory.getInstance().getLockService(database).init();
        ChangeLogHistoryServiceFactory.getInstance().resetAll();
        if (database.getConnection() != null) {
            ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement().executeUpdate("drop table " + database.getDatabaseChangeLogLockTableName());
            database.commit();
        }
        SnapshotGeneratorFactory.resetAll();
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
        if (database.supportsSchemas()) {
            database.dropDatabaseObjects(new CatalogAndSchema((String) null, DatabaseTestContext.ALT_SCHEMA));
        }
        if (supportsAltCatalogTests()) {
            if (database.supportsSchemas() && database.supportsCatalogs()) {
                database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
            } else if (database.supportsCatalogs()) {
                database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_SCHEMA, null));
            }
        }
        database.commit();
        SnapshotGeneratorFactory.resetAll();
    }
}
Also used : LockService(liquibase.lockservice.LockService) JdbcConnection(liquibase.database.jvm.JdbcConnection) CatalogAndSchema(liquibase.CatalogAndSchema) Before(org.junit.Before)

Example 20 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class AbstractIntegrationTest method runUpdateOnOldChangelogTableFormat.

@Test
public void runUpdateOnOldChangelogTableFormat() throws Exception {
    if (database == null) {
        return;
    }
    Liquibase liquibase = createLiquibase(completeChangeLog);
    clearDatabase(liquibase);
    ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement().execute("CREATE TABLE DATABASECHANGELOG (id varchar(150) NOT NULL, " + "author varchar(150) NOT NULL, " + "filename varchar(255) NOT NULL, " + "dateExecuted " + DataTypeFactory.getInstance().fromDescription("datetime", database).toDatabaseDataType(database) + " NOT NULL, " + "md5sum varchar(32), " + "description varchar(255), " + "comments varchar(255), " + "tag varchar(255), " + "liquibase varchar(10), " + "PRIMARY KEY(id, author, filename))");
    liquibase = createLiquibase(completeChangeLog);
    liquibase.update(this.contexts);
}
Also used : Liquibase(liquibase.Liquibase) JdbcConnection(liquibase.database.jvm.JdbcConnection) Test(org.junit.Test)

Aggregations

JdbcConnection (liquibase.database.jvm.JdbcConnection)26 DatabaseException (liquibase.exception.DatabaseException)15 SQLException (java.sql.SQLException)13 OfflineConnection (liquibase.database.OfflineConnection)11 DatabaseConnection (liquibase.database.DatabaseConnection)10 Connection (java.sql.Connection)8 Database (liquibase.database.Database)8 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)6 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 Driver (java.sql.Driver)3 ArrayList (java.util.ArrayList)3 Liquibase (liquibase.Liquibase)3 DatabaseFactory (liquibase.database.DatabaseFactory)3 Method (java.lang.reflect.Method)2 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 DataSource (javax.sql.DataSource)2 LiquibaseException (liquibase.exception.LiquibaseException)2