From 0758ae21426288646a0a127bbb76f8c3638daa1f Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 9 Mar 2020 10:59:17 +0100 Subject: [PATCH] [jOOQ/jOOQ#9928] Add support for JSON_OBJECT --- .../java/org/jooq/JSONObjectNullStep.java | 64 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 11 ++-- .../main/java/org/jooq/impl/JSONObject.java | 45 ++++++++++++- .../src/main/java/org/jooq/impl/Keywords.java | 1 + 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java diff --git a/jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java b/jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java new file mode 100644 index 0000000000..a0d85bb29e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java @@ -0,0 +1,64 @@ +/* + * 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 static org.jooq.SQLDialect.H2; + +import org.jooq.impl.DSL; + +/** + * A step in the construction of {@link DSL#jsonObject(JSONEntry...)} or + * {@link DSL#jsonbObject(JSONEntry...)} functions where the NULL + * clause can be defined. + * + * @author Lukas Eder + */ +public interface JSONObjectNullStep extends Field { + + /** + * Include NULL values in output JSON. + */ + @Support({ H2 }) + Field nullOnNull(); + + /** + * Exclude NULL values in output JSON. + */ + @Support({ H2 }) + Field absentOnNull(); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 2c19b84bbf..2dbe7a0df6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -235,6 +235,7 @@ import org.jooq.InsertValuesStepN; import org.jooq.JSON; import org.jooq.JSONB; import org.jooq.JSONEntry; +import org.jooq.JSONObjectNullStep; import org.jooq.Keyword; // ... // ... @@ -18140,7 +18141,7 @@ public class DSL { * The JSON object constructor. */ @Support({ H2, MARIADB, MYSQL, POSTGRES }) - public static Field jsonObject(Field key, Field value) { + public static JSONObjectNullStep jsonObject(Field key, Field value) { return jsonObject(jsonEntry(key, value)); } @@ -18148,7 +18149,7 @@ public class DSL { * The JSON object constructor. */ @Support({ H2, MARIADB, MYSQL, POSTGRES }) - public static Field jsonObject(JSONEntry... entries) { + public static JSONObjectNullStep jsonObject(JSONEntry... entries) { return jsonObject(Arrays.asList(entries)); } @@ -18156,7 +18157,7 @@ public class DSL { * The JSON object constructor. */ @Support({ H2, MARIADB, MYSQL, POSTGRES }) - public static Field jsonObject(Collection> entries) { + public static JSONObjectNullStep jsonObject(Collection> entries) { return new JSONObject<>(JSON, entries); } @@ -18164,7 +18165,7 @@ public class DSL { * The JSONB object constructor. */ @Support({ H2, MARIADB, MYSQL, POSTGRES }) - public static Field jsonbObject(JSONEntry... entries) { + public static JSONObjectNullStep jsonbObject(JSONEntry... entries) { return jsonbObject(Arrays.asList(entries)); } @@ -18172,7 +18173,7 @@ public class DSL { * The JSONB object constructor. */ @Support({ H2, MARIADB, MYSQL, POSTGRES }) - public static Field jsonbObject(Collection> entries) { + public static JSONObjectNullStep jsonbObject(Collection> entries) { return new JSONObject<>(JSONB, entries); } diff --git a/jOOQ/src/main/java/org/jooq/impl/JSONObject.java b/jOOQ/src/main/java/org/jooq/impl/JSONObject.java index acfa57cbf3..3a6f725d73 100644 --- a/jOOQ/src/main/java/org/jooq/impl/JSONObject.java +++ b/jOOQ/src/main/java/org/jooq/impl/JSONObject.java @@ -43,7 +43,12 @@ import static org.jooq.impl.DSL.row; import static org.jooq.impl.DSL.select; import static org.jooq.impl.DSL.unquotedName; import static org.jooq.impl.DSL.values; +import static org.jooq.impl.JSONObject.NullClause.ABSENT_ON_NULL; +import static org.jooq.impl.JSONObject.NullClause.NULL_ON_NULL; +import static org.jooq.impl.Keywords.K_ABSENT; import static org.jooq.impl.Keywords.K_JSON_OBJECT; +import static org.jooq.impl.Keywords.K_NULL; +import static org.jooq.impl.Keywords.K_ON; import static org.jooq.impl.Names.N_JSON_OBJECT; import static org.jooq.impl.Names.N_T; @@ -53,6 +58,7 @@ import org.jooq.Context; import org.jooq.DataType; import org.jooq.Field; import org.jooq.JSONEntry; +import org.jooq.JSONObjectNullStep; import org.jooq.Name; @@ -61,20 +67,48 @@ import org.jooq.Name; * * @author Lukas Eder */ -final class JSONObject extends AbstractField { +final class JSONObject extends AbstractField implements JSONObjectNullStep { /** * Generated UID */ private static final long serialVersionUID = 1772007627336725780L; private final QueryPartList> args; + private final NullClause nullClause; JSONObject(DataType type, Collection> args) { + this(type, args, null); + } + + JSONObject(DataType type, Collection> args, NullClause nullClause) { super(N_JSON_OBJECT, type); this.args = new QueryPartList<>(args); + this.nullClause = nullClause; } + // ------------------------------------------------------------------------- + // XXX: DSL API + // ------------------------------------------------------------------------- + + @Override + public final JSONObject nullOnNull() { + return new JSONObject<>(getDataType(), args, NULL_ON_NULL); + } + + @Override + public final JSONObject absentOnNull() { + return new JSONObject<>(getDataType(), args, ABSENT_ON_NULL); + } + + enum NullClause { + NULL_ON_NULL, ABSENT_ON_NULL + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + @Override public void accept(Context ctx) { switch (ctx.family()) { @@ -119,7 +153,14 @@ final class JSONObject extends AbstractField { default: - ctx.visit(K_JSON_OBJECT).sql('(').visit(args).sql(')'); + ctx.visit(K_JSON_OBJECT).sql('(').visit(args); + + if (nullClause == NULL_ON_NULL) + ctx.sql(' ').visit(K_NULL).sql(' ').visit(K_ON).sql(' ').visit(K_NULL); + else if (nullClause == ABSENT_ON_NULL) + ctx.sql(' ').visit(K_ABSENT).sql(' ').visit(K_ON).sql(' ').visit(K_NULL); + + ctx.sql(')'); break; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index 5d72dbdf30..63827ed4e1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -48,6 +48,7 @@ import org.jooq.Keyword; */ final class Keywords { + static final Keyword K_ABSENT = keyword("absent"); static final Keyword K_ADD = keyword("add"); static final Keyword K_AFTER = keyword("after"); static final Keyword K_ALIAS = keyword("alias");