Skip to content

Commit 82b459b

Browse files
feat: re-enable UnsupportedStatement
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent a57ea5c commit 82b459b

5 files changed

Lines changed: 93 additions & 127 deletions

File tree

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 19 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,11 @@ Statements Statements() #Statements: {
820820
Expression condition;
821821
}
822822
{
823+
(
823824
(
824825
( <ST_SEMICOLON> )*
826+
827+
// todo: allow also first statement to be an `UnsupportedStatement`
825828
try {
826829
(
827830
<K_IF> condition=Condition()
@@ -837,14 +840,16 @@ Statements Statements() #Statements: {
837840
(
838841
stm = SingleStatement()
839842
| stm = Block()
840-
) { stmts.add(stm); }
843+
)
844+
{
845+
stmts.add(stm);
846+
}
847+
841848
} catch (ParseException ex) {
842849
if ( getAsBoolean(Feature.allowUnsupportedStatements) ) {
843-
parseErrors.add(ex);
844850
UnsupportedStatement unsupportedStatement = new UnsupportedStatement( error_skipto(ST_SEMICOLON) );
845851
if (!unsupportedStatement.isEmpty()) {
846852
stmts.add( unsupportedStatement );
847-
System.out.println("Exception!");
848853
}
849854
} else if ( errorRecovery ) {
850855
parseErrors.add(ex);
@@ -866,7 +871,6 @@ Statements Statements() #Statements: {
866871

867872
(
868873
( <ST_SEMICOLON> )*
869-
870874
try {
871875
(
872876
<K_IF> condition=Condition()
@@ -885,11 +889,9 @@ Statements Statements() #Statements: {
885889
) { stmts.add(stm); }
886890
} catch (ParseException ex) {
887891
if ( getAsBoolean(Feature.allowUnsupportedStatements) ) {
888-
parseErrors.add(ex);
889892
UnsupportedStatement unsupportedStatement = new UnsupportedStatement( error_skipto(ST_SEMICOLON) );
890893
if (!unsupportedStatement.isEmpty()) {
891894
stmts.add( unsupportedStatement );
892-
System.out.println("Exception!");
893895
}
894896
} else if ( errorRecovery ) {
895897
parseErrors.add(ex);
@@ -908,86 +910,19 @@ Statements Statements() #Statements: {
908910

909911
)*
910912

911-
<EOF>
912-
{
913-
return stmts;
914-
}
915-
}
916-
917-
Statements Statements3() #Statements: {
918-
Statements stmts = new Statements();
919-
IfElseStatement ifElseStatement = null;
920-
Statement stm = null;
921-
Statement stm2 = null;
922-
Expression condition;
923-
}
924-
{
925-
(
926-
(
927-
<K_IF> condition=Condition()
928-
( stm = SingleStatement() | stm = Block() ) { ifElseStatement = new IfElseStatement(condition, stm); }
929-
[ LOOKAHEAD(2)
930-
[ <ST_SEMICOLON> { ifElseStatement.setUsingSemicolonForIfStatement(true); } ]
931-
<K_ELSE> ( stm2 = SingleStatement() | stm2 = Block() ) { ifElseStatement.setElseStatement(stm2); }
932-
]
933-
934-
{ stmts.add( ifElseStatement ); }
935-
)
936-
|
937-
(
938-
stm = SingleStatement()
939-
| stm = Block()
940-
) { stmts.add(stm); }
941-
|
942-
LOOKAHEAD( { getAsBoolean(Feature.allowUnsupportedStatements) } ) stm = UnsupportedStatement()
943-
{ if ( !((UnsupportedStatement) stm).isEmpty() ) stmts.add(stm); }
944-
945-
(
946-
<ST_SEMICOLON>
947-
try {
948-
{ if (stm2!=null)
949-
ifElseStatement.setUsingSemicolonForElseStatement(true);
950-
else if (ifElseStatement!=null)
951-
ifElseStatement.setUsingSemicolonForIfStatement(true); }
952-
(
953-
<K_IF> condition=Condition()
954-
( stm = SingleStatement() | stm = Block() ) { ifElseStatement = new IfElseStatement(condition, stm); }
955-
[ LOOKAHEAD(2)
956-
[ <ST_SEMICOLON> { ifElseStatement.setUsingSemicolonForIfStatement(true); } ]
957-
<K_ELSE> ( stm2 = SingleStatement() | stm2 = Block() ) { ifElseStatement.setElseStatement(stm2); }
958-
]
959-
960-
{ stmts.add( ifElseStatement ); }
961-
)
962-
|
963-
(
964-
stm = SingleStatement()
965-
| stm = Block()
966-
) { System.out.println("found normal statement"); stmts.add(stm); }
967-
|
968-
stm = UnsupportedStatement()
969-
{ System.out.println("found uns. statement"); if ( !((UnsupportedStatement) stm).isEmpty() ) stmts.add(stm); }
970-
971-
} catch (ParseException ex) {
972-
if ( getAsBoolean(Feature.allowUnsupportedStatements) ) {
973-
parseErrors.add(ex);
974-
UnsupportedStatement unsupportedStatement = new UnsupportedStatement( error_skipto(ST_SEMICOLON) );
975-
if (!unsupportedStatement.isEmpty()) {
976-
stmts.add( unsupportedStatement );
977-
System.out.println("Exception!");
978-
}
979-
} else if ( errorRecovery ) {
980-
parseErrors.add(ex);
981-
error_skipto(ST_SEMICOLON);
982-
stmts.add( null );
983-
} else {
984-
throw ex;
985-
}
913+
[
914+
LOOKAHEAD( { getAsBoolean(Feature.allowUnsupportedStatements) } )
915+
stm = UnsupportedStatement()
916+
{
917+
if (!( (UnsupportedStatement) stm).isEmpty()) {
918+
stmts.add( stm );
919+
}
986920
}
987-
)*
921+
]
988922

989-
<EOF>
990-
)
923+
)*
924+
925+
<EOF>
991926
{
992927
return stmts;
993928
}
@@ -998,10 +933,8 @@ List<String> error_skipto(int kind) {
998933
ArrayList<String> tokenImages = new ArrayList<String>();
999934
ParseException e = generateParseException();
1000935
Token t;
1001-
System.out.println("Current Token:" + getToken(0).image );
1002936
do {
1003937
t = getNextToken();
1004-
System.out.println("Next Token:" + t.image );
1005938
if (t.kind != kind && t.kind != EOF) {
1006939
tokenImages.add(t.image);
1007940
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,8 @@ public void testParseStatementsFail() throws Exception {
227227
@Override
228228
public void execute() throws Throwable {
229229
final Statements statements = CCJSqlParserUtil.parseStatements(
230-
sqlStr
231-
, parser -> parser.withErrorRecovery(true).withUnsupportedStatements(true)
232-
);
230+
sqlStr,
231+
parser -> parser.withErrorRecovery(true).withUnsupportedStatements(true));
233232
assertEquals(2, statements.size());
234233
assertInstanceOf(PlainSelect.class, statements.get(0));
235234
assertInstanceOf(UnsupportedStatement.class, statements.get(1));

src/test/java/net/sf/jsqlparser/statement/StatementsTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ public void testStatementsErrorRecovery2() throws JSQLParserException, ParseExce
6868
parser.setErrorRecovery(true);
6969
Statements parseStatements = parser.Statements();
7070

71-
assertEquals(1, parseStatements.size());
71+
assertEquals(2, parseStatements.size());
7272

7373
assertInstanceOf(Select.class, parseStatements.get(0));
7474
assertEquals(1, parser.getParseErrors().size());
7575
}
7676

7777
@Test
7878
public void testStatementsErrorRecovery3() throws JSQLParserException, ParseException {
79-
String sqlStr = "select * from mytable; select from; select * from mytable2";
80-
81-
CCJSqlParser parser = new CCJSqlParser(new StringProvider(sqlStr));
82-
parser.setErrorRecovery(true);
83-
84-
Statements statements = parser.Statements();
79+
CCJSqlParser parser =
80+
new CCJSqlParser("select * from mytable; select from; select * from mytable2");
81+
Statements statements = parser.withErrorRecovery().Statements();
8582

8683
assertEquals(3, statements.size());
8784

@@ -94,16 +91,17 @@ public void testStatementsErrorRecovery3() throws JSQLParserException, ParseExce
9491

9592
@Test
9693
public void testStatementsErrorRecovery4() throws JSQLParserException {
97-
String sqlStr = "select * from mytable; select from; select * from mytable2; select 4;";
98-
99-
Statements statements = CCJSqlParserUtil.parseStatements(sqlStr
100-
, parser -> parser.withUnsupportedStatements(true).withErrorRecovery(true));
94+
Statements statements = CCJSqlParserUtil.parseStatements(
95+
"select * from mytable; select from; select * from mytable2; select 4;",
96+
parser -> parser.withUnsupportedStatements());
10197

10298
assertEquals(4, statements.size());
10399

104100
assertInstanceOf(Select.class, statements.get(0));
105101
assertInstanceOf(UnsupportedStatement.class, statements.get(1));
106102
assertInstanceOf(Select.class, statements.get(2));
107103
assertInstanceOf(Select.class, statements.get(3));
104+
105+
assertEquals("select from", statements.get(1).toString());
108106
}
109107
}

src/test/java/net/sf/jsqlparser/statement/UnsupportedStatementTest.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
package net.sf.jsqlparser.statement;
1111

1212
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
1314
import static org.junit.jupiter.api.Assertions.assertTrue;
1415

1516
import net.sf.jsqlparser.JSQLParserException;
1617
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
1718
import net.sf.jsqlparser.statement.select.Select;
1819
import net.sf.jsqlparser.test.TestUtils;
1920
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Disabled;
2022
import org.junit.jupiter.api.Test;
2123
import org.junit.jupiter.api.function.Executable;
2224

@@ -36,16 +38,19 @@ public void execute() throws Throwable {
3638
});
3739
}
3840

41+
// This test does not work since the first statement MUST be a regular statement
42+
// for the current grammar to work
3943
@Test
44+
@Disabled
4045
public void testUnsupportedStatementsFirstInBlock() throws JSQLParserException {
4146
String sqlStr = "This is an unsupported statement; Select * from dual; Select * from dual;";
4247

4348
Statements statements = CCJSqlParserUtil.parseStatements(sqlStr,
4449
parser -> parser.withUnsupportedStatements(true));
45-
Assertions.assertEquals(3, statements.getStatements().size());
46-
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.getStatements().get(0));
47-
Assertions.assertInstanceOf(Select.class, statements.getStatements().get(1));
48-
Assertions.assertInstanceOf(Select.class, statements.getStatements().get(2));
50+
Assertions.assertEquals(3, statements.size());
51+
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.get(0));
52+
Assertions.assertInstanceOf(Select.class, statements.get(1));
53+
Assertions.assertInstanceOf(Select.class, statements.get(2));
4954

5055
Assertions.assertThrowsExactly(JSQLParserException.class, new Executable() {
5156
@Override
@@ -64,28 +69,48 @@ public void testUnsupportedStatementsMiddleInBlock() throws JSQLParserException
6469
parser -> parser.withUnsupportedStatements(true).withErrorRecovery(true));
6570
Assertions.assertEquals(3, statements.size());
6671

67-
Assertions.assertInstanceOf(Select.class, statements.getStatements().get(0));
68-
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.getStatements().get(1));
69-
Assertions.assertInstanceOf(Select.class, statements.getStatements().get(2));
72+
Assertions.assertInstanceOf(Select.class, statements.get(0));
73+
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.get(1));
74+
Assertions.assertInstanceOf(Select.class, statements.get(2));
75+
76+
Assertions.assertThrowsExactly(JSQLParserException.class, new Executable() {
77+
@Override
78+
public void execute() throws Throwable {
79+
CCJSqlParserUtil.parseStatements(sqlStr,
80+
parser -> parser.withUnsupportedStatements(false));
81+
}
82+
});
83+
}
84+
85+
@Test
86+
public void testTwoUnsupportedStatementsMiddleInBlock() throws JSQLParserException {
87+
String sqlStr =
88+
"Select * from dual; This is an unsupported statement; Some more rubbish; Select * from dual;";
89+
90+
Statements statements = CCJSqlParserUtil.parseStatements(sqlStr,
91+
parser -> parser.withUnsupportedStatements(true).withErrorRecovery(true));
92+
Assertions.assertEquals(4, statements.size());
7093

71-
// This will not fail, but always return the Unsupported Statements
72-
// Since we can't LOOKAHEAD in the Statements() production
94+
Assertions.assertInstanceOf(Select.class, statements.get(0));
95+
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.get(1));
96+
Assertions.assertInstanceOf(UnsupportedStatement.class, statements.get(2));
97+
Assertions.assertInstanceOf(Select.class, statements.get(3));
7398

74-
// Assertions.assertThrowsExactly(JSQLParserException.class, new Executable() {
75-
// @Override
76-
// public void execute() throws Throwable {
77-
// CCJSqlParserUtil.parseStatements(sqlStr, parser ->
78-
// parser.withUnsupportedStatements(false) );
79-
// }
80-
// });
99+
Assertions.assertThrowsExactly(JSQLParserException.class, new Executable() {
100+
@Override
101+
public void execute() throws Throwable {
102+
CCJSqlParserUtil.parseStatements(sqlStr,
103+
parser -> parser.withUnsupportedStatements(false));
104+
}
105+
});
81106
}
82107

83108
@Test
84109
void testAlter() throws JSQLParserException {
85110
String sqlStr =
86111
"ALTER INDEX idx_t_fa RENAME TO idx_t_fb";
87112
Statement statement = TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
88-
assertTrue(statement instanceof UnsupportedStatement);
113+
assertInstanceOf(UnsupportedStatement.class, statement);
89114
}
90115

91116
@Test

src/test/java/net/sf/jsqlparser/util/validation/ValidationTest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
public class ValidationTest extends ValidationTestAsserts {
3636

3737
public static void main(String args[]) {
38-
System.out.println("mysql" + MySqlVersion.V8_0.getNotContained(MariaDbVersion.V10_5_4.getFeatures()));
39-
System.out.println("mariadb" + MariaDbVersion.V10_5_4.getNotContained(MySqlVersion.V8_0.getFeatures()));
38+
System.out.println(
39+
"mysql" + MySqlVersion.V8_0.getNotContained(MariaDbVersion.V10_5_4.getFeatures()));
40+
System.out.println("mariadb"
41+
+ MariaDbVersion.V10_5_4.getNotContained(MySqlVersion.V8_0.getFeatures()));
4042
}
4143

4244
@Test
@@ -52,18 +54,21 @@ public void testValidationWithStatementValidator() throws JSQLParserException {
5254
Map<ValidationCapability, Set<ValidationException>> unsupportedErrors = validator
5355
.getValidationErrors(DatabaseType.SQLSERVER);
5456
assertErrorsSize(unsupportedErrors, 1);
55-
assertNotSupported(unsupportedErrors.get(DatabaseType.SQLSERVER), Feature.oracleOldJoinSyntax);
57+
assertNotSupported(unsupportedErrors.get(DatabaseType.SQLSERVER),
58+
Feature.oracleOldJoinSyntax);
5659

5760
unsupportedErrors = validator.getValidationErrors(DatabaseType.POSTGRESQL);
5861
assertErrorsSize(unsupportedErrors, 1);
59-
assertNotSupported(unsupportedErrors.get(DatabaseType.POSTGRESQL), Feature.oracleOldJoinSyntax);
62+
assertNotSupported(unsupportedErrors.get(DatabaseType.POSTGRESQL),
63+
Feature.oracleOldJoinSyntax);
6064
}
6165

6266
@Test
6367
public void testWithValidation() throws JSQLParserException {
6468

6569
String stmt = "SELECT * FROM tab1, tab2 WHERE tab1.id (+) = tab2.ref";
66-
List<ValidationError> errors = Validation.validate(Collections.singletonList(DatabaseType.SQLSERVER), stmt);
70+
List<ValidationError> errors =
71+
Validation.validate(Collections.singletonList(DatabaseType.SQLSERVER), stmt);
6772

6873
assertErrorsSize(errors, 1);
6974
assertEquals(stmt, errors.get(0).getStatements());
@@ -125,7 +130,9 @@ public void testWithValidationUpdateButAcceptOnlySelects() throws JSQLParserExce
125130

126131
String stmt = "UPDATE tab1 t1 SET t1.ref = ? WHERE t1.id = ?";
127132
List<ValidationError> errors = Validation.validate(
128-
Arrays.asList(DatabaseType.POSTGRESQL, FeaturesAllowed.SELECT.copy().add(FeaturesAllowed.JDBC)), stmt);
133+
Arrays.asList(DatabaseType.POSTGRESQL,
134+
FeaturesAllowed.SELECT.copy().add(FeaturesAllowed.JDBC)),
135+
stmt);
129136

130137
assertErrorsSize(errors, 1);
131138
assertNotAllowed(errors.get(0).getErrors(), Feature.update);
@@ -142,19 +149,23 @@ public void testWithValidatonAcceptOnlySelects() throws JSQLParserException {
142149

143150
@Test
144151
public void testFeatureSetName() {
145-
assertEquals("SELECT + jdbc", FeaturesAllowed.SELECT.copy().add(FeaturesAllowed.JDBC).getName());
152+
assertEquals("SELECT + jdbc",
153+
FeaturesAllowed.SELECT.copy().add(FeaturesAllowed.JDBC).getName());
146154
assertEquals("UPDATE + SELECT", FeaturesAllowed.UPDATE.getName());
147155
assertEquals("DELETE + SELECT", FeaturesAllowed.DELETE.getName());
148156
assertEquals("DELETE + SELECT + UPDATE + jdbc",
149-
FeaturesAllowed.DELETE.copy().add(FeaturesAllowed.UPDATE).add(FeaturesAllowed.JDBC).getName());
150-
assertEquals("UPDATE + SELECT", new FeaturesAllowed().add(FeaturesAllowed.UPDATE).getName());
157+
FeaturesAllowed.DELETE.copy().add(FeaturesAllowed.UPDATE).add(FeaturesAllowed.JDBC)
158+
.getName());
159+
assertEquals("UPDATE + SELECT",
160+
new FeaturesAllowed().add(FeaturesAllowed.UPDATE).getName());
151161
assertEquals("UPDATE + SELECT + feature set",
152162
FeaturesAllowed.UPDATE.copy().add(new FeaturesAllowed(Feature.commit)).getName());
153163
}
154164

155165
@Test
156166
public void testRowConstructorValidation() throws JSQLParserException {
157-
String stmt = "SELECT CAST(ROW(dataid, value, calcMark) AS ROW(datapointid CHAR, value CHAR, calcMark CHAR))";
167+
String stmt =
168+
"SELECT CAST(ROW(dataid, value, calcMark) AS ROW(datapointid CHAR, value CHAR, calcMark CHAR))";
158169
List<ValidationError> errors = Validation.validate(
159170
Arrays.asList(DatabaseType.ANSI_SQL, FeaturesAllowed.SELECT), stmt);
160171
assertErrorsSize(errors, 0);

0 commit comments

Comments
 (0)