[jOOQ/jOOQ#14388] Add support for ARRAY concatenation

This commit is contained in:
Lukas Eder 2022-12-09 14:51:53 +01:00
parent 0173f5dded
commit 5094b07ac5
5 changed files with 234 additions and 1 deletions

View File

@ -0,0 +1,167 @@
/*
* 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
*
* https://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: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.Internal.*;
import static org.jooq.impl.Keywords.*;
import static org.jooq.impl.Names.*;
import static org.jooq.impl.SQLDataType.*;
import static org.jooq.impl.Tools.*;
import static org.jooq.impl.Tools.BooleanDataKey.*;
import static org.jooq.impl.Tools.ExtendedDataKey.*;
import static org.jooq.impl.Tools.SimpleDataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Function1;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.impl.QOM.*;
import org.jooq.tools.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
/**
* The <code>ARRAY CONCAT</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class ArrayConcat<T>
extends
AbstractField<T[]>
implements
QOM.ArrayConcat<T>
{
final Field<T[]> arg1;
final Field<T[]> arg2;
ArrayConcat(
Field<T[]> arg1,
Field<T[]> arg2
) {
super(
N_ARRAY_CONCAT,
allNotNull(((DataType) OTHER).getArrayDataType(), arg1, arg2)
);
this.arg1 = nullSafeNotNull(arg1, ((DataType) OTHER).getArrayDataType());
this.arg2 = nullSafeNotNull(arg2, ((DataType) OTHER).getArrayDataType());
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
default:
ctx.sql('(').visit(arg1).sql(" || ").visit(arg2).sql(')');
break;
}
}
// -------------------------------------------------------------------------
// XXX: Query Object Model
// -------------------------------------------------------------------------
@Override
public final Field<T[]> $arg1() {
return arg1;
}
@Override
public final Field<T[]> $arg2() {
return arg2;
}
@Override
public final QOM.ArrayConcat<T> $arg1(Field<T[]> newValue) {
return $constructor().apply(newValue, $arg2());
}
@Override
public final QOM.ArrayConcat<T> $arg2(Field<T[]> newValue) {
return $constructor().apply($arg1(), newValue);
}
@Override
public final Function2<? super Field<T[]>, ? super Field<T[]>, ? extends QOM.ArrayConcat<T>> $constructor() {
return (a1, a2) -> new ArrayConcat<>(a1, a2);
}
// -------------------------------------------------------------------------
// XXX: The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof QOM.ArrayConcat<?> o) {
return
StringUtils.equals($arg1(), o.$arg1()) &&
StringUtils.equals($arg2(), o.$arg2())
;
}
else
return super.equals(that);
}
}

View File

@ -19692,6 +19692,50 @@ public class DSL {
return new ArrayGet<>(array, index);
}
/**
* The <code>ARRAY_CONCAT</code> function.
* <p>
* Concatenate two arrays.
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES, YUGABYTEDB })
public static <T> Field<T[]> arrayConcat(T[] arg1, T[] arg2) {
return new ArrayConcat<>(Tools.field(arg1), Tools.field(arg2));
}
/**
* The <code>ARRAY_CONCAT</code> function.
* <p>
* Concatenate two arrays.
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES, YUGABYTEDB })
public static <T> Field<T[]> arrayConcat(T[] arg1, Field<T[]> arg2) {
return new ArrayConcat<>(Tools.field(arg1), arg2);
}
/**
* The <code>ARRAY_CONCAT</code> function.
* <p>
* Concatenate two arrays.
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES, YUGABYTEDB })
public static <T> Field<T[]> arrayConcat(Field<T[]> arg1, T[] arg2) {
return new ArrayConcat<>(arg1, Tools.field(arg2, arg1));
}
/**
* The <code>ARRAY_CONCAT</code> function.
* <p>
* Concatenate two arrays.
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES, YUGABYTEDB })
public static <T> Field<T[]> arrayConcat(Field<T[]> arg1, Field<T[]> arg2) {
return new ArrayConcat<>(arg1, arg2);
}
// -------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------

View File

@ -345,6 +345,7 @@ final class Names {
static final Name N_ACOSH = systemName("acosh");
static final Name N_ACOTH = systemName("acoth");
static final Name N_ANY_VALUE = systemName("any_value");
static final Name N_ARRAY_CONCAT = systemName("array_concat");
static final Name N_ARRAY_GET = systemName("array_get");
static final Name N_ASCII = systemName("ascii");
static final Name N_ASIN = systemName("asin");

View File

@ -7747,11 +7747,18 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
if (r instanceof Field)
while (parseIf("||"))
r = concat((Field) r, toField(parseCollated()));
r = concatOperator((Field) r, toField(parseCollated()));
return r;
}
private final Field<?> concatOperator(Field<?> a1, Field<?> a2) {
if (a1.getDataType().isArray() && a2.getDataType().isArray())
return DSL.arrayConcat((Field) a1, (Field) a2);
else
return DSL.concat(a1, a2);
}
private final FieldOrRow parseCollated() {
FieldOrRow r = parseNumericOp();

View File

@ -4700,6 +4700,20 @@ public final class QOM {
@NotNull default Field<Integer> $index() { return $arg2(); }
}
/**
* The <code>ARRAY CONCAT</code> function.
* <p>
* Concatenate two arrays.
*/
public /*sealed*/ interface ArrayConcat<T>
extends
UReturnsNullOnNullInput,
UOperator2<Field<T[]>, Field<T[]>, Field<T[]>>,
org.jooq.Field<T[]>
//permits
// ArrayConcat
{}
/**
* The <code>NVL</code> function.
* <p>