Skip to content

Commit 0b85753

Browse files
committed
[feat] Initial support for JSON_TABLE
1 parent 297ef84 commit 0b85753

File tree

13 files changed

+478
-120
lines changed

13 files changed

+478
-120
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ default void visit(JsonFunction jsonFunction) {
652652
this.visit(jsonFunction, null);
653653
}
654654

655+
<S> T visit(JsonTable jsonTable, S context);
656+
657+
default void visit(JsonTable jsonTable) {
658+
this.visit(jsonTable, null);
659+
}
660+
655661
<S> T visit(ConnectByRootOperator connectByRootOperator, S context);
656662

657663
default void visit(ConnectByRootOperator connectByRootOperator) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,12 @@ public <S> T visit(JsonFunction jsonFunction, S context) {
728728
return visitExpressions(jsonFunction, context, subExpressions);
729729
}
730730

731+
@Override
732+
public <S> T visit(JsonTable jsonTable, S context) {
733+
// TODO: Implement
734+
return null;
735+
}
736+
731737
@Override
732738
public <S> T visit(ConnectByRootOperator connectByRootOperator, S context) {
733739
return connectByRootOperator.getColumn().accept(this, context);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2021 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
/*
11+
* Copyright (C) 2021 JSQLParser.
12+
*
13+
* This library is free software; you can redistribute it and/or modify it under the terms of the
14+
* GNU Lesser General Public License as published by the Free Software Foundation; either version
15+
* 2.1 of the License, or (at your option) any later version.
16+
*
17+
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
18+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License along with this library;
22+
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23+
* 02110-1301 USA
24+
*/
25+
26+
package net.sf.jsqlparser.expression;
27+
28+
/**
29+
*
30+
*/
31+
public enum JsonOnEmptyType {
32+
ERROR("ERROR"),
33+
NULL("NULL"),
34+
EMPTY("EMPTY"),
35+
EMPTY_ARRAY("EMPTY ARRAY"),
36+
EMPTY_OBJECT("EMPTY OBJECT"),
37+
TRUE("TRUE"),
38+
FALSE("FALSE");
39+
40+
private final String value;
41+
42+
JsonOnEmptyType(String value) {
43+
this.value = value;
44+
}
45+
46+
public String getValue() {
47+
return value;
48+
}
49+
50+
public static JsonOnEmptyType from(String type) {
51+
return Enum.valueOf(JsonOnEmptyType.class, type.toUpperCase());
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2021 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
/*
11+
* Copyright (C) 2021 JSQLParser.
12+
*
13+
* This library is free software; you can redistribute it and/or modify it under the terms of the
14+
* GNU Lesser General Public License as published by the Free Software Foundation; either version
15+
* 2.1 of the License, or (at your option) any later version.
16+
*
17+
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
18+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License along with this library;
22+
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23+
* 02110-1301 USA
24+
*/
25+
26+
package net.sf.jsqlparser.expression;
27+
28+
/**
29+
*
30+
*/
31+
public enum JsonOnErrorType {
32+
ERROR("ERROR"),
33+
NULL("NULL"),
34+
EMPTY("EMPTY"),
35+
EMPTY_ARRAY("EMPTY ARRAY"),
36+
EMPTY_OBJECT("EMPTY OBJECT"),
37+
TRUE("TRUE"),
38+
FALSE("FALSE");
39+
40+
private final String value;
41+
42+
JsonOnErrorType(String value) {
43+
this.value = value;
44+
}
45+
46+
public String getValue() {
47+
return value;
48+
}
49+
50+
public static JsonOnErrorType from(String type) {
51+
return Enum.valueOf(JsonOnErrorType.class, type.toUpperCase());
52+
}
53+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class JsonTable extends ASTNodeAccessImpl implements Expression {
9+
10+
private Expression expression;
11+
private boolean isFormatJson = false;
12+
13+
private String pathExpression;
14+
15+
private JsonOnErrorType onErrorType;
16+
private JsonTableType type;
17+
private JsonOnEmptyType onEmptyType;
18+
19+
private List<JsonTableColumn> jsonColumns = new ArrayList<>();
20+
21+
22+
public StringBuilder append(StringBuilder builder) {
23+
builder.append("JSON_TABLE(");
24+
builder.append(expression.toString());
25+
26+
if (isFormatJson) {
27+
builder.append(" FORMAT JSON");
28+
}
29+
30+
if (pathExpression != null) {
31+
builder.append(", ");
32+
builder.append(pathExpression);
33+
}
34+
35+
if (onErrorType != null) {
36+
builder.append(" ");
37+
builder.append(onErrorType);
38+
builder.append(" ON ERROR");
39+
}
40+
41+
if (type != null) {
42+
builder.append(" TYPE(");
43+
builder.append(type);
44+
builder.append(")");
45+
}
46+
47+
if (onEmptyType != null) {
48+
builder.append(" ");
49+
builder.append(onEmptyType);
50+
builder.append(" ON EMPTY");
51+
}
52+
53+
builder.append(" COLUMNS(");
54+
55+
for (JsonTableColumn column : jsonColumns) {
56+
column.append(builder);
57+
}
58+
59+
builder.append("))");
60+
return builder;
61+
}
62+
63+
@Override
64+
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
65+
return expressionVisitor.visit(this, context);
66+
}
67+
68+
public void setExpression(Expression expression) {
69+
this.expression = expression;
70+
}
71+
72+
public Expression getExpression() {
73+
return expression;
74+
}
75+
76+
public JsonTable withExpression(Expression expression) {
77+
setExpression(expression);
78+
return this;
79+
}
80+
81+
public void setPathExpression(String pathExpression) {
82+
this.pathExpression = pathExpression;
83+
}
84+
85+
public String getPathExpression() {
86+
return pathExpression;
87+
}
88+
89+
public JsonTable withPathExpression(String pathExpression) {
90+
setPathExpression(pathExpression);
91+
return this;
92+
}
93+
94+
public void setFormatJson(boolean usingJson) {
95+
this.isFormatJson = true;
96+
}
97+
98+
public boolean isFormatJson() {
99+
return isFormatJson;
100+
}
101+
102+
public JsonTable withFormatJson(boolean isFormatJson) {
103+
setFormatJson(isFormatJson);
104+
return this;
105+
}
106+
107+
public void setOnErrorType(JsonOnErrorType onErrorType) {
108+
this.onErrorType = onErrorType;
109+
}
110+
111+
public JsonOnErrorType getOnErrorType() {
112+
return onErrorType;
113+
}
114+
115+
public JsonTable withOnErrorType(JsonOnErrorType onErrorType) {
116+
setOnErrorType(onErrorType);
117+
return this;
118+
}
119+
120+
public void setType(JsonTableType type) {
121+
this.type = type;
122+
}
123+
124+
public JsonTableType getType() {
125+
return type;
126+
}
127+
128+
public JsonTable withType(JsonTableType type) {
129+
setType(type);
130+
return this;
131+
}
132+
133+
public void setOnEmptyType(JsonOnEmptyType onEmptyType) {
134+
this.onEmptyType = onEmptyType;
135+
}
136+
137+
public JsonOnEmptyType getOnEmptyType() {
138+
return onEmptyType;
139+
}
140+
141+
public JsonTable withOnEmptyType(JsonOnEmptyType onEmptyType) {
142+
setOnEmptyType(onEmptyType);
143+
return this;
144+
}
145+
146+
public void addColumn(JsonTableColumn column) {
147+
this.jsonColumns.add(column);
148+
}
149+
150+
public List<JsonTableColumn> getColumns() {
151+
return jsonColumns;
152+
}
153+
154+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
public class JsonTableColumn {
4+
5+
private String columnName;
6+
private JsonTableColumnType type;
7+
8+
private String jsonPath;
9+
10+
public void setColumnName(String columnName) {
11+
this.columnName = columnName;
12+
}
13+
14+
public String getColumnName() {
15+
return columnName;
16+
}
17+
18+
public void setJsonPath(String jsonPath) {
19+
this.jsonPath = jsonPath;
20+
}
21+
22+
public String getJsonPath() {
23+
return jsonPath;
24+
}
25+
26+
public void setType(JsonTableColumnType type) {
27+
this.type = type;
28+
}
29+
30+
public JsonTableColumnType getType() {
31+
return type;
32+
}
33+
34+
public StringBuilder append(StringBuilder builder) {
35+
36+
builder.append(columnName);
37+
38+
switch (type) {
39+
case ORDINALITY:
40+
builder.append(" FOR ORDINALITY");
41+
42+
break;
43+
}
44+
45+
return builder;
46+
}
47+
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
public enum JsonTableColumnType {
4+
JSON_EXISTS,
5+
JSON_QUERY,
6+
JSON_VALUE,
7+
JSON_NESTED_PATH,
8+
ORDINALITY
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2021 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
/*
11+
* Copyright (C) 2021 JSQLParser.
12+
*
13+
* This library is free software; you can redistribute it and/or modify it under the terms of the
14+
* GNU Lesser General Public License as published by the Free Software Foundation; either version
15+
* 2.1 of the License, or (at your option) any later version.
16+
*
17+
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
18+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License along with this library;
22+
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23+
* 02110-1301 USA
24+
*/
25+
26+
package net.sf.jsqlparser.expression;
27+
28+
/**
29+
*
30+
*/
31+
public enum JsonTableType {
32+
STRICT, LAX;
33+
34+
public static JsonTableType from(String type) {
35+
return Enum.valueOf(JsonTableType.class, type.toUpperCase());
36+
}
37+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,12 @@ public <S> Void visit(JsonFunction expression, S context) {
17211721
return null;
17221722
}
17231723

1724+
@Override
1725+
public <S> Void visit(JsonTable jsonTable, S context) {
1726+
// TODO: Implement
1727+
return null;
1728+
}
1729+
17241730
@Override
17251731
public <S> Void visit(ConnectByRootOperator connectByRootOperator, S context) {
17261732
connectByRootOperator.getColumn().accept(this, context);

0 commit comments

Comments
 (0)