Skip to content

Commit 45ce94e

Browse files
committed
simple first json syntax
1 parent bbf38d0 commit 45ce94e

7 files changed

Lines changed: 126 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,6 @@ public interface ExpressionVisitor {
143143
void visit(OracleHierarchicalExpression oexpr);
144144

145145
void visit(RegExpMatchOperator rexpr);
146+
147+
void visit(JsonExpression jsonExpr);
146148
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,9 @@ protected void visitBinaryExpression(BinaryExpression expr) {
302302
expr.getLeftExpression().accept(this);
303303
expr.getRightExpression().accept(this);
304304
}
305+
306+
@Override
307+
public void visit(JsonExpression jsonExpr) {
308+
visit(jsonExpr.getColumn());
309+
}
305310
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2014 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
/*
23+
* Copyright (C) 2014 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
41+
package net.sf.jsqlparser.expression;
42+
43+
import java.util.ArrayList;
44+
import java.util.List;
45+
import net.sf.jsqlparser.schema.Column;
46+
47+
/**
48+
*
49+
* @author toben
50+
*/
51+
public class JsonExpression implements Expression {
52+
53+
private Column column;
54+
55+
private List<String> idents = new ArrayList<String>();
56+
57+
@Override
58+
public void accept(ExpressionVisitor expressionVisitor) {
59+
expressionVisitor.visit(this);
60+
}
61+
62+
public Column getColumn() {
63+
return column;
64+
}
65+
66+
public void setColumn(Column column) {
67+
this.column = column;
68+
}
69+
70+
public List<String> getIdents() {
71+
return idents;
72+
}
73+
74+
public void setIdents(List<String> idents) {
75+
this.idents = idents;
76+
}
77+
78+
public void addIdent(String ident) {
79+
idents.add(ident);
80+
}
81+
82+
@Override
83+
public String toString() {
84+
StringBuilder b = new StringBuilder();
85+
b.append(column.toString());
86+
for (String ident : idents) {
87+
b.append("->").append(ident);
88+
}
89+
return b.toString();
90+
}
91+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,8 @@ public void visit(OracleHierarchicalExpression oexpr) {
461461
public void visit(RegExpMatchOperator rexpr) {
462462
visitBinaryExpression(rexpr);
463463
}
464+
465+
@Override
466+
public void visit(JsonExpression jsonExpr) {
467+
}
464468
}

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,9 @@ public void visit(OracleHierarchicalExpression oexpr) {
500500
public void visit(RegExpMatchOperator rexpr) {
501501
visitBinaryExpression(rexpr, " " + rexpr.getStringExpression() + " ");
502502
}
503+
504+
@Override
505+
public void visit(JsonExpression jsonExpr) {
506+
buffer.append(jsonExpr.toString());
507+
}
503508
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,8 @@ Expression PrimaryExpression():
16571657

16581658
| LOOKAHEAD(ExtractExpression()) retval=ExtractExpression()
16591659

1660+
| LOOKAHEAD([sign="+" | sign="-"] JsonExpression()) [sign="+" | sign="-"] retval=JsonExpression()
1661+
16601662
| LOOKAHEAD(["+" | "-"] Function()) [sign="+" | sign="-"] retval=Function()
16611663

16621664
| LOOKAHEAD(["+" | "-"] <S_DOUBLE>) [sign="+" | sign="-"] token=<S_DOUBLE> { retval = new DoubleValue(token.image); }
@@ -1710,6 +1712,19 @@ JdbcNamedParameter JdbcNamedParameter() : {
17101712
}
17111713
}
17121714

1715+
JsonExpression JsonExpression() : {
1716+
JsonExpression result = new JsonExpression();
1717+
Column column;
1718+
Token token;
1719+
}
1720+
{
1721+
column=Column() ("->" token=<S_CHAR_LITERAL> {result.addIdent(token.image);} )+
1722+
{
1723+
result.setColumn(column);
1724+
return result;
1725+
}
1726+
}
1727+
17131728
IntervalExpression IntervalExpression() : {
17141729
IntervalExpression interval = new IntervalExpression();
17151730
Token token;

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,4 +1430,8 @@ public void testNotEqualsTo() throws JSQLParserException {
14301430
assertSqlCanBeParsedAndDeparsed("SELECT * FROM foo WHERE a != b");
14311431
assertSqlCanBeParsedAndDeparsed("SELECT * FROM foo WHERE a <> b");
14321432
}
1433+
1434+
public void testJsonExpression() throws JSQLParserException {
1435+
assertSqlCanBeParsedAndDeparsed("SELECT data->'images'->'thumbnail'->'url' AS thumb FROM instagram");
1436+
}
14331437
}

0 commit comments

Comments
 (0)