Search in sources :

Example 26 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 27 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)

Example 28 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class ExecutablePreparedStatementBase method execute.

@Override
public void execute(PreparedStatementFactory factory) throws DatabaseException {
    // build the sql statement
    List<ColumnConfig> cols = new ArrayList<ColumnConfig>(getColumns().size());
    String sql = generateSql(cols);
    log.info("Prepared statement: " + sql);
    log.debug("Number of columns = " + cols.size());
    // create prepared statement
    PreparedStatement stmt = factory.create(sql);
    try {
        // attach params
        // index starts from 1
        int i = 1;
        for (ColumnConfig col : cols) {
            log.debug("Applying column parameter = " + i + " for column " + col.getName());
            applyColumnParameter(stmt, i, col);
            i++;
        }
        // trigger execution
        stmt.execute();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        for (Closeable closeable : closeables) {
            StreamUtil.closeQuietly(closeable);
        }
        JdbcUtils.closeStatement(stmt);
    }
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SQLException(java.sql.SQLException) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException)

Example 29 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class ExecutablePreparedStatementBase method applyColumnParameter.

private void applyColumnParameter(PreparedStatement stmt, int i, ColumnConfig col) throws SQLException, DatabaseException {
    if (col.getValue() != null) {
        log.debug("value is string = " + col.getValue());
        stmt.setString(i, col.getValue());
    } else if (col.getValueBoolean() != null) {
        log.debug("value is boolean = " + col.getValueBoolean());
        stmt.setBoolean(i, col.getValueBoolean());
    } else if (col.getValueNumeric() != null) {
        log.debug("value is numeric = " + col.getValueNumeric());
        Number number = col.getValueNumeric();
        if (number instanceof ColumnConfig.ValueNumeric) {
            ColumnConfig.ValueNumeric valueNumeric = (ColumnConfig.ValueNumeric) number;
            number = valueNumeric.getDelegate();
        }
        if (number instanceof Long) {
            stmt.setLong(i, number.longValue());
        } else if (number instanceof Integer) {
            stmt.setInt(i, number.intValue());
        } else if (number instanceof Double) {
            stmt.setDouble(i, number.doubleValue());
        } else if (number instanceof Float) {
            stmt.setFloat(i, number.floatValue());
        } else if (number instanceof BigDecimal) {
            stmt.setBigDecimal(i, (BigDecimal) number);
        } else if (number instanceof BigInteger) {
            stmt.setInt(i, number.intValue());
        } else {
        // TODO: Consider throwing an exception here
        }
    } else if (col.getValueDate() != null) {
        log.debug("value is date = " + col.getValueDate());
        if (col.getValueDate() instanceof Timestamp) {
            stmt.setTimestamp(i, (Timestamp) col.getValueDate());
        } else {
            stmt.setDate(i, new java.sql.Date(col.getValueDate().getTime()));
        }
    } else if (col.getValueBlobFile() != null) {
        log.debug("value is blob = " + col.getValueBlobFile());
        try {
            LOBContent<InputStream> lob = toBinaryStream(col.getValueBlobFile());
            if (lob.length <= Integer.MAX_VALUE) {
                stmt.setBinaryStream(i, lob.content, (int) lob.length);
            } else {
                stmt.setBinaryStream(i, lob.content, lob.length);
            }
        } catch (IOException e) {
            // wrap
            throw new DatabaseException(e.getMessage(), e);
        }
    } else if (col.getValueClobFile() != null) {
        try {
            log.debug("value is clob = " + col.getValueClobFile());
            LOBContent<Reader> lob = toCharacterStream(col.getValueClobFile(), col.getEncoding());
            if (lob.length <= Integer.MAX_VALUE) {
                stmt.setCharacterStream(i, lob.content, (int) lob.length);
            } else {
                stmt.setCharacterStream(i, lob.content, lob.length);
            }
        } catch (IOException e) {
            // wrap
            throw new DatabaseException(e.getMessage(), e);
        }
    } else {
        // NULL values might intentionally be set into a change, we must also add them to the prepared statement
        log.debug("value is explicit null");
        stmt.setNull(i, java.sql.Types.NULL);
    }
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) DatabaseException(liquibase.exception.DatabaseException)

Example 30 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class AbstractSQLChangeTest method generateStatements_willCallNativeSqlIfPossible.

@Test
public void generateStatements_willCallNativeSqlIfPossible() throws DatabaseException {
    ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("SOME SQL");
    Database database = mock(Database.class);
    DatabaseConnection connection = mock(DatabaseConnection.class);
    when(database.getConnection()).thenReturn(connection);
    when(connection.nativeSQL("SOME SQL")).thenReturn("SOME NATIVE SQL");
    SqlStatement[] statements = change.generateStatements(database);
    assertEquals(1, statements.length);
    assertEquals("SOME NATIVE SQL", ((RawSqlStatement) statements[0]).getSql());
    //If there is an error, it falls back to passed SQL
    when(connection.nativeSQL("SOME SQL")).thenThrow(new DatabaseException("Testing exception"));
    statements = change.generateStatements(database);
    assertEquals(1, statements.length);
    assertEquals("SOME SQL", ((RawSqlStatement) statements[0]).getSql());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseException(liquibase.exception.DatabaseException) Test(org.junit.Test)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)73 SQLException (java.sql.SQLException)25 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 CatalogAndSchema (liquibase.CatalogAndSchema)13 LiquibaseException (liquibase.exception.LiquibaseException)13 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 CachedRow (liquibase.snapshot.CachedRow)10 InvalidExampleException (liquibase.snapshot.InvalidExampleException)10 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)10 Executor (liquibase.executor.Executor)9 DatabaseConnection (liquibase.database.DatabaseConnection)7 OfflineConnection (liquibase.database.OfflineConnection)7 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)6 IOException (java.io.IOException)5