// XML配置SQL语句的插件
me.add(new SqlInXmlPlugin(true);
package com.utils.plugin.sqlinxml;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.utils.exception.CommonException;
import com.utils.kit.ClassUtil;
import com.utils.kit.JaxbKit;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
public class SqlKit {
private static Map<String, Map<String, String>> sqlMap = Maps.newHashMap();
private static final String CONFIG_REGEX = "*sql.xml";
private static final CCJSqlParserManager manager = new CCJSqlParserManager();
public static Statement parse(String sql) {
if(StringUtils.isEmpty(sql)) return null;
try {
return manager.parse(new StringReader(sql));
} catch(Exception ex) {
return null;
}
}
public static String singleWhere(Statement statement) {
if(statement instanceof Select) {
SelectBody body = ((Select) statement).getSelectBody();
if(body instanceof PlainSelect) {
Expression expression = ((PlainSelect) body).getWhere();
if(expression != null) {
return expression.toString();
}
}
}
return null;
}
public static String find(String groupToken, String sqlToken) {
if(groupToken == null || sqlToken == null) {
throw new CommonException(" groupToken and sqlToken need to set value . ");
}
if(sqlMap.isEmpty()) {
throw new CommonException("SqlInXmlPlugin not start or no xml files . ");
}
Map<String, String> items = sqlMap.get(groupToken);
String sql = null;
if(items != null) {
sql = items.get(sqlToken);
}
return sql;
}
public static String buildArgsForCondition_IN(Object [] sources) {
StringBuilder param = new StringBuilder("");
if(sources != null && sources.length > 0) {
if(sources instanceof String []) {
String [] source_string = (String[]) sources;
for(int i=0 ; i<source_string.length ; i++) {
if(i > 0) param.append(" , ");
param.append("'" + source_string[i] + "'");
}
} else if(sources instanceof Number []) {
Number [] source_number = (Number[]) sources;
param.append(StringUtils.join(source_number, " , "));
}
}
return param.append("") .toString();
}
public static void clearSqlMap() {
sqlMap.clear();
}
static void init(boolean includeAllJarsInLib,List<String> includeJars) { //初始化方法 先执行 包括解析xml文档
/**********先加载本地,如果需要jar则再加载远程文件************/
initLocal();
if(includeAllJarsInLib){//第三方
initJars(includeJars);
}
}
/**
* @description TODO 加载本地
* @author Tony
* @date 2017年8月9日下午5:14:08
*/
public static void initLocal(){
List<File> files = ClassUtil.listFiles(null, CONFIG_REGEX);
for (File xmlfile : files) {
SqlGroup group = JaxbKit.unmarshal(xmlfile, SqlGroup.class);
String name = group.name;
if (StringUtils.isBlank(name)) {
name = StringUtils.substringAfterLast(xmlfile.getName(), ".");
}
Map<String, String> sqlList = sqlMap.get(name);
if(sqlList == null) {
sqlList = Maps.newHashMap();
sqlMap.put(name, sqlList);
}
for (SqlItem sqlItem : group.sqlItems) {
sqlList.put(sqlItem.id, sqlItem.value);
}
}
}
/**
* @description TODO 加载jar包
* @author Tony
* @date 2017年8月9日下午5:14:08
*/
public static void initJars(List<String> includeJars){
List<InputStream> files = ClassUtil.of(Controller.class).
includeAllJarsInLib(true).inJars(includeJars)
.search(null,CONFIG_REGEX.replace("*", ""));//流格式调用,为了支持jar包调用
for (InputStream xmlfile : files) {
SqlGroup group = JaxbKit.unmarshal(xmlfile, SqlGroup.class);
String name = group.name;
Map<String, String> sqlList = sqlMap.get(name);
if(sqlList == null) {
sqlList = Maps.newHashMap();
sqlMap.put(name, sqlList);
}
for (SqlItem sqlItem : group.sqlItems) {
sqlList.put(sqlItem.id, sqlItem.value);
}
}
}
/**
* @Title formatSqlIn
* @Description 格式化sql中in字段 转化为or
* @param columnName 字段名
* @param size 个数
* @param sql 原始sql
* @return 设定文件
* @return String 返回类型
* @date 2015年12月29日 下午5:42:35
*/
public static String formatSqlIn(String columnName,int size,String sql) {
// 重新组装参数
if(size>0 && StrKit.notBlank(columnName,sql)){
if (size == 1) {
sql += " and "+columnName+" = ?";
} else {
sql += " and (";
for (int i = 0; i <size; i++) {
sql += " "+columnName+" = ? or";
}
sql = sql.substring(0, sql.length() - 2);
sql += ") ";
}
}
return sql;
}
}