Skip to content

Commit 1a73202

Browse files
committed
1 parent 96737b9 commit 1a73202

File tree

9 files changed

+352
-8
lines changed

9 files changed

+352
-8
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import net.sf.jsqlparser.expression.Expression;
15+
import net.sf.jsqlparser.expression.UserVariable;
16+
import net.sf.jsqlparser.statement.create.table.ColDataType;
17+
18+
public final class DeclareStatement implements Statement {
19+
20+
private UserVariable userVariable = null;
21+
private DeclareType type = DeclareType.TYPE;
22+
private String typeName;
23+
private List<TypeDefExpr> typeDefExprList = new ArrayList<>();
24+
25+
public DeclareStatement() {
26+
}
27+
28+
public void setUserVariable(UserVariable userVariable) {
29+
this.userVariable = userVariable;
30+
}
31+
32+
public UserVariable getUserVariable() {
33+
return userVariable;
34+
}
35+
36+
public DeclareType getType() {
37+
return type;
38+
}
39+
40+
public String getTypeName() {
41+
return typeName;
42+
}
43+
44+
public void setDeclareType(DeclareType type) {
45+
this.type = type;
46+
}
47+
48+
public void addType(ColDataType colDataType, Expression defaultExpr) {
49+
typeDefExprList.add(new TypeDefExpr(colDataType, defaultExpr));
50+
}
51+
52+
public void addType(UserVariable userVariable, ColDataType colDataType, Expression defaultExpr) {
53+
typeDefExprList.add(new TypeDefExpr(userVariable, colDataType, defaultExpr));
54+
}
55+
56+
public void addType(String columnName, ColDataType colDataType, Expression defaultExpr) {
57+
typeDefExprList.add(new TypeDefExpr(columnName, colDataType, defaultExpr));
58+
}
59+
60+
public List<TypeDefExpr> getTypeDefinitions() {
61+
return typeDefExprList;
62+
}
63+
64+
public void setTypeName(String typeName) {
65+
this.typeName = typeName;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
StringBuilder b = new StringBuilder("DECLARE ");
71+
if (type == DeclareType.AS) {
72+
b.append(userVariable.toString());
73+
b.append(" AS ").append(typeName);
74+
} else {
75+
if (type == DeclareType.TABLE) {
76+
b.append(userVariable.toString());
77+
b.append(" TABLE(");
78+
}
79+
80+
for (int i = 0; i < typeDefExprList.size(); i++) {
81+
if (i > 0) {
82+
b.append(", ");
83+
}
84+
final TypeDefExpr type = typeDefExprList.get(i);
85+
if (type.userVariable != null) {
86+
b.append(type.userVariable.toString()).append(" ");
87+
} else if (type.columnName != null) {
88+
b.append(type.columnName).append(" ");
89+
}
90+
b.append(type.colDataType.toString());
91+
if (type.defaultExpr != null) {
92+
b.append(" = ").append(type.defaultExpr.toString());
93+
}
94+
}
95+
96+
if (type == DeclareType.TABLE) {
97+
b.append(")");
98+
}
99+
}
100+
101+
return b.toString();
102+
}
103+
104+
@Override
105+
public void accept(StatementVisitor statementVisitor) {
106+
statementVisitor.visit(this);
107+
}
108+
109+
public static class TypeDefExpr {
110+
111+
public final String columnName;
112+
public final UserVariable userVariable;
113+
public final ColDataType colDataType;
114+
public final Expression defaultExpr;
115+
116+
public TypeDefExpr(ColDataType colDataType, Expression defaultExpr) {
117+
this((UserVariable) null, colDataType, defaultExpr);
118+
}
119+
120+
public TypeDefExpr(UserVariable userVariable, ColDataType colDataType, Expression defaultExpr) {
121+
this.userVariable = userVariable;
122+
this.colDataType = colDataType;
123+
this.defaultExpr = defaultExpr;
124+
this.columnName = null;
125+
}
126+
127+
public TypeDefExpr(String colName, ColDataType colDataType, Expression defaultExpr) {
128+
this.userVariable = null;
129+
this.colDataType = colDataType;
130+
this.defaultExpr = defaultExpr;
131+
this.columnName = colName;
132+
}
133+
}
134+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement;
11+
12+
/**
13+
*
14+
* @author tobens
15+
*/
16+
public enum DeclareType {
17+
TABLE, AS, TYPE
18+
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ public interface StatementVisitor {
8080
public void visit(ExplainStatement aThis);
8181

8282
public void visit(ShowStatement aThis);
83+
84+
public void visit(DeclareStatement aThis);
8385
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,8 @@ public void visit(ExplainStatement aThis) {
151151
@Override
152152
public void visit(ShowStatement aThis) {
153153
}
154+
155+
@Override
156+
public void visit(DeclareStatement aThis) {
157+
}
154158
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import net.sf.jsqlparser.schema.Table;
5858
import net.sf.jsqlparser.statement.Block;
5959
import net.sf.jsqlparser.statement.Commit;
60+
import net.sf.jsqlparser.statement.DeclareStatement;
6061
import net.sf.jsqlparser.statement.DescribeStatement;
6162
import net.sf.jsqlparser.statement.ExplainStatement;
6263
import net.sf.jsqlparser.statement.SetStatement;
@@ -846,4 +847,8 @@ public void visit(ShowStatement aThis) {
846847
public void visit(SimilarToExpression expr) {
847848
visitBinaryExpression(expr);
848849
}
850+
851+
@Override
852+
public void visit(DeclareStatement aThis) {
853+
}
849854
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.util.deparser;
11+
12+
import net.sf.jsqlparser.expression.ExpressionVisitor;
13+
import net.sf.jsqlparser.statement.DeclareStatement;
14+
import net.sf.jsqlparser.statement.DeclareType;
15+
16+
public class DeclareStatementDeParser {
17+
18+
protected StringBuilder buffer;
19+
private ExpressionVisitor expressionVisitor;
20+
21+
public DeclareStatementDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
22+
this.buffer = buffer;
23+
this.expressionVisitor = expressionVisitor;
24+
}
25+
26+
public StringBuilder getBuffer() {
27+
return buffer;
28+
}
29+
30+
public void setBuffer(StringBuilder buffer) {
31+
this.buffer = buffer;
32+
}
33+
34+
public void deParse(DeclareStatement declare) {
35+
buffer.append("DECLARE ");
36+
37+
if (declare.getUserVariable() != null) {
38+
declare.getUserVariable().accept(expressionVisitor);
39+
}
40+
41+
if (declare.getType() == DeclareType.AS) {
42+
buffer.append(" AS ");
43+
buffer.append(declare.getTypeName());
44+
return;
45+
}
46+
47+
if (declare.getType() == DeclareType.TABLE) {
48+
buffer.append(" TABLE (");
49+
}
50+
51+
if (declare.getTypeDefinitions() != null) {
52+
for (int i = 0; i < declare.getTypeDefinitions().size(); i++) {
53+
if (i > 0) {
54+
buffer.append(", ");
55+
}
56+
DeclareStatement.TypeDefExpr type = declare.getTypeDefinitions().get(i);
57+
if (type.userVariable != null) {
58+
type.userVariable.accept(expressionVisitor);
59+
buffer.append(" ");
60+
} else if (type.columnName != null) {
61+
buffer.append(type.columnName).append(" ");
62+
}
63+
buffer.append(type.colDataType.toString());
64+
if (type.defaultExpr != null) {
65+
buffer.append(" = ");
66+
type.defaultExpr.accept(expressionVisitor);
67+
}
68+
}
69+
}
70+
71+
if (declare.getType() == DeclareType.TABLE) {
72+
buffer.append(")");
73+
}
74+
}
75+
76+
public ExpressionVisitor getExpressionVisitor() {
77+
return expressionVisitor;
78+
}
79+
80+
public void setExpressionVisitor(ExpressionVisitor visitor) {
81+
expressionVisitor = visitor;
82+
}
83+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Iterator;
1313
import net.sf.jsqlparser.statement.Block;
1414
import net.sf.jsqlparser.statement.Commit;
15+
import net.sf.jsqlparser.statement.DeclareStatement;
1516
import net.sf.jsqlparser.statement.DescribeStatement;
1617
import net.sf.jsqlparser.statement.ExplainStatement;
1718
import net.sf.jsqlparser.statement.SetStatement;
@@ -267,4 +268,10 @@ public void visit(ExplainStatement explain) {
267268
public void visit(ShowStatement show) {
268269
new ShowStatementDeParser(buffer).deParse(show);
269270
}
271+
272+
@Override
273+
public void visit(DeclareStatement declare) {
274+
expressionDeParser.setBuffer(buffer);
275+
new DeclareStatementDeParser(expressionDeParser, buffer).deParse(declare);
276+
}
270277
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
options{
1212
IGNORE_CASE = true ;
1313
STATIC = false;
14-
DEBUG_PARSER = false;
14+
DEBUG_PARSER = true;
1515
DEBUG_LOOKAHEAD = false;
1616
DEBUG_TOKEN_MANAGER = false;
17-
CACHE_TOKENS = false;
17+
CACHE_TOKENS = false;
1818
// FORCE_LA_CHECK = true;
1919
UNICODE_INPUT = true;
20-
JAVA_TEMPLATE_TYPE = "modern";
21-
JDK_VERSION = "1.7";
20+
JAVA_TEMPLATE_TYPE = "modern";
21+
JDK_VERSION = "1.7";
2222
TOKEN_EXTENDS = "BaseToken";
2323
COMMON_TOKEN_ACTION = true;
24-
NODE_DEFAULT_VOID = true;
25-
TRACK_TOKENS = true;
26-
VISITOR = true;
24+
NODE_DEFAULT_VOID = true;
25+
TRACK_TOKENS = true;
26+
VISITOR = true;
2727
}
2828

2929
PARSER_BEGIN(CCJSqlParser)
@@ -141,6 +141,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
141141
| <K_CREATE:"CREATE">
142142
| <K_CROSS:"CROSS">
143143
| <K_CURRENT: "CURRENT">
144+
| <K_DECLARE: "DECLARE">
144145
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
145146
| <K_DATE_LITERAL : ( "YEAR" | "MONTH" | "DAY" | "HOUR" | "MINUTE" | "SECOND" ) >
146147
| <K_DEFERRABLE : "DEFERRABLE">
@@ -434,6 +435,8 @@ Statement SingleStatement() :
434435
stm = Describe()
435436
|
436437
stm = Explain()
438+
|
439+
stm = Declare()
437440
)
438441
{ return stm; }
439442
} catch (ParseException e) {
@@ -510,6 +513,40 @@ void error_skipto(int kind) {
510513
} while (t.kind != kind && t.kind != EOF);
511514
}
512515

516+
DeclareStatement Declare(): {
517+
UserVariable userVariable;
518+
ColDataType colDataType;
519+
Expression defaultExpr = null;
520+
DeclareStatement stmt = new DeclareStatement();
521+
String typeName;
522+
String columnName;
523+
} {
524+
<K_DECLARE> userVariable = UserVariable()
525+
(
526+
( <K_TABLE> "(" columnName = RelObjectName() colDataType = ColDataType()
527+
{ stmt.setUserVariable(userVariable);
528+
stmt.setDeclareType(DeclareType.TABLE);
529+
stmt.addType(columnName, colDataType, null); }
530+
("," columnName = RelObjectName() colDataType = ColDataType())* { stmt.addType(columnName, colDataType, null); } ")"
531+
)
532+
|
533+
<K_AS> typeName = RelObjectName()
534+
{ stmt.setUserVariable(userVariable);
535+
stmt.setDeclareType(DeclareType.AS);
536+
stmt.setTypeName(typeName); }
537+
|
538+
(colDataType = ColDataType() ["=" defaultExpr = Expression()]
539+
{ stmt.setDeclareType(DeclareType.TYPE);
540+
stmt.addType(userVariable, colDataType, defaultExpr); }
541+
("," userVariable = UserVariable() colDataType = ColDataType() { defaultExpr = null; }
542+
["=" defaultExpr = Expression()] { stmt.addType(userVariable, colDataType, defaultExpr); } )*
543+
)
544+
)
545+
{
546+
return stmt;
547+
}
548+
}
549+
513550
SetStatement Set(): {
514551
String name;
515552
Expression value;
@@ -1118,7 +1155,7 @@ String RelObjectNameExt2():
11181155
String result=null;
11191156
}
11201157
{
1121-
( result=RelObjectNameExt() | tk=<K_FROM> )
1158+
( result=RelObjectNameExt() | tk=<K_FROM> | tk=<K_GROUP> )
11221159
{
11231160
if (tk!=null) result=tk.image;
11241161
return result;

0 commit comments

Comments
 (0)