Skip to content

Commit

Permalink
fix: return NULL when parsing empty Strings
Browse files Browse the repository at this point in the history
- fixes #1963

Signed-off-by: Andreas Reichel <[email protected]>
  • Loading branch information
manticore-projects committed Feb 23, 2024
1 parent 17f5f2a commit 94fb872
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static Statement parse(String sql) throws JSQLParserException {
public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
throws JSQLParserException {

if (sql == null || sql.isEmpty()) {
return null;
}

ExecutorService executorService = Executors.newSingleThreadExecutor();
Statement statement = null;
try {
Expand All @@ -92,8 +96,11 @@ public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
public static Statement parse(String sql, ExecutorService executorService,
Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
Statement statement = null;
if (sql == null || sql.isEmpty()) {
return null;
}

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

public static CCJSqlParser newParser(String sql) {
if (sql == null || sql.isEmpty()) {
return null;
}

return new CCJSqlParser(new StringProvider(sql));
}

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

public static Node parseAST(String sql) throws JSQLParserException {
if (sql == null || sql.isEmpty()) {
return null;
}

CCJSqlParser parser = newParser(sql);
try {
parser.Statement();
Expand Down Expand Up @@ -162,20 +177,31 @@ public static Statement parse(InputStream is, String encoding) throws JSQLParser
}

public static Expression parseExpression(String expression) throws JSQLParserException {
if (expression == null || expression.isEmpty()) {
return null;
}

return parseExpression(expression, true);
}

public static Expression parseExpression(String expression, boolean allowPartialParse)
throws JSQLParserException {
if (expression == null || expression.isEmpty()) {
return null;
}

return parseExpression(expression, allowPartialParse, p -> {
});
}

@SuppressWarnings("PMD.CyclomaticComplexity")
public static Expression parseExpression(String expressionStr, boolean allowPartialParse,
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
Expression expression = null;
if (expressionStr == null || expressionStr.isEmpty()) {
return null;
}

Expression expression = null;
// first, try to parse fast and simple
try {
CCJSqlParser parser = newParser(expressionStr).withAllowComplexParsing(false);
Expand Down Expand Up @@ -225,6 +251,9 @@ public static Expression parseExpression(String expressionStr, boolean allowPart
* @see #parseCondExpression(String, boolean)
*/
public static Expression parseCondExpression(String condExpr) throws JSQLParserException {
if (condExpr == null || condExpr.isEmpty()) {
return null;
}
return parseCondExpression(condExpr, true);
}

Expand All @@ -238,15 +267,21 @@ public static Expression parseCondExpression(String condExpr) throws JSQLParserE
*/
public static Expression parseCondExpression(String condExpr, boolean allowPartialParse)
throws JSQLParserException {
if (condExpr == null || condExpr.isEmpty()) {
return null;
}
return parseCondExpression(condExpr, allowPartialParse, p -> {
});
}

@SuppressWarnings("PMD.CyclomaticComplexity")
public static Expression parseCondExpression(String conditionalExpressionStr,
boolean allowPartialParse, Consumer<CCJSqlParser> consumer) throws JSQLParserException {
Expression expression = null;
if (conditionalExpressionStr == null || conditionalExpressionStr.isEmpty()) {
return null;
}

Expression expression = null;
// first, try to parse fast and simple
try {
CCJSqlParser parser =
Expand Down Expand Up @@ -323,11 +358,19 @@ public Statement call() throws ParseException {
* @return the statements parsed
*/
public static Statements parseStatements(String sqls) throws JSQLParserException {
if (sqls == null || sqls.isEmpty()) {
return null;
}

return parseStatements(sqls, null);
}

public static Statements parseStatements(String sqls, Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
if (sqls == null || sqls.isEmpty()) {
return null;
}

ExecutorService executorService = Executors.newSingleThreadExecutor();
final Statements statements = parseStatements(sqls, executorService, consumer);
executorService.shutdown();
Expand All @@ -343,8 +386,11 @@ public static Statements parseStatements(String sqls, Consumer<CCJSqlParser> con
public static Statements parseStatements(String sqls, ExecutorService executorService,
Consumer<CCJSqlParser> consumer)
throws JSQLParserException {
Statements statements = null;
if (sqls == null || sqls.isEmpty()) {
return null;
}

Statements statements = null;
CCJSqlParser parser = newParser(sqls);
if (consumer != null) {
consumer.accept(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -507,4 +508,10 @@ void testUnbalancedPosition() {
" from tab";
assertEquals(1122, CCJSqlParserUtil.getUnbalancedPosition(sqlStr));
}

@Test
void testParseEmpty() throws JSQLParserException {
assertNull(CCJSqlParserUtil.parse(""));
assertNull(CCJSqlParserUtil.parse((String) null));
}
}

0 comments on commit 94fb872

Please sign in to comment.