Search in sources :

Example 11 with OfflineConnection

use of liquibase.database.OfflineConnection in project liquibase by liquibase.

the class DB2Database method getDefaultCatalogName.

@Override
public String getDefaultCatalogName() {
    if (defaultCatalogName != null) {
        return defaultCatalogName;
    }
    if (defaultSchemaName != null) {
        return defaultSchemaName;
    }
    if (getConnection() == null) {
        return null;
    }
    if (getConnection() instanceof OfflineConnection) {
        return ((OfflineConnection) getConnection()).getSchema();
    }
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = ((JdbcConnection) getConnection()).createStatement();
        rs = stmt.executeQuery("select current schema from sysibm.sysdummy1");
        if (rs.next()) {
            String result = rs.getString(1);
            if (result != null) {
                this.defaultSchemaName = StringUtils.trimToNull(result);
            } else {
                this.defaultSchemaName = StringUtils.trimToNull(super.getDefaultSchemaName());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not determine current schema", e);
    } finally {
        JdbcUtils.close(rs, stmt);
    }
    return defaultSchemaName;
}
Also used : GetViewDefinitionStatement(liquibase.statement.core.GetViewDefinitionStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) DateParseException(liquibase.exception.DateParseException) ParseException(java.text.ParseException)

Example 12 with OfflineConnection

use of liquibase.database.OfflineConnection in project libresonic by Libresonic.

the class SpringLiquibase method createDatabase.

@Override
protected Database createDatabase(Connection c, ResourceAccessor resourceAccessor) throws DatabaseException {
    DatabaseConnection liquibaseConnection;
    if (c == null) {
        log.warning("Null connection returned by liquibase datasource. Using offline unknown database");
        liquibaseConnection = new OfflineConnection("offline:unknown", resourceAccessor);
    } else {
        liquibaseConnection = new JdbcConnection(c);
    }
    DatabaseFactory factory = DatabaseFactory.getInstance();
    overrideHsqlDbImplementation(factory);
    Database database = factory.findCorrectDatabaseImplementation(liquibaseConnection);
    if (StringUtils.trimToNull(this.defaultSchema) != null) {
        database.setDefaultSchemaName(this.defaultSchema);
    }
    return database;
}
Also used : DatabaseFactory(liquibase.database.DatabaseFactory) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection)

Example 13 with OfflineConnection

use of liquibase.database.OfflineConnection in project liquibase by liquibase.

the class Scope method describe.

public String describe() {
    String databaseName = null;
    Database database = getDatabase();
    if (database != null) {
        databaseName = database.getShortName();
        DatabaseConnection connection = database.getConnection();
        if (connection == null) {
            databaseName = "unconnected " + databaseName;
        } else if (connection instanceof OfflineConnection) {
            databaseName = "offline " + databaseName;
        } else if (connection instanceof JdbcConnection) {
            databaseName = "jdbc " + databaseName;
        }
    }
    return "scope(database=" + databaseName + ")";
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection)

Example 14 with OfflineConnection

use of liquibase.database.OfflineConnection in project liquibase by liquibase.

the class JdbcExecutor method executeDb2ZosComplexStatement.

private void executeDb2ZosComplexStatement(SqlStatement sqlStatement) throws DatabaseException {
    DatabaseConnection con = database.getConnection();
    if (con instanceof OfflineConnection) {
        throw new DatabaseException("Cannot execute commands against an offline database");
    }
    Sql[] sqls = SqlGeneratorFactory.getInstance().generateSql(sqlStatement, database);
    for (Sql sql : sqls) {
        try {
            if (sql instanceof CallableSql) {
                CallableStatement call = null;
                ResultSet resultSet = null;
                try {
                    call = ((JdbcConnection) con).getUnderlyingConnection().prepareCall(sql.toSql());
                    resultSet = call.executeQuery();
                    checkCallStatus(resultSet, ((CallableSql) sql).getExpectedStatus());
                } finally {
                    JdbcUtil.close(resultSet, call);
                }
            } else {
                Statement stmt = null;
                try {
                    stmt = ((JdbcConnection) con).getUnderlyingConnection().createStatement();
                    stmt.execute(sql.toSql());
                    con.commit();
                } finally {
                    JdbcUtil.closeStatement(stmt);
                }
            }
        } catch (Exception e) {
            throw new DatabaseException(e.getMessage() + " [Failed SQL: " + getErrorCode(e) + sql.toSql() + "]", e);
        }
    }
}
Also used : CallableSql(liquibase.sql.CallableSql) CallableStatement(java.sql.CallableStatement) CompoundStatement(liquibase.statement.CompoundStatement) ExecutablePreparedStatement(liquibase.statement.ExecutablePreparedStatement) SqlStatement(liquibase.statement.SqlStatement) CallableSqlStatement(liquibase.statement.CallableSqlStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql) CallableSql(liquibase.sql.CallableSql)

Example 15 with OfflineConnection

use of liquibase.database.OfflineConnection in project liquibase by liquibase.

the class JdbcExecutor method execute.

// Incorrect warning, at least at this point. The situation here is not that we inject some unsanitised parameter
// into a query. Instead, we process a whole query. The check should be performed at the places where
// the query is composed.
@SuppressWarnings("squid:S2077")
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.
        JdbcUtil.closeStatement(stmt);
        stmt = null;
        throw new DatabaseException("Error executing SQL " + StringUtil.join(applyVisitors(action.getStatement(), sqlVisitors), "; on " + con.getURL()) + ": " + ex.getMessage(), ex);
    } finally {
        JdbcUtil.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)

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