[jOOQ/jOOQ#10873] Add alternative DSL.key(...).value(...) syntax to produce JSONEntry

This commit is contained in:
Lukas Eder 2020-11-05 22:16:03 +01:00
parent 5e1ab3ed5b
commit 38c8426d76
7 changed files with 131 additions and 8 deletions

View File

@ -41,6 +41,8 @@ import org.jetbrains.annotations.NotNull;
/**
* A JSON entry for JSON objects.
*
* @author Lukas Eder
*/
public interface JSONEntry<T> extends QueryPart {

View File

@ -0,0 +1,66 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
/**
* A step in the creation of {@link JSONEntry} values.
*
* @author Lukas Eder
*/
public interface JSONEntryValueStep {
/**
* The JSON entry value.
*/
@NotNull
<T> JSONEntry<T> value(T value);
/**
* The JSON entry value.
*/
@NotNull
<T> JSONEntry<T> value(Field<T> value);
/**
* The JSON entry value.
*/
@NotNull
<T> JSONEntry<T> value(Select<? extends Record1<T>> value);
}

View File

@ -246,6 +246,7 @@ import org.jooq.JSONArrayAggOrderByStep;
import org.jooq.JSONArrayNullStep;
import org.jooq.JSONB;
import org.jooq.JSONEntry;
import org.jooq.JSONEntryValueStep;
import org.jooq.JSONExistsOnStep;
import org.jooq.JSONObjectAggNullStep;
import org.jooq.JSONObjectNullStep;
@ -20624,6 +20625,28 @@ public class DSL {
return new JSONArray<>(JSONB, fields);
}
/**
* A constructor for JSON entries to be used with
* {@link #jsonObject(JSONEntry...)}.
* <p>
* This is the same as calling {@link #jsonEntry(String, Field)}.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static JSONEntryValueStep key(String key) {
return key(Tools.field(key));
}
/**
* A constructor for JSON entries to be used with
* {@link #jsonObject(JSONEntry...)}.
* <p>
* This is the same as calling {@link #jsonEntry(Field, Field)}.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static JSONEntryValueStep key(Field<String> key) {
return new JSONEntryImpl<>(key);
}
/**
* A constructor for JSON entries to be used with
* {@link #jsonObject(JSONEntry...)}.
@ -20657,6 +20680,16 @@ public class DSL {
return new JSONEntryImpl<>(key, value);
}
/**
* A constructor for JSON entries to be used with
* {@link #jsonObject(JSONEntry...)}.
*/
@NotNull
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static <T> JSONEntry<T> jsonEntry(Field<String> key, T value) {
return jsonEntry(key, Tools.field(value));
}
/**
* The JSON object constructor.
*/

View File

@ -50,6 +50,9 @@ import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.JSONEntry;
import org.jooq.JSONEntryValueStep;
import org.jooq.Record1;
import org.jooq.Select;
/**
@ -57,7 +60,7 @@ import org.jooq.JSONEntry;
*
* @author Lukas Eder
*/
final class JSONEntryImpl<T> extends AbstractQueryPart implements JSONEntry<T> {
final class JSONEntryImpl<T> extends AbstractQueryPart implements JSONEntry<T>, JSONEntryValueStep {
/**
* Generated UID
@ -66,6 +69,10 @@ final class JSONEntryImpl<T> extends AbstractQueryPart implements JSONEntry<T> {
private final Field<String> key;
private final Field<T> value;
JSONEntryImpl(Field<String> key) {
this(key, null);
}
JSONEntryImpl(Field<String> key, Field<T> value) {
this.key = key;
this.value = value;
@ -81,6 +88,21 @@ final class JSONEntryImpl<T> extends AbstractQueryPart implements JSONEntry<T> {
return value;
}
@Override
public final <X> JSONEntry<X> value(X newValue) {
return value(Tools.field(newValue));
}
@Override
public final <X> JSONEntry<X> value(Field<X> newValue) {
return new JSONEntryImpl<>(key, newValue);
}
@Override
public final <X> JSONEntry<X> value(Select<? extends Record1<X>> newValue) {
return value(DSL.field(newValue));
}
@Override
public void accept(Context<?> ctx) {
switch (ctx.family()) {

View File

@ -43,6 +43,7 @@ import static org.jooq.impl.DSL.asterisk;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.jsonEntry;
import static org.jooq.impl.DSL.jsonObject;
import static org.jooq.impl.DSL.key;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.unquotedName;
@ -188,10 +189,9 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
.sql(')');
}
else if (!args.isEmpty() && isJSONArray(args.get(0).value())) {
ctx.visit(jsonObject(jsonEntry(
args.get(0).key(),
DSL.field("{0}({1}, {2})", getDataType(), N_JSON_MERGE, inline("[]"), args.get(0).value())
)));
ctx.visit(jsonObject(
key(args.get(0).key()).value(DSL.field("{0}({1}, {2})", getDataType(), N_JSON_MERGE, inline("[]"), args.get(0).value()))
));
}
else
acceptStandard(ctx);

View File

@ -161,6 +161,7 @@ import static org.jooq.impl.DSL.jsonEntry;
import static org.jooq.impl.DSL.jsonExists;
import static org.jooq.impl.DSL.jsonTable;
import static org.jooq.impl.DSL.jsonValue;
import static org.jooq.impl.DSL.key;
import static org.jooq.impl.DSL.keyword;
import static org.jooq.impl.DSL.lag;
import static org.jooq.impl.DSL.lastValue;
@ -7801,8 +7802,7 @@ final class ParserImpl implements Parser {
else
parse(ctx, ',');
Field<?> value = parseField(ctx);
return jsonEntry(key, value);
return key(key).value(parseField(ctx));
}
private static final Field<?> parseArrayValueConstructorIf(ParserContext ctx) {

View File

@ -106,10 +106,10 @@ import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.groupingSets;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.jsonArrayAgg;
import static org.jooq.impl.DSL.jsonEntry;
import static org.jooq.impl.DSL.jsonObject;
import static org.jooq.impl.DSL.jsonbArrayAgg;
import static org.jooq.impl.DSL.jsonbObject;
import static org.jooq.impl.DSL.key;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.one;