I need to write a boolean logic parser which will translate the boolean logic language to a SQL WHERE clause.
The order of the operands will always be in the correct order (with value on the right).
Here is a relatively simple example. There could be nested parentheses and the use of NOT operators, etc.
(CACOUNT=01 OR CACOUNT=02 OR CACOUNT=03)
AND Q4=1 AND NAME=TIMOTHY
Here is what the WHERE clause would resemble.
WHERE (
EXISTS (
SELECT 1 FROM MyVerticalTable b
WHERE b.Key=a.Key AND b.Key='CACOUNT' AND b.Value='01'
)
OR EXISTS (
SELECT 1 FROM MyVerticalTable b
WHERE b.Key=a.Key AND b.Key='CACOUNT' AND b.Value='02'
)
OR EXISTS (
SELECT 1 FROM MyVerticalTable b
WHERE b.Key=a.Key AND b.Key='CACOUNT' AND b.Value='03'
)
)
AND EXISTS (
SELECT 1 FROM MyVerticalTable b
WHERE b.Key=a.Key AND b.Key='Q4' AND b.Value='1'
)
AND EXISTS (
SELECT 1 FROM MyVerticalTable b
WHERE b.Key=a.Key AND b.Key='NAME' AND b.Value='TIMOTHY'
)