diff --git a/jOOQ/src/main/java/org/jooq/JSONValueDefaultStep.java b/jOOQ/src/main/java/org/jooq/JSONValueDefaultStep.java new file mode 100644 index 0000000000..3920608abc --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/JSONValueDefaultStep.java @@ -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 + * ON EMPTY and ON ERROR clauses can be defined for + * default values. + * + * @author Lukas Eder + */ +public interface JSONValueDefaultStep { + + + + + + + + + + + + + + + + + + +} diff --git a/jOOQ/src/main/java/org/jooq/JSONValueOnStep.java b/jOOQ/src/main/java/org/jooq/JSONValueOnStep.java new file mode 100644 index 0000000000..fb63f957ac --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/JSONValueOnStep.java @@ -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 + * ON EMPTY and ON ERROR clauses can be defined. + * + * @author Lukas Eder + */ +public interface JSONValueOnStep extends Field { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index acb050499d..193b27e994 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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 jsonValue(Field json, String path) { + return jsonValue(json, Tools.field(path)); + } + + /** + * The JSON value extractor function. + */ + @Support({ MARIADB }) + public static JSONValueOnStep jsonValue(Field json, Field path) { + return new JSONValue<>(SQLDataType.JSON, json, path); + } + + /** + * The JSON value extractor function. + */ + @Support({ MARIADB }) + public static JSONValueOnStep jsonbValue(Field json, String path) { + return jsonbValue(json, Tools.field(path)); + } + + /** + * The JSON value extractor function. + */ + @Support({ MARIADB }) + public static JSONValueOnStep jsonbValue(Field json, Field path) { + return new JSONValue<>(SQLDataType.JSONB, json, path); + } + /** * The JSON array constructor. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/JSONValue.java b/jOOQ/src/main/java/org/jooq/impl/JSONValue.java new file mode 100644 index 0000000000..d43c02700f --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/JSONValue.java @@ -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 +extends AbstractField +implements + JSONValueOnStep, + JSONValueDefaultStep { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1772007627336725780L; + + private final Field json; + private final Field path; + + + + + + + + + + + + JSONValue(DataType type, Field json, Field path) { + this(type, json, path, null, null, null, null, null); + } + + private JSONValue( + DataType type, + Field json, + Field 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()); + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index c954d6f834..22fcac23cb 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -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"); diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index fe78b50b71..47f5f77871 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -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"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 10e391f789..bf044332e8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -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 path = (Field) 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();