From 4878681804738bae9a1f3140c5e5a67eeaa17bd5 Mon Sep 17 00:00:00 2001 From: mj1618 Date: Mon, 16 Feb 2015 21:10:52 +0800 Subject: [PATCH] Have added support for byte[] to be inserted into blob-like fields. I experienced issues while trying to save images in a BLOB on mysql, adding this fixed the problem as it doesn't convert the binary data into another format. --- src/main/java/com/jcabi/jdbc/ColumnOutcome.java | 3 +++ src/main/java/com/jcabi/jdbc/PrepareArgs.java | 2 ++ src/main/java/com/jcabi/jdbc/SingleOutcome.java | 3 +++ 3 files changed, 8 insertions(+) diff --git a/src/main/java/com/jcabi/jdbc/ColumnOutcome.java b/src/main/java/com/jcabi/jdbc/ColumnOutcome.java index 702b439..b316253 100644 --- a/src/main/java/com/jcabi/jdbc/ColumnOutcome.java +++ b/src/main/java/com/jcabi/jdbc/ColumnOutcome.java @@ -77,6 +77,7 @@ public ColumnOutcome(@NotNull(message = "type can't be NULL") if (tpe.equals(String.class) || tpe.equals(Long.class) || tpe.equals(Boolean.class) || tpe.equals(Byte.class) || tpe.equals(Date.class) || tpe.equals(Utc.class) + || tpe.equals(byte[].class) ) { this.type = tpe.getName(); } else { @@ -121,6 +122,8 @@ private T fetch(final ResultSet rset) throws SQLException { result = rset.getDate(1); } else if (tpe.equals(Utc.class)) { result = new Utc(Utc.getTimestamp(rset, 1)); + } else if (tpe.equals(byte[].class)) { + result = rset.getBytes(1); } else { throw new IllegalStateException( String.format("type %s is not allowed", tpe.getName()) diff --git a/src/main/java/com/jcabi/jdbc/PrepareArgs.java b/src/main/java/com/jcabi/jdbc/PrepareArgs.java index b1c5326..ebf5dcf 100644 --- a/src/main/java/com/jcabi/jdbc/PrepareArgs.java +++ b/src/main/java/com/jcabi/jdbc/PrepareArgs.java @@ -73,6 +73,8 @@ public void prepare(final PreparedStatement stmt) throws SQLException { stmt.setInt(pos, Integer.class.cast(arg)); } else if (arg instanceof Utc) { Utc.class.cast(arg).setTimestamp(stmt, pos); + } else if (arg instanceof byte[]) { + stmt.setBytes(pos,(byte[])arg); } else { stmt.setString(pos, arg.toString()); } diff --git a/src/main/java/com/jcabi/jdbc/SingleOutcome.java b/src/main/java/com/jcabi/jdbc/SingleOutcome.java index cb964e6..dcdc6bf 100644 --- a/src/main/java/com/jcabi/jdbc/SingleOutcome.java +++ b/src/main/java/com/jcabi/jdbc/SingleOutcome.java @@ -105,6 +105,7 @@ public SingleOutcome( if (tpe.equals(String.class) || tpe.equals(Long.class) || tpe.equals(Boolean.class) || tpe.equals(Byte.class) || tpe.equals(Date.class) || tpe.equals(Utc.class) + || tpe.equals(byte[].class) ) { this.type = tpe.getName(); } else { @@ -152,6 +153,8 @@ private T fetch(final ResultSet rset) throws SQLException { result = rset.getDate(1); } else if (tpe.equals(Utc.class)) { result = new Utc(Utc.getTimestamp(rset, 1)); + } else if (tpe.equals(byte[].class)) { + result = rset.getBytes(1); } else { throw new IllegalStateException( String.format("type %s is not allowed", tpe.getName())