From fbe6650cee485872fb9c0e0c5e76db60f011c05f Mon Sep 17 00:00:00 2001 From: ooxi Date: Wed, 28 May 2014 16:09:43 +0200 Subject: [PATCH] Overloading all MySQL specific DSL functions to accept BLOB arguments --- .../test/java/org/jooq/test/MySQLTest.java | 18 +- .../java/org/jooq/util/mysql/MySQLDSL.java | 156 +++++++++++++++--- 2 files changed, 146 insertions(+), 28 deletions(-) diff --git a/jOOQ-test/src/test/java/org/jooq/test/MySQLTest.java b/jOOQ-test/src/test/java/org/jooq/test/MySQLTest.java index 33b743a19c..67cc5969cc 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/MySQLTest.java +++ b/jOOQ-test/src/test/java/org/jooq/test/MySQLTest.java @@ -822,7 +822,7 @@ public class MySQLTest extends jOOQAbstractTest< } @Test - public void testMySQLEncryptionFunctions() throws Exception { + public void testMySQLStringEncryptionFunctions() throws Exception { assertNotNull(create().select(password("abc")).fetchOne(0)); assertNotNull(create().select(md5("abc")).fetchOne(0)); assertNotNull(create().select(sha1("abc")).fetchOne(0)); @@ -835,6 +835,22 @@ public class MySQLTest extends jOOQAbstractTest< assertEquals(3, create().select(uncompressedLength(compress("abc"))).fetchOne(0)); } + @Test + public void testMySQLByteArrayEncryptionFunctions() throws Exception { + final byte[] MESSAGE = new byte[] {-74, 71, -79, -124, -58}; + final byte[] SECRET = new byte[] {-122, -123, 4, -12, -37}; + + assertNotNull(create().select(password(MESSAGE)).fetchOne(0)); + assertNotNull(create().select(sha1(MESSAGE)).fetchOne(0)); + assertNotNull(create().select(sha2(MESSAGE, 256)).fetchOne(0)); + assertEquals(MESSAGE, create().select(decode(encode(MESSAGE, SECRET), val(SECRET))).fetchOne(0)); + assertEquals(MESSAGE, create().select(aesDecrypt(aesEncrypt(MESSAGE, SECRET), val(SECRET))).fetchOne(0)); + assertEquals(MESSAGE, create().select(desDecrypt(desEncrypt(MESSAGE, SECRET), val(SECRET))).fetchOne(0)); + assertEquals(MESSAGE, create().select(desDecrypt(desEncrypt(MESSAGE))).fetchOne(0)); + assertEquals(MESSAGE, create().select(uncompress(compress(MESSAGE))).fetchOne(0)); + assertEquals(3, create().select(uncompressedLength(compress(MESSAGE))).fetchOne(0)); + } + @Test public void testMySQLJavaKeywordEnums() throws Exception { reset = false; diff --git a/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDSL.java b/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDSL.java index eba229a2a8..fdd56e281e 100644 --- a/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDSL.java +++ b/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDSL.java @@ -44,6 +44,7 @@ import org.jooq.EnumType; import org.jooq.Field; import org.jooq.SQLDialect; import org.jooq.impl.DSL; +import static org.jooq.impl.DSL.val; /** * The {@link SQLDialect#MYSQL} specific DSL. @@ -76,8 +77,17 @@ public class MySQLDSL extends DSL { *

* Don't mix this up with the various {@link DSL#decode()} methods! */ - public static Field decode(Field cryptString, Field keyString) { - return function("decode", String.class, cryptString, keyString); + public static Field decode(byte[] cryptString, byte[] keyString) { + return decode(val(cryptString), val(keyString)); + } + + /** + * Get the MySQL-specific DECODE() function + *

+ * Don't mix this up with the various {@link DSL#decode()} methods! + */ + public static Field decode(Field cryptString, Field keyString) { + return function("decode", cryptString.getType(), cryptString, keyString); } /** @@ -90,8 +100,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific ENCODE() function */ - public static Field encode(Field string, Field keyString) { - return function("encode", String.class, string, keyString); + public static Field encode(byte[] string, byte[] keyString) { + return encode(val(string), val(keyString)); + } + + /** + * Get the MySQL-specific ENCODE() function + */ + public static Field encode(Field string, Field keyString) { + return function("encode", string.getType(), string, keyString); } /** @@ -104,8 +121,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific AES_DECRYPT() function */ - public static Field aesDecrypt(Field cryptString, Field keyString) { - return function("aes_decrypt", String.class, cryptString, keyString); + public static Field aesDecrypt(byte[] cryptString, byte[] keyString) { + return aesDecrypt(val(cryptString), val(keyString)); + } + + /** + * Get the MySQL-specific AES_DECRYPT() function + */ + public static Field aesDecrypt(Field cryptString, Field keyString) { + return function("aes_decrypt", cryptString.getType(), cryptString, keyString); } /** @@ -118,8 +142,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific AES_ENCRYPT() function */ - public static Field aesEncrypt(Field string, Field keyString) { - return function("aes_encrypt", String.class, string, keyString); + public static Field aesEncrypt(byte[] string, byte[] keyString) { + return aesEncrypt(val(string), val(keyString)); + } + + /** + * Get the MySQL-specific AES_ENCRYPT() function + */ + public static Field aesEncrypt(Field string, Field keyString) { + return function("aes_encrypt", string.getType(), string, keyString); } /** @@ -132,8 +163,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific DES_DECRYPT() function */ - public static Field desDecrypt(Field cryptString) { - return function("des_decrypt", String.class, cryptString); + public static Field desDecrypt(byte[] cryptString) { + return desDecrypt(val(cryptString)); + } + + /** + * Get the MySQL-specific DES_DECRYPT() function + */ + public static Field desDecrypt(Field cryptString) { + return function("des_decrypt", cryptString.getType(), cryptString); } /** @@ -146,8 +184,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific DES_DECRYPT() function */ - public static Field desDecrypt(Field cryptString, Field keyString) { - return function("des_decrypt", String.class, cryptString, keyString); + public static Field desDecrypt(byte[] cryptString, byte[] keyString) { + return desDecrypt(val(cryptString), val(keyString)); + } + + /** + * Get the MySQL-specific DES_DECRYPT() function + */ + public static Field desDecrypt(Field cryptString, Field keyString) { + return function("des_decrypt", cryptString.getType(), cryptString, keyString); } /** @@ -160,8 +205,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific DES_ENCRYPT() function */ - public static Field desEncrypt(Field string) { - return function("des_encrypt", String.class, string); + public static Field desEncrypt(byte[] string) { + return desEncrypt(val(string)); + } + + /** + * Get the MySQL-specific DES_ENCRYPT() function + */ + public static Field desEncrypt(Field string) { + return function("des_encrypt", string.getType(), string); } /** @@ -174,8 +226,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific DES_ENCRYPT() function */ - public static Field desEncrypt(Field string, Field keyString) { - return function("des_encrypt", String.class, string, keyString); + public static Field desEncrypt(byte[] string, byte[] keyString) { + return desEncrypt(val(string), val(keyString)); + } + + /** + * Get the MySQL-specific DES_ENCRYPT() function + */ + public static Field desEncrypt(Field string, Field keyString) { + return function("des_encrypt", string.getType(), string, keyString); } /** @@ -188,8 +247,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific COMPRESS() function */ - public static Field compress(Field string) { - return function("compress", String.class, string); + public static Field compress(byte[] string) { + return compress(val(string)); + } + + /** + * Get the MySQL-specific COMPRESS() function + */ + public static Field compress(Field string) { + return function("compress", string.getType(), string); } /** @@ -202,8 +268,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific UNCOMPRESS() function */ - public static Field uncompress(Field string) { - return function("uncompress", String.class, string); + public static Field uncompress(byte[] string) { + return uncompress(val(string)); + } + + /** + * Get the MySQL-specific UNCOMPRESS() function + */ + public static Field uncompress(Field string) { + return function("uncompress", string.getType(), string); } /** @@ -216,7 +289,14 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific UNCOMPRESSED_LENGTH() function */ - public static Field uncompressedLength(Field string) { + public static Field uncompressedLength(byte[] string) { + return uncompressedLength(val(string)); + } + + /** + * Get the MySQL-specific UNCOMPRESSED_LENGTH() function + */ + public static Field uncompressedLength(Field string) { return function("uncompressed_length", Integer.class, string); } @@ -230,8 +310,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific SHA1() function */ - public static Field sha1(Field string) { - return function("sha1", String.class, string); + public static Field sha1(byte[] string) { + return sha1(val(string)); + } + + /** + * Get the MySQL-specific SHA1() function + */ + public static Field sha1(Field string) { + return function("sha1", string.getType(), string); } /** @@ -241,11 +328,19 @@ public class MySQLDSL extends DSL { return sha2(val(string), val(hashLength)); } + /** * Get the MySQL-specific SHA2() function */ - public static Field sha2(Field string, Field hashLength) { - return function("sha2", String.class, string, hashLength); + public static Field sha2(byte[] string, int hashLength) { + return sha2(val(string), val(hashLength)); + } + + /** + * Get the MySQL-specific SHA2() function + */ + public static Field sha2(Field string, Field hashLength) { + return function("sha2", string.getType(), string, hashLength); } /** @@ -258,8 +353,15 @@ public class MySQLDSL extends DSL { /** * Get the MySQL-specific PASSWORD() function */ - public static Field password(Field string) { - return function("password", String.class, string); + public static Field password(byte[] string) { + return password(val(string)); + } + + /** + * Get the MySQL-specific PASSWORD() function + */ + public static Field password(Field string) { + return function("password", string.getType(), string); } // -------------------------------------------------------------------------