Search in sources :

Example 1 with DatabaseException

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);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException)

Example 2 with DatabaseException

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Properties(java.util.Properties) DatabaseFactory(liquibase.database.DatabaseFactory) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) BuildException(org.apache.tools.ant.BuildException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with DatabaseException

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);
    }
}
Also used : DropAllCommand(liquibase.command.core.DropAllCommand) CommandExecutionException(liquibase.command.CommandExecutionException) DatabaseException(liquibase.exception.DatabaseException)

Example 4 with DatabaseException

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);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DatabaseException(liquibase.exception.DatabaseException) FileInputStream(java.io.FileInputStream)

Example 5 with DatabaseException

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);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UtfBomAwareReader(liquibase.resource.UtfBomAwareReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)139 SQLException (java.sql.SQLException)65 JdbcConnection (liquibase.database.jvm.JdbcConnection)34 PreparedStatement (java.sql.PreparedStatement)33 ResultSet (java.sql.ResultSet)28 Statement (java.sql.Statement)24 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)24 CustomChangeException (liquibase.exception.CustomChangeException)22 CatalogAndSchema (liquibase.CatalogAndSchema)17 LiquibaseException (liquibase.exception.LiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 InvalidExampleException (liquibase.snapshot.InvalidExampleException)14 RawSqlStatement (liquibase.statement.core.RawSqlStatement)14 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)13 CachedRow (liquibase.snapshot.CachedRow)13 SqlStatement (liquibase.statement.SqlStatement)13 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 ArrayList (java.util.ArrayList)11