[jOOQ/jOOQ#9928] Add support for JSON_OBJECT <JSON constructor null clause>
This commit is contained in:
parent
420048f9f8
commit
0758ae2142
64
jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java
Normal file
64
jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java
Normal file
@ -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 <code>NULL</code>
|
||||
* clause can be defined.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface JSONObjectNullStep<T> extends Field<T> {
|
||||
|
||||
/**
|
||||
* Include <code>NULL</code> values in output JSON.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
Field<T> nullOnNull();
|
||||
|
||||
/**
|
||||
* Exclude <code>NULL</code> values in output JSON.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
Field<T> absentOnNull();
|
||||
}
|
||||
@ -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<JSON> jsonObject(Field<String> key, Field<?> value) {
|
||||
public static JSONObjectNullStep<JSON> jsonObject(Field<String> 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<JSON> jsonObject(JSONEntry<?>... entries) {
|
||||
public static JSONObjectNullStep<JSON> 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<JSON> jsonObject(Collection<? extends JSONEntry<?>> entries) {
|
||||
public static JSONObjectNullStep<JSON> jsonObject(Collection<? extends JSONEntry<?>> 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<JSONB> jsonbObject(JSONEntry<?>... entries) {
|
||||
public static JSONObjectNullStep<JSONB> 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<JSONB> jsonbObject(Collection<? extends JSONEntry<?>> entries) {
|
||||
public static JSONObjectNullStep<JSONB> jsonbObject(Collection<? extends JSONEntry<?>> entries) {
|
||||
return new JSONObject<>(JSONB, entries);
|
||||
}
|
||||
|
||||
|
||||
@ -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<J> extends AbstractField<J> {
|
||||
final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep<J> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1772007627336725780L;
|
||||
private final QueryPartList<JSONEntry<?>> args;
|
||||
private final NullClause nullClause;
|
||||
|
||||
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> args) {
|
||||
this(type, args, null);
|
||||
}
|
||||
|
||||
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> args, NullClause nullClause) {
|
||||
super(N_JSON_OBJECT, type);
|
||||
|
||||
this.args = new QueryPartList<>(args);
|
||||
this.nullClause = nullClause;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX: DSL API
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public final JSONObject<J> nullOnNull() {
|
||||
return new JSONObject<>(getDataType(), args, NULL_ON_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final JSONObject<J> 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<J> extends AbstractField<J> {
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user