Skip to content

Commit

Permalink
Merge branch 'feedzai:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
victorcmg committed Jun 7, 2023
2 parents d626297 + 1f1c20e commit 011ba2e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import static com.feedzai.commons.sql.abstraction.util.StringUtils.quotize;
import static com.feedzai.commons.sql.abstraction.util.StringUtils.readString;
import static java.lang.String.format;
import static java.util.Objects.isNull;

/**
* Provides a set of functions to interact with the database.
Expand Down Expand Up @@ -2354,12 +2355,19 @@ private byte[] getReusableByteBuffer() {

/**
* Converts an object to byte array.
* <p>
* If the given {@code val} is {@code null}, returns {@code null}.
*
* @param val The object to convert.
* @return The byte array representation of the object.
* @return The byte array representation of the object, if the given {@code val} is present; otherwise, returns
* {@code null}.
* @throws IOException If the buffer is not enough to make the conversion.
*/
protected final synchronized byte[] objectToArray(Object val) throws IOException {
if (isNull(val)) {
return null;
}

final ByteArrayOutputStream bos = new InitiallyReusableByteArrayOutputStream(getReusableByteBuffer());
final ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static com.feedzai.commons.sql.abstraction.util.StringUtils.md5;
import static com.feedzai.commons.sql.abstraction.util.StringUtils.quotize;
import static java.lang.String.format;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.lang3.StringUtils.join;

Expand Down Expand Up @@ -236,7 +237,9 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
switch (dbColumn.getDbColumnType()) {
case BLOB:
final byte[] valArray = objectToArray(value);
if (fromBatch && valArray.length > MIN_SIZE_FOR_BLOB) {
if (isNull(valArray)) {
ps.setBytes(index, null);
} else if (fromBatch && valArray.length > MIN_SIZE_FOR_BLOB) {
// Use a blob for columns greater than 4K when inside a batch
// update because the Oracle driver creates one and only releases
// it when the PreparedStatement is closed. This will be closed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,27 @@ public void testBlob() throws Exception {
assertEquals(updBlob, result.get(0).get("COL2").<BlobTest>toBlob());
}

/**
* Tests that persisting a null value to a BLOB type column persists a null value.
*
* @throws Exception If anything goes wrong on engine-related operations, namely adding an entity, persisting an
* entry and querying an entry.
*/
@Test
public void testBlobPersistNull() throws Exception {
DbEntity entity = dbEntity().name("TEST").addColumn("COL1", STRING).addColumn("COL2", BLOB)
.build();
engine.addEntity(entity);
EntityEntry entry = entry().set("COL1", "CENINHAS").set("COL2", null)
.build();

engine.persist("TEST", entry);

List<Map<String, ResultColumn>> result = engine.query(select(all()).from(table("TEST")));
assertEquals("CENINHAS", result.get(0).get("COL1").toString());
assertNull(result.get(0).get("COL2").toObject());
}

@Test
public void testBlobSettingWithIndexTest() throws Exception {
DbEntity entity = dbEntity().name("TEST").addColumn("COL1", STRING).addColumn("COL2", BLOB)
Expand Down

0 comments on commit 011ba2e

Please sign in to comment.