question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic queries having tokens in square brackets

See original GitHub issue

Describe the bug SQL statements which have tokens as part of the query end up in error.

To Reproduce Steps to reproduce the behavior:

  1. Example SQL select spr.column_1, spr.column_2, spr.column_3, spr.column_4, spr.column_5, spr.column_6 from spr_table spr where spr.column_1 > 0 and [WHERECLAUSE]

  2. Exception

 net.sf.jsqlparser.JSQLParserException
 	at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:137)
 	at com.scanner.JsqlParserExample.main(JsqlParserExample.java:12)
 Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "[" "["
     at line 21, column 1.
 
 Was expecting one of:
 
     "!"
     "("
     "NOT"
 
 	at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:22439)
 	at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:22286)
 	at net.sf.jsqlparser.parser.CCJSqlParser.AndExpression(CCJSqlParser.java:7606)
 	at net.sf.jsqlparser.parser.CCJSqlParser.OrExpression(CCJSqlParser.java:7475)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Expression(CCJSqlParser.java:7446)
 	at net.sf.jsqlparser.parser.CCJSqlParser.WhereClause(CCJSqlParser.java:6425)
 	at net.sf.jsqlparser.parser.CCJSqlParser.PlainSelect(CCJSqlParser.java:3794)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SetOperationList(CCJSqlParser.java:3973)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SelectBody(CCJSqlParser.java:3649)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Select(CCJSqlParser.java:3642)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SingleStatement(CCJSqlParser.java:124)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Statements(CCJSqlParser.java:479)
 	at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:135)
 	... 1 more
 Caused by:
 net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "[" "["
     at line 21, column 1.
 
Was expecting one of:
 
     "!"
     "("
     "NOT"
 
 	at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:22439)
 	at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:22286)
 	at net.sf.jsqlparser.parser.CCJSqlParser.AndExpression(CCJSqlParser.java:7606)
 	at net.sf.jsqlparser.parser.CCJSqlParser.OrExpression(CCJSqlParser.java:7475)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Expression(CCJSqlParser.java:7446)
 	at net.sf.jsqlparser.parser.CCJSqlParser.WhereClause(CCJSqlParser.java:6425)
 	at net.sf.jsqlparser.parser.CCJSqlParser.PlainSelect(CCJSqlParser.java:3794)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SetOperationList(CCJSqlParser.java:3973)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SelectBody(CCJSqlParser.java:3649)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Select(CCJSqlParser.java:3642)
 	at net.sf.jsqlparser.parser.CCJSqlParser.SingleStatement(CCJSqlParser.java:124)
 	at net.sf.jsqlparser.parser.CCJSqlParser.Statements(CCJSqlParser.java:479)
 	at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:135)
 	

Expected behavior The parser should attempt to fetch/extract/parse as much details as possible. In the above example, DML operation and the table name could have been extracted.

System

  • Database = ORACLE
  • Java Version = 1.8
  • JSqlParser version = tested with 3.1 & 3.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wumpzcommented, Aug 30, 2020

So can we close this?

1reaction
wumpzcommented, Aug 17, 2020

Those queries with this query tokens are indeed incorrect from JSqlParsers perspective, because to this parser it is all SQL. Therefore it interprets those bracket parts like I described above. First you should replace those parts with a valid SQL and then send it through JSqlParser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a need to include brackets [] when running a query in ...
Enclosing a string in square braces is tells SQL Server that it should not try to parse whatever is inside them.
Read more >
How to write soql query with open, close braces
Try this. string teamquery = 'SELECT field1__c, field2__c '+ 'FROM ' +packageNamespace+'Object__c '+ 'WHERE '+ packageNamespace+'field1__c ...
Read more >
Use Tokens in Job Steps - SQL Server Agent | Microsoft Learn
Replaces one double quotation mark with two double quotation marks. Escapes right brackets (]) in the token replacement string. Replaces one ...
Read more >
The Curse and Blessings of Dynamic SQL - Erland Sommarskog
Just as when I call a stored procedure, I need to specify OUTPUT also for the actual parameter. I don't pass a value...
Read more >
Tokens | DNN Sharp Documentation Center
The value of any form fields can be references using a token, which is its ID between square brackets. For example, if the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found