From a7812c8fac0c5ccaa2b1693bede07a861c76ee39 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 30 Nov 2020 14:07:46 +0100 Subject: [PATCH] [jOOQ/jOOQ#11053] Pull up PostgresDSL.array() to DSL.array() --- .../main/java/org/jooq/impl/ArraySelect.java | 111 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 13 ++ .../org/jooq/util/postgres/PostgresDSL.java | 14 --- 3 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/ArraySelect.java diff --git a/jOOQ/src/main/java/org/jooq/impl/ArraySelect.java b/jOOQ/src/main/java/org/jooq/impl/ArraySelect.java new file mode 100644 index 0000000000..f8859f8427 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ArraySelect.java @@ -0,0 +1,111 @@ +/* + * 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. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +// ... +// ... +// ... +import static org.jooq.SQLDialect.POSTGRES; +import static org.jooq.impl.DSL.arrayAgg; +import static org.jooq.impl.Keywords.K_ARRAY; +import static org.jooq.impl.Keywords.K_INT; +import static org.jooq.impl.Names.N_ARRAY; +import static org.jooq.impl.QueryPartListView.wrap; +import static org.jooq.impl.Tools.selectQueryImpl; +import static org.jooq.impl.Tools.visitSubquery; + +import java.util.Collection; +import java.util.Set; + +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.Record1; +import org.jooq.SQLDialect; +import org.jooq.Select; +import org.jooq.Table; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Lukas Eder + */ +final class ArraySelect extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = -6629785423729163857L; + + private final Select> select; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + ArraySelect(Select> select) { + super(N_ARRAY, (DataType) select.getSelect().get(0).getDataType().getArrayDataType()); + + this.select = select; + } + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + case H2: { + Table t = select.asTable("t", "c"); + Field c = t.field("c"); + + // [#11053] TODO: Move ORDER BY clause from subquery to ARRAY_AGG + // See https://github.com/jOOQ/jOOQ/issues/11053#issuecomment-735773248 + visitSubquery(ctx, DSL.select(arrayAgg(c)).from(t), true); + break; + } + + + + + + case HSQLDB: + case POSTGRES: + default: + ctx.visit(K_ARRAY); + visitSubquery(ctx, select, true); + + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 7c6f0f0ae6..29583f31df 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -21299,6 +21299,19 @@ public class DSL { return new Array<>(fields); } + /** + * The PostgreSQL array(select) function. + *

+ * Example:

+     * {1, 2, 3} = array(select 1 union select 2 union select 3)
+     * 
+ */ + @NotNull + @Support({ H2, HSQLDB, POSTGRES }) + public static Field array(Select> select) { + return new ArraySelect<>(select); + } + /** * Calculate the cardinality of an array field. */ diff --git a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java index 4042c02487..238068ab65 100644 --- a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java +++ b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java @@ -123,20 +123,6 @@ public class PostgresDSL extends DSL { return DSL.condition("{0} && {1}", left, right); } - /** - * The PostgreSQL array(select) function. - *

- * Example:

-     * {1, 2, 3} = array(select 1 union select 2 union select 3)
-     * 
- */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - @NotNull - @Support({ POSTGRES }) - public static Field array(Select> select) { - return DSL.field("array({0})", (DataType) select.getSelect().get(0).getDataType().getArrayDataType(), select); - } - /** * The PostgreSQL array_append(anyarray, anyelement) function. *