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.
PreparedStatementdirectly. Could you actually develop that magic parser/preparer and then modify every query to use it, in less time than it would take to just rewrite every query as aPreparedStatement? How many queries are we talking about here?