[jOOQ/jOOQ#10175] Add support for ARRAY_GET

This includes [jOOQ/jOOQ#10300] Add DataType.getArrayComponentType() and getArrayComponentDataType()
This commit is contained in:
Lukas Eder 2020-06-22 12:33:23 +02:00
parent aad8d49ead
commit 39db60b9b9
7 changed files with 170 additions and 6 deletions

View File

@ -79,6 +79,16 @@ inline fun Field<Boolean>.orNotExists(select: Select<*>): Condition = condition(
inline fun Field<Boolean>.not(): Condition = condition(this).not()
// ----------------------------------------------------------------------------
// Extensions to make Field<T[]> aware of its being an array
// ----------------------------------------------------------------------------
@Support
inline operator fun <T> Field<Array<T>>.get(index: Int) = arrayGet(this, index)
@Support
inline operator fun <T> Field<Array<T>>.get(index: Field<Int>) = arrayGet(this, index)
// ----------------------------------------------------------------------------
// Extensions to make Select<Record1<T>> a scalar subquery of type Field<T>
// ----------------------------------------------------------------------------
@ -194,4 +204,3 @@ inline fun <reified T: Any> Select<Record1<T>>.notIn(vararg values: T): Conditio
@Support
inline fun <reified T: Any> Select<Record1<T>>.notIn(vararg values: Field<*>): Condition = field(this).notIn(values.asList())

View File

@ -140,6 +140,20 @@ public interface DataType<T> extends Named {
@NotNull
DataType<T[]> getArrayDataType();
/**
* Retrieve the Java component type if this is an ARRAY type, or
* <code>null</code>, otherwise.
*/
@Nullable
Class<?> getArrayComponentType();
/**
* Retrieve the Java component data type if this is an ARRAY type, or
* <code>null</code>, otherwise.
*/
@Nullable
DataType<?> getArrayComponentDataType();

View File

@ -123,6 +123,17 @@ final class ArrayDataType<T> extends DefaultDataType<T[]> {
return getArrayType(configuration, castTypeName);
}
@Override
public final Class<?> getArrayComponentType() {
return elementType.getType();
}
@Override
public final DataType<?> getArrayComponentDataType() {
return elementType;
}
private static String getArrayType(Configuration configuration, String dataType) {
switch (configuration.family()) {

View File

@ -0,0 +1,101 @@
/*
* 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.impl.DSL.cardinality;
import static org.jooq.impl.DSL.when;
import static org.jooq.impl.Names.N_ARRAY_GET;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
final class ArrayGet<T> extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1852008882778358276L;
private final Field<T[]> field;
private final Field<Integer> index;
@SuppressWarnings("unchecked")
ArrayGet(Field<T[]> field, Field<Integer> index) {
super(N_ARRAY_GET, (DataType<T>) field.getDataType().getArrayComponentDataType());
this.field = field;
this.index = index;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case H2:
ctx.visit(N_ARRAY_GET).sql('(').visit(field).sql(", ").visit(index).sql(')');
break;
case HSQLDB:
ctx.visit(when(cardinality(field).ge(index), new Standard()));
break;
default:
ctx.visit(new Standard());
break;
}
}
private class Standard extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 7727869539823288556L;
Standard() {
super(ArrayGet.this.getQualifiedName(), ArrayGet.this.getDataType());
}
@Override
public void accept(Context<?> ctx) {
ctx.visit(field).sql('[').visit(index).sql(']');
}
}
}

View File

@ -21126,6 +21126,24 @@ public class DSL {
return new Cardinality(field);
}
/**
* Get an array element at a given index (1 based)
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES })
public static <T> Field<T> arrayGet(Field<T[]> field, int index) {
return arrayGet(field, Tools.field(index));
}
/**
* Get an array element at a given index (1 based)
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES })
public static <T> Field<T> arrayGet(Field<T[]> field, Field<Integer> index) {
return new ArrayGet<>(field, index);
}
/**
* Get the max value over a field: max(field).
*/

View File

@ -797,11 +797,6 @@ public class DefaultDataType<T> extends AbstractNamed implements DataType<T> {
return binding.converter();
}
@Override
public final Class<T[]> getArrayType() {
return arrayType;
}
@Override
public final String getTypeName() {
return typeName;
@ -835,11 +830,26 @@ public class DefaultDataType<T> extends AbstractNamed implements DataType<T> {
return getDataType(configuration).getCastTypeName();
}
@Override
public final Class<T[]> getArrayType() {
return arrayType;
}
@Override
public final DataType<T[]> getArrayDataType() {
return new ArrayDataType<>(this);
}
@Override
public /* non-final */ Class<?> getArrayComponentType() {
return null;
}
@Override
public /* non-final */ DataType<?> getArrayComponentDataType() {
return null;
}

View File

@ -59,6 +59,7 @@ final class Names {
static final Name N_AGE = unquotedName("age");
static final Name N_ARRAY = unquotedName("array");
static final Name N_ARRAY_AGG = unquotedName("array_agg");
static final Name N_ARRAY_GET = unquotedName("array_get");
static final Name N_ARRAY_LENGTH = unquotedName("array_length");
static final Name N_ASC = unquotedName("asc");
static final Name N_ASCII = unquotedName("ascii");