[jOOQ/jOOQ#8950] Added JSON_ARRAY() NULL clause support

This commit is contained in:
Lukas Eder 2020-03-11 14:22:31 +01:00
parent 436e56c8dd
commit 6bfbb82c83
6 changed files with 54 additions and 23 deletions

View File

@ -46,9 +46,9 @@ import static org.jooq.SQLDialect.POSTGRES;
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.
* A step in the construction of {@link DSL#jsonArrayAgg(Field)} or
* {@link DSL#jsonbArrayAgg(Field)} functions where the <code>NULL</code> clause
* can be defined.
*
* @author Lukas Eder
*/

View File

@ -48,10 +48,11 @@ import java.util.Collection;
import org.jooq.impl.DSL;
/**
* The SQL standard <code>ARRAY_AGG()</code> function.
* A step in the construction of {@link DSL#jsonArrayAgg(Field)} or
* {@link DSL#jsonbArrayAgg(Field)} functions where the <code>ORDER BY</code>
* clause can be defined.
*
* @author Lukas Eder
* @see DSL#arrayAgg(Field)
*/
public interface JSONArrayAggOrderByStep<J> extends JSONArrayAggNullStep<J> {

View File

@ -46,8 +46,8 @@ import static org.jooq.SQLDialect.POSTGRES;
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>
* A step in the construction of {@link DSL#jsonObjectAgg(JSONEntry)} or
* {@link DSL#jsonbObjectAgg(JSONEntry)} functions where the <code>NULL</code>
* clause can be defined.
*
* @author Lukas Eder

View File

@ -233,6 +233,7 @@ import org.jooq.InsertValuesStep9;
import org.jooq.InsertValuesStepN;
import org.jooq.JSON;
import org.jooq.JSONArrayAggOrderByStep;
import org.jooq.JSONArrayNullStep;
import org.jooq.JSONB;
import org.jooq.JSONEntry;
import org.jooq.JSONObjectAggNullStep;
@ -18102,7 +18103,7 @@ public class DSL {
* The JSON array constructor.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static Field<JSON> jsonArray(Field<?>... fields) {
public static JSONArrayNullStep<JSON> jsonArray(Field<?>... fields) {
return jsonArray(Arrays.asList(fields));
}
@ -18110,7 +18111,7 @@ public class DSL {
* The JSON array constructor.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static Field<JSON> jsonArray(Collection<? extends Field<?>> fields) {
public static JSONArrayNullStep<JSON> jsonArray(Collection<? extends Field<?>> fields) {
return new JSONArray<>(JSON, fields);
}
@ -18118,7 +18119,7 @@ public class DSL {
* The JSONB array constructor.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static Field<JSONB> jsonbArray(Field<?>... fields) {
public static JSONArrayNullStep<JSONB> jsonbArray(Field<?>... fields) {
return jsonbArray(Arrays.asList(fields));
}
@ -18126,7 +18127,7 @@ public class DSL {
* The JSONB array constructor.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static Field<JSONB> jsonbArray(Collection<? extends Field<?>> fields) {
public static JSONArrayNullStep<JSONB> jsonbArray(Collection<? extends Field<?>> fields) {
return new JSONArray<>(JSONB, fields);
}

View File

@ -39,6 +39,9 @@ package org.jooq.impl;
import static org.jooq.SQLDialect.H2;
import static org.jooq.impl.DSL.unquotedName;
import static org.jooq.impl.JSONNullClause.ABSENT_ON_NULL;
import static org.jooq.impl.JSONNullClause.NULL_ON_NULL;
import static org.jooq.impl.JSONObject.acceptJSONNullClause;
import static org.jooq.impl.Keywords.K_JSON_ARRAY;
import static org.jooq.impl.Keywords.K_NULL;
import static org.jooq.impl.Keywords.K_ON;
@ -49,6 +52,7 @@ import java.util.Collection;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.JSONArrayNullStep;
/**
@ -56,20 +60,44 @@ import org.jooq.Field;
*
* @author Lukas Eder
*/
final class JSONArray<J> extends AbstractField<J> {
final class JSONArray<J> extends AbstractField<J> implements JSONArrayNullStep<J> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1772007627336725780L;
private final QueryPartList<Field<?>> args;
private final JSONNullClause nullClause;
JSONArray(DataType<J> type, Collection<? extends Field<?>> args) {
this(type, args, null);
}
JSONArray(DataType<J> type, Collection<? extends Field<?>> args, JSONNullClause nullClause) {
super(N_JSON_ARRAY, type);
this.args = new QueryPartList<>(args);
this.nullClause = nullClause;
}
// -------------------------------------------------------------------------
// XXX: DSL API
// -------------------------------------------------------------------------
@Override
public final JSONArray<J> nullOnNull() {
return new JSONArray<>(getDataType(), args, NULL_ON_NULL);
}
@Override
public final JSONArray<J> absentOnNull() {
return new JSONArray<>(getDataType(), args, ABSENT_ON_NULL);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public void accept(Context<?> ctx) {
switch (ctx.family()) {
@ -88,6 +116,8 @@ final class JSONArray<J> extends AbstractField<J> {
// Workaround for https://github.com/h2database/h2database/issues/2496
if (ctx.family() == H2 && args.isEmpty())
ctx.visit(K_NULL).sql(' ').visit(K_ON).sql(' ').visit(K_NULL);
else
acceptJSONNullClause(ctx, nullClause);
ctx.sql(')');
break;

View File

@ -176,18 +176,16 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
default:
ctx.visit(K_JSON_OBJECT).sql('(').visit(args);
if (!NO_SUPPORT_ABSENT_ON_NULL.contains(ctx.dialect()))
// Workaround for https://github.com/h2database/h2database/issues/2496
if (args.isEmpty() && ctx.family() == H2)
ctx.visit(K_NULL).sql(' ').visit(K_ON).sql(' ').visit(K_NULL);
// Workaround for https://github.com/h2database/h2database/issues/2496
if (args.isEmpty() && ctx.family() == H2)
ctx.visit(K_NULL).sql(' ').visit(K_ON).sql(' ').visit(K_NULL);
else
acceptJSONNullClause(ctx, nullClause);
else
acceptJSONNullClause(ctx, nullClause);
ctx.sql(')');
break;
@ -195,9 +193,10 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
}
static final void acceptJSONNullClause(Context<?> ctx, JSONNullClause nullClause) {
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);
if (!NO_SUPPORT_ABSENT_ON_NULL.contains(ctx.dialect()))
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);
}
}