[jOOQ/jOOQ#8944] Add support for the JSON object constructor
This commit is contained in:
parent
c751499831
commit
ee94ae341d
54
jOOQ/src/main/java/org/jooq/JSONEntry.java
Normal file
54
jOOQ/src/main/java/org/jooq/JSONEntry.java
Normal file
@ -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<T> extends QueryPart {
|
||||
|
||||
/**
|
||||
* The JSON entry key.
|
||||
*/
|
||||
Field<String> key();
|
||||
|
||||
/**
|
||||
* The JSON entry value.
|
||||
*/
|
||||
Field<T> value();
|
||||
}
|
||||
@ -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<JSON> jsonArray(Collection<? extends Field<?>> fields) {
|
||||
return new JSONArray(JSON, fields);
|
||||
return new JSONArray<>(JSON, fields);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -17715,7 +17716,55 @@ public class DSL {
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSONB> jsonbArray(Collection<? extends Field<?>> 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 <T> JSONEntry<T> jsonEntry(Field<String> key, Field<T> value) {
|
||||
return new JSONEntryImpl<>(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSON object constructor.
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSON> jsonObject(Field<String> key, Field<?> value) {
|
||||
return jsonObject(jsonEntry(key, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSON object constructor.
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSON> jsonObject(JSONEntry<?>... entries) {
|
||||
return jsonObject(Arrays.asList(entries));
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSON object constructor.
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSON> jsonObject(Collection<? extends JSONEntry<?>> entries) {
|
||||
return new JSONObject<>(JSON, entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSONB object constructor.
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSONB> jsonbObject(JSONEntry<?>... entries) {
|
||||
return jsonbObject(Arrays.asList(entries));
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSONB object constructor.
|
||||
*/
|
||||
@Support({ MYSQL, POSTGRES })
|
||||
public static Field<JSONB> jsonbObject(Collection<? extends JSONEntry<?>> entries) {
|
||||
return new JSONObject<>(JSONB, entries);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
81
jOOQ/src/main/java/org/jooq/impl/JSONArray.java
Normal file
81
jOOQ/src/main/java/org/jooq/impl/JSONArray.java
Normal file
@ -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<J> extends AbstractField<J> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1772007627336725780L;
|
||||
private final QueryPartList<Field<?>> args;
|
||||
|
||||
JSONArray(DataType<J> type, Collection<? extends Field<?>> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
jOOQ/src/main/java/org/jooq/impl/JSONEntryImpl.java
Normal file
90
jOOQ/src/main/java/org/jooq/impl/JSONEntryImpl.java
Normal file
@ -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<T> extends AbstractQueryPart implements JSONEntry<T> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 6734093632906565848L;
|
||||
private final Field<String> key;
|
||||
private final Field<T> value;
|
||||
|
||||
JSONEntryImpl(Field<String> key, Field<T> value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Field<String> key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Field<T> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
jOOQ/src/main/java/org/jooq/impl/JSONObject.java
Normal file
124
jOOQ/src/main/java/org/jooq/impl/JSONObject.java
Normal file
@ -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<J> extends AbstractField<J> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1772007627336725780L;
|
||||
private final QueryPartList<JSONEntry<?>> args;
|
||||
|
||||
JSONObject(DataType<J> type, Collection<? extends JSONEntry<?>> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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");
|
||||
|
||||
@ -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<JSONEntry<?>> 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<String> key = (Field<String>) 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, '[');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user