[#2626] Add org.jooq.SQL for custom SQL templating

This commit is contained in:
Lukas Eder 2014-12-09 16:07:14 +01:00
parent 7e2ef65322
commit 717a1945ad
4 changed files with 154 additions and 222 deletions

View File

@ -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 {
}

View File

@ -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.
* <p>
* A plain SQL <code>QueryPart</code> is a <code>QueryPart</code> that can
* contain user-defined plain SQL, because sometimes it is easier to express
* things directly in SQL.
* <p>
* <b>NOTE</b>: 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.
* <p>
* A plain SQL <code>QueryPart</code> is a <code>QueryPart</code> that can
* contain user-defined plain SQL, because sometimes it is easier to express
* things directly in SQL.
* <p>
* <b>NOTE</b>: 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.
* <p>
* A plain SQL <code>QueryPart</code> is a <code>QueryPart</code> 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
* <p>
* <b>NOTE</b>: 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<Record> 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<Record> 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<Record> 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<Object> 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<Object> 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 <T> Field<T> field(String sql, Class<T> 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 <T> Field<T> field(String sql, Class<T> 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 <T> Field<T> field(String sql, DataType<T> 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 <T> Field<T> field(String sql, DataType<T> 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<Object> 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 <T> Field<T> field(String sql, Class<T> type, QueryPart... parts) {
return field(template(sql), type, (Object[]) parts);
}
/**
* A custom SQL clause that can render arbitrary SQL elements.
* <p>
* This is useful for constructing more complex SQL syntax elements wherever
* <code>Field</code> types are expected. An example for this is MySQL's
* <code>GROUP_CONCAT</code> aggregate function, which has MySQL-specific
* keywords that are hard to reflect in jOOQ's DSL: <code><pre>
* GROUP_CONCAT([DISTINCT] expr [,expr ...]
* [ORDER BY {unsigned_integer | col_name | expr}
* [ASC | DESC] [,col_name ...]]
* [SEPARATOR str_val])
* </pre></code>
* <p>
* The above MySQL function can be expressed as such: <code><pre>
* field("GROUP_CONCAT(DISTINCT {0} ORDER BY {1} ASC DEPARATOR '-')", expr1, expr2);
* </pre></code>
* <p>
* <b>NOTE</b>: 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 <T> Field<T> field(String sql, DataType<T> 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<Object> 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 <T> Field<T> field(org.jooq.Template template, Class<T> 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 <T> Field<T> field(org.jooq.Template template, DataType<T> type, Object... parameters) {
return new SQLField<T>(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);
}
/**

View File

@ -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<Record> fetch(org.jooq.Template template, Object... parameters) {
return resultQuery(template, parameters).fetch();
}
@Override
public Cursor<Record> 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<Record> fetchLazy(org.jooq.Template template, Object... parameters) {
return resultQuery(template, parameters).fetchLazy();
}
@Override
public List<Result<Record>> 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<Result<Record>> 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<Record> resultQuery(String sql) {
return resultQuery(template(sql), new Object[0]);
return resultQuery(sql, new Object[0]);
}
@Override
public ResultQuery<Record> resultQuery(String sql, Object... bindings) {
return resultQuery(template(sql), bindings);
return new SQLResultQuery(configuration(), sql(sql, bindings));
}
@Override
public ResultQuery<Record> resultQuery(String sql, QueryPart... parts) {
return resultQuery(template(sql), (Object[]) parts);
}
@SuppressWarnings("deprecation")
ResultQuery<Record> resultQuery(org.jooq.Template template, Object... parameters) {
return new SQLResultQuery(configuration(), queryPart(template, parameters));
return resultQuery(sql, (Object[]) parts);
}
// -------------------------------------------------------------------------

View File

@ -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<QueryPart> 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<QueryPart> 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;
}
}