Reputation: 1204
Is there an ANTLR grammar for just the where clause of an ANSI SQL query?
i am trying to parse the conditions in where clause and change the column in that condition to something relevant to the indexes i have, so that my performance can be improved.
i want to automate this feature, so want to parse the dynamic sql and change the where clause dynamically.
Upvotes: 1
Views: 983
Reputation: 95392
Probably not.
I assume you've found an ANTLR grammar for all of ANSI SQL. Picking out the subset that is just the where clause shouldn't really be hard.
What you'll have a harder time with is modifying the AST and regenerating source text, which I presume you want. ANTLR parsers provide no specific help in modifying the AST other than a library for hacking at the nodes as you visit them. Regeneration of source is completely on you; it can be implemented by spitting strings as you walk the tree or better, using String templates. But what has happened now is that your interest in modifying the statement has turned into a big(?) engineering job before you can do it. See my essay on "Life After Parsing" (from bio or by googling).
I think what you want is a program transformation system. These are tools that given a grammar, can parse source code to ASTs, and carry out transformations on the AST to achieve the effect you want. PrettyPrinters to regenerate source text are usually provided.
Upvotes: 1