SQL文的管理和解析(三)PreparedSql的實現

根據上文,可以對PreparedSql.java的實現有一個基本的思路。

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();
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章