use of liquibase.exception.DatabaseException in project ORCID-Source by ORCID.
the class AdministrativeChangesOptionChangeTask method execute.
@Override
public void execute(Database database) throws CustomChangeException {
LOGGER.info("Running...");
final JdbcConnection conn = (JdbcConnection) database.getConnection();
try (PreparedStatement selectStatement = conn.prepareStatement(SELECT_SQL);
PreparedStatement updateStatement = conn.prepareStatement(UPDATE_SQL)) {
boolean done = false;
conn.setAutoCommit(false);
while (!done) {
LOGGER.info("Getting next batch...");
done = true;
ResultSet resultsSet = selectStatement.executeQuery();
while (resultsSet.next()) {
done = false;
String orcid = resultsSet.getString(1);
LOGGER.debug("Processing orcid: {}", orcid);
updateStatement.setString(1, orcid);
updateStatement.addBatch();
}
updateStatement.executeBatch();
conn.commit();
}
} catch (DatabaseException | SQLException e) {
throw new CustomChangeException("Problem populating administrative changes option", e);
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class DatabaseType method createDatabase.
public Database createDatabase(ClassLoader classLoader) {
logParameters();
validateParameters();
try {
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
if (databaseClass != null) {
Database databaseInstance = (Database) ClasspathUtils.newInstance(databaseClass, classLoader, Database.class);
databaseFactory.register(databaseInstance);
}
DatabaseConnection jdbcConnection;
if (getUrl().startsWith("offline:")) {
jdbcConnection = new OfflineConnection(getUrl(), new ClassLoaderResourceAccessor(classLoader));
} else {
Driver driver = (Driver) ClasspathUtils.newInstance(getDriver(), classLoader, Driver.class);
if (driver == null) {
throw new BuildException("Unable to create Liquibase Database instance. Could not instantiate the JDBC driver.");
}
Properties connectionProps = new Properties();
String user = getUser();
if (user != null && !user.isEmpty()) {
connectionProps.setProperty(USER, user);
}
String password = getPassword();
if (password != null && !password.isEmpty()) {
connectionProps.setProperty(PASSWORD, password);
}
ConnectionProperties connectionProperties = getConnectionProperties();
if (connectionProperties != null) {
connectionProps.putAll(connectionProperties.buildProperties());
}
Connection connection = driver.connect(getUrl(), connectionProps);
if (connection == null) {
throw new BuildException("Unable to create Liquibase Database instance. Could not connect to the database.");
}
jdbcConnection = new JdbcConnection(connection);
}
Database database = databaseFactory.findCorrectDatabaseImplementation(jdbcConnection);
String schemaName = getDefaultSchemaName();
if (schemaName != null) {
database.setDefaultSchemaName(schemaName);
}
String catalogName = getDefaultCatalogName();
if (catalogName != null) {
database.setDefaultCatalogName(catalogName);
}
String currentDateTimeFunction = getCurrentDateTimeFunction();
if (currentDateTimeFunction != null) {
database.setCurrentDateTimeFunction(currentDateTimeFunction);
}
database.setOutputDefaultSchema(isOutputDefaultSchema());
database.setOutputDefaultCatalog(isOutputDefaultCatalog());
String liquibaseSchemaName = getLiquibaseSchemaName();
if (liquibaseSchemaName != null) {
database.setLiquibaseSchemaName(liquibaseSchemaName);
}
String liquibaseCatalogName = getLiquibaseCatalogName();
if (liquibaseCatalogName != null) {
database.setLiquibaseCatalogName(liquibaseCatalogName);
}
String databaseChangeLogTableName = getDatabaseChangeLogTableName();
if (databaseChangeLogTableName != null) {
database.setDatabaseChangeLogTableName(databaseChangeLogTableName);
}
String databaseChangeLogLockTableName = getDatabaseChangeLogLockTableName();
if (databaseChangeLogLockTableName != null) {
database.setDatabaseChangeLogLockTableName(databaseChangeLogLockTableName);
}
String liquibaseTablespaceName = getLiquibaseTablespaceName();
if (liquibaseTablespaceName != null) {
database.setLiquibaseTablespaceName(liquibaseTablespaceName);
}
return database;
} catch (SQLException e) {
throw new BuildException("Unable to create Liquibase database instance. A JDBC error occurred. " + e.toString(), e);
} catch (DatabaseException e) {
throw new BuildException("Unable to create Liquibase database instance. " + e.toString(), e);
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class Liquibase method dropAll.
/**
* Drops all database objects in the passed schema(s).
*/
public final void dropAll(CatalogAndSchema... schemas) throws DatabaseException {
if (schemas == null || schemas.length == 0) {
schemas = new CatalogAndSchema[] { new CatalogAndSchema(getDatabase().getDefaultCatalogName(), getDatabase().getDefaultSchemaName()) };
}
DropAllCommand dropAll = (DropAllCommand) CommandFactory.getInstance().getCommand("dropAll");
dropAll.setDatabase(this.getDatabase());
dropAll.setSchemas(schemas);
try {
dropAll.execute();
} catch (CommandExecutionException e) {
throw new DatabaseException(e);
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class ExecutablePreparedStatementBase method toBinaryStream.
@SuppressWarnings("resource")
private LOBContent<InputStream> toBinaryStream(String valueLobFile) throws DatabaseException, IOException {
InputStream in = getResourceAsStream(valueLobFile);
if (in == null) {
throw new DatabaseException("BLOB resource not found: " + valueLobFile);
}
try {
if (in instanceof FileInputStream) {
InputStream bufferedInput = createStream(in);
return new LOBContent<InputStream>(bufferedInput, ((FileInputStream) in).getChannel().size());
}
in = createStream(in);
final int IN_MEMORY_THRESHOLD = 100000;
if (in.markSupported()) {
in.mark(IN_MEMORY_THRESHOLD);
}
long length = StreamUtil.getContentLength(in);
if (in.markSupported() && length <= IN_MEMORY_THRESHOLD) {
in.reset();
} else {
StreamUtil.closeQuietly(in);
in = getResourceAsStream(valueLobFile);
in = createStream(in);
}
return new LOBContent<InputStream>(in, length);
} finally {
if (in != null) {
closeables.add(in);
}
}
}
use of liquibase.exception.DatabaseException in project liquibase by liquibase.
the class ExecutablePreparedStatementBase method toCharacterStream.
private LOBContent<Reader> toCharacterStream(String valueLobFile, String encoding) throws IOException, DatabaseException {
InputStream in = getResourceAsStream(valueLobFile);
if (in == null) {
throw new DatabaseException("CLOB resource not found: " + valueLobFile);
}
final int IN_MEMORY_THRESHOLD = 100000;
Reader reader = null;
try {
reader = createReader(in, encoding);
if (reader.markSupported()) {
reader.mark(IN_MEMORY_THRESHOLD);
}
long length = StreamUtil.getContentLength(reader);
if (reader.markSupported() && length <= IN_MEMORY_THRESHOLD) {
reader.reset();
} else {
StreamUtil.closeQuietly(reader);
in = getResourceAsStream(valueLobFile);
reader = createReader(in, encoding);
}
return new LOBContent<Reader>(reader, length);
} finally {
if (reader != null) {
closeables.add(reader);
}
if (in != null) {
closeables.add(in);
}
}
}
Aggregations