Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3269,13 +3269,14 @@ Statement Statement() #Statement:
ifElseStatement.setUsingSemicolonForIfStatement(true);
}
]
<EOF>
)
|
(
(stm = SingleStatement()
| stm = Block())

( <ST_SEMICOLON> | <EOF> )
( <ST_SEMICOLON> )* <EOF>
)
|
LOOKAHEAD( { stm==null && getAsBoolean(Feature.allowUnsupportedStatements) } ) stm = UnsupportedStatement()
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,36 @@ void testParseEmpty() {
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse((String) null));
}

@Test
void testParseRejectsUnconsumedInput() throws Exception {
// "\n\n\n" separates statements exactly like ";" does, so both inputs below hold
// two statements; a single statement parse must not silently return the first only
String newlineSeparated = "SELECT a FROM dual WHERE x = ?\n\n\n AND y = ?";
String semicolonSeparated = "SELECT a FROM dual; SELECT b FROM dual";

assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(newlineSeparated));
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(semicolonSeparated));
assertThrows(ParseException.class,
() -> CCJSqlParserUtil.newParser(newlineSeparated).Statement());
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil
.parse(new ByteArrayInputStream(
newlineSeparated.getBytes(StandardCharsets.UTF_8))));
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parseAST(newlineSeparated));

// trailing separators and comments do not make a statement incomplete
assertEquals("SELECT a FROM dual",
CCJSqlParserUtil.parse("SELECT a FROM dual;").toString());
assertEquals("SELECT a FROM dual",
CCJSqlParserUtil.parse("SELECT a FROM dual\n\n\n").toString());
assertEquals("SELECT a FROM dual",
CCJSqlParserUtil.parse("SELECT a FROM dual -- done").toString());

// the multi statement API keeps working
assertEquals(2, CCJSqlParserUtil.parseStatements(semicolonSeparated).size());
assertEquals(3, CCJSqlParserUtil.parseStatements(
"SELECT * FROM DUAL\n\n\nSELECT * FROM DUAL\n\n\n\nSELECT * FROM dual").size());
}

@Test
void testSingleStatementWithEmptyLines() throws JSQLParserException {
String sqlStr = "update shop_info set title=?,\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@ void preservesRoutineBodyAndFollowingStatement(Dialect dialect, String sql) thro
}

@Test
void directStatementParserLeavesFollowingStatementAvailable() throws Exception {
void directSingleStatementParserLeavesFollowingStatementAvailable() throws Exception {
// walking a script statement by statement is SingleStatement()'s job; Statement() reads
// the input as exactly one statement and rejects whatever is left over
CCJSqlParser parser = parser("CREATE FUNCTION f() RETURNS int LANGUAGE SQL RETURN 1; "
+ FOLLOWING + ";", Dialect.POSTGRESQL);
assertThat(parser.Statement()).isInstanceOf(CreateFunction.class);
assertThat(parser.SingleStatement()).isInstanceOf(CreateFunction.class);
assertThat(parser.getNextToken().kind).isEqualTo(CCJSqlParserConstants.ST_SEMICOLON);
assertThat(parser.getToken(1).kind).isEqualTo(CCJSqlParserConstants.K_SELECT);
assertThat(parser.Statement().toString()).isEqualTo(FOLLOWING);
assertThat(parser.SingleStatement().toString()).isEqualTo(FOLLOWING);
assertThat(parser.getNextToken().kind).isEqualTo(CCJSqlParserConstants.ST_SEMICOLON);
assertThat(parser.getToken(1).kind).isEqualTo(CCJSqlParserConstants.EOF);
}

Expand Down