diff --git a/jOOQ/src/main/java/org/jooq/JSONEntry.java b/jOOQ/src/main/java/org/jooq/JSONEntry.java new file mode 100644 index 0000000000..56c0d95b3d --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/JSONEntry.java @@ -0,0 +1,54 @@ +/* + * 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; + +/** + * A JSON entry for JSON objects. + */ +public interface JSONEntry extends QueryPart { + + /** + * The JSON entry key. + */ + Field key(); + + /** + * The JSON entry value. + */ + Field value(); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index a1980c8a3d..959c54408c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -229,6 +229,7 @@ import org.jooq.InsertValuesStep9; import org.jooq.InsertValuesStepN; import org.jooq.JSON; import org.jooq.JSONB; +import org.jooq.JSONEntry; import org.jooq.Keyword; // ... // ... @@ -17699,7 +17700,7 @@ public class DSL { */ @Support({ MYSQL, POSTGRES }) public static Field jsonArray(Collection> fields) { - return new JSONArray(JSON, fields); + return new JSONArray<>(JSON, fields); } /** @@ -17715,7 +17716,55 @@ public class DSL { */ @Support({ MYSQL, POSTGRES }) public static Field jsonbArray(Collection> fields) { - return new JSONArray(JSONB, fields); + return new JSONArray<>(JSONB, fields); + } + + /** + * A constructor for JSON entries to be used with {@link #jsonObject(JSONEntry...)}. + */ + @Support({ MYSQL, POSTGRES }) + public static JSONEntry jsonEntry(Field key, Field value) { + return new JSONEntryImpl<>(key, value); + } + + /** + * The JSON object constructor. + */ + @Support({ MYSQL, POSTGRES }) + public static Field jsonObject(Field key, Field value) { + return jsonObject(jsonEntry(key, value)); + } + + /** + * The JSON object constructor. + */ + @Support({ MYSQL, POSTGRES }) + public static Field jsonObject(JSONEntry... entries) { + return jsonObject(Arrays.asList(entries)); + } + + /** + * The JSON object constructor. + */ + @Support({ MYSQL, POSTGRES }) + public static Field jsonObject(Collection> entries) { + return new JSONObject<>(JSON, entries); + } + + /** + * The JSONB object constructor. + */ + @Support({ MYSQL, POSTGRES }) + public static Field jsonbObject(JSONEntry... entries) { + return jsonbObject(Arrays.asList(entries)); + } + + /** + * The JSONB object constructor. + */ + @Support({ MYSQL, POSTGRES }) + public static Field jsonbObject(Collection> entries) { + return new JSONObject<>(JSONB, entries); } // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/JSONArray.java b/jOOQ/src/main/java/org/jooq/impl/JSONArray.java new file mode 100644 index 0000000000..2925a6782f --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/JSONArray.java @@ -0,0 +1,81 @@ +/* + * 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.Keywords.K_JSON_ARRAY; + +import java.util.Collection; + +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; + + +/** + * The JSON array constructor. + * + * @author Lukas Eder + */ +final class JSONArray extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1772007627336725780L; + private final QueryPartList> args; + + JSONArray(DataType type, Collection> args) { + super(DSL.name("json_array"), type); + + this.args = new QueryPartList<>(args); + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + case POSTGRES: + ctx.visit(unquotedName("json_build_array")).sql('(').visit(args).sql(')'); + break; + + default: + ctx.visit(K_JSON_ARRAY).sql('(').visit(args).sql(')'); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/JSONEntryImpl.java b/jOOQ/src/main/java/org/jooq/impl/JSONEntryImpl.java new file mode 100644 index 0000000000..f33353c8e7 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/JSONEntryImpl.java @@ -0,0 +1,90 @@ +/* + * 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.Keywords.K_KEY; +import static org.jooq.impl.Keywords.K_VALUE; + +import org.jooq.Context; +import org.jooq.Field; +import org.jooq.JSONEntry; + + +/** + * The JSON object entry. + * + * @author Lukas Eder + */ +final class JSONEntryImpl extends AbstractQueryPart implements JSONEntry { + + /** + * Generated UID + */ + private static final long serialVersionUID = 6734093632906565848L; + private final Field key; + private final Field value; + + JSONEntryImpl(Field key, Field value) { + this.key = key; + this.value = value; + } + + @Override + public final Field key() { + return key; + } + + @Override + public final Field value() { + return value; + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + case MYSQL: + case POSTGRES: + ctx.visit(key).sql(", ").visit(value); + break; + + default: + ctx.visit(K_KEY).sql(' ').visit(key).sql(' ').visit(K_VALUE).sql(' ').visit(value); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/JSONObject.java b/jOOQ/src/main/java/org/jooq/impl/JSONObject.java new file mode 100644 index 0000000000..9eb219339c --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/JSONObject.java @@ -0,0 +1,124 @@ +/* + * 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.asterisk; +import static org.jooq.impl.DSL.inline; +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.Keywords.K_AUTO; +import static org.jooq.impl.Keywords.K_FOR; +import static org.jooq.impl.Keywords.K_JSON; +import static org.jooq.impl.Keywords.K_JSON_OBJECT; +import static org.jooq.impl.Keywords.K_WITHOUT_ARRAY_WRAPPER; + +import java.util.Collection; + +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.JSONEntry; +import org.jooq.Name; + + +/** + * The JSON array constructor. + * + * @author Lukas Eder + */ +final class JSONObject extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1772007627336725780L; + private final QueryPartList> args; + + JSONObject(DataType type, Collection> args) { + super(DSL.name("json_array"), type); + + this.args = new QueryPartList<>(args); + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + case POSTGRES: + ctx.visit(unquotedName("json_build_object")).sql('(').visit(args).sql(')'); + break; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + default: + ctx.visit(K_JSON_OBJECT).sql('(').visit(args).sql(')'); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index 593913e1af..195b7a7af0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -61,6 +61,7 @@ final class Keywords { static final Keyword K_AS = keyword("as"); static final Keyword K_AS_OF = keyword("as of"); static final Keyword K_ATOMIC = keyword("atomic"); + static final Keyword K_AUTO = keyword("auto"); static final Keyword K_AUTO_INCREMENT = keyword("auto_increment"); static final Keyword K_AUTOINCREMENT = keyword("autoincrement"); static final Keyword K_BEGIN = keyword("begin"); @@ -193,6 +194,7 @@ final class Keywords { static final Keyword K_IS_NOT_NULL = keyword("is not null"); static final Keyword K_IS_NULL = keyword("is null"); static final Keyword K_ITERATE = keyword("iterate"); + static final Keyword K_JSON = keyword("json"); static final Keyword K_JSON_ARRAY = keyword("json_array"); static final Keyword K_JSON_OBJECT = keyword("json_object"); static final Keyword K_KEEP = keyword("keep"); @@ -341,6 +343,7 @@ final class Keywords { static final Keyword K_USE = keyword("use"); static final Keyword K_USING = keyword("using"); static final Keyword K_USING_INDEX = keyword("using index"); + static final Keyword K_VALUE = keyword("value"); static final Keyword K_VALUES = keyword("values"); static final Keyword K_VARCHAR = keyword("varchar"); static final Keyword K_VERSIONS_BETWEEN = keyword("versions between"); @@ -361,6 +364,7 @@ final class Keywords { static final Keyword K_WITH_ROLLUP = keyword("with rollup"); static final Keyword K_WITH_TIES = keyword("with ties"); static final Keyword K_WITHIN_GROUP = keyword("within group"); + static final Keyword K_WITHOUT_ARRAY_WRAPPER = keyword("without_array_wrapper"); static final Keyword K_XMLTABLE = keyword("xmltable"); static final Keyword K_YEAR = keyword("year"); static final Keyword K_YEAR_MONTH = keyword("year_month"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index b5e1226fca..08cf09bb7f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -147,6 +147,7 @@ import static org.jooq.impl.DSL.iif; 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.keyword; import static org.jooq.impl.DSL.lag; import static org.jooq.impl.DSL.lastValue; @@ -406,6 +407,7 @@ import org.jooq.InsertOnDuplicateStep; import org.jooq.InsertReturningStep; import org.jooq.InsertSetStep; import org.jooq.InsertValuesStepN; +import org.jooq.JSONEntry; import org.jooq.JoinType; import org.jooq.Keyword; // ... @@ -5699,6 +5701,8 @@ final class ParserImpl implements Parser { if (J.is(type)) if ((field = parseFieldJSONArrayConstructorIf(ctx)) != null) return field; + else if ((field = parseFieldJSONObjectConstructorIf(ctx)) != null) + return field; break; @@ -6253,6 +6257,40 @@ final class ParserImpl implements Parser { return null; } + private static final Field parseFieldJSONObjectConstructorIf(ParserContext ctx) { + if (parseKeywordIf(ctx, "JSON_OBJECT")) { + parse(ctx, '('); + if (parseIf(ctx, ')')) + return DSL.jsonObject(); + + List> result = new ArrayList<>(); + do { + result.add(parseJSONEntry(ctx)); + } + while (parseIf(ctx, ',')); + parse(ctx, ')'); + + return DSL.jsonObject(result); + } + + return null; + } + + private static final JSONEntry parseJSONEntry(ParserContext ctx) { + boolean valueRequired = parseKeywordIf(ctx, "KEY"); + + Field key = (Field) parseField(ctx, Type.S); + if (parseKeywordIf(ctx, "VALUE")) + ; + else if (valueRequired) + throw ctx.expected("VALUE"); + else + parse(ctx, ','); + + Field value = parseField(ctx); + return jsonEntry(key, value); + } + private static final Field parseArrayValueConstructorIf(ParserContext ctx) { if (parseKeywordIf(ctx, "ARRAY")) { parse(ctx, '[');