4040@ SuppressWarnings ("PMD.CyclomaticComplexity" )
4141public final class CCJSqlParserUtil {
4242 public final static Logger LOGGER = Logger .getLogger (CCJSqlParserUtil .class .getName ());
43- public final static int ALLOWED_NESTING_DEPTH = 16 ;
4443
4544 static {
4645 LOGGER .setLevel (Level .OFF );
@@ -50,7 +49,7 @@ private CCJSqlParserUtil() {}
5049
5150 public static Statement parse (Reader statementReader ) throws JSQLParserException {
5251 ExecutorService executorService = Executors .newSingleThreadExecutor ();
53- Statement statement = null ;
52+ Statement statement ;
5453 CCJSqlParser parser = new CCJSqlParser (new StreamProvider (statementReader ));
5554 try {
5655 statement = parseStatement (parser , executorService );
@@ -86,7 +85,7 @@ public static Statement parse(String sql, Consumer<CCJSqlParser> consumer)
8685 }
8786
8887 ExecutorService executorService = Executors .newSingleThreadExecutor ();
89- Statement statement = null ;
88+ Statement statement ;
9089 try {
9190 statement = parse (sql , executorService , consumer );
9291 } finally {
@@ -102,20 +101,22 @@ public static Statement parse(String sql, ExecutorService executorService,
102101 return null ;
103102 }
104103
105- Statement statement = null ;
104+ Statement statement ;
106105 // first, try to parse fast and simple
107106 CCJSqlParser parser = newParser (sql );
108107 if (consumer != null ) {
109108 consumer .accept (parser );
110109 }
111- boolean allowComplex = parser .getConfiguration ().getAsBoolean (Feature .allowComplexParsing );
110+ boolean allowComplex = parser .getAsBoolean (Feature .allowComplexParsing );
111+ int allowedNestingDepth = parser .getAsInt (Feature .allowedNestingDepth );
112112 LOGGER .info ("Allowed Complex Parsing: " + allowComplex );
113113 try {
114114 LOGGER .info ("Trying SIMPLE parsing " + (allowComplex ? "first" : "only" ));
115115 statement = parseStatement (parser .withAllowComplexParsing (false ), executorService );
116116 } catch (JSQLParserException ex ) {
117117 LOGGER .info ("Nesting Depth" + getNestingDepth (sql ));
118- if (allowComplex && getNestingDepth (sql ) <= ALLOWED_NESTING_DEPTH ) {
118+ if (allowComplex
119+ && (allowedNestingDepth < 0 || getNestingDepth (sql ) <= allowedNestingDepth )) {
119120 LOGGER .info ("Trying COMPLEX parsing when SIMPLE parsing failed" );
120121 // beware: the parser must not be reused, but needs to be re-initiated
121122 parser = newParser (sql );
@@ -222,23 +223,21 @@ public static Expression parseExpression(String expressionStr, boolean allowPart
222223 } catch (JSQLParserException ex1 ) {
223224 // when fast simple parsing fails, try complex parsing but only if it has a chance to
224225 // succeed
225- if (getNestingDepth (expressionStr ) <= ALLOWED_NESTING_DEPTH ) {
226- CCJSqlParser parser = newParser (expressionStr ).withAllowComplexParsing (true );
227- if (consumer != null ) {
228- consumer .accept (parser );
229- }
230- try {
231- expression = parser .Expression ();
232- if (!allowPartialParse
233- && parser .getNextToken ().kind != CCJSqlParserTokenManager .EOF ) {
234- throw new JSQLParserException (
235- "could only parse partial expression " + expression .toString ());
236- }
237- } catch (JSQLParserException ex ) {
238- throw ex ;
239- } catch (ParseException ex ) {
240- throw new JSQLParserException (ex );
226+ CCJSqlParser parser = newParser (expressionStr ).withAllowComplexParsing (true );
227+ if (consumer != null ) {
228+ consumer .accept (parser );
229+ }
230+ try {
231+ expression = parser .Expression ();
232+ if (!allowPartialParse
233+ && parser .getNextToken ().kind != CCJSqlParserTokenManager .EOF ) {
234+ throw new JSQLParserException (
235+ "could only parse partial expression " + expression .toString ());
241236 }
237+ } catch (JSQLParserException ex ) {
238+ throw ex ;
239+ } catch (ParseException ex ) {
240+ throw new JSQLParserException (ex );
242241 }
243242 }
244243 return expression ;
@@ -301,24 +300,22 @@ public static Expression parseCondExpression(String conditionalExpressionStr,
301300 throw new JSQLParserException (ex );
302301 }
303302 } catch (JSQLParserException ex1 ) {
304- if (getNestingDepth (conditionalExpressionStr ) <= ALLOWED_NESTING_DEPTH ) {
305- CCJSqlParser parser =
306- newParser (conditionalExpressionStr ).withAllowComplexParsing (true );
307- if (consumer != null ) {
308- consumer .accept (parser );
309- }
310- try {
311- expression = parser .Expression ();
312- if (!allowPartialParse
313- && parser .getNextToken ().kind != CCJSqlParserTokenManager .EOF ) {
314- throw new JSQLParserException (
315- "could only parse partial expression " + expression .toString ());
316- }
317- } catch (JSQLParserException ex ) {
318- throw ex ;
319- } catch (ParseException ex ) {
320- throw new JSQLParserException (ex );
303+ CCJSqlParser parser =
304+ newParser (conditionalExpressionStr ).withAllowComplexParsing (true );
305+ if (consumer != null ) {
306+ consumer .accept (parser );
307+ }
308+ try {
309+ expression = parser .Expression ();
310+ if (!allowPartialParse
311+ && parser .getNextToken ().kind != CCJSqlParserTokenManager .EOF ) {
312+ throw new JSQLParserException (
313+ "could only parse partial expression " + expression .toString ());
321314 }
315+ } catch (JSQLParserException ex ) {
316+ throw ex ;
317+ } catch (ParseException ex ) {
318+ throw new JSQLParserException (ex );
322319 }
323320 }
324321 return expression ;
@@ -334,15 +331,15 @@ public static Expression parseCondExpression(String conditionalExpressionStr,
334331
335332 public static Statement parseStatement (CCJSqlParser parser , ExecutorService executorService )
336333 throws JSQLParserException {
337- Statement statement = null ;
334+ Statement statement ;
338335 Future <Statement > future = executorService .submit (new Callable <Statement >() {
339336 @ Override
340337 public Statement call () throws ParseException {
341338 return parser .Statement ();
342339 }
343340 });
344341 try {
345- statement = future .get (parser .getConfiguration (). getAsLong (Feature .timeOut ),
342+ statement = future .get (parser .getAsLong (Feature .timeOut ),
346343 TimeUnit .MILLISECONDS );
347344 } catch (TimeoutException ex ) {
348345 parser .interrupted = true ;
@@ -397,15 +394,17 @@ public static Statements parseStatements(String sqls, ExecutorService executorSe
397394 if (consumer != null ) {
398395 consumer .accept (parser );
399396 }
400- boolean allowComplex = parser .getConfiguration ().getAsBoolean (Feature .allowComplexParsing );
397+ boolean allowComplex = parser .getAsBoolean (Feature .allowComplexParsing );
398+ int allowedNestingDepth = parser .getAsInt (Feature .allowedNestingDepth );
401399
402400 // first, try to parse fast and simple
403401 try {
404402 statements = parseStatements (parser .withAllowComplexParsing (false ), executorService );
405403 } catch (JSQLParserException ex ) {
406404 // when fast simple parsing fails, try complex parsing but only if it has a chance to
407405 // succeed
408- if (allowComplex && getNestingDepth (sqls ) <= ALLOWED_NESTING_DEPTH ) {
406+ if (allowComplex
407+ && (allowedNestingDepth < 0 || getNestingDepth (sqls ) <= allowedNestingDepth )) {
409408 // beware: parser must not be re-used but needs to be re-initiated
410409 parser = newParser (sqls );
411410 if (consumer != null ) {
@@ -434,7 +433,7 @@ public Statements call() throws ParseException {
434433 }
435434 });
436435 try {
437- statements = future .get (parser .getConfiguration (). getAsLong (Feature .timeOut ),
436+ statements = future .get (parser .getAsLong (Feature .timeOut ),
438437 TimeUnit .MILLISECONDS );
439438 } catch (TimeoutException ex ) {
440439 parser .interrupted = true ;
@@ -450,17 +449,14 @@ public static void streamStatements(StatementListener listener, InputStream is,
450449 throws JSQLParserException {
451450 try {
452451 CCJSqlParser parser = newParser (is , encoding );
453- while ( true ) {
452+ do {
454453 Statement stmt = parser .SingleStatement ();
455454 listener .accept (stmt );
456455 if (parser .getToken (1 ).kind == CCJSqlParserTokenManager .ST_SEMICOLON ) {
457456 parser .getNextToken ();
458457 }
459458
460- if (parser .getToken (1 ).kind == CCJSqlParserTokenManager .EOF ) {
461- break ;
462- }
463- }
459+ } while (parser .getToken (1 ).kind != CCJSqlParserTokenManager .EOF );
464460 } catch (Exception ex ) {
465461 throw new JSQLParserException (ex );
466462 }
0 commit comments