diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/RenderAndBindTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/RenderAndBindTests.java index c9675a19ac..2c1e526f30 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/RenderAndBindTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/RenderAndBindTests.java @@ -313,17 +313,14 @@ extends BaseTest> 4]; + buff[i + i + 1] = hex[c & 0xf]; + } + return new String(buff); + } + + /** + * Postgres uses octals instead of hex encoding + */ + static String convertBytesToPostgresOctal(byte[] binary) { + StringBuilder sb = new StringBuilder(); + + for (byte b : binary) { + sb.append("\\\\"); + sb.append(leftPad(toOctalString(b), 3, '0')); + } + + return sb.toString(); + } } \ No newline at end of file diff --git a/jOOQ/src/main/java/org/jooq/impl/Val.java b/jOOQ/src/main/java/org/jooq/impl/Val.java index bec9ef8add..74a5266032 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Val.java +++ b/jOOQ/src/main/java/org/jooq/impl/Val.java @@ -42,12 +42,12 @@ import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.H2; import static org.jooq.SQLDialect.HSQLDB; import static org.jooq.SQLDialect.INGRES; +import static org.jooq.SQLDialect.MYSQL; import static org.jooq.SQLDialect.ORACLE; import static org.jooq.SQLDialect.POSTGRES; import static org.jooq.SQLDialect.SQLITE; import static org.jooq.SQLDialect.SQLSERVER; import static org.jooq.SQLDialect.SYBASE; -import static org.jooq.conf.StatementType.STATEMENT; import java.math.BigDecimal; import java.util.Arrays; @@ -64,8 +64,6 @@ import org.jooq.Param; import org.jooq.RenderContext; import org.jooq.SQLDialect; import org.jooq.UDTRecord; -import org.jooq.exception.DataAccessException; -import org.jooq.tools.JooqLogger; import org.jooq.tools.StringUtils; /** @@ -270,24 +268,41 @@ class Val extends AbstractField implements Param, BindingProvider { context.sql(val.toString()); } } + + // [#1154] Binary data cannot always be inlined else if (type == byte[].class) { byte[] binary = (byte[]) val; - // [#1154] Binary data cannot always be inlined - if (dialect == H2) { + if (asList(ASE, SQLSERVER, SYBASE).contains(dialect)) { + context.sql("0x") + .sql(Util.convertBytesToHex(binary)); + } + else if (dialect == DB2) { + context.sql("blob(X'") + .sql(Util.convertBytesToHex(binary)) + .sql("')"); + } + else if (asList(DERBY, H2, HSQLDB, INGRES, MYSQL, SQLITE).contains(dialect)) { context.sql("X'") - .sql(StringUtils.convertBytesToHex(binary)) + .sql(Util.convertBytesToHex(binary)) .sql("'"); } - else if (Util.getStatementType(context.getSettings()) == STATEMENT) { - throw new DataAccessException("Cannot inline binary data in dialect " + dialect + ". Use StatementType.PREPARED_STATEMENT instead"); + else if (asList(ORACLE).contains(dialect)) { + context.sql("hextoraw('") + .sql(Util.convertBytesToHex(binary)) + .sql("')"); + } + else if (dialect == POSTGRES) { + context.sql("E'") + .sql(Util.convertBytesToPostgresOctal(binary)) + .sql("'::bytea"); } // This default behaviour is used in debug logging for dialects // that do not support inlining binary data else { - context.sql("'") - .sql(Arrays.toString(binary).replace("'", "''")) + context.sql("X'") + .sql(Util.convertBytesToHex(binary)) .sql("'"); } } diff --git a/jOOQ/src/main/java/org/jooq/tools/StringUtils.java b/jOOQ/src/main/java/org/jooq/tools/StringUtils.java index 09ee20ae0c..66a6ab6339 100644 --- a/jOOQ/src/main/java/org/jooq/tools/StringUtils.java +++ b/jOOQ/src/main/java/org/jooq/tools/StringUtils.java @@ -936,38 +936,4 @@ public final class StringUtils { String cc = toCamelCase(string); return cc.substring(0, 1).toLowerCase() + cc.substring(1); } - - // ------------------------------------------------------------------------ - // XXX This section is taken from the H2 Database - // ------------------------------------------------------------------------ - - private static final char[] HEX = "0123456789abcdef".toCharArray(); - - /** - * Convert a byte array to a hex encoded string. - * - * @param value the byte array - * @return the hex encoded string - */ - public static String convertBytesToHex(byte[] value) { - return convertBytesToHex(value, value.length); - } - - /** - * Convert a byte array to a hex encoded string. - * - * @param value the byte array - * @param len the number of bytes to encode - * @return the hex encoded string - */ - public static String convertBytesToHex(byte[] value, int len) { - char[] buff = new char[len + len]; - char[] hex = HEX; - for (int i = 0; i < len; i++) { - int c = value[i] & 0xff; - buff[i + i] = hex[c >> 4]; - buff[i + i + 1] = hex[c & 0xf]; - } - return new String(buff); - } }