tags:

views:

313

answers:

1

I need to parse a boolean search string of keywords and operators into a SQL query to be executed from C# code. I think something like ANTLR is what I need for this task, but I'm not sure how to do it.

Are there any good tutorials on how to do this? Or maybe I need a different tool?

An example of what I mean is below. The only operators I need are AND and OR. I would also like to be able to use parenthesis.

input expression: (blue AND green) OR yellow

output:

SELECT * FROM table WHERE (CONTAINS(Description, "blue") AND CONTAINS(Description, "green")) OR >CONTAINS(Description, "yellow"

If possible I would also like to support implicit AND insertion. So the default operator if none is specified is AND.

input : green (blue OR yellow)

output: SELECT * FROM table WHERE CONTAINS(Description, "green") AND (CONTAINS(Description, "blue") OR >CONTAINS(Description, "yellow"))

A: 

I'd probably do this with straight text processing. Essentially what you want to do is take the original expression, pull out all the colors and replace them with the CONTAINS clause. In pseudocode:

list of tokens = split input expression into tokens separated by spaces
foreach token in tokens
    if token is not keyword
        text = "CONTAINS(Description, \"" + token + "\")"
    else 
        text = token

    output = output + text
end

Where the keywords are "(", "AND", etc. You then just put the output in your SQL statement

< insert warning about SQL injection attacks here >

David Norman
That works if you don't have to worry about implicit operators. Can that be modified to work with implicit operators as well?
Evan
Yes, but it would be more complicated. You'd have to look at adjacent tokens and decide where to put the ANDs, e.g, between two non-keywords, between a keyword and an opening paren, etc. The whole point of the text processing is that the syntax of the input is very similar to the syntax of the output.
David Norman