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; /** * @author chenqf */ public class DBAUtils { private static final Logger logger = LoggerFactory.getLogger(DBAUtils.class); 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 columns, Map params, String dataWhere) { return parseWhere(sql, columns, params, dataWhere, false); } public static String parseWhere(String sql, Map columns, Map 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; if (sql.contains("sqlWhere") || !sql.contains(" 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())) { String temp = params.get("sqlWhere").toString(); params.replace("sqlWhere", "(" + temp + ") and (" + dataWhere + ")"); } else { params.put("sqlWhere", dataWhere); } } } while (sql.contains("", 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(""); String str = sql.substring(b + 1, c); int e1 = str.indexOf("#{"); int e2 = str.indexOf("}", e1 + 1); if (e1 < 0) { e1 = str.indexOf("${"); } 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)) 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 (str.trim().substring(0, 3).toLowerCase().equals("or ")) { str = str.substring(str.toLowerCase().indexOf("or ") + 2); } } } n++; } else { str = ""; } sql = sql.length() > c + 5 ? sql.substring(0, a) + str + sql.substring(c + 5) : sql.substring(0, a) + str; } } if (sql.contains("")) { sql = n > 0 ? sql.replaceAll("", " where ") : sql.replaceAll("", ""); if (!StringUtils.isEmpty(dataWhere) && !sql.contains("sqlWhere")) { sql = n > 0 ? sql.replaceAll("", " and (" + dataWhere + ")") : sql.replaceAll("", " (" + dataWhere + ")"); } else { sql = sql.replaceAll("", ""); } } if (sql.contains("=isLike")) { int a1, a2, b1, b2; a1 = sql.indexOf("[==isLike]"); a2 = sql.indexOf("", 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); } else { sql = sql.length() > a2 + 5 ? sql.substring(0, a1) + sql.substring(a2 + 5) : sql.substring(0, a1); } } b1 = sql.indexOf("[!=isLike]"); b2 = sql.indexOf("", 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 = parseParams(sql, columns, params); return sql; } public static String parseParams(String sql, Map columns, Map 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); Object obj = params.get(key); String value = obj != null ? obj.toString() : ""; String dataType = "0"; if (columns != null) { Map map = (Map) 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); } } return sql; } /*生成where条件*/ public static String convertWhere(Map colums, Map params, String dataWhere) { String where = convertWhere(colums, params, dataWhere, ""); return where; } public static String convertWhere(Map colums, Map 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")) isLike = true; if (colums != null) { Iterator keys = colums.keySet().iterator(); isKey = keys.hasNext(); } for (String key : params.keySet()) { Object obj = params.get(key); value = ""; if (obj != null) value = obj.toString(); 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; } else { String[] cns = key.split(","); String wh = ""; for (String cn : cns) { String columnName = cn;//StringUtils.propertyToField(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"; } else { wh += columnName + "='" + value + "'"; } } } where += !StringUtils.isEmpty(wh) ? (!StringUtils.isEmpty(where) ? " and " : "") + "(" + wh + ")" : ""; } } } if (!StringUtils.isEmpty(dataWhere)) { if (!StringUtils.isEmpty(where)) { where = "(" + where + ") and (" + dataWhere + ")"; } else { where = dataWhere; } } if (!StringUtils.isEmpty(where)) where = "(" + where + ")"; return where; } /*生成insert语句*/ public static String parseInsert(Map params, Map columns) { String ins = ""; String vas = ""; String sql = ""; for (String key : columns.keySet()) { String value = ""; if (params.get(key) != null) value = params.get(key).toString(); Map map = (Map) columns.get(key); String dataType = map.get("dataType").toString(); 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)) { } } value = DBAUtils.escape(value); if (!StringUtils.isEmpty(value)) { if (!StringUtils.isEmpty(ins)) ins += ","; ins += map.get("columnName").toString(); if (!StringUtils.isEmpty(vas)) vas += ","; if (StringUtils.isEmpty(value)) { vas += "null"; } else { if (dataType.equals("C")) { vas += "'" + value + "'"; } else if (dataType.equals("N")) { vas += value; } else { String str = ""; Date date = new Date(); if (StringUtils.isNumeric(value)) { date = new Date(Long.valueOf(value)); str = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } else { str = DateUtil.formatDate(IDCUtils.parseDate(value), "yyyy-MM-dd HH:mm:ss"); } vas += "cast('" + str + "' as datetime)"; } } } } if (!StringUtils.isEmpty(ins) && !StringUtils.isEmpty(vas)) { sql = "(" + ins + ") values (" + vas + ")"; } return sql; } 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 + ")") : ""; } 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" : ""; } 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); } else { tname += tableName.substring(i, i + 1).toLowerCase(); } } return tname; } /*获取实际表名*/ public static String tableRealName(String tableName) { String tname = ""; String result = ""; tableName = tableName.replaceAll("202c", ","); String[] tabs = tableName.split(","); 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")) { 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); } else { tname += t.substring(i, i + 1); } } } else { tname = t; } result += tname; } } return result; } public static String tableJoinWhere(String tableName, Map columns) { String result = ""; if (tableName.contains(",")) { String[] tabs = tableName.split(","); 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"; } return result; } /*从update语句解析select*/ public static String updateSqlToSelectSql(String sql, Map map) { String aSql = sql.toLowerCase(); String result = ""; if (map == null) map = new HashMap(); if (aSql.contains("update ")) { int uIdx = aSql.indexOf("update "); int sIdx = aSql.indexOf(" set "); 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[] 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("'", "")); break; } } } } map.put("tableName", tName); } return result; } public static String escape(String str) { String value = str; if (value != null && value.contains("'")) { value = value.replaceAll("'", "AAA12345ZZZ"); value = value.replaceAll("AAA12345ZZZ", "''"); } return value; } }