Skip to content

Commit

Permalink
1.增加模块jees-loader用于处理热加载
Browse files Browse the repository at this point in the history
  • Loading branch information
aiyoyoyo committed Feb 23, 2024
1 parent 2002c88 commit b845baa
Show file tree
Hide file tree
Showing 9 changed files with 794 additions and 99 deletions.
5 changes: 5 additions & 0 deletions jees-jdbs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- Commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ public <T> List<T> selectByMap(String _db, Object _tableOrParam, int _offset, in
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
@Override
public <T> List<T> selectByMap(String _db, String _table, Set<String> _column,
Map _param, int _offset, int _limit, Class<T> _cls) {
Expand All @@ -604,107 +605,122 @@ public <T> List<T> selectByMap(String _db, String _table, Set<String> _column,
if (type.equalsIgnoreCase("sqlite")) {
db = "";
}

String sql = "SELECT ";
// 生成column字段
if (_column != null && _column.size() > 0) {
for (String col : _column) {
sql += col + ",";
}
sql = sql.substring(0, sql.length() - 1);
} else {
sql += "*";
}
sql += " FROM " + db + _table + " WHERE 1=1 ";

// 生成 where
if (_param != null && _param.size() > 0) {
Set<String> keys = _param.keySet();
for (String key : keys) {
if( key.equalsIgnoreCase("orderBy") ) continue;
if( key.equalsIgnoreCase("groupBy") ) continue;
// 生成where的值
Object value = _param.get(key);
if (value != null) {
// 不判断 列是否存在, 仅排除特殊字段
String sql_value = "";
// 仅支持简单条件,以防字符串拼接注入
if (value instanceof String) {
String tmp_val = ((String) value).trim();
// 特殊字符开头的处理
if (tmp_val.startsWith("%") || tmp_val.endsWith("%") || tmp_val.startsWith("!%")) {
if (tmp_val.startsWith("!%")) {
tmp_val = " NOT LIKE " + tmp_val;
} else {
if (tmp_val.startsWith("'") && tmp_val.endsWith("'")) {
tmp_val = " LIKE " + tmp_val;
} else {
tmp_val = " LIKE '" + tmp_val + "'";
}
}
} else if (tmp_val.startsWith(">") || tmp_val.startsWith("<") || tmp_val.startsWith("=") || tmp_val.startsWith("!=")
|| tmp_val.toLowerCase().startsWith("not")) {
// TODO 不做处理直接拼接,但是需要判定结尾是否合法 防止sql注入
// not like 要重新
// > >= < <= <> = 都需要限定是数字或者时间
} else {
// TODO 处理字符串中的特殊符号 # ' " 等
tmp_val = " = '" + tmp_val + "'";
}
sql_value = tmp_val;
} else if (value instanceof List || value instanceof Set || value.getClass().isArray()) {
List<Object> list = toList(value);
if (list.isEmpty()) {
} else {
sql_value += " IN (";
String tmp_o = "";
for (Object o : list) {
if (o == null) {
continue;
}
if (o instanceof String) {
tmp_o += "'" + o + "',";
} else if (o instanceof Integer || o instanceof Double
|| o instanceof Float || o instanceof Boolean) {
tmp_o += o + ",";
} else {
log.warn("--未支持的数据类型:" + o);
}
}
sql_value += tmp_o.substring(0, tmp_o.length() - 1) + ")";
}
} else {
// 仅限整型和布尔型
sql_value = " = " + value;
}

// 这里仅列出 type == mysql;
if (!sql_value.trim().isEmpty()) {
switch (type) {
case "sqlite":
sql += " AND " + key + sql_value;
break;
case "mysql":
default:
sql += " AND " + "`" + key + "`" + sql_value;
break;
}
}
} else {
// 这里可能有2种情况,一种是查 is null 一种是不查,这里取不查
}
}
String groupBy = (String) _param.get("groupBy");
if (StringUtil.isNotEmpty(groupBy)) {
sql += " GROUP BY " + groupBy;
}
// 生成 order by
String order_by = (String) _param.get("orderBy");
if (StringUtil.isNotEmpty(order_by)) {
sql += " ORDER BY " + order_by;
}
if(!_table.contains(".")){
_table = db + _table;
}

SQLBuilder builder = new SQLBuilder(type);


builder.select(_column != null && !_column.isEmpty() ? _column.toArray(new String[0]) : new String[]{"*"})
.from(_table)
.where(_param)
.groupBy((String) _param.get("groupBy"))
.orderBy((String) _param.get("orderBy"));

String sql = builder.build();


// String sql = "SELECT ";
// // 生成column字段
// if (_column != null && _column.size() > 0) {
// for (String col : _column) {
// sql += col + ",";
// }
// sql = sql.substring(0, sql.length() - 1);
// } else {
// sql += "*";
// }
// sql += " FROM " + db + _table + " WHERE 1=1 ";
//
// // 生成 where
// if (_param != null && _param.size() > 0) {
// Set<String> keys = _param.keySet();
// for (String key : keys) {
// if( key.equalsIgnoreCase("orderBy") ) continue;
// if( key.equalsIgnoreCase("groupBy") ) continue;
// // 生成where的值
// Object value = _param.get(key);
// if (value != null) {
// // 不判断 列是否存在, 仅排除特殊字段
// String sql_value = "";
// // 仅支持简单条件,以防字符串拼接注入
// if (value instanceof String) {
// String tmp_val = ((String) value).trim();
// // 特殊字符开头的处理
// if (tmp_val.startsWith("%") || tmp_val.endsWith("%") || tmp_val.startsWith("!%")) {
// if (tmp_val.startsWith("!%")) {
// tmp_val = " NOT LIKE " + tmp_val;
// } else {
// if (tmp_val.startsWith("'") && tmp_val.endsWith("'")) {
// tmp_val = " LIKE " + tmp_val;
// } else {
// tmp_val = " LIKE '" + tmp_val + "'";
// }
// }
// } else if (tmp_val.startsWith(">") || tmp_val.startsWith("<") || tmp_val.startsWith("=") || tmp_val.startsWith("!=")
// || tmp_val.toLowerCase().startsWith("not")) {
// // TODO 不做处理直接拼接,但是需要判定结尾是否合法 防止sql注入
// // not like 要重新
// // > >= < <= <> = 都需要限定是数字或者时间
// } else {
// // TODO 处理字符串中的特殊符号 # ' " 等
// tmp_val = " = '" + tmp_val + "'";
// }
// sql_value = tmp_val;
// } else if (value instanceof List || value instanceof Set || value.getClass().isArray()) {
// List<Object> list = toList(value);
// if (list.isEmpty()) {
// } else {
// sql_value += " IN (";
// String tmp_o = "";
// for (Object o : list) {
// if (o == null) {
// continue;
// }
// if (o instanceof String) {
// tmp_o += "'" + o + "',";
// } else if (o instanceof Integer || o instanceof Double
// || o instanceof Float || o instanceof Boolean) {
// tmp_o += o + ",";
// } else {
// log.warn("--未支持的数据类型:" + o);
// }
// }
// sql_value += tmp_o.substring(0, tmp_o.length() - 1) + ")";
// }
// } else {
// // 仅限整型和布尔型
// sql_value = " = " + value;
// }
//
// // 这里仅列出 type == mysql;
// if (!sql_value.trim().isEmpty()) {
// switch (type) {
// case "sqlite":
// sql += " AND " + key + sql_value;
// break;
// case "mysql":
// default:
// sql += " AND " + "`" + key + "`" + sql_value;
// break;
// }
// }
// } else {
// // 这里可能有2种情况,一种是查 is null 一种是不查,这里取不查
// }
// }
// String groupBy = (String) _param.get("groupBy");
// if (StringUtil.isNotEmpty(groupBy)) {
// sql += " GROUP BY " + groupBy;
// }
// // 生成 order by
// String order_by = (String) _param.get("orderBy");
// if (StringUtil.isNotEmpty(order_by)) {
// sql += " ORDER BY " + order_by;
// }
// }

log.debug("生成查询语句:" + sql);
// TODO sql的替换字符串拼接
return this.selectBySQL(_db, sql, _offset, _limit, new String[]{}, new String[]{}, _cls);
Expand Down
Loading

0 comments on commit b845baa

Please sign in to comment.