Get the position of an Where/From AST item in the input
(Sorry for opening a new issue. I could not find a way to reopen the previous one.)
Unfortunately, this does not work for all items. Or am I missing something?
For example, I encountered the following problems:
FromItem (PlainSelect#getFromItem) is not an ASTNodeAccess. Expression (PlainSelect#getWhere) is null for complex expressions.
Originally posted by @Wisser in https://github.com/JSQLParser/JSqlParser/issues/1219#issuecomment-922805082
Greetings.
Apologies, I am just guessing:
FromItem is an interface and you would need to cast it to one of its implementations, which will give the ASTNode.
Same for the Expression.
Please post your particular query and the code you have written so far. I would like to experiment a bit with it.
Although SelectItem is also an interface but extends ASTNodeAccess.
To me, it looks like some housekeeping was needed on these old parts. Post your samples and I will see, if I can do anything.
Hello,
thank you very much for your effort.
It looks like not all implementations of FromItem also implement ASTNodeAccess.
Also, ASTNodeAccess.getASTNode() is null in some cases.
You'll find some test code demonstrating both in the attachment. It produces for me:
'1 + 2': is ASTNodeAccess but ASTNode is null
'Values(1)': is no ASTNodeAccess. It's a net.sf.jsqlparser.statement.select.TableFunction
'x IS NULL': is ASTNodeAccess but ASTNode is null
'x + y': is ASTNodeAccess but ASTNode is null
Thank you for the examples, I will try to look at it and fix it -- but no promises.
As always the connection to ASTNodes was request driven (and is). Unfortunately JavaCC does not provide some kind of automatism to achieve that. Therefore we have to construct this manually.
Another point is, that not all kind of productions create ASTNodes. I tested this and found a performance degradation of nearly 50%. However, maybe newer versions of JavaCC does provide this in a more performant way.
Thanks for the clarification.
Would it be possible to adjust the parser so that every Expression (which is in fact an ASTNodeAccess), always returns a non-zero ASTNode?
Would it not be better to write a Visitor, which builds the AST with additional information on each node.
I'm afraid I don't understand what you mean.
JSQLParser supports the Visitor Pattern to travers through all the elements of a Statement or Expression. This is used for the Deparser and the Validator already.
Now, instead of Deparsing into a String you could use the same mechanic to write a Tree or a XML representing a Tree.
I already use a Visitor. That is not the problem. I want to find the exact position of Expressions (and other phrases) in the SQL statement underlying the AST (the parser's input).
As mentioned here: https://github.com/JSQLParser/JSqlParser/issues/1219
I want to find the exact position of
Expressions (and other phrases) in the SQL statement underlying the AST (the parser's input). As mentioned here: #1219
Yes, I understand that. Please look into the Deparser Visitor and imagine to write Nodes into a Tree instead of Strings into a StringBuilder. Each Node could hold the Start/End Position and the Caption and the Node Class Name as additional attribute.
I still don't get it, I'm afraid. The two statements:
Select 1
and
Select
1
(two lines)
lead to the same AST. Only the position information differs. But I do not get this information.