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;
}
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;
}
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 + ")";
}
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);
}
}
}
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);
}
}
Aggregations