package com.foreverwin.mesnac.common.util;

import org.apache.commons.lang.StringUtils;

import java.util.Map;





public class SqlUtil {
	private static final String reg = "\\:\\s*[a-zA-Z\\.0-9\\_\\-\\?\\*\\/]+\\s*";
	private static final String mreg = "\\/\\*([\\S\\s]*?)\\*\\/";
	public static Object parseSql(final Map<String, Object> queryParam,String sql) throws Exception{

		sql = StringUtils.trim(RegUtils.findAndReplace(mreg, sql, new RegUtils.FindCallback(){
				public String execute(String source) {
					return " ";
				}
			}));
		try{
			RegUtils.findAndReplace(reg, sql, new RegUtils.FindCallback(){
				public String execute(String source) {
					String s = StringUtils.trim(StringUtils.replace(source, ":", ""));
					if(queryParam.get(s) == null || StringUtils.isBlank(queryParam.get(s).toString()))
						queryParam.put(s, "");
					return source;
				}

			});
			return sql;
		}catch(Exception ex){
			throw new Exception("初始化参数失败!\n>>> PARAM >>>:\n"+queryParam+sql,ex);
		}
	}

}
