Skip to content

Commit 35a1c97

Browse files
committed
fixes #777
1 parent 8dda4a6 commit 35a1c97

File tree

3 files changed

+103
-22
lines changed

3 files changed

+103
-22
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static Statement parse(InputStream is, String encoding) throws JSQLParser
7070
public static Expression parseExpression(String expression) throws JSQLParserException {
7171
return parseExpression(expression, true);
7272
}
73-
73+
7474
public static Expression parseExpression(String expression, boolean allowPartialParse) throws JSQLParserException {
7575
CCJSqlParser parser = new CCJSqlParser(new StringProvider(expression));
7676
try {
@@ -124,4 +124,23 @@ public static Statements parseStatements(String sqls) throws JSQLParserException
124124
}
125125
}
126126

127+
public static void streamStatements(StatementListener listener, InputStream is, String encoding) throws JSQLParserException {
128+
try {
129+
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(is, encoding));
130+
while (true) {
131+
Statement stmt = parser.SingleStatement();
132+
listener.accept(stmt);
133+
if (parser.getToken(1).kind == CCJSqlParserTokenManager.ST_SEMICOLON) {
134+
parser.getNextToken();
135+
}
136+
137+
if (parser.getToken(1).kind == CCJSqlParserTokenManager.EOF) {
138+
break;
139+
}
140+
}
141+
} catch (Exception ex) {
142+
throw new JSQLParserException(ex);
143+
}
144+
}
145+
127146
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/*
11+
* Copyright (C) 2019 JSQLParser.
12+
*
13+
* This library is free software; you can redistribute it and/or
14+
* modify it under the terms of the GNU Lesser General Public
15+
* License as published by the Free Software Foundation; either
16+
* version 2.1 of the License, or (at your option) any later version.
17+
*
18+
* This library is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
* Lesser General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU Lesser General Public
24+
* License along with this library; if not, write to the Free Software
25+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26+
* MA 02110-1301 USA
27+
*/
28+
package net.sf.jsqlparser.parser;
29+
30+
import net.sf.jsqlparser.statement.Statement;
31+
32+
/**
33+
*
34+
* @author Tobias Warneke ([email protected])
35+
*/
36+
public interface StatementListener {
37+
38+
void accept(Statement statement);
39+
}

src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
*/
1010
package net.sf.jsqlparser.parser;
1111

12+
import java.io.ByteArrayInputStream;
13+
import java.io.StringReader;
14+
import java.nio.charset.StandardCharsets;
15+
import java.util.ArrayList;
16+
import java.util.List;
1217
import net.sf.jsqlparser.JSQLParserException;
1318
import net.sf.jsqlparser.expression.Expression;
1419
import net.sf.jsqlparser.expression.LongValue;
1520
import net.sf.jsqlparser.expression.Parenthesis;
1621
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
1722
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
1823
import net.sf.jsqlparser.schema.Column;
24+
import net.sf.jsqlparser.statement.Statement;
1925
import net.sf.jsqlparser.statement.Statements;
2026
import org.junit.After;
2127
import org.junit.AfterClass;
2228
import static org.junit.Assert.*;
23-
24-
import java.io.ByteArrayInputStream;
25-
import java.io.StringReader;
26-
import java.nio.charset.StandardCharsets;
27-
2829
import org.junit.Before;
2930
import org.junit.BeforeClass;
3031
import org.junit.Test;
@@ -81,12 +82,12 @@ public void testParseExpressionNonPartial() throws Exception {
8182

8283
@Test(expected = JSQLParserException.class)
8384
public void testParseExpressionFromStringFail() throws Exception {
84-
CCJSqlParserUtil.parse("whatever$");
85+
CCJSqlParserUtil.parse("whatever$");
8586
}
8687

8788
@Test(expected = JSQLParserException.class)
8889
public void testParseExpressionFromRaderFail() throws Exception {
89-
CCJSqlParserUtil.parse(new StringReader("whatever$"));
90+
CCJSqlParserUtil.parse(new StringReader("whatever$"));
9091
}
9192

9293
@Test
@@ -103,19 +104,19 @@ public void testParseCondExpression() throws Exception {
103104

104105
@Test(expected = JSQLParserException.class)
105106
public void testParseCondExpressionFail() throws Exception {
106-
CCJSqlParserUtil.parseCondExpression(";");
107+
CCJSqlParserUtil.parseCondExpression(";");
107108

108109
}
109110

110111
@Test(expected = JSQLParserException.class)
111112
public void testParseFromStreamFail() throws Exception {
112-
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)));
113+
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)));
113114

114115
}
115116

116117
@Test(expected = JSQLParserException.class)
117118
public void testParseFromStreamWithEncodingFail() throws Exception {
118-
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8.name());
119+
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8.name());
119120

120121
}
121122

@@ -160,14 +161,36 @@ public void testParseStatementsIssue691() throws Exception {
160161
+ "SELECT * FROM dual;\n", result.toString());
161162
}
162163

164+
@Test
165+
public void testStreamStatementsIssue777() throws Exception {
166+
final List<Statement> list = new ArrayList<>();
167+
168+
CCJSqlParserUtil.streamStatements(new StatementListener() {
169+
@Override
170+
public void accept(Statement statement) {
171+
list.add(statement);
172+
}
173+
}, new ByteArrayInputStream(("select * from dual;\n"
174+
+ "select\n"
175+
+ "*\n"
176+
+ "from\n"
177+
+ "dual;\n"
178+
+ "\n"
179+
+ "-- some comment\n"
180+
+ "select *\n"
181+
+ "from dual;").getBytes(StandardCharsets.UTF_8)), "UTF-8");
182+
183+
assertEquals(list.size(), 3);
184+
}
185+
163186
@Test(expected = JSQLParserException.class)
164187
public void testParseStatementsFail() throws Exception {
165-
CCJSqlParserUtil.parseStatements("select * from dual;WHATEVER!!");
188+
CCJSqlParserUtil.parseStatements("select * from dual;WHATEVER!!");
166189
}
167190

168191
@Test(expected = JSQLParserException.class)
169192
public void testParseASTFail() throws Exception {
170-
CCJSqlParserUtil.parseAST("select * from dual;WHATEVER!!");
193+
CCJSqlParserUtil.parseAST("select * from dual;WHATEVER!!");
171194
}
172195

173196
@Test
@@ -180,14 +203,14 @@ public void testParseStatementsIssue691_2() throws Exception {
180203

181204
@Test
182205
public void testParseStatementIssue742() throws Exception {
183-
Statements result = CCJSqlParserUtil.parseStatements("CREATE TABLE `table_name` (\n" +
184-
" `id` bigint(20) NOT NULL AUTO_INCREMENT,\n" +
185-
" `another_column_id` bigint(20) NOT NULL COMMENT 'column id as sent by SYSTEM',\n" +
186-
" PRIMARY KEY (`id`),\n" +
187-
" UNIQUE KEY `uk_another_column_id` (`another_column_id`)\n" +
188-
")");
189-
assertEquals("CREATE TABLE `table_name` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `another_column_id` " +
190-
"bigint (20) NOT NULL COMMENT 'column id as sent by SYSTEM', PRIMARY KEY (`id`), UNIQUE KEY `uk_another_column_id` " +
191-
"(`another_column_id`));\n", result.toString());
206+
Statements result = CCJSqlParserUtil.parseStatements("CREATE TABLE `table_name` (\n"
207+
+ " `id` bigint(20) NOT NULL AUTO_INCREMENT,\n"
208+
+ " `another_column_id` bigint(20) NOT NULL COMMENT 'column id as sent by SYSTEM',\n"
209+
+ " PRIMARY KEY (`id`),\n"
210+
+ " UNIQUE KEY `uk_another_column_id` (`another_column_id`)\n"
211+
+ ")");
212+
assertEquals("CREATE TABLE `table_name` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `another_column_id` "
213+
+ "bigint (20) NOT NULL COMMENT 'column id as sent by SYSTEM', PRIMARY KEY (`id`), UNIQUE KEY `uk_another_column_id` "
214+
+ "(`another_column_id`));\n", result.toString());
192215
}
193216
}

0 commit comments

Comments
 (0)