SQL Grammer Parameters

Oct 15, 2013 at 10:02 AM
First off great work. This project is immense.

I would like to use it to basically interrogate an SQL statement for the Parameters. Now that doesnt seem part of the SQL Grammer and I am not exactly sure how to implement it.

Basically a parameter starts with @ and contains one or more alpha numeric characters after it. Parameters can only appear int the WHERE Clause.

Any help, or pointers on how to start adding these commands would be very helpful.

Cheers, Nico
Oct 15, 2013 at 6:07 PM
just start with defining parameter as IdentifierTerminal with only '@' as start symbol - you can specify it in constructor. Everything else should be the same, just add the parameter in appropriate place in expressions, (somewhere along with column references and constants/literals)
Oct 16, 2013 at 12:40 AM
Hello rivantsov,

Thanks for the advice.. So i have added an IndetifierTerminal as below
var Parameter = new IdentifierTerminal("Parameter", "", "@");
and then added it to the expression such as
expression.Rule = term |  unExpr | binExpr | Parameter;
However by doing this now matches all IDs as parameters so I am not sure why when using the IdentifierTerminal start character of @ should do it?

Please help :)
Oct 16, 2013 at 1:28 AM

So I think I have gotten this to work but it made me have to enter the @ character as punctuation. Not sure if this is what it should be doing, but here is the changes.
NonTerminal p_term = new NonTerminal("Parameter");
Terminal p_id = new IdentifierTerminal("param");
p_term.Rule = "@" + p_id;

expression.Rule = term | unExpr | binExpr | p_term;
Next I need to figure out how to rebuild the statement after manipulating it... Hmmm
Oct 16, 2013 at 4:12 AM
well, this is wrong way to go, with @ as punctuation symbol.
Sorry I misled you in my advice. Specifying '@' in constructor adds the symbol to default list of characters, not replaces it. That's why this behavior. Here's an easy solution - do not define extra terminal for parameter, just modify the regular identifier by using AddPrefix method and specifying '@' as a valid prefix to the name. After parsing, the token has Details property which is an instance of CompoundTokenDetails class, which has Prefix property - that's how you can determine if it is parameter or not.
Alternatively, following the same road as before, you define parameter as IdentifierTerminal, but instead of specifying '@' in constructor you set the property AllFirstChars to this single symbol - then it will work, and you will have a separate terminal for parameters, might be easier to analyze the tree nodes.