Dynamic queries having tokens in square brackets
See original GitHub issueDescribe the bug SQL statements which have tokens as part of the query end up in error.
To Reproduce Steps to reproduce the behavior:
-
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]
-
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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So can we close this?
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.