Skip to content

Commit 8d0dec1

Browse files
committed
fixes #828
1 parent c766ebc commit 8d0dec1

File tree

4 files changed

+89
-59
lines changed

4 files changed

+89
-59
lines changed

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

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import net.sf.jsqlparser.expression.Expression;
1515
import net.sf.jsqlparser.expression.UserVariable;
1616
import net.sf.jsqlparser.statement.create.table.ColDataType;
17+
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
1718

1819
public final class DeclareStatement implements Statement {
1920

2021
private UserVariable userVariable = null;
2122
private DeclareType type = DeclareType.TYPE;
2223
private String typeName;
2324
private List<TypeDefExpr> typeDefExprList = new ArrayList<>();
25+
private List<ColumnDefinition> colDefs = new ArrayList<>();
2426

2527
public DeclareStatement() {
2628
}
@@ -53,8 +55,12 @@ public void addType(UserVariable userVariable, ColDataType colDataType, Expressi
5355
typeDefExprList.add(new TypeDefExpr(userVariable, colDataType, defaultExpr));
5456
}
5557

56-
public void addType(String columnName, ColDataType colDataType, Expression defaultExpr) {
57-
typeDefExprList.add(new TypeDefExpr(columnName, colDataType, defaultExpr));
58+
public void addColumnDefinition(ColumnDefinition colDef) {
59+
colDefs.add(colDef);
60+
}
61+
62+
public List<ColumnDefinition> getColumnDefinitions() {
63+
return colDefs;
5864
}
5965

6066
public List<TypeDefExpr> getTypeDefinitions() {
@@ -74,61 +80,53 @@ public String toString() {
7480
} else {
7581
if (type == DeclareType.TABLE) {
7682
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+
b.append(" TABLE (");
84+
for (int i = 0; i < colDefs.size(); i++) {
85+
if (i > 0) {
86+
b.append(", ");
87+
}
88+
b.append(colDefs.get(i).toString());
8389
}
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) {
9790
b.append(")");
91+
} else {
92+
for (int i = 0; i < typeDefExprList.size(); i++) {
93+
if (i > 0) {
94+
b.append(", ");
95+
}
96+
final TypeDefExpr type = typeDefExprList.get(i);
97+
if (type.userVariable != null) {
98+
b.append(type.userVariable.toString()).append(" ");
99+
}
100+
b.append(type.colDataType.toString());
101+
if (type.defaultExpr != null) {
102+
b.append(" = ").append(type.defaultExpr.toString());
103+
}
104+
}
98105
}
99106
}
100-
101107
return b.toString();
102108
}
103109

104110
@Override
105-
public void accept(StatementVisitor statementVisitor) {
111+
public void accept(StatementVisitor statementVisitor
112+
) {
106113
statementVisitor.visit(this);
107114
}
108115

109116
public static class TypeDefExpr {
110117

111-
public final String columnName;
112118
public final UserVariable userVariable;
113119
public final ColDataType colDataType;
114120
public final Expression defaultExpr;
115121

116122
public TypeDefExpr(ColDataType colDataType, Expression defaultExpr) {
117-
this((UserVariable) null, colDataType, defaultExpr);
123+
this(null, colDataType, defaultExpr);
118124
}
119125

120126
public TypeDefExpr(UserVariable userVariable, ColDataType colDataType, Expression defaultExpr) {
121127
this.userVariable = userVariable;
122128
this.colDataType = colDataType;
123129
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;
132130
}
133131
}
134132
}

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,31 @@ public void deParse(DeclareStatement declare) {
4646

4747
if (declare.getType() == DeclareType.TABLE) {
4848
buffer.append(" TABLE (");
49-
}
50-
51-
if (declare.getTypeDefinitions() != null) {
52-
for (int i = 0; i < declare.getTypeDefinitions().size(); i++) {
49+
for (int i = 0; i < declare.getColumnDefinitions().size(); i++) {
5350
if (i > 0) {
5451
buffer.append(", ");
5552
}
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-
}
53+
buffer.append(declare.getColumnDefinitions().get(i).toString());
6854
}
69-
}
70-
71-
if (declare.getType() == DeclareType.TABLE) {
7255
buffer.append(")");
56+
} else {
57+
if (declare.getTypeDefinitions() != null) {
58+
for (int i = 0; i < declare.getTypeDefinitions().size(); i++) {
59+
if (i > 0) {
60+
buffer.append(", ");
61+
}
62+
DeclareStatement.TypeDefExpr type = declare.getTypeDefinitions().get(i);
63+
if (type.userVariable != null) {
64+
type.userVariable.accept(expressionVisitor);
65+
buffer.append(" ");
66+
}
67+
buffer.append(type.colDataType.toString());
68+
if (type.defaultExpr != null) {
69+
buffer.append(" = ");
70+
type.defaultExpr.accept(expressionVisitor);
71+
}
72+
}
73+
}
7374
}
7475
}
7576

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,15 @@ DeclareStatement Declare(): {
520520
DeclareStatement stmt = new DeclareStatement();
521521
String typeName;
522522
String columnName;
523+
ColumnDefinition colDef;
523524
} {
524525
<K_DECLARE> userVariable = UserVariable()
525526
(
526-
( <K_TABLE> "(" columnName = RelObjectName() colDataType = ColDataType()
527+
( <K_TABLE> "(" colDef = ColumnDefinition()
527528
{ stmt.setUserVariable(userVariable);
528529
stmt.setDeclareType(DeclareType.TABLE);
529-
stmt.addType(columnName, colDataType, null); }
530-
("," columnName = RelObjectName() colDataType = ColDataType())* { stmt.addType(columnName, colDataType, null); } ")"
530+
stmt.addColumnDefinition(colDef); }
531+
("," colDef = ColumnDefinition() { stmt.addColumnDefinition(colDef); })* ")"
531532
)
532533
|
533534
<K_AS> typeName = RelObjectName()
@@ -3501,6 +3502,29 @@ CreateIndex CreateIndex():
35013502
}
35023503
}
35033504

3505+
ColumnDefinition ColumnDefinition(): {
3506+
ColumnDefinition coldef;
3507+
String columnName;
3508+
ColDataType colDataType;
3509+
List<String> columnSpecs = new ArrayList<String>();
3510+
List<String> parameter;
3511+
} {
3512+
columnName=RelObjectName()
3513+
3514+
colDataType = ColDataType()
3515+
3516+
( parameter=CreateParameter() { columnSpecs.addAll(parameter); } )*
3517+
3518+
{
3519+
coldef = new ColumnDefinition();
3520+
coldef.setColumnName(columnName);
3521+
coldef.setColDataType(colDataType);
3522+
if (columnSpecs.size() > 0)
3523+
coldef.setColumnSpecStrings(columnSpecs);
3524+
return coldef;
3525+
}
3526+
}
3527+
35043528
CreateTable CreateTable():
35053529
{
35063530
CreateTable createTable = new CreateTable();
@@ -3542,8 +3566,9 @@ CreateTable CreateTable():
35423566
table=Table()
35433567
[
35443568
("("
3545-
columnName=RelObjectName()
3569+
coldef = ColumnDefinition()
35463570

3571+
/*
35473572
colDataType = ColDataType()
35483573
{
35493574
columnSpecs = new ArrayList();
@@ -3558,7 +3583,8 @@ CreateTable CreateTable():
35583583
if (columnSpecs.size() > 0)
35593584
coldef.setColumnSpecStrings(columnSpecs);
35603585
columnDefinitions.add(coldef);
3561-
}
3586+
}*/
3587+
{ columnDefinitions.add(coldef); }
35623588

35633589
(
35643590
","
@@ -3654,6 +3680,10 @@ CreateTable CreateTable():
36543680
}
36553681
|
36563682
(
3683+
3684+
coldef = ColumnDefinition()
3685+
3686+
/*
36573687
columnName=RelObjectName()
36583688

36593689
colDataType = ColDataType()
@@ -3670,7 +3700,8 @@ CreateTable CreateTable():
36703700
if (columnSpecs.size() > 0)
36713701
coldef.setColumnSpecStrings(columnSpecs);
36723702
columnDefinitions.add(coldef);
3673-
}
3703+
} */
3704+
{ columnDefinitions.add(coldef); }
36743705
)
36753706
)
36763707
)*

src/test/java/net/sf/jsqlparser/statement/DeclareStatementTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testDeclareTypeList2() throws JSQLParserException {
4444

4545
@Test
4646
public void testDeclareTable() throws JSQLParserException {
47-
assertSqlCanBeParsedAndDeparsed("DECLARE @MyTableVar table(EmpID int NOT NULL, OldVacationHours int, NewVacationHours int, ModifiedDate datetime)");
47+
assertSqlCanBeParsedAndDeparsed("DECLARE @MyTableVar TABLE (EmpID int NOT NULL, OldVacationHours int, NewVacationHours int, ModifiedDate datetime)");
4848
}
4949

5050
@Test

0 commit comments

Comments
 (0)