[antlr-interest] Help required with altering the structure of a statement

Varun Bhargava varun.bhargava at carsales.com.au
Sun Aug 3 23:13:31 PDT 2008


Hi All,

 

I have a task to substitute some placeholders in a statement with
parameters values that the user can select from a UI. Here's an example
of what I want

 

SELECT id from mytable WHERE (a = 23 AND (b = @PARAM1 OR d = 34) OR e =
44)

 

I need to substitute @PARAM1 with the actual value that the user selects
from a UI and replace it in the WHERE clause, then regenerate the
statement.

 

If @PARAM1 is NULL then I need to remove the comparison clause (b =
@PARAM1) and regenerate the query which should now look like the
following.

 

SELECT id from mytable WHERE (a = 23 AND (d = 34) OR e = 44)

 

 

Here is the pseudocode of what I have thought :

 

1.	Iterate the nodes and when the node is a placeholder, replace it
with the value
2.	If the value that will replace the parameter is NULL then mark
the comparison to be deleted from the tree
3.	Find the dangling instances of ANDs and Ors after NULL
comparison nodes have been deleted 

 

I am an absolute beginner to ANTLR but just can't figure out how to do
the above without using a heap of if's and else's and dirty code. I've
been told this is trivial for ANTLR. Can someone please help me out on
how I can achieve this?

 

Rewrite rules? StringTemplates? I just can't find any good examples to
get me started.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080804/4aa224d9/attachment.html 


More information about the antlr-interest mailing list