[jOOQ/jOOQ#16227] Add JSON_ARRAY_LENGTH() function support

Native support in PostgreSQL. More dialects to be added soon.
This commit is contained in:
Lukas Eder 2024-02-07 14:21:55 +01:00
parent 57129a5b49
commit 9981af4bd1
6 changed files with 416 additions and 0 deletions

View File

@ -23312,6 +23312,54 @@ public class DSL {
return new JSONBGetAttributeAsText(field, attribute);
}
/**
* The <code>JSON_ARRAY_LENGTH</code> function.
* <p>
* Calculate the length of a JSON array.
*
* @param field is wrapped as {@link DSL#val(Object)}.
*/
@NotNull
@Support
public static Field<Integer> jsonArrayLength(JSON field) {
return new JSONArrayLength(Tools.field(field));
}
/**
* The <code>JSON_ARRAY_LENGTH</code> function.
* <p>
* Calculate the length of a JSON array.
*/
@NotNull
@Support
public static Field<Integer> jsonArrayLength(Field<JSON> field) {
return new JSONArrayLength(field);
}
/**
* The <code>JSONB_ARRAY_LENGTH</code> function.
* <p>
* Calculate the length of a JSONB array.
*
* @param field is wrapped as {@link DSL#val(Object)}.
*/
@NotNull
@Support
public static Field<Integer> jsonbArrayLength(JSONB field) {
return new JSONBArrayLength(Tools.field(field));
}
/**
* The <code>JSONB_ARRAY_LENGTH</code> function.
* <p>
* Calculate the length of a JSONB array.
*/
@NotNull
@Support
public static Field<Integer> jsonbArrayLength(Field<JSONB> field) {
return new JSONBArrayLength(field);
}
/**
* The <code>JSON_KEYS</code> function.
* <p>

View File

@ -0,0 +1,163 @@
/*
* 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>JSON ARRAY LENGTH</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class JSONArrayLength
extends
AbstractField<Integer>
implements
QOM.JSONArrayLength
{
final Field<JSON> field;
JSONArrayLength(
Field<JSON> field
) {
super(
N_JSON_ARRAY_LENGTH,
allNotNull(INTEGER, field)
);
this.field = nullSafeNotNull(field, JSON);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
final boolean parenthesised(Context<?> ctx) {
return true;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case POSTGRES:
case YUGABYTEDB:
ctx.visit(function(N_JSON_ARRAY_LENGTH, getDataType(), field));
break;
default:
ctx.visit(function(N_JSON_ARRAY_LENGTH, getDataType(), field));
break;
}
}
// -------------------------------------------------------------------------
// XXX: Query Object Model
// -------------------------------------------------------------------------
@Override
public final Field<JSON> $arg1() {
return field;
}
@Override
public final QOM.JSONArrayLength $arg1(Field<JSON> newValue) {
return $constructor().apply(newValue);
}
@Override
public final Function1<? super Field<JSON>, ? extends QOM.JSONArrayLength> $constructor() {
return (a1) -> new JSONArrayLength(a1);
}
// -------------------------------------------------------------------------
// XXX: The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof QOM.JSONArrayLength o) {
return
StringUtils.equals($field(), o.$field())
;
}
else
return super.equals(that);
}
}

View File

@ -0,0 +1,163 @@
/*
* 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>JSONB ARRAY LENGTH</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class JSONBArrayLength
extends
AbstractField<Integer>
implements
QOM.JSONBArrayLength
{
final Field<JSONB> field;
JSONBArrayLength(
Field<JSONB> field
) {
super(
N_JSONB_ARRAY_LENGTH,
allNotNull(INTEGER, field)
);
this.field = nullSafeNotNull(field, JSONB);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
final boolean parenthesised(Context<?> ctx) {
return true;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case POSTGRES:
case YUGABYTEDB:
ctx.visit(function(N_JSONB_ARRAY_LENGTH, getDataType(), field));
break;
default:
ctx.visit(function(N_JSONB_ARRAY_LENGTH, getDataType(), field));
break;
}
}
// -------------------------------------------------------------------------
// XXX: Query Object Model
// -------------------------------------------------------------------------
@Override
public final Field<JSONB> $arg1() {
return field;
}
@Override
public final QOM.JSONBArrayLength $arg1(Field<JSONB> newValue) {
return $constructor().apply(newValue);
}
@Override
public final Function1<? super Field<JSONB>, ? extends QOM.JSONBArrayLength> $constructor() {
return (a1) -> new JSONBArrayLength(a1);
}
// -------------------------------------------------------------------------
// XXX: The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof QOM.JSONBArrayLength o) {
return
StringUtils.equals($field(), o.$field())
;
}
else
return super.equals(that);
}
}

View File

@ -431,6 +431,7 @@ final class Names {
static final Name N_INSTR = systemName("instr");
static final Name N_ISJSON = systemName("isjson");
static final Name N_JSONB_ARRAY = systemName("jsonb_array");
static final Name N_JSONB_ARRAY_LENGTH = systemName("jsonb_array_length");
static final Name N_JSONB_GET_ATTRIBUTE = systemName("jsonb_get_attribute");
static final Name N_JSONB_GET_ATTRIBUTE_AS_TEXT = systemName("jsonb_get_attribute_as_text");
static final Name N_JSONB_GET_ELEMENT = systemName("jsonb_get_element");
@ -442,6 +443,7 @@ final class Names {
static final Name N_JSONB_REPLACE = systemName("jsonb_replace");
static final Name N_JSONB_SET = systemName("jsonb_set");
static final Name N_JSON_ARRAY = systemName("json_array");
static final Name N_JSON_ARRAY_LENGTH = systemName("json_array_length");
static final Name N_JSON_EXTRACT = systemName("json_extract");
static final Name N_JSON_GET_ATTRIBUTE = systemName("json_get_attribute");
static final Name N_JSON_GET_ATTRIBUTE_AS_TEXT = systemName("json_get_attribute_as_text");

View File

@ -8964,6 +8964,8 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
return field;
else if ((field = parseFieldJSONLiteralIf()) != null)
return field;
else if (parseFunctionNameIf("JSON_ARRAY_LENGTH"))
return parseFunctionArgs1(DSL::jsonArrayLength);
else if (parseFunctionNameIf("JSON_KEYS"))
return parseFunctionArgs1(DSL::jsonKeys);
else if (parseFunctionNameIf("JSON_INSERT"))
@ -8976,6 +8978,8 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
return parseFunctionArgs3(DSL::jsonSet);
else if (parseFunctionNameIf("JSON_VALID"))
return parseFunctionArgs1(f -> case_(f.isJson()).when(trueCondition(), one()).when(falseCondition(), zero()));
else if (parseFunctionNameIf("JSONB_ARRAY_LENGTH"))
return parseFunctionArgs1(DSL::jsonbArrayLength);
else if (parseFunctionNameIf("JSONB_KEYS"))
return parseFunctionArgs1(DSL::jsonbKeys);
else if (parseFunctionNameIf("JSONB_INSERT"))

View File

@ -6971,6 +6971,42 @@ public final class QOM {
@NotNull default JSONBGetAttributeAsText $attribute(Field<String> newAttribute) { return $arg2(newAttribute); }
}
/**
* The <code>JSON ARRAY LENGTH</code> function.
* <p>
* Calculate the length of a JSON array.
*/
public /*sealed*/ interface JSONArrayLength
extends
UReturnsNullOnNullInput,
UOperator1<Field<JSON>, JSONArrayLength>,
org.jooq.Field<Integer>
//permits
// JSONArrayLength
{
@NotNull default Field<JSON> $field() { return $arg1(); }
@CheckReturnValue
@NotNull default JSONArrayLength $field(Field<JSON> newField) { return $arg1(newField); }
}
/**
* The <code>JSONB ARRAY LENGTH</code> function.
* <p>
* Calculate the length of a JSONB array.
*/
public /*sealed*/ interface JSONBArrayLength
extends
UReturnsNullOnNullInput,
UOperator1<Field<JSONB>, JSONBArrayLength>,
org.jooq.Field<Integer>
//permits
// JSONBArrayLength
{
@NotNull default Field<JSONB> $field() { return $arg1(); }
@CheckReturnValue
@NotNull default JSONBArrayLength $field(Field<JSONB> newField) { return $arg1(newField); }
}
/**
* The <code>JSON KEYS</code> function.
* <p>