All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kukababy.plus.utils.HoldUtil Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.kukababy.plus.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pager.CustSqlInfo;
import com.kukababy.plus.pager.SqlFilter;
import com.kukababy.plus.pager.SqlInfo;
import com.kukababy.plus.pager.SqlWhere;
import com.kukababy.plus.pojo.P;

/**
 * 
 * 
* *
 * 描述:Sql的占位符处理,最终的SQL语句和?对应的值
 * 
* *
* * @author [email protected] * @date 2019年3月5日 下午10:48:48 */ public class HoldUtil { /** * sql语句占位符规则 sql=select * from a where id=:id
*/ // private static Pattern holdP = Pattern.compile(":[a-zA-Z-_]{1,}"); private static Pattern holdP = Pattern.compile("#\\{[a-zA-Z-_]{1,}\\}"); /** * * 描述:最终sql和对应的值
*
* * @param sql * @param sqlFilter * @return */ public static SqlInfo getSqlAndVals(String sql, SqlFilter sqlFilter) { Map holdMap = getHoldValsSql(sql, sqlFilter.getHoldName2ValMap()); return getSqlAndVals(sql, holdMap, sqlFilter); } /** * * 描述:最终sql语句与对应的值
*
* * @param sql * @param holdWhereColVals * @return */ public static SqlInfo getSqlAndVals(String sql, P... holdWhereColVals) { List

holdWheres = new ArrayList(); for (P p : holdWhereColVals) { holdWheres.add(p); } Map holdValMap = new HashMap(); for (P p : holdWhereColVals) { holdValMap.put(p.getCol(), p.getVal()); } Map holdMap = getHoldValsSql(sql, holdValMap); SqlFilter sqlFilter = SqlUtil.list2SqlFilter(holdWheres); return getSqlAndVals(sql, holdMap, sqlFilter); } public static CustSqlInfo getCustSqlAndVals(String sql, List

params) { Map holdValMap = new HashMap(); for (P p : params) { holdValMap.put(p.getCol(), p.getVal()); } Map holdMap = getHoldValsSql(sql, holdValMap); List holdVals = null; if (holdMap != null) { sql = (String) holdMap.get("sql"); holdVals = (List) holdMap.get("holdVals"); } else { holdVals = new ArrayList(); } CustSqlInfo custSqlInfo = new CustSqlInfo(sql, holdVals); return custSqlInfo; } private static SqlInfo getSqlAndVals(String sql, Map holdMap, SqlFilter sqlFilter) { List holdVals = null; if (holdMap != null) { sql = (String) holdMap.get("sql"); holdVals = (List) holdMap.get("holdVals"); } SqlInfo sqlInfo = SqlWhere.getWheres(sqlFilter); String execSql = sql + sqlInfo.getWhereSql(); sqlInfo.setExecSql(execSql); if (holdVals != null) { holdVals.addAll(sqlInfo.getWhereVals()); sqlInfo.setExecVals(holdVals); } else { sqlInfo.setExecVals(sqlInfo.getWhereVals()); } return sqlInfo; } /** * * 描述:通过Sql和请求参数,返回新的sql和占位符的值
*
* * @param holdSql * @param holdName2ValMap * @return */ private static Map getHoldValsSql(String holdSql, Map holdName2ValMap) { Map holdNamesAndSqlMap = getHoldNamesAndSql(holdSql, holdName2ValMap); if (holdNamesAndSqlMap == null) { return null; } else { Map res = new HashMap(); List holdVals = getHoldVals((List) holdNamesAndSqlMap.get("holdNames"), holdName2ValMap); res.put("sql", holdNamesAndSqlMap.get("sql")); res.put("holdVals", holdVals); return res; } } /** * * 描述:解析占位符后,新的sql
*
* * @param origSql * @return */ private static Map getHoldNamesAndSql(String holdSql, Map holdValMap) { Matcher m = holdP.matcher(holdSql); // 获取 matcher 对象 StringBuffer sb = null; int end = 0; List holdNames = null; while (m.find()) { if (sb == null) { sb = new StringBuffer(); holdNames = new ArrayList(); } end = m.end(); // 提取占位符名称 String holdName = holdSql.substring(m.start() + 2, m.end() - 1); String replaceChar = getHoldReplaceChar(holdName, holdValMap); // 替换问号串 if (replaceChar != null) { m.appendReplacement(sb, replaceChar); } else { throw new PlusRuntimeException("变量" + holdName + "没有赋值 ->" + holdSql); } holdNames.add(holdName);// 占位符名称加入age如 #{age} } if (end == 0) { return null; } else { Map res = new HashMap(); sb.append(holdSql.substring(end)); res.put("sql", sb.toString()); res.put("holdNames", holdNames); return res; } } /** * * 描述:占位替换问号?处理,返回问号字符串,
* 1、可能一个占位符返回多个问号,比如in 语句
* * @param holdName * @param holdValMap * @return */ private static String getHoldReplaceChar(String holdName, Map holdValMap) { String resReplaceChar = null; if (holdValMap.containsKey(holdName)) { Object valObj = holdValMap.get(holdName); if (valObj instanceof List) {// 多个?号 比如查询的in语句 StringBuilder sbChar = new StringBuilder();// 多个?号 int i = 0; for (Object obj : (List) valObj) { if (i > 0) { sbChar.append(","); } sbChar.append("?"); i++; } // 替换成多个问号? resReplaceChar = sbChar.toString(); } else { resReplaceChar = "?"; } } return resReplaceChar; } /** * * 描述:通过SQL语句的所有占位符,找到对应的值
* 1、同时holdName2ValMap里去除占位符的对象
* * @param holdNames * @param holdName2ValMap * @return */ private static List getHoldVals(List holdNames, Map holdName2ValMap) { List holdVals = new ArrayList(); for (String holdName : holdNames) { for (Entry entry : holdName2ValMap.entrySet()) { if (holdName.equals(entry.getKey())) { Object valObj = entry.getValue(); if (valObj instanceof List) {// 数组占多个值 比如查询的in语句 for (Object obj : (List) valObj) { holdVals.add(obj); } } else { holdVals.add(valObj); } break; } } } return holdVals; } }