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