[jOOQ/jOOQ#9927] Add support for JSON_ARRAYAGG - H2 support

This commit is contained in:
Lukas Eder 2020-03-10 17:46:36 +01:00
parent 54ee4381c4
commit 0844eb0c9b
12 changed files with 370 additions and 49 deletions

View File

@ -0,0 +1,70 @@
/*
* 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 static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
// ...
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.
*
* @author Lukas Eder
*/
public interface JSONArrayAggNullStep<T> extends AggregateFilterStep<T> {
/**
* Include <code>NULL</code> values in output JSON.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
AggregateFilterStep<T> nullOnNull();
/**
* Exclude <code>NULL</code> values in output JSON.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
AggregateFilterStep<T> absentOnNull();
}

View File

@ -0,0 +1,71 @@
/*
* 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 static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
import java.util.Collection;
import org.jooq.impl.DSL;
/**
* The SQL standard <code>ARRAY_AGG()</code> function.
*
* @author Lukas Eder
* @see DSL#arrayAgg(Field)
*/
public interface JSONArrayAggOrderByStep<J> extends JSONArrayAggNullStep<J> {
/**
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
JSONArrayAggNullStep<J> orderBy(OrderField<?>... fields);
/**
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
JSONArrayAggNullStep<J> orderBy(Collection<? extends OrderField<?>> fields);
}

View File

@ -48,6 +48,7 @@ import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.Keywords.K_DISTINCT;
import static org.jooq.impl.Keywords.K_FILTER;
import static org.jooq.impl.Keywords.K_ORDER_BY;
import static org.jooq.impl.Keywords.K_WHERE;
import java.util.Arrays;
@ -160,6 +161,12 @@ implements
.sql(')');
}
final void acceptOrderBy(Context<?> ctx) {
if (!Tools.isEmpty(withinGroupOrderBy))
ctx.sql(' ').visit(K_ORDER_BY).sql(' ')
.visit(withinGroupOrderBy);
}
// -------------------------------------------------------------------------
// XXX Aggregate function API
// -------------------------------------------------------------------------
@ -268,9 +275,9 @@ implements
@Override
public final AbstractAggregateFunction<T> orderBy(OrderField<?>... fields) {
public /* non-final */ AbstractAggregateFunction<T> orderBy(OrderField<?>... fields) {
if (windowSpecification != null)
windowSpecification.orderBy(fields);
super.orderBy(fields);
else
withinGroupOrderBy(fields);
@ -278,7 +285,7 @@ implements
}
@Override
public final AbstractAggregateFunction<T> orderBy(Collection<? extends OrderField<?>> fields) {
public /* non-final */ AbstractAggregateFunction<T> orderBy(Collection<? extends OrderField<?>> fields) {
if (windowSpecification != null)
windowSpecification.orderBy(fields);
else

View File

@ -37,7 +37,6 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Keywords.K_ORDER_BY;
import static org.jooq.impl.Names.N_ARRAY_AGG;
import java.util.Arrays;
@ -63,11 +62,7 @@ final class ArrayAgg<T> extends DefaultAggregateFunction<T[]> {
public final void accept(Context<?> ctx) {
ctx.visit(N_ARRAY_AGG).sql('(');
acceptArguments1(ctx, new QueryPartList<>(Arrays.asList(arguments.get(0))));
if (!Tools.isEmpty(withinGroupOrderBy))
ctx.sql(' ').visit(K_ORDER_BY).sql(' ')
.visit(withinGroupOrderBy);
acceptOrderBy(ctx);
ctx.sql(')');
acceptFilterClause(ctx);

View File

@ -81,11 +81,6 @@ package org.jooq.impl;

View File

@ -59,6 +59,7 @@ import static org.jooq.SQLDialect.HSQLDB;
import static org.jooq.SQLDialect.MARIADB;
// ...
// ...
// ...
import static org.jooq.SQLDialect.MYSQL;
// ...
// ...
@ -231,6 +232,7 @@ import org.jooq.InsertValuesStep8;
import org.jooq.InsertValuesStep9;
import org.jooq.InsertValuesStepN;
import org.jooq.JSON;
import org.jooq.JSONArrayAggOrderByStep;
import org.jooq.JSONB;
import org.jooq.JSONEntry;
import org.jooq.JSONObjectNullStep;
@ -18191,6 +18193,22 @@ public class DSL {
return new JSONObject<>(JSONB, entries);
}
/**
* The JSON array aggregate function
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static JSONArrayAggOrderByStep<JSON> jsonArrayAgg(Field<?> value) {
return new JSONArrayAgg<JSON>(JSON, value);
}
/**
* The JSON array aggregate function
*/
@Support({ H2, MARIADB, MYSQL, POSTGRES })
public static JSONArrayAggOrderByStep<JSONB> jsonbArrayAgg(Field<?> value) {
return new JSONArrayAgg<JSONB>(JSONB, value);
}
// -------------------------------------------------------------------------
// XXX Aggregate functions
// -------------------------------------------------------------------------

View File

@ -0,0 +1,131 @@
/*
* 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.impl;
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.Names.N_JSONB_AGG;
import static org.jooq.impl.Names.N_JSON_AGG;
import static org.jooq.impl.Names.N_JSON_ARRAYAGG;
import static org.jooq.impl.SQLDataType.JSON;
import java.util.Collection;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.JSONArrayAggOrderByStep;
import org.jooq.OrderField;
/**
* The JSON array constructor.
*
* @author Lukas Eder
*/
final class JSONArrayAgg<J>
extends AbstractAggregateFunction<J>
implements JSONArrayAggOrderByStep<J> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1772007627336725780L;
private JSONNullClause nullClause;
JSONArrayAgg(DataType<J> type, Field<?> arg) {
super(false, N_JSON_ARRAYAGG, type, arg);
}
@Override
public void accept(Context<?> ctx) {
switch (ctx.family()) {
case POSTGRES:
if (nullClause == ABSENT_ON_NULL)
ctx.visit(unquotedName("json_strip_nulls")).sql('(');
ctx.visit(getDataType() == JSON ? N_JSON_AGG : N_JSONB_AGG).sql('(');
ctx.visit(arguments.get(0));
acceptOrderBy(ctx);
ctx.sql(')');
if (nullClause == ABSENT_ON_NULL)
ctx.sql(')');
break;
default:
ctx.visit(N_JSON_ARRAYAGG).sql('(');
ctx.visit(arguments.get(0));
acceptOrderBy(ctx);
acceptJSONNullClause(ctx, nullClause);
ctx.sql(')');
break;
}
}
@Override
public final JSONArrayAgg<J> nullOnNull() {
nullClause = NULL_ON_NULL;
return this;
}
@Override
public final JSONArrayAgg<J> absentOnNull() {
nullClause = ABSENT_ON_NULL;
return this;
}
@Override
public final JSONArrayAgg<J> orderBy(OrderField<?>... fields) {
return (JSONArrayAgg<J>) super.orderBy(fields);
}
@Override
public final JSONArrayAgg<J> orderBy(Collection<? extends OrderField<?>> fields) {
return (JSONArrayAgg<J>) super.orderBy(fields);
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.impl;
enum JSONNullClause {
NULL_ON_NULL, ABSENT_ON_NULL
}

View File

@ -47,8 +47,8 @@ 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.JSONNullClause.ABSENT_ON_NULL;
import static org.jooq.impl.JSONNullClause.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;
@ -83,13 +83,13 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
static final Set<SQLDialect> NO_SUPPORT_ABSENT_ON_NULL = SQLDialect.supportedBy(MARIADB, MYSQL);
private final QueryPartList<JSONEntry<?>> args;
private final NullClause nullClause;
private final JSONNullClause nullClause;
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> args) {
this(type, args, null);
}
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> args, NullClause nullClause) {
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> args, JSONNullClause nullClause) {
super(N_JSON_OBJECT, type);
this.args = new QueryPartList<>(args);
@ -110,16 +110,12 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
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) {
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
@ -127,12 +123,12 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
case POSTGRES:
if (nullClause == NullClause.ABSENT_ON_NULL)
if (nullClause == ABSENT_ON_NULL)
ctx.visit(unquotedName("json_strip_nulls")).sql('(');
ctx.visit(unquotedName("json_build_object")).sql('(').visit(args).sql(')');
if (nullClause == NullClause.ABSENT_ON_NULL)
if (nullClause == ABSENT_ON_NULL)
ctx.sql(')');
break;
@ -190,14 +186,18 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
else 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);
else
acceptJSONNullClause(ctx, nullClause);
ctx.sql(')');
break;
}
}
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);
}
}

View File

@ -58,7 +58,6 @@ import static org.jooq.impl.Keywords.F_XMLSERIALIZE;
import static org.jooq.impl.Keywords.F_XMLTEXT;
import static org.jooq.impl.Keywords.K_AS;
import static org.jooq.impl.Keywords.K_DISTINCT;
import static org.jooq.impl.Keywords.K_ORDER_BY;
import static org.jooq.impl.Keywords.K_SEPARATOR;
import static org.jooq.impl.Names.N_GROUP_CONCAT;
import static org.jooq.impl.Names.N_LIST;
@ -129,10 +128,7 @@ final class ListAgg extends DefaultAggregateFunction<String> {
private final void acceptGroupConcat(Context<?> ctx) {
ctx.visit(N_GROUP_CONCAT).sql('(');
acceptArguments1(ctx, new QueryPartList<>(Arrays.asList(arguments.get(0))));
if (!Tools.isEmpty(withinGroupOrderBy))
ctx.sql(' ').visit(K_ORDER_BY).sql(' ')
.visit(withinGroupOrderBy);
acceptOrderBy(ctx);
if (arguments.size() > 1)
if (ctx.family() == SQLITE)
@ -173,10 +169,7 @@ final class ListAgg extends DefaultAggregateFunction<String> {
else
ctx.sql(", ''");
if (!Tools.isEmpty(withinGroupOrderBy))
ctx.sql(' ').visit(K_ORDER_BY).sql(' ')
.visit(withinGroupOrderBy);
acceptOrderBy(ctx);
ctx.sql(')');
}
@ -221,9 +214,6 @@ final class ListAgg extends DefaultAggregateFunction<String> {

View File

@ -84,8 +84,11 @@ final class Names {
static final Name N_GROUP_CONCAT = DSL.unquotedName("group_concat");
static final Name N_IIF = DSL.unquotedName("iif");
static final Name N_JOIN = DSL.unquotedName("join");
static final Name N_JSON_AGG = DSL.unquotedName("json_agg");
static final Name N_JSON_ARRAY = DSL.unquotedName("json_array");
static final Name N_JSON_ARRAYAGG = DSL.unquotedName("json_arrayagg");
static final Name N_JSON_OBJECT = DSL.unquotedName("json_object");
static final Name N_JSONB_AGG = DSL.unquotedName("jsonb_agg");
static final Name N_LEFT = DSL.unquotedName("left");
static final Name N_LIST = DSL.unquotedName("list");
static final Name N_LISTAGG = DSL.unquotedName("listagg");

View File

@ -504,7 +504,6 @@ import org.jooq.conf.RenderNameCase;
import org.jooq.conf.RenderQuotedNames;
import org.jooq.conf.Settings;
import org.jooq.conf.SettingsTools;
import org.jooq.impl.JSONObject.NullClause;
import org.jooq.tools.StringUtils;
import org.jooq.tools.reflect.Reflect;
import org.jooq.types.DayToSecond;
@ -6682,7 +6681,7 @@ final class ParserImpl implements Parser {
return DSL.jsonObject();
List<JSONEntry<?>> result = new ArrayList<>();
NullClause nullClause = parseJSONObjectNullClauseIf(ctx);
JSONNullClause nullClause = parseJSONObjectNullClauseIf(ctx);
if (nullClause == null) {
do {
@ -6695,9 +6694,9 @@ final class ParserImpl implements Parser {
parse(ctx, ')');
JSONObjectNullStep<JSON> o = DSL.jsonObject(result);
return nullClause == NullClause.NULL_ON_NULL
return nullClause == JSONNullClause.NULL_ON_NULL
? o.nullOnNull()
: nullClause == NullClause.ABSENT_ON_NULL
: nullClause == JSONNullClause.ABSENT_ON_NULL
? o.absentOnNull()
: o;
}
@ -6705,11 +6704,11 @@ final class ParserImpl implements Parser {
return null;
}
private static final NullClause parseJSONObjectNullClauseIf(ParserContext ctx) {
private static final JSONNullClause parseJSONObjectNullClauseIf(ParserContext ctx) {
if (parseKeywordIf(ctx, "NULL ON NULL"))
return NullClause.NULL_ON_NULL;
return JSONNullClause.NULL_ON_NULL;
else if (parseKeywordIf(ctx, "ABSENT ON NULL"))
return NullClause.ABSENT_ON_NULL;
return JSONNullClause.ABSENT_ON_NULL;
else
return null;
}