1

I am using JSqlParser 3.0 and my query is SELECT * from [dev-testdb].dbo.EMPLOYEES. I am trying to remove [] characters because JSqlParser 3.0 version is not supported for these characters.I guess 1.x does but I have to use 3.0.

After remove brackets, my query seems like SELECT * from dev-testdb.dbo.EMPLOYEES . I am debugging my project and on this command

final Statement statement = CCJSqlParserUtil.parse(sql); I catch this exception

net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "-" "-"
    at line 1, column 18.

How can I fix this 3 characters when I parse with JSqlParser?

displayname
  • 148
  • 1
  • 15
  • You have to have the brackets. A name like `dev-testdb` must be delimit identified. This is why you should avoid names that have that requirement. – Thom A Dec 02 '19 at 08:43
  • There multiple issues regarding your issue on JSqlParser's Github repo. Try to check those issues: https://github.com/JSQLParser/JSqlParser/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+Encountered+unexpected+token – furkanayd Dec 02 '19 at 08:44
  • Yes, I just removed '[ ]' characters when there is no '-' character. But now I cannot remove them too. Is there any way support this query style ? – displayname Dec 02 '19 at 08:48
  • Sounds like the problem is the language, and it's lack of support of delimit identified objects, than the Query. Delimit Identifying objects is quite common, and if a language doesn't support that, it's (significantly) flawed, in my view. – Thom A Dec 02 '19 at 08:50
  • @Larnu: in standard SQL you would have to write that as `"dev-testdb"` the square brackets are for array access in SQL. @bilinmeyenkarakter as SQL Server also supports standard double quotes for quoted identifiers, you could simply replace `[` and `]` with `"` –  Dec 02 '19 at 10:28

1 Answers1

2

It is not an issue of JSqlParser but its evolution. :)

Bracketquotation was switched off by default, to support all kinds of array syntax, which uses in most cases square brackets as well. This change was introduced with JSqlParser 3.0.

The array parsing is the default behaviour. Square bracket quotation has to be enabled using a parser flag (CCJSqlParser.withSquareBracketQuotation).

Here is the discussion: https://github.com/JSQLParser/JSqlParser/issues/677.

To activate this bracket quotation again use something like:

CCJSqlParserUtil.parse("select * from [mytable]", parser -> parser.withSquareBracketQuotation(true));

The lambda expression is used to somehow modify the used parsers configuration.

wumpz
  • 8,257
  • 3
  • 30
  • 25