Skip to content

Commit 4be2700

Browse files
committed
first statements version
1 parent ff94a3d commit 4be2700

File tree

6 files changed

+157
-8
lines changed

6 files changed

+157
-8
lines changed

src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sf.jsqlparser.JSQLParserException;
2828
import net.sf.jsqlparser.expression.Expression;
2929
import net.sf.jsqlparser.statement.Statement;
30+
import net.sf.jsqlparser.statement.Statements;
3031

3132
/**
3233
* Toolfunctions to start and use JSqlParser.
@@ -83,6 +84,18 @@ public static Expression parseExpression(String expression) throws JSQLParserExc
8384
throw new JSQLParserException(ex);
8485
}
8586
}
87+
88+
/**
89+
* Parse a statement list.
90+
*/
91+
public static Statements parseStatements(String sqls) throws JSQLParserException {
92+
CCJSqlParser parser = new CCJSqlParser(new StringReader(sqls));
93+
try {
94+
return parser.Statements();
95+
} catch (Exception ex) {
96+
throw new JSQLParserException(ex);
97+
}
98+
}
8699

87100
private CCJSqlParserUtil() {
88101
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ public interface StatementVisitor {
5656
void visit(CreateView createView);
5757

5858
void visit(Alter alter);
59+
60+
void visit(Statements stmts);
5961
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
package net.sf.jsqlparser.statement;
23+
24+
import java.util.List;
25+
26+
/**
27+
*
28+
* @author toben
29+
*/
30+
public class Statements {
31+
32+
private List<Statement> statements;
33+
34+
public List<Statement> getStatements() {
35+
return statements;
36+
}
37+
38+
public void setStatements(List<Statement> statements) {
39+
this.statements = statements;
40+
}
41+
42+
public void accept(StatementVisitor statementVisitor) {
43+
statementVisitor.visit(this);
44+
}
45+
46+
@Override
47+
public String toString() {
48+
StringBuilder b = new StringBuilder();
49+
for (Statement stmt : statements) {
50+
b.append(stmt.toString()).append(";\n");
51+
}
52+
return b.toString();
53+
}
54+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
package net.sf.jsqlparser.util.deparser;
2323

2424
import java.util.Iterator;
25+
import net.sf.jsqlparser.statement.Statement;
2526

2627
import net.sf.jsqlparser.statement.StatementVisitor;
28+
import net.sf.jsqlparser.statement.Statements;
2729
import net.sf.jsqlparser.statement.alter.Alter;
2830
import net.sf.jsqlparser.statement.create.index.CreateIndex;
2931
import net.sf.jsqlparser.statement.create.table.CreateTable;
@@ -117,12 +119,10 @@ public void visit(Select select) {
117119
}
118120
}
119121
select.getSelectBody().accept(selectDeParser);
120-
121122
}
122123

123124
@Override
124125
public void visit(Truncate truncate) {
125-
// TODO Auto-generated method stub
126126
}
127127

128128
@Override
@@ -148,4 +148,9 @@ public void setBuffer(StringBuilder buffer) {
148148
public void visit(Alter alter) {
149149

150150
}
151+
152+
@Override
153+
public void visit(Statements stmts) {
154+
stmts.accept(this);
155+
}
151156
}

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,16 @@ a configurable addition. One must rebuild JSqlParser with this new "Letterset".
232232
| < #ADDITIONAL_LETTERS: ["ä","ö","ü","Ä","Ö","Ü","ß"] >
233233
}
234234

235+
Statement Statement() :
236+
{ Statement stm; }
237+
{
238+
stm = SingleStatement()
239+
[";"]
240+
<EOF>
241+
{ return stm; }
242+
}
235243

236-
Statement Statement() :
244+
Statement SingleStatement() :
237245
{ Statement stm;}
238246
{
239247
(
@@ -245,7 +253,7 @@ Statement Statement() :
245253
|
246254
stm = Delete()
247255
|
248-
stm = Replace()
256+
stm = Replace()
249257
|
250258
stm = Alter()
251259
|
@@ -262,9 +270,24 @@ Statement Statement() :
262270
|
263271
stm = Truncate()
264272
)
273+
{ return stm; }
274+
}
275+
276+
Statements Statements() :
277+
{ Statements stmts = new Statements();
278+
List<Statement> list = new ArrayList<Statement>();
279+
Statement stm; }
280+
{
281+
stm = SingleStatement() { list.add(stm); }
282+
(LOOKAHEAD(2) ";" stm = SingleStatement() { list.add(stm); } )*
283+
265284
[";"]
266285
<EOF>
267-
{ return stm; }
286+
287+
{
288+
stmts.setStatements(list);
289+
return stmts;
290+
}
268291
}
269292

270293
Update Update():
@@ -1044,9 +1067,9 @@ OrderByElement OrderByElement():
10441067
columnReference = SimpleExpression()
10451068
[<K_ASC> | (<K_DESC> { orderByElement.setAsc(false); } ) ]
10461069
[<K_NULLS> (
1047-
[<K_FIRST> { orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_FIRST); } ] |
1048-
[<K_LAST> { orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_LAST); } ]
1049-
)
1070+
<K_FIRST> { orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_FIRST); } |
1071+
<K_LAST> { orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_LAST); }
1072+
)?
10501073
]
10511074
{
10521075
orderByElement.setExpression(columnReference);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package net.sf.jsqlparser.statement;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
5+
import net.sf.jsqlparser.statement.select.Select;
6+
import org.junit.After;
7+
import org.junit.AfterClass;
8+
import org.junit.Before;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
*
15+
* @author toben
16+
*/
17+
public class StatementsTest {
18+
19+
public StatementsTest() {
20+
}
21+
22+
@BeforeClass
23+
public static void setUpClass() {
24+
}
25+
26+
@AfterClass
27+
public static void tearDownClass() {
28+
}
29+
30+
@Before
31+
public void setUp() {
32+
}
33+
34+
@After
35+
public void tearDown() {
36+
}
37+
38+
/**
39+
* Test of toString method, of class Statements.
40+
*/
41+
@Test
42+
public void testStatements() throws JSQLParserException {
43+
String sqls = "select * from mytable; select * from mytable2;";
44+
Statements parseStatements = CCJSqlParserUtil.parseStatements(sqls);
45+
46+
assertEquals("SELECT * FROM mytable;\nSELECT * FROM mytable2;\n", parseStatements.toString());
47+
48+
assertTrue(parseStatements.getStatements().get(0) instanceof Select);
49+
assertTrue(parseStatements.getStatements().get(1) instanceof Select);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)