Skip to content

Commit 46f3679

Browse files
committed
[feat] JSON_OBJECT support for AllColumns and AllTableColumns
1 parent a87f797 commit 46f3679

File tree

6 files changed

+240
-119
lines changed

6 files changed

+240
-119
lines changed

src/main/java/net/sf/jsqlparser/expression/JsonFunction.java

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1616

1717
/**
18+
* Represents a JSON-Function.<br>
19+
* Currently supported are the types in {@link JsonFunctionType}.<br>
20+
* <br>
21+
* For JSON_OBJECT and JSON_OBJECTAGG the parameters are available from {@link #getKeyValuePairs()}<br>
22+
* <br>
23+
* For JSON_ARRAY and JSON_ARRAYAGG the parameters are availble from {@link #getExpressions()}.<br>
24+
*
1825
* @author <a href="mailto:[email protected]">Andreas Reichel</a>
1926
*/
20-
2127
public class JsonFunction extends ASTNodeAccessImpl implements Expression {
2228
private final ArrayList<JsonKeyValuePair> keyValuePairs = new ArrayList<>();
2329
private final ArrayList<JsonFunctionExpression> expressions = new ArrayList<>();
@@ -27,10 +33,21 @@ public class JsonFunction extends ASTNodeAccessImpl implements Expression {
2733

2834
private boolean isStrict = false;
2935

36+
/**
37+
* Returns the Parameters of an JSON_OBJECT or JSON_OBJECTAGG<br>
38+
* The KeyValuePairs may not have both key and value set, in some cases only the Key is set.
39+
*
40+
* @return A List of KeyValuePairs, never NULL
41+
*/
3042
public ArrayList<JsonKeyValuePair> getKeyValuePairs() {
3143
return keyValuePairs;
3244
}
3345

46+
/**
47+
* Returns the parameters of JSON_ARRAY or JSON_ARRAYAGG<br>
48+
*
49+
* @return A List of {@link JsonFunctionExpression}s, never NULL
50+
*/
3451
public ArrayList<JsonFunctionExpression> getExpressions() {
3552
return expressions;
3653
}
@@ -138,13 +155,9 @@ public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
138155
public StringBuilder append(StringBuilder builder) {
139156
switch (functionType) {
140157
case OBJECT:
141-
appendObject(builder);
142-
break;
143158
case POSTGRES_OBJECT:
144-
appendPostgresObject(builder);
145-
break;
146159
case MYSQL_OBJECT:
147-
appendMySqlObject(builder);
160+
appendObject(builder);
148161
break;
149162
case ARRAY:
150163
appendArray(builder);
@@ -163,14 +176,14 @@ public StringBuilder appendObject(StringBuilder builder) {
163176
if (i > 0) {
164177
builder.append(", ");
165178
}
166-
if (keyValuePair.isUsingValueKeyword()) {
167-
if (keyValuePair.isUsingKeyKeyword()) {
168-
builder.append("KEY ");
169-
}
170-
builder.append(keyValuePair.getKey()).append(" VALUE ")
171-
.append(keyValuePair.getValue());
172-
} else {
173-
builder.append(keyValuePair.getKey()).append(":").append(keyValuePair.getValue());
179+
if (keyValuePair.isUsingKeyKeyword() && keyValuePair.getSeparator() == JsonKeyValuePairSeparator.VALUE) {
180+
builder.append("KEY ");
181+
}
182+
builder.append(keyValuePair.getKey());
183+
184+
if (keyValuePair.getValue() != null) {
185+
builder.append(keyValuePair.getSeparator().getSeparatorString());
186+
builder.append(keyValuePair.getValue());
174187
}
175188

176189
if (keyValuePair.isUsingFormatJson()) {
@@ -220,44 +233,6 @@ private void appendUniqueKeys(StringBuilder builder) {
220233
}
221234
}
222235

223-
224-
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
225-
public StringBuilder appendPostgresObject(StringBuilder builder) {
226-
builder.append("JSON_OBJECT( ");
227-
for (JsonKeyValuePair keyValuePair : keyValuePairs) {
228-
builder.append(keyValuePair.getKey());
229-
if (keyValuePair.getValue() != null) {
230-
builder.append(", ").append(keyValuePair.getValue());
231-
}
232-
}
233-
234-
appendOnNullType(builder);
235-
if (isStrict) {
236-
builder.append(" STRICT");
237-
}
238-
appendUniqueKeys(builder);
239-
240-
builder.append(" ) ");
241-
242-
return builder;
243-
}
244-
245-
public StringBuilder appendMySqlObject(StringBuilder builder) {
246-
builder.append("JSON_OBJECT( ");
247-
int i = 0;
248-
for (JsonKeyValuePair keyValuePair : keyValuePairs) {
249-
if (i > 0) {
250-
builder.append(", ");
251-
}
252-
builder.append(keyValuePair.getKey());
253-
builder.append(", ").append(keyValuePair.getValue());
254-
i++;
255-
}
256-
builder.append(" ) ");
257-
258-
return builder;
259-
}
260-
261236
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
262237
public StringBuilder appendArray(StringBuilder builder) {
263238
builder.append("JSON_ARRAY( ");
@@ -271,18 +246,7 @@ public StringBuilder appendArray(StringBuilder builder) {
271246
i++;
272247
}
273248

274-
if (onNullType != null) {
275-
switch (onNullType) {
276-
case NULL:
277-
builder.append(" NULL ON NULL ");
278-
break;
279-
case ABSENT:
280-
builder.append(" ABSENT ON NULL ");
281-
break;
282-
default:
283-
// "ON NULL" was omitted
284-
}
285-
}
249+
appendOnNullType(builder);
286250
builder.append(") ");
287251

288252
return builder;

src/main/java/net/sf/jsqlparser/expression/JsonFunctionType.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@
1414
* @author <a href="mailto:[email protected]">Andreas Reichel</a>
1515
*/
1616
public enum JsonFunctionType {
17-
OBJECT, ARRAY, POSTGRES_OBJECT, MYSQL_OBJECT;
17+
OBJECT,
18+
ARRAY,
19+
20+
/**
21+
* Not used anymore
22+
*/
23+
@Deprecated
24+
POSTGRES_OBJECT,
25+
26+
/**
27+
* Not used anymore
28+
*/
29+
@Deprecated
30+
MYSQL_OBJECT;
1831

1932
public static JsonFunctionType from(String type) {
2033
return Enum.valueOf(JsonFunctionType.class, type.toUpperCase());

src/main/java/net/sf/jsqlparser/expression/JsonKeyValuePair.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
public class JsonKeyValuePair implements Serializable {
2121
private final Object key;
2222
private final Object value;
23-
private boolean usingKeyKeyword = false;
24-
private boolean usingValueKeyword = false;
23+
private boolean usingKeyKeyword;
24+
private JsonKeyValuePairSeparator separator;
2525
private boolean usingFormatJson = false;
2626

27+
public JsonKeyValuePair(Object key, Object value, boolean usingKeyKeyword, boolean usingValueKeyword) {
28+
this(key, value, usingKeyKeyword, usingValueKeyword ? JsonKeyValuePairSeparator.VALUE : JsonKeyValuePairSeparator.COLON);
29+
}
30+
2731
public JsonKeyValuePair(Object key, Object value, boolean usingKeyKeyword,
28-
boolean usingValueKeyword) {
32+
JsonKeyValuePairSeparator separator) {
2933
this.key = Objects.requireNonNull(key, "The KEY of the Pair must not be null");
3034
this.value = value;
3135
this.usingKeyKeyword = usingKeyKeyword;
32-
this.usingValueKeyword = usingValueKeyword;
36+
this.separator = separator;
3337
}
3438

3539
public boolean isUsingKeyKeyword() {
@@ -45,19 +49,44 @@ public JsonKeyValuePair withUsingKeyKeyword(boolean usingKeyKeyword) {
4549
return this;
4650
}
4751

52+
/**
53+
* Use {@link #getSeparator()}
54+
*/
55+
@Deprecated
4856
public boolean isUsingValueKeyword() {
49-
return usingValueKeyword;
57+
return separator == JsonKeyValuePairSeparator.VALUE;
5058
}
5159

60+
/**
61+
* Use {@link #setSeparator(JsonKeyValuePairSeparator)}
62+
*/
63+
@Deprecated
5264
public void setUsingValueKeyword(boolean usingValueKeyword) {
53-
this.usingValueKeyword = usingValueKeyword;
65+
separator = usingValueKeyword ? JsonKeyValuePairSeparator.VALUE : JsonKeyValuePairSeparator.COLON;
5466
}
5567

68+
/**
69+
* Use {@link #withSeparator(JsonKeyValuePairSeparator)}
70+
*/
71+
@Deprecated
5672
public JsonKeyValuePair withUsingValueKeyword(boolean usingValueKeyword) {
5773
this.setUsingValueKeyword(usingValueKeyword);
5874
return this;
5975
}
6076

77+
public JsonKeyValuePairSeparator getSeparator() {
78+
return separator;
79+
}
80+
81+
public void setSeparator(JsonKeyValuePairSeparator separator) {
82+
this.separator = separator;
83+
}
84+
85+
public JsonKeyValuePair withSeparator(JsonKeyValuePairSeparator separator) {
86+
this.setSeparator(separator);
87+
return this;
88+
}
89+
6190
public boolean isUsingFormatJson() {
6291
return usingFormatJson;
6392
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
/**
4+
* Describes the string used to separate the key from the value.
5+
*/
6+
public enum JsonKeyValuePairSeparator {
7+
VALUE(" VALUE "),
8+
COLON(":"),
9+
10+
// Used in MySQL dialect
11+
COMMA(","),
12+
13+
// Is used in case they KeyValuePair has only a key and no value
14+
NOT_USED("");
15+
16+
private final String separator;
17+
18+
JsonKeyValuePairSeparator(String separator) {
19+
this.separator = separator;
20+
}
21+
22+
public String getSeparatorString() {
23+
return separator;
24+
}
25+
}

0 commit comments

Comments
 (0)