[jOOQ/jOOQ#11053] Pull up PostgresDSL.array() to DSL.array()

This commit is contained in:
Lukas Eder 2020-11-30 14:07:46 +01:00
parent c5262f505d
commit a7812c8fac
3 changed files with 124 additions and 14 deletions

View File

@ -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<T> extends AbstractField<T[]> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6629785423729163857L;
private final Select<? extends Record1<T>> select;
@SuppressWarnings({ "rawtypes", "unchecked" })
ArraySelect(Select<? extends Record1<T>> 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;
}
}
}

View File

@ -21299,6 +21299,19 @@ public class DSL {
return new Array<>(fields);
}
/**
* The PostgreSQL <code>array(select)</code> function.
* <p>
* Example: <code><pre>
* {1, 2, 3} = array(select 1 union select 2 union select 3)
* </pre></code>
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES })
public static <T> Field<T[]> array(Select<? extends Record1<T>> select) {
return new ArraySelect<>(select);
}
/**
* Calculate the cardinality of an array field.
*/

View File

@ -123,20 +123,6 @@ public class PostgresDSL extends DSL {
return DSL.condition("{0} && {1}", left, right);
}
/**
* The PostgreSQL <code>array(select)</code> function.
* <p>
* Example: <code><pre>
* {1, 2, 3} = array(select 1 union select 2 union select 3)
* </pre></code>
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@NotNull
@Support({ POSTGRES })
public static <T> Field<T[]> array(Select<? extends Record1<T>> select) {
return DSL.field("array({0})", (DataType) select.getSelect().get(0).getDataType().getArrayDataType(), select);
}
/**
* The PostgreSQL <code>array_append(anyarray, anyelement)</code> function.
* <p>