Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address issue #110 - Support for storing binary data #167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ a VARCHAR(36) column. For other engines UUID optimistic casting can be enabled u
When this config is present UUIDs will be handled as a native type.


Storing data to a binary column encounters an issue because the internally stored Based64 encoding of the array
isn't recognized as a byte array. Enable optimistic casting of a Base64-encoded string to a byte[] using:

{ "castBase64": true }

== Use as OSGi bundle

Vert.x JDBC client can be used as an OSGi bundle. However notice that you would need to deploy all dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
Expand All @@ -63,15 +64,18 @@ public final class JDBCStatementHelper {
private static final Pattern DATE = Pattern.compile("^\\d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}$");
private static final Pattern TIME = Pattern.compile("^\\d{2}:\\d{2}:\\d{2}$");
private static final Pattern UUID = Pattern.compile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$");
private static final Pattern BASE64 = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$");

private final boolean castUUID;
private final boolean castBase64;

public JDBCStatementHelper() {
this(new JsonObject());
}

public JDBCStatementHelper(JsonObject config) {
this.castUUID = config.getBoolean("castUUID", false);
this.castBase64 = config.getBoolean("castBase64", false);
}

public void fillStatement(PreparedStatement statement, JsonArray in) throws SQLException {
Expand Down Expand Up @@ -302,6 +306,11 @@ public Object optimisticCast(String value) {
return java.util.UUID.fromString(value);
}

// possible byte[]
if (castBase64 && BASE64.matcher(value).matches()) {
return Base64.getDecoder().decode(value);
}

} catch (RuntimeException e) {
log.debug(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@RunWith(Parameterized.class)
public class OptimisticCastTest {

private JDBCStatementHelper helper = new JDBCStatementHelper(new JsonObject().put("castUUID", true));
private JDBCStatementHelper helper = new JDBCStatementHelper(new JsonObject().put("castUUID", true).put("castBase64", true));

@Parameterized.Parameters
public static Collection<Object[]> generateData() {
Expand All @@ -23,6 +23,7 @@ public static Collection<Object[]> generateData() {
{"2016-03-16", "java.sql.Date"},
{"2016-03-16T16:00:00Z", "java.sql.Timestamp"},
{"f47ac10b-58cc-4372-a567-0e02b2c3d479", "java.util.UUID"},
{"Dyu+lY5vAxJhM8UCCOtk7w==", "[B"},
// bad variations
{"2016-03-16T16:00:00", "java.lang.String"},
{"24:00:00", "java.lang.String"},
Expand All @@ -43,4 +44,4 @@ public OptimisticCastTest(String value, String expectedType) {
public void testOptimisticCast() {
assertEquals(value, expectedType, helper.optimisticCast(value).getClass().getName());
}
}
}
10 changes: 8 additions & 2 deletions src/test/java/io/vertx/ext/jdbc/impl/actions/SQLConvertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
Expand All @@ -21,7 +22,7 @@
@RunWith(Parameterized.class)
public class SQLConvertTest {

private JsonObject config = new JsonObject().put("castUUID", true);
private JsonObject config = new JsonObject().put("castUUID", true).put("castBase64", true);

private JDBCStatementHelper helper = new JDBCStatementHelper(config);

Expand All @@ -38,6 +39,7 @@ public static Collection<Object[]> generateData() {
params.add(new Object[]{dateTime.toLocalDate().toString(), java.sql.Date.class});

params.add(new Object[]{"f47ac10b-58cc-4372-a567-0e02b2c3d479", UUID.class});
params.add(new Object[]{"Dyu+lY5vAxJhM8UCCOtk7w==", byte[].class});

return params;
}
Expand All @@ -56,6 +58,10 @@ public void testSQLConvert() throws SQLException {
assertThat(cast, instanceOf(expectedSqlType));

Object convert = JDBCStatementHelper.convertSqlValue(cast);
assertEquals(value, convert);
if ( convert instanceof byte[] ) {
assertArrayEquals(Base64.getDecoder().decode(value), (byte[])convert);
} else {
assertEquals(value, convert);
}
}
}