Problem Statement: How would you go about writing an SQL WHERE clause parser to evaluate a given condition against the given data in a Hashmap. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class ParserTest { public static void main(String[] args) throws ParseException { Map<String, Object> data = new HashMap<>(); data.put("Acct1", "1"); data.put("Acct2", "2"); data.put("Acct3", Integer.valueOf("3")); data.put("Acct4", "4"); String whereClause = "Acct4 = '4' AND Acct1 NOT IN ('3', '2') AND Acct2 = '2' AND Acct3 IN (4,3,1)"; TokenStream tokenStream = tokenize(whereClause); //System.out.println(""); Expr expr = parse(tokenStream); boolean result = expr.evaluate(data); System.out.println(result); } |
Outputs:
|
1 2 3 |
result=true |
Solution, step by step Step 1: Let’s write a tokenize method using regular expressions to tokenize the WHERE condition supplied...