Skip to content

Commit 94fb872

Browse files
fix: return NULL when parsing empty Strings
- fixes #1963 Signed-off-by: Andreas Reichel <[email protected]>
1 parent 17f5f2a commit 94fb872

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

+50-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public static Statement parse(String sql) throws JSQLParserException {
7979
public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
8080
throws JSQLParserException {
8181

82+
if (sql == null || sql.isEmpty()) {
83+
return null;
84+
}
85+
8286
ExecutorService executorService = Executors.newSingleThreadExecutor();
8387
Statement statement = null;
8488
try {
@@ -92,8 +96,11 @@ public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
9296
public static Statement parse(String sql, ExecutorService executorService,
9397
Consumer<CCJSqlParser> consumer)
9498
throws JSQLParserException {
95-
Statement statement = null;
99+
if (sql == null || sql.isEmpty()) {
100+
return null;
101+
}
96102

103+
Statement statement = null;
97104
// first, try to parse fast and simple
98105
CCJSqlParser parser = newParser(sql);
99106
if (consumer != null) {
@@ -122,6 +129,10 @@ public static Statement parse(String sql, ExecutorService executorService,
122129
}
123130

124131
public static CCJSqlParser newParser(String sql) {
132+
if (sql == null || sql.isEmpty()) {
133+
return null;
134+
}
135+
125136
return new CCJSqlParser(new StringProvider(sql));
126137
}
127138

@@ -134,6 +145,10 @@ public static CCJSqlParser newParser(InputStream is, String encoding) throws IOE
134145
}
135146

136147
public static Node parseAST(String sql) throws JSQLParserException {
148+
if (sql == null || sql.isEmpty()) {
149+
return null;
150+
}
151+
137152
CCJSqlParser parser = newParser(sql);
138153
try {
139154
parser.Statement();
@@ -162,20 +177,31 @@ public static Statement parse(InputStream is, String encoding) throws JSQLParser
162177
}
163178

164179
public static Expression parseExpression(String expression) throws JSQLParserException {
180+
if (expression == null || expression.isEmpty()) {
181+
return null;
182+
}
183+
165184
return parseExpression(expression, true);
166185
}
167186

168187
public static Expression parseExpression(String expression, boolean allowPartialParse)
169188
throws JSQLParserException {
189+
if (expression == null || expression.isEmpty()) {
190+
return null;
191+
}
192+
170193
return parseExpression(expression, allowPartialParse, p -> {
171194
});
172195
}
173196

174197
@SuppressWarnings("PMD.CyclomaticComplexity")
175198
public static Expression parseExpression(String expressionStr, boolean allowPartialParse,
176199
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
177-
Expression expression = null;
200+
if (expressionStr == null || expressionStr.isEmpty()) {
201+
return null;
202+
}
178203

204+
Expression expression = null;
179205
// first, try to parse fast and simple
180206
try {
181207
CCJSqlParser parser = newParser(expressionStr).withAllowComplexParsing(false);
@@ -225,6 +251,9 @@ public static Expression parseExpression(String expressionStr, boolean allowPart
225251
* @see #parseCondExpression(String, boolean)
226252
*/
227253
public static Expression parseCondExpression(String condExpr) throws JSQLParserException {
254+
if (condExpr == null || condExpr.isEmpty()) {
255+
return null;
256+
}
228257
return parseCondExpression(condExpr, true);
229258
}
230259

@@ -238,15 +267,21 @@ public static Expression parseCondExpression(String condExpr) throws JSQLParserE
238267
*/
239268
public static Expression parseCondExpression(String condExpr, boolean allowPartialParse)
240269
throws JSQLParserException {
270+
if (condExpr == null || condExpr.isEmpty()) {
271+
return null;
272+
}
241273
return parseCondExpression(condExpr, allowPartialParse, p -> {
242274
});
243275
}
244276

245277
@SuppressWarnings("PMD.CyclomaticComplexity")
246278
public static Expression parseCondExpression(String conditionalExpressionStr,
247279
boolean allowPartialParse, Consumer<CCJSqlParser> consumer) throws JSQLParserException {
248-
Expression expression = null;
280+
if (conditionalExpressionStr == null || conditionalExpressionStr.isEmpty()) {
281+
return null;
282+
}
249283

284+
Expression expression = null;
250285
// first, try to parse fast and simple
251286
try {
252287
CCJSqlParser parser =
@@ -323,11 +358,19 @@ public Statement call() throws ParseException {
323358
* @return the statements parsed
324359
*/
325360
public static Statements parseStatements(String sqls) throws JSQLParserException {
361+
if (sqls == null || sqls.isEmpty()) {
362+
return null;
363+
}
364+
326365
return parseStatements(sqls, null);
327366
}
328367

329368
public static Statements parseStatements(String sqls, Consumer<CCJSqlParser> consumer)
330369
throws JSQLParserException {
370+
if (sqls == null || sqls.isEmpty()) {
371+
return null;
372+
}
373+
331374
ExecutorService executorService = Executors.newSingleThreadExecutor();
332375
final Statements statements = parseStatements(sqls, executorService, consumer);
333376
executorService.shutdown();
@@ -343,8 +386,11 @@ public static Statements parseStatements(String sqls, Consumer<CCJSqlParser> con
343386
public static Statements parseStatements(String sqls, ExecutorService executorService,
344387
Consumer<CCJSqlParser> consumer)
345388
throws JSQLParserException {
346-
Statements statements = null;
389+
if (sqls == null || sqls.isEmpty()) {
390+
return null;
391+
}
347392

393+
Statements statements = null;
348394
CCJSqlParser parser = newParser(sqls);
349395
if (consumer != null) {
350396
consumer.accept(parser);

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

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertNull;
1516
import static org.junit.jupiter.api.Assertions.assertThrows;
1617
import static org.junit.jupiter.api.Assertions.assertTrue;
1718

@@ -507,4 +508,10 @@ void testUnbalancedPosition() {
507508
" from tab";
508509
assertEquals(1122, CCJSqlParserUtil.getUnbalancedPosition(sqlStr));
509510
}
511+
512+
@Test
513+
void testParseEmpty() throws JSQLParserException {
514+
assertNull(CCJSqlParserUtil.parse(""));
515+
assertNull(CCJSqlParserUtil.parse((String) null));
516+
}
510517
}

0 commit comments

Comments
 (0)