|
|
|
@ -1,15 +1,18 @@
|
|
|
|
|
package com.glxp.api.idc.utils;
|
|
|
|
|
|
|
|
|
|
import com.glxp.api.util.DateUtil;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.glxp.api.util.DateUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author chenqf
|
|
|
|
@ -19,75 +22,74 @@ public class DBAUtils {
|
|
|
|
|
private static final String keywords = ",limit,offset,sort,order,isLike,menuId,menuCode,page,isAuto,pageNo,pageSize,pageNum,sqlOrder,join,";
|
|
|
|
|
|
|
|
|
|
/*解析where条件*/
|
|
|
|
|
public static String parseWhere(String sql, Map<String, Object> columns, Map<String, Object> params, String dataWhere) {
|
|
|
|
|
return parseWhere(sql, columns, params, dataWhere, false);
|
|
|
|
|
public static String parseWhere(String sql,Map<String,Object> columns,Map<String,Object> params,String dataWhere) {
|
|
|
|
|
return parseWhere(sql,columns,params,dataWhere,false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String parseWhere(String sql, Map<String, Object> columns, Map<String, Object> params, String dataWhere, boolean isSimpleWhere) {
|
|
|
|
|
String where = "";
|
|
|
|
|
public static String parseWhere(String sql,Map<String,Object> columns,Map<String,Object> params,String dataWhere,boolean isSimpleWhere) {
|
|
|
|
|
String where="";
|
|
|
|
|
boolean isAnd0 = false;
|
|
|
|
|
boolean isLike = params != null && params.get("isLike") != null && params.get("isLike").toString().equals("true") ? true : false;
|
|
|
|
|
boolean isLike = params!=null&¶ms.get("isLike")!=null&¶ms.get("isLike").toString().equals("true") ? true : false;
|
|
|
|
|
|
|
|
|
|
if (sql.contains("sqlWhere") || !sql.contains("<if")) {
|
|
|
|
|
where = convertWhere(columns, params, dataWhere, sql);
|
|
|
|
|
if (!sql.contains("<if") && !StringUtils.isEmpty(where)) {
|
|
|
|
|
sql += sql.toLowerCase().contains(" where ") ? " and " + where : " where " + where;
|
|
|
|
|
if(sql.contains("sqlWhere")||!sql.contains("<if")) {
|
|
|
|
|
where = convertWhere(columns,params,dataWhere,sql);
|
|
|
|
|
if(!sql.contains("<if")&&!StringUtils.isEmpty(where)) {
|
|
|
|
|
sql+=sql.toLowerCase().contains(" where ") ? " and "+where : " where " +where;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int n = 0;
|
|
|
|
|
if (sql.contains("<if")) {
|
|
|
|
|
int n=0;
|
|
|
|
|
if(sql.contains("<if")) {
|
|
|
|
|
int s1 = sql.indexOf("<if");
|
|
|
|
|
if (isSimpleWhere && s1 > 0) {
|
|
|
|
|
for (int i = s1 - 1; i > -1; i--) {
|
|
|
|
|
if (!sql.substring(i, i + 1).equals(" ") && !sql.substring(i, i + 1).equals("\n")) {
|
|
|
|
|
if(isSimpleWhere&&s1>0) {
|
|
|
|
|
for(int i=s1-1;i>-1;i--) {
|
|
|
|
|
if(!sql.substring(i,i+1).equals(" ")&&!sql.substring(i,i+1).equals("\n")) {
|
|
|
|
|
isAnd0 = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!StringUtils.isEmpty(dataWhere)) {
|
|
|
|
|
if (sql.contains("sqlWhere")) {
|
|
|
|
|
if (params.get("sqlWhere") != null && !StringUtils.isEmpty(params.get("sqlWhere").toString())) {
|
|
|
|
|
if(!StringUtils.isEmpty(dataWhere)) {
|
|
|
|
|
if(sql.contains("sqlWhere")) {
|
|
|
|
|
if(params.get("sqlWhere")!=null&&!StringUtils.isEmpty(params.get("sqlWhere").toString())) {
|
|
|
|
|
String temp = params.get("sqlWhere").toString();
|
|
|
|
|
params.replace("sqlWhere", "(" + temp + ") and (" + dataWhere + ")");
|
|
|
|
|
params.replace("sqlWhere", "("+temp+") and ("+dataWhere+")");
|
|
|
|
|
} else {
|
|
|
|
|
params.put("sqlWhere", dataWhere);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (sql.contains("<if")) {
|
|
|
|
|
while(sql.contains("<if")) {
|
|
|
|
|
int a = sql.indexOf("<if");
|
|
|
|
|
int b = sql.indexOf(">", a + 1);
|
|
|
|
|
int h = sql.indexOf("isLike", a + 1);
|
|
|
|
|
int j = sql.indexOf("!=", a + 1);
|
|
|
|
|
if (h > a && h < b) {
|
|
|
|
|
sql = j > 1 && j < b ? sql.substring(0, a) + "[!=isLike]" + sql.substring(b + 1) : sql.substring(0, a) + "[==isLike]" + sql.substring(b + 1);
|
|
|
|
|
n = 0;
|
|
|
|
|
int b = sql.indexOf(">",a+1);
|
|
|
|
|
int h = sql.indexOf("isLike",a+1);
|
|
|
|
|
int j = sql.indexOf("!=",a+1);
|
|
|
|
|
if(h>a&&h<b) {
|
|
|
|
|
sql = j>1&&j<b ? sql.substring(0,a)+"[!=isLike]"+sql.substring(b+1) : sql.substring(0,a)+"[==isLike]"+sql.substring(b+1);
|
|
|
|
|
n=0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int c = sql.indexOf("</if>");
|
|
|
|
|
String str = sql.substring(b + 1, c);
|
|
|
|
|
String str = sql.substring(b+1,c);
|
|
|
|
|
int e1 = str.indexOf("#{");
|
|
|
|
|
int e2 = str.indexOf("}", e1 + 1);
|
|
|
|
|
if (e1 < 0) {
|
|
|
|
|
int e2 = str.indexOf("}",e1 +1);
|
|
|
|
|
if(e1<0) {
|
|
|
|
|
e1 = str.indexOf("${");
|
|
|
|
|
}
|
|
|
|
|
String key = str.substring(e1 + 2, e2).trim();
|
|
|
|
|
String key =str.substring(e1 +2,e2).trim();
|
|
|
|
|
Object obj = params.get(key);
|
|
|
|
|
String value = obj != null ? obj.toString() : "";
|
|
|
|
|
if (key.equals("sqlWhere")) {
|
|
|
|
|
if (StringUtils.isEmpty(value))
|
|
|
|
|
String value = obj!=null ? obj.toString() : "";
|
|
|
|
|
if(key.equals("sqlWhere")) {
|
|
|
|
|
if(StringUtils.isEmpty(value))
|
|
|
|
|
value = where;
|
|
|
|
|
}
|
|
|
|
|
if (!StringUtils.isEmpty(value)) {
|
|
|
|
|
str = str.length() > e2 + 1 ? str.substring(0, e1) + "'" + value + "'" + str.substring(e2 + 1) : str.substring(0, e1) + "'" + value + "'";
|
|
|
|
|
if (!isAnd0 && n == 0) {
|
|
|
|
|
if (str.length() > 3) {
|
|
|
|
|
if (str.trim().substring(0, 4).toLowerCase().equals("and ")) {
|
|
|
|
|
str = str.substring(str.toLowerCase().indexOf("and ") + 3);
|
|
|
|
|
if(!StringUtils.isEmpty(value)) {
|
|
|
|
|
str = str.length()>e2+1 ? str.substring(0,e1)+"'"+value+"'"+str.substring(e2+1) : str.substring(0,e1)+"'"+value+"'";
|
|
|
|
|
if(!isAnd0&&n==0) {
|
|
|
|
|
if(str.length()>3) {
|
|
|
|
|
if(str.trim().substring(0, 4).toLowerCase().equals("and ")) {
|
|
|
|
|
str = str.substring(str.toLowerCase().indexOf("and ")+3);
|
|
|
|
|
}
|
|
|
|
|
if (str.trim().substring(0, 3).toLowerCase().equals("or ")) {
|
|
|
|
|
str = str.substring(str.toLowerCase().indexOf("or ") + 2);
|
|
|
|
|
if(str.trim().substring(0, 3).toLowerCase().equals("or ")) {
|
|
|
|
|
str = str.substring(str.toLowerCase().indexOf("or ")+2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -95,60 +97,60 @@ public class DBAUtils {
|
|
|
|
|
} else {
|
|
|
|
|
str = "";
|
|
|
|
|
}
|
|
|
|
|
sql = sql.length() > c + 5 ? sql.substring(0, a) + str + sql.substring(c + 5) : sql.substring(0, a) + str;
|
|
|
|
|
sql = sql.length()>c+5 ? sql.substring(0,a) +str+sql.substring(c+5) : sql.substring(0,a) +str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (sql.contains("<where>")) {
|
|
|
|
|
sql = n > 0 ? sql.replaceAll("<where>", " where ") : sql.replaceAll("<where>", "");
|
|
|
|
|
if (!StringUtils.isEmpty(dataWhere) && !sql.contains("sqlWhere")) {
|
|
|
|
|
sql = n > 0 ? sql.replaceAll("</where>", " and (" + dataWhere + ")") : sql.replaceAll("</where>", " (" + dataWhere + ")");
|
|
|
|
|
if(sql.contains("<where>")) {
|
|
|
|
|
sql = n>0 ? sql.replaceAll("<where>", " where ") : sql.replaceAll("<where>", "");
|
|
|
|
|
if(!StringUtils.isEmpty(dataWhere)&&!sql.contains("sqlWhere")) {
|
|
|
|
|
sql = n>0 ? sql.replaceAll("</where>", " and ("+dataWhere+")") : sql.replaceAll("</where>", " ("+dataWhere+")");
|
|
|
|
|
} else {
|
|
|
|
|
sql = sql.replaceAll("</where>", "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sql.contains("=isLike")) {
|
|
|
|
|
int a1, a2, b1, b2;
|
|
|
|
|
if(sql.contains("=isLike")) {
|
|
|
|
|
int a1,a2,b1,b2;
|
|
|
|
|
a1 = sql.indexOf("[==isLike]");
|
|
|
|
|
a2 = sql.indexOf("</if>", a1 + 1);
|
|
|
|
|
a2 = sql.indexOf("</if>",a1+1);
|
|
|
|
|
|
|
|
|
|
if (a1 > 0 && a2 > 0) {
|
|
|
|
|
if (isLike) {
|
|
|
|
|
sql = sql.length() > a2 + 5 ? sql.substring(0, a1) + sql.substring(a1 + 10, a2) + sql.substring(a2 + 5) : sql.substring(0, a1) + sql.substring(a1 + 10, a2);
|
|
|
|
|
if(a1>0&&a2>0) {
|
|
|
|
|
if(isLike) {
|
|
|
|
|
sql = sql.length()>a2+5 ? sql.substring(0,a1)+sql.substring(a1 +10, a2) +sql.substring(a2+5) : sql.substring(0,a1)+sql.substring(a1 +10, a2);
|
|
|
|
|
} else {
|
|
|
|
|
sql = sql.length() > a2 + 5 ? sql.substring(0, a1) + sql.substring(a2 + 5) : sql.substring(0, a1);
|
|
|
|
|
sql = sql.length()>a2+5 ? sql.substring(0,a1)+sql.substring(a2+5) : sql.substring(0,a1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b1 = sql.indexOf("[!=isLike]");
|
|
|
|
|
b2 = sql.indexOf("</if>", b1 + 1);
|
|
|
|
|
if (b1 > 0 && b2 > 0) {
|
|
|
|
|
if (!isLike) {
|
|
|
|
|
sql = sql.length() > b2 + 5 ? sql.substring(0, b1) + sql.substring(b1 + 10, b2) + sql.substring(b2 + 5) : sql.substring(0, b1) + sql.substring(b1 + 10, b2);
|
|
|
|
|
b2 = sql.indexOf("</if>",b1 +1);
|
|
|
|
|
if(b1>0&&b2>0) {
|
|
|
|
|
if(!isLike) {
|
|
|
|
|
sql = sql.length()>b2+5 ? sql.substring(0,b1)+sql.substring(b1 +10, b2) +sql.substring(b2+5) : sql.substring(0,b1)+sql.substring(b1 +10, b2);
|
|
|
|
|
} else {
|
|
|
|
|
sql = sql.length() > b2 + 5 ? sql.substring(0, b1) + sql.substring(b2 + 5) : sql.substring(0, b1);
|
|
|
|
|
sql = sql.length()>b2+5 ? sql.substring(0,b1)+sql.substring(b2+5) : sql.substring(0,b1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sql = parseParams(sql, columns, params);
|
|
|
|
|
sql =parseParams(sql,columns,params);
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String parseParams(String sql, Map<String, Object> columns, Map<String, Object> params) {
|
|
|
|
|
String[] iStr = {"#{", "${"};
|
|
|
|
|
for (int k = 0; k < 2; k++) {
|
|
|
|
|
while (sql.contains(iStr[k])) {
|
|
|
|
|
public static String parseParams(String sql,Map<String,Object> columns,Map<String,Object> params) {
|
|
|
|
|
String[] iStr={"#{","${"};
|
|
|
|
|
for(int k=0;k<2;k++) {
|
|
|
|
|
while(sql.contains(iStr[k])) {
|
|
|
|
|
int a1 = sql.indexOf(iStr[k]);
|
|
|
|
|
int a2 = sql.indexOf("}", a1 + 1);
|
|
|
|
|
String key = sql.substring(a1 + 2, a2);
|
|
|
|
|
int a2 = sql.indexOf("}",a1 +1);
|
|
|
|
|
String key = sql.substring(a1+2,a2);
|
|
|
|
|
Object obj = params.get(key);
|
|
|
|
|
String value = obj != null ? obj.toString() : "";
|
|
|
|
|
String value = obj!=null ? obj.toString() : "";
|
|
|
|
|
String dataType = "0";
|
|
|
|
|
if (columns != null) {
|
|
|
|
|
Map<String, Object> map = (Map<String, Object>) columns.get(key);
|
|
|
|
|
dataType = map != null && map.get("dataType") != null ? map.get("dataType").toString() : "0";
|
|
|
|
|
if(columns!=null) {
|
|
|
|
|
Map<String,Object> map = (Map<String, Object>) columns.get(key);
|
|
|
|
|
dataType = map!=null&&map.get("dataType")!=null ? map.get("dataType").toString() : "0";
|
|
|
|
|
}
|
|
|
|
|
sql = sql.substring(0, a1) + (dataType.equals("0") ? "'" : "") + value + (dataType.equals("0") ? "'" : "") + sql.substring(a2 + 1);
|
|
|
|
|
sql = sql.substring(0,a1)+(dataType.equals("0") ? "'" : "")+value+(dataType.equals("0") ? "'" : "")+sql.substring(a2 +1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -158,197 +160,195 @@ public class DBAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*生成where条件*/
|
|
|
|
|
public static String convertWhere(Map<String, Object> colums, Map<String, Object> params, String dataWhere) {
|
|
|
|
|
String where = convertWhere(colums, params, dataWhere, "");
|
|
|
|
|
public static String convertWhere(Map<String,Object> colums,Map<String,Object> params,String dataWhere) {
|
|
|
|
|
String where=convertWhere(colums,params,dataWhere,"");
|
|
|
|
|
return where;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String convertWhere(Map<String, Object> colums, Map<String, Object> params, String dataWhere, String sql) {
|
|
|
|
|
String where = "";
|
|
|
|
|
String value = "";
|
|
|
|
|
public static String convertWhere(Map<String,Object> colums,Map<String,Object> params,String dataWhere,String sql) {
|
|
|
|
|
String where="";
|
|
|
|
|
String value ="";
|
|
|
|
|
boolean isLike = false;
|
|
|
|
|
boolean isKey = false;
|
|
|
|
|
if (params != null && params.get("isLike") != null && params.get("isLike").toString().equals("true"))
|
|
|
|
|
if(params!=null&¶ms.get("isLike")!=null&¶ms.get("isLike").toString().equals("true"))
|
|
|
|
|
isLike = true;
|
|
|
|
|
if (colums != null) {
|
|
|
|
|
if(colums!=null) {
|
|
|
|
|
Iterator keys = colums.keySet().iterator();
|
|
|
|
|
isKey = keys.hasNext();
|
|
|
|
|
}
|
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
|
for(String key : params.keySet()){
|
|
|
|
|
Object obj = params.get(key);
|
|
|
|
|
value = "";
|
|
|
|
|
if (obj != null)
|
|
|
|
|
if(obj!=null)
|
|
|
|
|
value = obj.toString();
|
|
|
|
|
if (!StringUtils.isEmpty(value) && !keywords.contains("," + key + ",") && (StringUtils.isEmpty(sql) || (!StringUtils.isEmpty(sql) && !sql.contains("{" + key + "}")))) {
|
|
|
|
|
if(!StringUtils.isEmpty(value)&&!keywords.contains(","+key+",")&&(StringUtils.isEmpty(sql)||(!StringUtils.isEmpty(sql)&&!sql.contains("{"+key+"}")))) {
|
|
|
|
|
|
|
|
|
|
if (key.equals("sqlWhere") || key.equals("join")) {
|
|
|
|
|
where += (!StringUtils.isEmpty(where) ? " and " : "") + " " + value;
|
|
|
|
|
if(key.equals("sqlWhere")||key.equals("join")) {
|
|
|
|
|
where+=(!StringUtils.isEmpty(where) ? " and " : "") +" "+value;
|
|
|
|
|
} else {
|
|
|
|
|
String[] cns = key.split(",");
|
|
|
|
|
String wh = "";
|
|
|
|
|
for (String cn : cns) {
|
|
|
|
|
for (String cn:cns) {
|
|
|
|
|
String columnName = cn;//StringUtils.propertyToField(cn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (colums != null && isKey && colums.get(cn) == null) {
|
|
|
|
|
logger.error("列名" + columnName + "不存在,属性" + cn + "错误.");
|
|
|
|
|
if(colums!=null&&isKey&&colums.get(cn)==null) {
|
|
|
|
|
logger.error("列名"+columnName+"不存在,属性"+cn+"错误.");
|
|
|
|
|
} else {
|
|
|
|
|
if (!StringUtils.isEmpty(wh))
|
|
|
|
|
wh += " or ";
|
|
|
|
|
if (isLike) {
|
|
|
|
|
wh += "instr(" + columnName + ",'" + value + "')>0";
|
|
|
|
|
if(!StringUtils.isEmpty(wh))
|
|
|
|
|
wh+=" or ";
|
|
|
|
|
if(isLike) {
|
|
|
|
|
wh+="instr("+columnName+",'"+value+"')>0";
|
|
|
|
|
} else {
|
|
|
|
|
wh += columnName + "='" + value + "'";
|
|
|
|
|
wh+=columnName+"='"+value+"'";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
where += !StringUtils.isEmpty(wh) ? (!StringUtils.isEmpty(where) ? " and " : "") + "(" + wh + ")" : "";
|
|
|
|
|
where+=!StringUtils.isEmpty(wh) ? (!StringUtils.isEmpty(where) ? " and " : "") + "("+wh+")" : "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!StringUtils.isEmpty(dataWhere)) {
|
|
|
|
|
if (!StringUtils.isEmpty(where)) {
|
|
|
|
|
where = "(" + where + ") and (" + dataWhere + ")";
|
|
|
|
|
if(!StringUtils.isEmpty(dataWhere)) {
|
|
|
|
|
if(!StringUtils.isEmpty(where)) {
|
|
|
|
|
where = "("+where+") and ("+dataWhere+")";
|
|
|
|
|
} else {
|
|
|
|
|
where = dataWhere;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!StringUtils.isEmpty(where))
|
|
|
|
|
where = "(" + where + ")";
|
|
|
|
|
if(!StringUtils.isEmpty(where))
|
|
|
|
|
where = "("+where+")";
|
|
|
|
|
return where;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*生成insert语句*/
|
|
|
|
|
public static String parseInsert(Map<String, Object> params, Map<String, Object> columns) {
|
|
|
|
|
String ins = "";
|
|
|
|
|
String vas = "";
|
|
|
|
|
String sql = "";
|
|
|
|
|
for (String key : columns.keySet()) {
|
|
|
|
|
public static String parseInsert(Map<String,Object> params,Map<String,Object> columns) {
|
|
|
|
|
String ins="";
|
|
|
|
|
String vas="";
|
|
|
|
|
String sql="";
|
|
|
|
|
for(String key : columns.keySet()){
|
|
|
|
|
String value = "";
|
|
|
|
|
|
|
|
|
|
if (params.get(key) != null)
|
|
|
|
|
if(params.get(key)!=null)
|
|
|
|
|
value = params.get(key).toString();
|
|
|
|
|
Map<String, Object> map = (Map<String, Object>) columns.get(key);
|
|
|
|
|
Map<String,Object> map = (Map<String, Object>) columns.get(key);
|
|
|
|
|
String dataType = map.get("dataType").toString();
|
|
|
|
|
String extra = map.get("extra") != null && map.get("extra").toString().toLowerCase().contains("auto") ? "A" : "N";
|
|
|
|
|
String extra = map.get("extra")!=null&&map.get("extra").toString().toLowerCase().contains("auto") ? "A" : "N";
|
|
|
|
|
|
|
|
|
|
if (map.get("columnKey").toString().equals("PRI") && extra.equals("N")) {
|
|
|
|
|
if (StringUtils.isEmpty(value)) {
|
|
|
|
|
if(map.get("columnKey").toString().equals("PRI")&&extra.equals("N")) {
|
|
|
|
|
if(StringUtils.isEmpty(value)) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = DBAUtils.escape(value);
|
|
|
|
|
if (!StringUtils.isEmpty(value)) {
|
|
|
|
|
if (!StringUtils.isEmpty(ins))
|
|
|
|
|
ins += ",";
|
|
|
|
|
ins += map.get("columnName").toString();
|
|
|
|
|
if(!StringUtils.isEmpty(value)) {
|
|
|
|
|
if(!StringUtils.isEmpty(ins))
|
|
|
|
|
ins+=",";
|
|
|
|
|
ins+=map.get("columnName").toString();
|
|
|
|
|
|
|
|
|
|
if (!StringUtils.isEmpty(vas))
|
|
|
|
|
vas += ",";
|
|
|
|
|
if(!StringUtils.isEmpty(vas))
|
|
|
|
|
vas+=",";
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(value)) {
|
|
|
|
|
vas += "null";
|
|
|
|
|
if(StringUtils.isEmpty(value)) {
|
|
|
|
|
vas+="null";
|
|
|
|
|
} else {
|
|
|
|
|
if (dataType.equals("C")) {
|
|
|
|
|
vas += "'" + value + "'";
|
|
|
|
|
if(dataType.equals("C")) {
|
|
|
|
|
vas+="'"+value+"'";
|
|
|
|
|
} else if (dataType.equals("N")) {
|
|
|
|
|
vas += value;
|
|
|
|
|
vas+=value;
|
|
|
|
|
} else {
|
|
|
|
|
String str = "";
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
if (StringUtils.isNumeric(value)) {
|
|
|
|
|
if(StringUtils.isNumeric(value)) {
|
|
|
|
|
date = new Date(Long.valueOf(value));
|
|
|
|
|
str = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
str = DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
} else {
|
|
|
|
|
str = DateUtil.formatDate(IDCUtils.parseDate(value), "yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
str = DateUtil.formatDate(IDCUtils.parseDate(value),"yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
}
|
|
|
|
|
vas += "cast('" + str + "' as datetime)";
|
|
|
|
|
vas+="cast('"+str+"' as datetime)";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!StringUtils.isEmpty(ins) && !StringUtils.isEmpty(vas)) {
|
|
|
|
|
sql = "(" + ins + ") values (" + vas + ")";
|
|
|
|
|
if(!StringUtils.isEmpty(ins)&&!StringUtils.isEmpty(vas)) {
|
|
|
|
|
sql = "("+ins+") values ("+vas+")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String convertColumnType(String dataType, String dbType, String width) {
|
|
|
|
|
|
|
|
|
|
public static String convertColumnType(String dataType,String dbType,String width) {
|
|
|
|
|
String columnType = dataType.toLowerCase();
|
|
|
|
|
if (dbType.contains("oracle.")) {
|
|
|
|
|
columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar2" + width) : ("varchar2(" + width + ")") :
|
|
|
|
|
columnType.contains("char") ? width.contains("(") ? ("char" + width) : ("char(" + width + ")") :
|
|
|
|
|
columnType.contains("date") || columnType.contains("time") ? "date" :
|
|
|
|
|
columnType.contains("int") || columnType.contains("num") || columnType.contains("dec") ? width.contains("(") ? ("number" + width + "") : ("number(" + width + ")") : "";
|
|
|
|
|
if(dbType.contains("oracle.")) {
|
|
|
|
|
columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar2" +width) : ("varchar2(" +width+")") :
|
|
|
|
|
columnType.contains("char") ? width.contains("(") ? ("char" +width) : ("char(" +width+")") :
|
|
|
|
|
columnType.contains("date")||columnType.contains("time") ? "date" :
|
|
|
|
|
columnType.contains("int")||columnType.contains("num")||columnType.contains("dec") ? width.contains("(") ? ("number"+width+"") : ("number("+width+")") : "";
|
|
|
|
|
} else {
|
|
|
|
|
columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar" + width) : ("varchar(" + width + ")") :
|
|
|
|
|
columnType.contains("char") ? width.contains("(") ? ("char" + width) : ("char(" + width + ")") :
|
|
|
|
|
columnType.contains("date") || columnType.contains("time") ? "datetime" :
|
|
|
|
|
columnType.contains("int") || columnType.contains("num") || columnType.contains("dec") ? width != null && width.contains(",") ? width.contains("(") ? ("decimal" + width + "") : ("decimal(" + width + ")") : "int" : "";
|
|
|
|
|
columnType = columnType.contains("varchar") ? width.contains("(") ? ("varchar" +width) : ("varchar(" +width+")") :
|
|
|
|
|
columnType.contains("char") ? width.contains("(") ? ("char" +width) : ("char(" +width+")") :
|
|
|
|
|
columnType.contains("date")||columnType.contains("time") ? "datetime" :
|
|
|
|
|
columnType.contains("int")||columnType.contains("num")||columnType.contains("dec") ? width !=null && width.contains(",") ? width.contains("(") ? ("decimal"+width+"") : ("decimal("+width+")") : "int" : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return columnType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210";
|
|
|
|
|
private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789";
|
|
|
|
|
|
|
|
|
|
/*表名简单转义*/
|
|
|
|
|
public static String tableAliasName(String tableName) {
|
|
|
|
|
String tname = "2d";
|
|
|
|
|
for (int i = 0; i < tableName.length(); i++) {
|
|
|
|
|
int idx = realNamechars.indexOf(tableName.substring(i, i + 1).toLowerCase());
|
|
|
|
|
if (idx > -1) {
|
|
|
|
|
tname += aliasNameChars.substring(idx, idx + 1);
|
|
|
|
|
String tname="2d";
|
|
|
|
|
for(int i=0;i<tableName.length();i++) {
|
|
|
|
|
int idx = realNamechars.indexOf(tableName.substring(i,i+1).toLowerCase());
|
|
|
|
|
if(idx>-1) {
|
|
|
|
|
tname+=aliasNameChars.substring(idx,idx+1);
|
|
|
|
|
} else {
|
|
|
|
|
tname += tableName.substring(i, i + 1).toLowerCase();
|
|
|
|
|
tname+=tableName.substring(i,i+1).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*获取实际表名*/
|
|
|
|
|
public static String tableRealName(String tableName) {
|
|
|
|
|
String tname = "";
|
|
|
|
|
String tname="";
|
|
|
|
|
String result = "";
|
|
|
|
|
tableName = tableName.replaceAll("202c", ",");
|
|
|
|
|
String[] tabs = tableName.split(",");
|
|
|
|
|
for (int n = 0; n < tabs.length; n++) {
|
|
|
|
|
for(int n=0;n<tabs.length;n++) {
|
|
|
|
|
String t = tabs[n];
|
|
|
|
|
tname = "";
|
|
|
|
|
if (!StringUtils.isEmpty(t)) {
|
|
|
|
|
if (!StringUtils.isEmpty(result))
|
|
|
|
|
result += ",";
|
|
|
|
|
if (t.length() > 2 && t.substring(0, 2).toLowerCase().equals("2d")) {
|
|
|
|
|
tname="";
|
|
|
|
|
if(!StringUtils.isEmpty(t)) {
|
|
|
|
|
if(!StringUtils.isEmpty(result))
|
|
|
|
|
result +=",";
|
|
|
|
|
if (t.length()>2&&t.substring(0, 2).toLowerCase().equals("2d")) {
|
|
|
|
|
t = t.substring(2);
|
|
|
|
|
for (int i = 0; i < t.length(); i++) {
|
|
|
|
|
int idx = aliasNameChars.indexOf(t.substring(i, i + 1).toLowerCase());
|
|
|
|
|
if (idx > -1) {
|
|
|
|
|
tname += realNamechars.substring(idx, idx + 1);
|
|
|
|
|
for(int i=0;i<t.length();i++) {
|
|
|
|
|
int idx = aliasNameChars.indexOf(t.substring(i,i+1).toLowerCase());
|
|
|
|
|
if(idx>-1) {
|
|
|
|
|
tname+=realNamechars.substring(idx,idx+1);
|
|
|
|
|
} else {
|
|
|
|
|
tname += t.substring(i, i + 1);
|
|
|
|
|
tname+=t.substring(i,i+1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
tname = t;
|
|
|
|
|
}
|
|
|
|
|
result += tname;
|
|
|
|
|
result+=tname;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String tableJoinWhere(String tableName, Map<String, Object> columns) {
|
|
|
|
|
public static String tableJoinWhere(String tableName,Map<String,Object> columns) {
|
|
|
|
|
String result = "";
|
|
|
|
|
if (tableName.contains(",")) {
|
|
|
|
|
if(tableName.contains(",")) {
|
|
|
|
|
String[] tabs = tableName.split(",");
|
|
|
|
|
String mTname = ((Map) columns.get("id")).get("tableName").toString();
|
|
|
|
|
String mTname = ((Map)columns.get("id")).get("tableName").toString();
|
|
|
|
|
//String sTname = ((Map)columns.get(StringUtils.uncapitalize(StringUtils.columnToJava(mTname,"false"))+"Id")).get("tableName").toString();
|
|
|
|
|
//result = mTname+".id="+sTname+"."+mTname+"_id";
|
|
|
|
|
}
|
|
|
|
@ -358,25 +358,25 @@ public class DBAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*从update语句解析select*/
|
|
|
|
|
public static String updateSqlToSelectSql(String sql, Map<String, Object> map) {
|
|
|
|
|
public static String updateSqlToSelectSql(String sql,Map<String,Object> map) {
|
|
|
|
|
String aSql = sql.toLowerCase();
|
|
|
|
|
String result = "";
|
|
|
|
|
if (map == null)
|
|
|
|
|
map = new HashMap<String, Object>();
|
|
|
|
|
if (aSql.contains("update ")) {
|
|
|
|
|
if(map==null)
|
|
|
|
|
map = new HashMap<String,Object>();
|
|
|
|
|
if(aSql.contains("update ")) {
|
|
|
|
|
int uIdx = aSql.indexOf("update ");
|
|
|
|
|
int sIdx = aSql.indexOf(" set ");
|
|
|
|
|
String tName = aSql.substring(uIdx + 7, sIdx);
|
|
|
|
|
String tName = aSql.substring(uIdx+7,sIdx);
|
|
|
|
|
int wIdx = aSql.indexOf(" where ");
|
|
|
|
|
String where = aSql.substring(wIdx + 7);
|
|
|
|
|
if (where.contains("=")) {
|
|
|
|
|
result = "select * from " + tName + " where ";
|
|
|
|
|
String where = aSql.substring(wIdx+7);
|
|
|
|
|
if(where.contains("=")) {
|
|
|
|
|
result = "select * from "+tName+" where ";
|
|
|
|
|
String[] tWh = where.split(" and ");
|
|
|
|
|
for (int i = 0; i < tWh.length; i++) {
|
|
|
|
|
if (tWh[i].contains("=")) {
|
|
|
|
|
if (!tWh[i].substring(0, tWh[i].indexOf("=")).toLowerCase().trim().equals("data_version")) {
|
|
|
|
|
result += tWh[i];
|
|
|
|
|
map.put("businessId", tWh[i].substring(tWh[i].indexOf("=") + 1).replaceAll("'", ""));
|
|
|
|
|
for(int i=0;i<tWh.length;i++) {
|
|
|
|
|
if(tWh[i].contains("=")) {
|
|
|
|
|
if(!tWh[i].substring(0, tWh[i].indexOf("=")).toLowerCase().trim().equals("data_version")) {
|
|
|
|
|
result+= tWh[i];
|
|
|
|
|
map.put("businessId", tWh[i].substring(tWh[i].indexOf("=")+1).replaceAll("'", ""));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -389,7 +389,7 @@ public class DBAUtils {
|
|
|
|
|
|
|
|
|
|
public static String escape(String str) {
|
|
|
|
|
String value = str;
|
|
|
|
|
if (value != null && value.contains("'")) {
|
|
|
|
|
if(value!=null&&value.contains("'")) {
|
|
|
|
|
value = value.replaceAll("'", "CCCBBBAAA12345ZZZYYYXXX");
|
|
|
|
|
value = value.replaceAll("CCCBBBAAA12345ZZZYYYXXX", "''");
|
|
|
|
|
}
|
|
|
|
|