From 717a1945ad07220bf79f29fd5280e796c997a8fa Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 9 Dec 2014 16:07:14 +0100 Subject: [PATCH] [#2626] Add org.jooq.SQL for custom SQL templating --- jOOQ/src/main/java/org/jooq/SQL.java | 50 ++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 219 ++++++------------ .../java/org/jooq/impl/DefaultDSLContext.java | 52 +---- .../impl/{SQLTemplate.java => SQLImpl.java} | 55 ++--- 4 files changed, 154 insertions(+), 222 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/SQL.java rename jOOQ/src/main/java/org/jooq/impl/{SQLTemplate.java => SQLImpl.java} (64%) diff --git a/jOOQ/src/main/java/org/jooq/SQL.java b/jOOQ/src/main/java/org/jooq/SQL.java new file mode 100644 index 0000000000..0db3a3310a --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/SQL.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq; + +/** + * A plain SQL {@link QueryPart}. + * + * @author Lukas Eder + */ +public interface SQL extends QueryPart { + +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 86bca6f225..754fb1de93 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -213,6 +213,7 @@ import org.jooq.Row7; import org.jooq.Row8; import org.jooq.Row9; import org.jooq.RowN; +import org.jooq.SQL; import org.jooq.SQLDialect; import org.jooq.Schema; import org.jooq.Select; @@ -5515,16 +5516,67 @@ public class DSL { // ------------------------------------------------------------------------- /** - * Create a new {@link org.jooq.Template} that can transform parameter - * objects into a {@link QueryPart}. + * A custom SQL clause that can render arbitrary expressions. + *

+ * A plain SQL QueryPart is a QueryPart that can + * contain user-defined plain SQL, because sometimes it is easier to express + * things directly in SQL. + *

+ * 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! * - * @param sql The input SQL. - * @return A template that can transform parameter objects into a - * {@link QueryPart}. + * @param sql The SQL + * @return A query part wrapping the plain SQL */ - @SuppressWarnings("deprecation") - static org.jooq.Template template(String sql) { - return new SQLTemplate(sql); + @Support + public static SQL sql(String sql) { + return sql(sql, new Object[0]); + } + + /** + * A custom SQL clause that can render arbitrary expressions. + *

+ * A plain SQL QueryPart is a QueryPart that can + * contain user-defined plain SQL, because sometimes it is easier to express + * things directly in SQL. + *

+ * 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! + * + * @param sql The SQL clause, containing {numbered placeholders} where query + * parts can be injected + * @param parts The {@link QueryPart} objects that are rendered at the + * {numbered placeholder} locations + * @return A query part wrapping the plain SQL + */ + @Support + public static SQL sql(String sql, QueryPart... parts) { + return sql(sql, (Object[]) parts); + } + + /** + * A custom SQL clause that can render arbitrary expressions. + *

+ * A plain SQL QueryPart is a QueryPart that can + * contain user-defined plain SQL, because sometimes it is easier to express + * things directly in SQL. There must be as many binding variables contained + * in the SQL, as passed in the bindings parameter + *

+ * 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! + * + * @param sql The SQL + * @return A query part wrapping the plain SQL + */ + @Support + public static SQL sql(String sql, Object... bindings) { + return new SQLImpl(sql, bindings); } /** @@ -5544,7 +5596,7 @@ public class DSL { */ @Support public static QueryPart queryPart(String sql) { - return queryPart(template(sql), new Object[0]); + return sql(sql); } /** @@ -5567,7 +5619,7 @@ public class DSL { */ @Support public static QueryPart queryPart(String sql, QueryPart... parts) { - return queryPart(template(sql), (Object[]) parts); + return sql(sql, parts); } /** @@ -5588,22 +5640,9 @@ public class DSL { */ @Support public static QueryPart queryPart(String sql, Object... bindings) { - return queryPart(template(sql), bindings); + return sql(sql, bindings); } - /** - * A custom SQL clause that can render arbitrary expressions from a - * template. - * - * @param template The template generating a delegate query part - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static QueryPart queryPart(org.jooq.Template template, Object... parameters) { - return template.transform(parameters); - } // ------------------------------------------------------------------------- // XXX Plain SQL API // ------------------------------------------------------------------------- @@ -5871,7 +5910,7 @@ public class DSL { */ @Support public static Table table(String sql, Object... bindings) { - return table(template(sql), bindings); + return new SQLTable(sql(sql, bindings)); } /** @@ -5906,21 +5945,7 @@ public class DSL { */ @Support public static Table table(String sql, QueryPart... parts) { - return table(template(sql), (Object[]) parts); - } - - /** - * A custom SQL clause that can render arbitrary table expressions from a - * template. - * - * @param template The template generating a delegate query part - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static Table table(org.jooq.Template template, Object... parameters) { - return new SQLTable(queryPart(template, parameters)); + return table(sql, (Object[]) parts); } /** @@ -5997,7 +6022,7 @@ public class DSL { */ @Support public static Field field(String sql) { - return field(template(sql), new Object[0]); + return field(sql, new Object[0]); } /** @@ -6025,7 +6050,7 @@ public class DSL { */ @Support public static Field field(String sql, Object... bindings) { - return field(template(sql), Object.class, bindings); + return field(sql, Object.class, bindings); } /** @@ -6053,7 +6078,7 @@ public class DSL { */ @Support public static Field field(String sql, Class type) { - return field(template(sql), type, new Object[0]); + return field(sql, type, new Object[0]); } /** @@ -6082,7 +6107,7 @@ public class DSL { */ @Support public static Field field(String sql, Class type, Object... bindings) { - return field(template(sql), getDataType(type), bindings); + return field(sql, getDataType(type), bindings); } /** @@ -6110,7 +6135,7 @@ public class DSL { */ @Support public static Field field(String sql, DataType type) { - return field(template(sql), type, new Object[0]); + return field(sql, type, new Object[0]); } /** @@ -6139,7 +6164,7 @@ public class DSL { */ @Support public static Field field(String sql, DataType type, Object... bindings) { - return field(template(sql), type, bindings); + return new SQLField(type, sql(sql, bindings)); } /** @@ -6172,7 +6197,7 @@ public class DSL { * @return A field wrapping the plain SQL */ public static Field field(String sql, QueryPart... parts) { - return field(template(sql), (Object[]) parts); + return field(sql, (Object[]) parts); } /** @@ -6206,85 +6231,7 @@ public class DSL { * @return A field wrapping the plain SQL */ public static Field field(String sql, Class type, QueryPart... parts) { - return field(template(sql), type, (Object[]) parts); - } - - /** - * A custom SQL clause that can render arbitrary SQL elements. - *

- * This is useful for constructing more complex SQL syntax elements wherever - * Field types are expected. An example for this is MySQL's - * GROUP_CONCAT aggregate function, which has MySQL-specific - * keywords that are hard to reflect in jOOQ's DSL:

-     * GROUP_CONCAT([DISTINCT] expr [,expr ...]
-     *       [ORDER BY {unsigned_integer | col_name | expr}
-     *           [ASC | DESC] [,col_name ...]]
-     *       [SEPARATOR str_val])
-     *       
- *

- * The above MySQL function can be expressed as such:

-     * field("GROUP_CONCAT(DISTINCT {0} ORDER BY {1} ASC DEPARATOR '-')", expr1, expr2);
-     * 
- *

- * 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 clause, containing {numbered placeholders} where query - * parts can be injected - * @param type The field type - * @param parts The {@link QueryPart} objects that are rendered at the - * {numbered placeholder} locations - * @return A field wrapping the plain SQL - */ - public static Field field(String sql, DataType type, QueryPart... parts) { - return field(template(sql), type, (Object[]) parts); - } - - /** - * A custom SQL clause that can render arbitrary field expressions from a - * template. - * - * @param template The template generating a delegate query part - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static Field field(org.jooq.Template template, Object... parameters) { - return field(template, Object.class, parameters); - } - - /** - * A custom SQL clause that can render arbitrary field expressions from a - * template. - * - * @param template The template generating a delegate query part - * @param type The field type - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static Field field(org.jooq.Template template, Class type, Object... parameters) { - return field(template, getDataType(type), parameters); - } - - /** - * A custom SQL clause that can render arbitrary field expressions from a - * template. - * - * @param template The template generating a delegate query part - * @param type The field type - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static Field field(org.jooq.Template template, DataType type, Object... parameters) { - return new SQLField(type, queryPart(template, parameters)); + return field(sql, getDataType(type), (Object[]) parts); } /** @@ -6369,7 +6316,7 @@ public class DSL { */ @Support public static Condition condition(String sql) { - return condition(template(sql), new Object[0]); + return condition(sql, new Object[0]); } /** @@ -6395,7 +6342,7 @@ public class DSL { */ @Support public static Condition condition(String sql, Object... bindings) { - return condition(template(sql), bindings); + return new SQLCondition(sql(sql, bindings)); } /** @@ -6424,21 +6371,7 @@ public class DSL { */ @Support public static Condition condition(String sql, QueryPart... parts) { - return condition(template(sql), (Object[]) parts); - } - - /** - * A custom SQL clause that can render arbitrary condition expressions from - * a template. - * - * @param template The template generating a delegate query part - * @param parameters The parameters provided to the template - * @return A query part wrapping the plain SQL - */ - @SuppressWarnings("deprecation") - @Support - static Condition condition(org.jooq.Template template, Object... parameters) { - return new SQLCondition(queryPart(template, parameters)); + return condition(sql, (Object[]) parts); } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 32e9f8cd6f..7111be20b1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -45,10 +45,9 @@ import static org.jooq.conf.ParamType.NAMED; import static org.jooq.conf.ParamType.NAMED_OR_INLINED; import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.name; -import static org.jooq.impl.DSL.queryPart; import static org.jooq.impl.DSL.sequence; +import static org.jooq.impl.DSL.sql; import static org.jooq.impl.DSL.table; -import static org.jooq.impl.DSL.template; import static org.jooq.impl.DSL.trueCondition; import static org.jooq.impl.Utils.list; import static org.jooq.tools.Convert.convert; @@ -442,22 +441,17 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri @Override public Query query(String sql) { - return query(template(sql), new Object[0]); + return query(sql, new Object[0]); } @Override public Query query(String sql, Object... bindings) { - return query(template(sql), bindings); + return new SQLQuery(configuration(), sql(sql, bindings)); } @Override public Query query(String sql, QueryPart... parts) { - return query(template(sql), (Object[]) parts); - } - - @SuppressWarnings("deprecation") - Query query(org.jooq.Template template, Object... parameters) { - return new SQLQuery(configuration(), queryPart(template, parameters)); + return query(sql, (Object[]) parts); } @Override @@ -475,11 +469,6 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return resultQuery(sql, parts).fetch(); } - @SuppressWarnings("deprecation") - Result fetch(org.jooq.Template template, Object... parameters) { - return resultQuery(template, parameters).fetch(); - } - @Override public Cursor fetchLazy(String sql) { return resultQuery(sql).fetchLazy(); @@ -495,11 +484,6 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return resultQuery(sql, parts).fetchLazy(); } - @SuppressWarnings("deprecation") - Cursor fetchLazy(org.jooq.Template template, Object... parameters) { - return resultQuery(template, parameters).fetchLazy(); - } - @Override public List> fetchMany(String sql) { return resultQuery(sql).fetchMany(); @@ -515,11 +499,6 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return resultQuery(sql, parts).fetchMany(); } - @SuppressWarnings("deprecation") - List> fetchMany(org.jooq.Template template, Object... parameters) { - return resultQuery(template, parameters).fetchMany(); - } - @Override public Record fetchOne(String sql) { return resultQuery(sql).fetchOne(); @@ -535,11 +514,6 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return resultQuery(sql, parts).fetchOne(); } - @SuppressWarnings("deprecation") - Record fetchOne(org.jooq.Template template, Object... parameters) { - return resultQuery(template, parameters).fetchOne(); - } - @Override public Object fetchValue(String sql) { return fetchValue((ResultQuery) resultQuery(sql)); @@ -582,32 +556,22 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri @Override public int execute(String sql, QueryPart... parts) { - return query(sql, parts).execute(); - } - - @SuppressWarnings("deprecation") - int execute(org.jooq.Template template, Object... parameters) { - return query(template, parameters).execute(); + return query(sql, (Object[]) parts).execute(); } @Override public ResultQuery resultQuery(String sql) { - return resultQuery(template(sql), new Object[0]); + return resultQuery(sql, new Object[0]); } @Override public ResultQuery resultQuery(String sql, Object... bindings) { - return resultQuery(template(sql), bindings); + return new SQLResultQuery(configuration(), sql(sql, bindings)); } @Override public ResultQuery resultQuery(String sql, QueryPart... parts) { - return resultQuery(template(sql), (Object[]) parts); - } - - @SuppressWarnings("deprecation") - ResultQuery resultQuery(org.jooq.Template template, Object... parameters) { - return new SQLResultQuery(configuration(), queryPart(template, parameters)); + return resultQuery(sql, (Object[]) parts); } // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/SQLTemplate.java b/jOOQ/src/main/java/org/jooq/impl/SQLImpl.java similarity index 64% rename from jOOQ/src/main/java/org/jooq/impl/SQLTemplate.java rename to jOOQ/src/main/java/org/jooq/impl/SQLImpl.java index 2a1435d5bc..2406f6bc0d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SQLTemplate.java +++ b/jOOQ/src/main/java/org/jooq/impl/SQLImpl.java @@ -47,50 +47,35 @@ import java.util.List; import org.jooq.Clause; import org.jooq.Context; import org.jooq.QueryPart; -import org.jooq.Template; +import org.jooq.SQL; -@SuppressWarnings("deprecation") -class SQLTemplate implements Template { +class SQLImpl extends AbstractQueryPart implements SQL { - private final String sql; + /** + * Generated UID + */ + private static final long serialVersionUID = -7514156096865122018L; + private static final Clause[] CLAUSES = { TEMPLATE }; + private final String sql; + private final List substitutes; - SQLTemplate(String sql) { + SQLImpl(String sql, Object... input) { this.sql = sql; + this.substitutes = Utils.queryParts(input); } @Override - public final QueryPart transform(Object... input) { - return new SQLTemplateQueryPart(sql, input); + public final void accept(Context ctx) { + Utils.renderAndBind(ctx, sql, substitutes); } - private static class SQLTemplateQueryPart extends AbstractQueryPart { + @Override + public final Clause[] clauses(Context ctx) { + return CLAUSES; + } - /** - * Generated UID - */ - private static final long serialVersionUID = -7514156096865122018L; - private static final Clause[] CLAUSES = { TEMPLATE }; - private final String sql; - private final List substitutes; - - SQLTemplateQueryPart(String sql, Object... input) { - this.sql = sql; - this.substitutes = Utils.queryParts(input); - } - - @Override - public final void accept(Context ctx) { - Utils.renderAndBind(ctx, sql, substitutes); - } - - @Override - public final Clause[] clauses(Context ctx) { - return CLAUSES; - } - - @Override - public String toString() { - return sql; - } + @Override + public String toString() { + return sql; } }