Skip to content

Commit

Permalink
PDB-384: Make BLOB accept null values on all database engines
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui Ferreira committed May 24, 2023
1 parent 06bb644 commit 362d11f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 org.apache.commons.lang3.StringUtils.join;

/**
Expand Down Expand Up @@ -124,6 +125,11 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
case JSON:
case CLOB:
case BLOB:
if (isNull(value)) {
ps.setBytes(index, null);
break;
}

ps.setBytes(index, objectToArray(value));

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 org.apache.commons.lang3.StringUtils.join;

/**
Expand Down Expand Up @@ -122,6 +123,11 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
final boolean fromBatch) throws Exception {
switch (dbColumn.getDbColumnType()) {
case BLOB:
if (isNull(value)) {
ps.setBytes(index, null);
break;
}

ps.setBytes(index, objectToArray(value));

break;
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 @@ -235,6 +236,11 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
final boolean fromBatch) throws Exception {
switch (dbColumn.getDbColumnType()) {
case BLOB:
if (isNull(value)) {
ps.setBytes(index, null);
break;
}

final byte[] valArray = objectToArray(value);
if (fromBatch && valArray.length > MIN_SIZE_FOR_BLOB) {
// Use a blob for columns greater than 4K when inside a batch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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 org.apache.commons.lang3.StringUtils.join;

/**
Expand Down Expand Up @@ -143,6 +144,11 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
final boolean fromBatch) throws Exception {
switch (dbColumn.getDbColumnType()) {
case BLOB:
if (isNull(value)) {
ps.setBytes(index, null);
break;
}

ps.setBytes(index, objectToArray(value));
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 org.apache.commons.lang3.StringUtils.join;

/**
Expand Down Expand Up @@ -149,6 +150,11 @@ protected void setPreparedStatementValue(final PreparedStatement ps,
final boolean fromBatch) throws Exception {
switch (dbColumn.getDbColumnType()) {
case BLOB:
if (isNull(value)) {
ps.setBytes(index, null);
break;
}

ps.setBytes(index, objectToArray(value));
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.feedzai.commons.sql.abstraction.engine.impl.abs;

import com.feedzai.commons.sql.abstraction.ddl.DbColumn;
import com.feedzai.commons.sql.abstraction.ddl.DbColumnType;
import com.feedzai.commons.sql.abstraction.ddl.DbEntity;
import com.feedzai.commons.sql.abstraction.ddl.DbEntityType;
Expand All @@ -32,6 +33,7 @@
import com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseConfiguration;
import com.feedzai.commons.sql.abstraction.entry.EntityEntry;
import com.google.common.collect.Sets;
import java.sql.PreparedStatement;
import org.assertj.core.api.MapAssert;
import org.assertj.core.data.MapEntry;
import org.junit.Before;
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 362d11f

Please sign in to comment.