根据上文,可以对PreparedSql.java的实现有一个基本的思路。
PreparedSql应该有两个基本参数,一个是原始SQL文,一个是参数集;输出应该是拼接好的SQL,在这里把原始SQL文作为构造方法的参数,参数集可以在SQL执行的时候传入。那除了构造方法外,应该还有两个public方法:prepareParameter和getSql。下面是具体的实现,细节就不多说了,主要是使用了正则表达式,大家可以在
< http://download.csdn.net/source/313803>下载整个工程。
PreparedSql应该有两个基本参数,一个是原始SQL文,一个是参数集;输出应该是拼接好的SQL,在这里把原始SQL文作为构造方法的参数,参数集可以在SQL执行的时候传入。那除了构造方法外,应该还有两个public方法:prepareParameter和getSql。下面是具体的实现,细节就不多说了,主要是使用了正则表达式,大家可以在
< http://download.csdn.net/source/313803>下载整个工程。
package
sqlmanager;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* PreparedSql.<br>
*
* @author Liufei
* @since 1.0.0
* @version 1.0.0 2007/05/15
* <p>
*/
class PreparedSql {
/** originalSql */
private String originalSql;
/** JDBC SQL. */
private String sql;
/** SQL (Parameters have replaced) */
private String templateSql;
/** VARIABLE_KEYWORD */
private static final String VARIABLE_KEYWORD = ":[/w|/[|/]]+";
/** REPLACE_KEYWORD */
private static final String REPLACE_KEYWORD = "(/{):/w+ .*?(/})";
/** parameterList */
private String[] parameterList;
/** replaceList */
private Map <String, String> replaceList;
/** replaceKeyList */
private Map <String, String> replaceKeyList;
/** setParameterLog */
private StringBuffer setParameterLog = new StringBuffer();
/**
*
* constructor。<br>
*
* @param originalSql
*
* @since 1.0
* <p>
*/
public PreparedSql(String originalSql) {
this.setOriginalSql(originalSql);
// 把需要替换的参数理出
prepare();
}
private void prepare() {
this.replaceList = new HashMap <String, String>();
Pattern pattern = Pattern.compile(REPLACE_KEYWORD);
Matcher matcher = pattern.matcher(this.originalSql);
Pattern patternKey = Pattern.compile(":/w+");
while (matcher.find()) {
String value = matcher.group();
Matcher matcherKey = patternKey.matcher(value);
if (matcherKey.find()) {
this.replaceList.put(matcherKey.group(), value);
}
}
}
/**
*
* prepareParameter。<br>
*
* @param pList pList
*
* @since 1.0
* <p>
*/
public void prepareParameter(Map <String, Object> pList) {
this.templateSql = new String(this.originalSql);
if ((pList != null) && (pList.size() != 0)) {
this.replaceKeyList = new HashMap <String, String>();
Iterator <String> iterator = pList.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (this.replaceList.get(key) != null) {
String value = this.replaceList.get(key);
Object obj = pList.get(key);
int count = 0;
if (obj instanceof Map[]) {
count = ((Map[]) obj).length;
} else if (obj instanceof Map) {
count = 1;
}
Pattern patternValue = Pattern.compile(" .*?(/})");
Matcher matcherValue = patternValue.matcher(value);
String tempSql = "";
if (matcherValue.find()) {
tempSql = matcherValue.group().substring(0, matcherValue.group().length() - 1);
}
StringBuffer addSql = new StringBuffer("");
for (int i = 0; i < count; i++) {
Pattern pattern = Pattern.compile(VARIABLE_KEYWORD);
Matcher matcher = pattern.matcher(tempSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String varStr = matcher.group();
matcher.appendReplacement(sb, varStr + "[" + i + "]");
this.replaceKeyList.put(varStr, key);
}
matcher.appendTail(sb);
addSql.append(sb.toString());
}
Pattern pattern = Pattern.compile("(/{)" + key + " .*?(/})");
Matcher matcher = pattern.matcher(this.templateSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, addSql.toString());
}
matcher.appendTail(sb);
this.templateSql = sb.toString();
}
}
}
Pattern patternNull = Pattern.compile(REPLACE_KEYWORD);
Matcher matcherNull = patternNull.matcher(this.templateSql);
StringBuffer sbNull = new StringBuffer();
while (matcherNull.find()) {
matcherNull.appendReplacement(sbNull, "");
}
matcherNull.appendTail(sbNull);
this.templateSql = sbNull.toString();
Vector <String> params = new Vector <String>(32);
this.sql = new String(this.templateSql);
Pattern pattern = Pattern.compile(VARIABLE_KEYWORD);
Matcher matcher = pattern.matcher(this.templateSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
params.add(matcher.group());
matcher.appendReplacement(sb, "?");
}
matcher.appendTail(sb);
this.sql = sb.toString();
this.parameterList = new String[params.size()];
params.copyInto(this.parameterList);
}
/**
*
* getSql.<br>
*
* @return SQL
*
* @since 1.0
* <p>
*/
public String getSql() {
return this.sql;
}
private void setOriginalSql(String originalSql) {
this.originalSql = originalSql;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer("SQL=");
if (this.sql == null) {
result.append(this.originalSql);
} else {
result.append(this.sql);
}
result.append(" ");
result.append(this.setParameterLog.toString());
return result.toString();
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* PreparedSql.<br>
*
* @author Liufei
* @since 1.0.0
* @version 1.0.0 2007/05/15
* <p>
*/
class PreparedSql {
/** originalSql */
private String originalSql;
/** JDBC SQL. */
private String sql;
/** SQL (Parameters have replaced) */
private String templateSql;
/** VARIABLE_KEYWORD */
private static final String VARIABLE_KEYWORD = ":[/w|/[|/]]+";
/** REPLACE_KEYWORD */
private static final String REPLACE_KEYWORD = "(/{):/w+ .*?(/})";
/** parameterList */
private String[] parameterList;
/** replaceList */
private Map <String, String> replaceList;
/** replaceKeyList */
private Map <String, String> replaceKeyList;
/** setParameterLog */
private StringBuffer setParameterLog = new StringBuffer();
/**
*
* constructor。<br>
*
* @param originalSql
*
* @since 1.0
* <p>
*/
public PreparedSql(String originalSql) {
this.setOriginalSql(originalSql);
// 把需要替换的参数理出
prepare();
}
private void prepare() {
this.replaceList = new HashMap <String, String>();
Pattern pattern = Pattern.compile(REPLACE_KEYWORD);
Matcher matcher = pattern.matcher(this.originalSql);
Pattern patternKey = Pattern.compile(":/w+");
while (matcher.find()) {
String value = matcher.group();
Matcher matcherKey = patternKey.matcher(value);
if (matcherKey.find()) {
this.replaceList.put(matcherKey.group(), value);
}
}
}
/**
*
* prepareParameter。<br>
*
* @param pList pList
*
* @since 1.0
* <p>
*/
public void prepareParameter(Map <String, Object> pList) {
this.templateSql = new String(this.originalSql);
if ((pList != null) && (pList.size() != 0)) {
this.replaceKeyList = new HashMap <String, String>();
Iterator <String> iterator = pList.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (this.replaceList.get(key) != null) {
String value = this.replaceList.get(key);
Object obj = pList.get(key);
int count = 0;
if (obj instanceof Map[]) {
count = ((Map[]) obj).length;
} else if (obj instanceof Map) {
count = 1;
}
Pattern patternValue = Pattern.compile(" .*?(/})");
Matcher matcherValue = patternValue.matcher(value);
String tempSql = "";
if (matcherValue.find()) {
tempSql = matcherValue.group().substring(0, matcherValue.group().length() - 1);
}
StringBuffer addSql = new StringBuffer("");
for (int i = 0; i < count; i++) {
Pattern pattern = Pattern.compile(VARIABLE_KEYWORD);
Matcher matcher = pattern.matcher(tempSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String varStr = matcher.group();
matcher.appendReplacement(sb, varStr + "[" + i + "]");
this.replaceKeyList.put(varStr, key);
}
matcher.appendTail(sb);
addSql.append(sb.toString());
}
Pattern pattern = Pattern.compile("(/{)" + key + " .*?(/})");
Matcher matcher = pattern.matcher(this.templateSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, addSql.toString());
}
matcher.appendTail(sb);
this.templateSql = sb.toString();
}
}
}
Pattern patternNull = Pattern.compile(REPLACE_KEYWORD);
Matcher matcherNull = patternNull.matcher(this.templateSql);
StringBuffer sbNull = new StringBuffer();
while (matcherNull.find()) {
matcherNull.appendReplacement(sbNull, "");
}
matcherNull.appendTail(sbNull);
this.templateSql = sbNull.toString();
Vector <String> params = new Vector <String>(32);
this.sql = new String(this.templateSql);
Pattern pattern = Pattern.compile(VARIABLE_KEYWORD);
Matcher matcher = pattern.matcher(this.templateSql);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
params.add(matcher.group());
matcher.appendReplacement(sb, "?");
}
matcher.appendTail(sb);
this.sql = sb.toString();
this.parameterList = new String[params.size()];
params.copyInto(this.parameterList);
}
/**
*
* getSql.<br>
*
* @return SQL
*
* @since 1.0
* <p>
*/
public String getSql() {
return this.sql;
}
private void setOriginalSql(String originalSql) {
this.originalSql = originalSql;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer("SQL=");
if (this.sql == null) {
result.append(this.originalSql);
} else {
result.append(this.sql);
}
result.append(" ");
result.append(this.setParameterLog.toString());
return result.toString();
}
}