From a73bbbc6b0bcdc9c2c4cbd7d62eb48e0e546bed8 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 2 Dec 2014 20:20:48 +0100 Subject: [PATCH] [#1391] Implemented emulation for other databases --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 8 +- jOOQ/src/main/java/org/jooq/impl/Every.java | 90 +++++++++++++++++++ .../src/main/java/org/jooq/impl/Function.java | 43 +++------ 3 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Every.java diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index bb2f80c10a..4970035387 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -9803,17 +9803,17 @@ public class DSL { /** * Get the every value over a field: every(field). */ - @Support(POSTGRES) + @Support public static AggregateFunction every(Field field) { - return new Function("every", SQLDataType.BOOLEAN, nullSafe(field)); + return every(condition(nullSafe(field))); } /** * Get the every value over a condition: every(condition). */ - @Support(POSTGRES) + @Support public static AggregateFunction every(Condition condition) { - return every(field(condition)); + return new Every(condition); } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/Every.java b/jOOQ/src/main/java/org/jooq/impl/Every.java new file mode 100644 index 0000000000..99a685fe07 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Every.java @@ -0,0 +1,90 @@ +/** + * 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.impl; + +import static org.jooq.impl.DSL.inline; +import static org.jooq.impl.DSL.one; +import static org.jooq.impl.DSL.zero; + +import org.jooq.Condition; +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +class Every extends Function { + + /** + * Generated UID + */ + private static final long serialVersionUID = 7292087943334025737L; + + private final Condition condition; + + Every(Condition condition) { + super("every", SQLDataType.BOOLEAN, DSL.field(condition)); + + this.condition = condition; + } + + @SuppressWarnings("serial") + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + case POSTGRES: + super.accept(ctx); + break; + + default: + final Field sum = DSL.field("{0}", Integer.class, new CustomQueryPart() { + @Override + public void accept(Context c) { + c.visit(DSL.sum(DSL.decode().when(condition, zero()).otherwise(one()))); + toSQLOverClause(c); + } + }); + + ctx.visit(DSL.decode().when(sum.eq(zero()), inline(true)).otherwise(inline(false))); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Function.java b/jOOQ/src/main/java/org/jooq/impl/Function.java index 5ae921eb08..55c52a4dc0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Function.java +++ b/jOOQ/src/main/java/org/jooq/impl/Function.java @@ -169,7 +169,7 @@ class Function extends AbstractField implements this.withinGroupOrderBy = new SortFieldList(); } - private static String last(String... strings) { + final static String last(String... strings) { if (strings != null && strings.length > 0) { return strings[strings.length - 1]; } @@ -180,30 +180,9 @@ class Function extends AbstractField implements // ------------------------------------------------------------------------- // XXX QueryPart API // ------------------------------------------------------------------------- -// -// @Override -// public final void bind(BindContext ctx) { -// if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) { -// ctx.visit(arguments.get(0)); -// ctx.visit(withinGroupOrderBy); -// -// if (arguments.size() > 1) { -// ctx.visit(arguments.get(1)); -// } -// } -// else { -// ctx.visit(arguments) -// .visit(keepDenseRankOrderBy) -// .visit(withinGroupOrderBy); -// -// QueryPart window = window(ctx); -// if (window != null) -// ctx.visit(window); -// } -// } @Override - public final void accept(Context ctx) { + public /* final */ void accept(Context ctx) { if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) { toSQLGroupConcat(ctx); } @@ -228,7 +207,7 @@ class Function extends AbstractField implements xxx x xxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxx xxx xx - xxxxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx xxxx x + xxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx xxxx x xx xxxx xx x xxxxxxxx xxxx xx xxxx xxx xxxxx xxx xxxx xxxxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxx xxxxx xx xxx xx xxxxxxxxxxxxxxx xx @@ -276,7 +255,7 @@ class Function extends AbstractField implements /** * [#1275] LIST_AGG simulation for Postgres, Sybase */ - private void toSQLStringAgg(Context ctx) { + final void toSQLStringAgg(Context ctx) { toSQLFunctionName(ctx); ctx.sql("("); @@ -306,7 +285,7 @@ class Function extends AbstractField implements /** * [#1273] LIST_AGG simulation for MySQL and CUBRID */ - private final void toSQLGroupConcat(Context ctx) { + final void toSQLGroupConcat(Context ctx) { toSQLFunctionName(ctx); ctx.sql("("); @@ -329,7 +308,7 @@ class Function extends AbstractField implements ctx.sql(")"); } - private final void toSQLOverClause(Context ctx) { + final void toSQLOverClause(Context ctx) { QueryPart window = window(ctx); // Render this clause only if needed @@ -349,7 +328,7 @@ class Function extends AbstractField implements } @SuppressWarnings("unchecked") - private final QueryPart window(Context ctx) { + final QueryPart window(Context ctx) { if (windowSpecification != null) return windowSpecification; @@ -386,7 +365,7 @@ class Function extends AbstractField implements /** * Render KEEP (DENSE_RANK [FIRST | LAST] ORDER BY {...}) clause */ - private void toSQLKeepDenseRankOrderByClause(Context ctx) { + final void toSQLKeepDenseRankOrderByClause(Context ctx) { if (!keepDenseRankOrderBy.isEmpty()) { ctx.sql(" ").keyword("keep") .sql(" (").keyword("dense_rank") @@ -400,7 +379,7 @@ class Function extends AbstractField implements /** * Render WITHIN GROUP (ORDER BY ..) clause */ - private final void toSQLWithinGroupClause(Context ctx) { + final void toSQLWithinGroupClause(Context ctx) { if (!withinGroupOrderBy.isEmpty()) { ctx.sql(" ").keyword("within group") .sql(" (").keyword("order by") @@ -412,7 +391,7 @@ class Function extends AbstractField implements /** * Render function arguments and argument modifiers */ - private final void toSQLArguments(Context ctx) { + final void toSQLArguments(Context ctx) { toSQLFunctionName(ctx); ctx.sql("("); @@ -464,7 +443,7 @@ class Function extends AbstractField implements ctx.sql(")"); } - private final void toSQLFunctionName(Context ctx) { + final void toSQLFunctionName(Context ctx) { if (name != null) { ctx.visit(name); }