I have a java project where PreparedStatements are not being used in any of the SQL query. Now I want to implement PreparedStatements for all those queries. Instead of modifying each query by putting '?' , I want to write a function which takes SQL query string which parses it and returns the map of values of various SQL clauses.
public class SQLParser {
public static void main(String args[]) {
String sqlString = "select * from table t where columnA = 'hello' and columnB = 'hai'";
Map<Integer,String> values = new HashMap<>();
values = parseQuery(sqlString);
System.out.println(values); // prints { {1,'hello'} , {2,'hai'} }
}
private static Map<Integer,String> parseQuery(String sqlString) {
Map<Integer,String> values = new HashMap<>();
//
// ???? I want this function
//
return values;
}
}
Few examples are
sqlString : select * from table t where columnA = 'hello' AND columnB = 'hai'
output : { {1,'hello'} , {2,'hai'} }
sqlString : select * from table t where columnA IN ('hello' ,'hai')
output : { {1,'hello'} , {2,'hai'} }
sqlString : select * from table t where columnA > 17 AND columnB BETWEEN 10 AND 20;
output : { {1,'17'} , {2, '10' } , {3, '20'} }
Basically It should support all possible clauses and their combinations.