From 1c42b05ad61cdb536a71c3b30c6ec0e8dab5bbf4 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 14 Jul 2012 13:43:44 +0200 Subject: [PATCH] [#1570] Add Factory.condition(String, QueryPart...) similar to Factory.field(String, QueryPart...) --- jOOQ/src/main/java/org/jooq/impl/Factory.java | 29 +++++++++++++++++++ .../main/java/org/jooq/impl/RegexpLike.java | 6 ++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index f63a50d6ff..e116d15370 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -1466,6 +1466,35 @@ public class Factory implements FactoryOperations { return new SQLCondition(sql, bindings); } + /** + * A custom SQL clause that can render arbitrary SQL elements. + *

+ * This is useful for constructing more complex SQL syntax elements wherever + * Condition types are expected. An example for this are + * Postgres's various operators, some of which are missing in the jOOQ API. + * For instance, the "overlap" operator for arrays: + *

ARRAY[1,4,3] && ARRAY[2,1]
+ *

+ * The above Postgres operator can be expressed as such:

+     * condition("{0} && {1}", array1, array2);
+     * 
+ *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! One way to escape + * literals is to use {@link #name(String...)} and similar methods + * + * @param sql The SQL + * @param parts The {@link QueryPart} objects that are rendered at the + * {numbered placeholder} locations + * @return A condition wrapping the plain SQL + */ + @Support + public static Condition condition(String sql, QueryPart... parts) { + return new SQLCondition(sql, parts); + } + /** * {@inheritDoc} */ diff --git a/jOOQ/src/main/java/org/jooq/impl/RegexpLike.java b/jOOQ/src/main/java/org/jooq/impl/RegexpLike.java index 924d6762ed..2dc692d3a0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RegexpLike.java +++ b/jOOQ/src/main/java/org/jooq/impl/RegexpLike.java @@ -81,7 +81,7 @@ class RegexpLike extends AbstractCondition { case HSQLDB: { // [#1570] TODO: Replace this by Factory.condition(String, QueryPart...) - context.sql(Factory.field("{regexp_matches}({0}, {1})", Boolean.class, search, pattern)); + context.sql(Factory.condition("{regexp_matches}({0}, {1})", search, pattern)); break; } @@ -89,7 +89,7 @@ class RegexpLike extends AbstractCondition { case POSTGRES: { // [#1570] TODO: Replace this by Factory.condition(String, QueryPart...) - context.sql(Factory.field("{0} ~ {1}", Boolean.class, search, pattern)); + context.sql(Factory.condition("{0} ~ {1}", search, pattern)); break; } @@ -97,7 +97,7 @@ class RegexpLike extends AbstractCondition { case ORACLE: { // [#1570] TODO: Replace this by Factory.condition(String, QueryPart...) - context.sql(Factory.field("{regexp_like}({0}, {1})", Boolean.class, search, pattern)); + context.sql(Factory.condition("{regexp_like}({0}, {1})", search, pattern)); break; }