[jOOQ/jOOQ#8950] Add support for JSON_VALUE()

This commit is contained in:
Lukas Eder 2020-03-25 10:59:43 +01:00
parent 5286a415d7
commit 9ec5cc07bf
7 changed files with 467 additions and 1 deletions

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 org.jooq.impl.DSL;
/**
* A step in the construction of {@link DSL#jsonValue(Field, Field)} or
* {@link DSL#jsonbValue(Field, Field)} functions where the
* <code>ON EMPTY</code> and <code>ON ERROR</code> clauses can be defined for
* default values.
*
* @author Lukas Eder
*/
public interface JSONValueDefaultStep<J> {
}

View File

@ -0,0 +1,91 @@
/*
* 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.jooq.impl.DSL;
/**
* A step in the construction of {@link DSL#jsonValue(Field, Field)} or
* {@link DSL#jsonbValue(Field, Field)} functions where the
* <code>ON EMPTY</code> and <code>ON ERROR</code> clauses can be defined.
*
* @author Lukas Eder
*/
public interface JSONValueOnStep<J> extends Field<J> {
}

View File

@ -238,6 +238,7 @@ import org.jooq.JSONB;
import org.jooq.JSONEntry;
import org.jooq.JSONObjectAggNullStep;
import org.jooq.JSONObjectNullStep;
import org.jooq.JSONValueOnStep;
import org.jooq.Keyword;
// ...
// ...
@ -18364,6 +18365,38 @@ public class DSL {
// XXX JSON functions
// -------------------------------------------------------------------------
/**
* The JSON value extractor function.
*/
@Support({ MARIADB })
public static JSONValueOnStep<JSON> jsonValue(Field<?> json, String path) {
return jsonValue(json, Tools.field(path));
}
/**
* The JSON value extractor function.
*/
@Support({ MARIADB })
public static JSONValueOnStep<JSON> jsonValue(Field<?> json, Field<String> path) {
return new JSONValue<>(SQLDataType.JSON, json, path);
}
/**
* The JSON value extractor function.
*/
@Support({ MARIADB })
public static JSONValueOnStep<JSONB> jsonbValue(Field<?> json, String path) {
return jsonbValue(json, Tools.field(path));
}
/**
* The JSON value extractor function.
*/
@Support({ MARIADB })
public static JSONValueOnStep<JSONB> jsonbValue(Field<?> json, Field<String> path) {
return new JSONValue<>(SQLDataType.JSONB, json, path);
}
/**
* The JSON array constructor.
*/

View File

@ -0,0 +1,204 @@
/*
* 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.JSONValue.Behaviour.DEFAULT;
import static org.jooq.impl.JSONValue.Behaviour.ERROR;
import static org.jooq.impl.JSONValue.Behaviour.NULL;
import static org.jooq.impl.Keywords.K_EMPTY;
import static org.jooq.impl.Keywords.K_ERROR;
import static org.jooq.impl.Keywords.K_ON;
import static org.jooq.impl.Names.N_JSON_VALUE;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.JSONValueDefaultStep;
import org.jooq.JSONValueOnStep;
import org.jooq.Keyword;
/**
* The JSON value constructor.
*
* @author Lukas Eder
*/
final class JSONValue<J>
extends AbstractField<J>
implements
JSONValueOnStep<J>,
JSONValueDefaultStep<J> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1772007627336725780L;
private final Field<?> json;
private final Field<String> path;
JSONValue(DataType<J> type, Field<?> json, Field<String> path) {
this(type, json, path, null, null, null, null, null);
}
private JSONValue(
DataType<J> type,
Field<?> json,
Field<String> path,
Behaviour onError,
Field<?> onErrorDefault,
Behaviour onEmpty,
Field<?> onEmptyDefault,
Field<?> default_
) {
super(N_JSON_VALUE, type);
this.json = json;
this.path = path;
}
// -------------------------------------------------------------------------
// XXX: DSL API
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(N_JSON_VALUE).sql('(').visit(json).sql(", ").visit(path);
ctx.sql(')');
}
enum Behaviour {
ERROR, NULL, DEFAULT;
final Keyword keyword;
Behaviour() {
this.keyword = DSL.keyword(name().toLowerCase());
}
}
}

View File

@ -144,6 +144,7 @@ final class Keywords {
static final Keyword K_ELSE = keyword("else");
static final Keyword K_ELSEIF = keyword("elseif");
static final Keyword K_ELSIF = keyword("elsif");
static final Keyword K_EMPTY = keyword("empty");
static final Keyword K_ENABLE = keyword("enable");
static final Keyword K_END = keyword("end");
static final Keyword K_END_CATCH = keyword("end catch");
@ -152,6 +153,7 @@ final class Keywords {
static final Keyword K_END_TRY = keyword("end try");
static final Keyword K_ENFORCED = keyword("enforced");
static final Keyword K_ENUM = keyword("enum");
static final Keyword K_ERROR = keyword("error");
static final Keyword K_ESCAPE = keyword("escape");
static final Keyword K_EXCEPT = keyword("except");
static final Keyword K_EXCEPTION = keyword("exception");

View File

@ -94,6 +94,7 @@ final class Names {
static final Name N_JSON_OBJECT = DSL.unquotedName("json_object");
static final Name N_JSON_OBJECT_AGG = DSL.unquotedName("json_object_agg");
static final Name N_JSON_OBJECTAGG = DSL.unquotedName("json_objectagg");
static final Name N_JSON_VALUE = DSL.unquotedName("json_value");
static final Name N_JSONB_AGG = DSL.unquotedName("jsonb_agg");
static final Name N_JSONB_OBJECT = DSL.unquotedName("jsonb_object");
static final Name N_JSONB_OBJECT_AGG = DSL.unquotedName("jsonb_object_agg");

View File

@ -154,6 +154,7 @@ import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.isnull;
import static org.jooq.impl.DSL.isoDayOfWeek;
import static org.jooq.impl.DSL.jsonEntry;
import static org.jooq.impl.DSL.jsonValue;
import static org.jooq.impl.DSL.keyword;
import static org.jooq.impl.DSL.lag;
import static org.jooq.impl.DSL.lastValue;
@ -445,6 +446,8 @@ import org.jooq.JSONArrayNullStep;
import org.jooq.JSONEntry;
import org.jooq.JSONObjectAggNullStep;
import org.jooq.JSONObjectNullStep;
import org.jooq.JSONValueDefaultStep;
import org.jooq.JSONValueOnStep;
import org.jooq.JoinType;
import org.jooq.Keyword;
// ...
@ -6188,6 +6191,8 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseFieldJSONObjectAggIf(ctx)) != null)
return field;
else if ((field = parseFieldJSONValueIf(ctx)) != null)
return field;
break;
@ -6960,6 +6965,65 @@ final class ParserImpl implements Parser {
return null;
}
private static final Field<?> parseFieldJSONValueIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "JSON_VALUE")) {
parse(ctx, '(');
Field<?> json = parseField(ctx);
parse(ctx, ',');
Field<String> path = (Field<String>) parseField(ctx);
JSONValueOnStep<?> s1 = jsonValue(json, path);
JSONValue.Behaviour behaviour = parseBehaviourIf(ctx);
parse(ctx, ')');
return s1;
}
return null;
}
private static final JSONValue.Behaviour parseBehaviourIf(ParserContext ctx) {
if (parseKeywordIf(ctx, "ERROR") && ctx.requireProEdition())
return JSONValue.Behaviour.ERROR;
else if (parseKeywordIf(ctx, "NULL") && ctx.requireProEdition())
return JSONValue.Behaviour.NULL;
else if (parseKeywordIf(ctx, "DEFAULT") && ctx.requireProEdition())
return JSONValue.Behaviour.DEFAULT;
else
return null;
}
private static final Field<?> parseFieldJSONArrayConstructorIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "JSON_ARRAY")) {
parse(ctx, '(');
@ -7011,7 +7075,7 @@ final class ParserImpl implements Parser {
}
private static final Field<?> parseFieldJSONObjectConstructorIf(ParserContext ctx) {
if (parseKeywordIf(ctx, "JSON_OBJECT")) {
if (parseFunctionNameIf(ctx, "JSON_OBJECT")) {
parse(ctx, '(');
if (parseIf(ctx, ')'))
return DSL.jsonObject();