org.liveSense.api.sql.NamedParameterProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.liveSense.service.dataSourceProvider Show documentation
Show all versions of org.liveSense.service.dataSourceProvider Show documentation
Simple DBCP pooled datasource provider.
The newest version!
package org.liveSense.api.sql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.liveSense.api.sql.exceptions.SQLException;
public class NamedParameterProcessor {
//CONSTS
private static final String UNKNOWN_PARAMETER = "Unknown parameter";
//FIELDS
private Map> parameters = new HashMap>();
private String sqlStatement;
//GETTERS AND SETTERS
public Map> getParameters() {
return parameters;
}
public String getSqlStatement() {
return sqlStatement;
}
//CONSTRUCTORS
public NamedParameterProcessor(String sql)
throws SQLException {
int paramCount = 0;
int ps = sql.indexOf(':');
while (ps != -1) {
//check whether we found it in a string literal
if (StringUtils.countMatches(sql.substring(1, ps), "'") % 2 == 0) {
paramCount++;
//get parameter name
StringBuffer sb = new StringBuffer();
ps++;
while ((ps + 1 <= sql.length()) && (sql.substring(ps, ps + 1).matches("[a-zA-Z0-9_]+"))) {
sb.append(sql.substring(ps, ps + 1));
ps++;
}
//store position
String paramName = sb.toString();
List list = parameters.get(paramName);
if (list == null) {
list = new ArrayList();
parameters.put(paramName, list);
}
list.add(paramCount);
StringBuffer sb2 = new StringBuffer(sql);
sb2.delete(ps - paramName.length() - 1, ps);
sb2.insert(ps - paramName.length() - 1, "?");
sql = sb2.toString();
ps = ps - paramName.length();
}
ps = sql.indexOf(':', ps + 1);
}
sqlStatement = sql;
}
//METHODS
public List