|
@@ -13,33 +13,55 @@ import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class DatabaseScriptUtil {
|
|
|
-
|
|
|
- private static final List<String> events = new ArrayList<>();
|
|
|
- private static final Pattern regExpression = Pattern.compile("(?<=《)([^》]+)?(?=》)");//提取书名号变量的正则表达式
|
|
|
- private static final Map<String, List<String>> sqlStrVarList = new HashMap<>();//SQL语句的书名号变量列表
|
|
|
+ /**
|
|
|
+ * 数据处理相关的静态变量和初始化。
|
|
|
+ * EVENTS: 存储事件编号的列表。
|
|
|
+ * REG_EXPRESSION: 用于提取书名号括起来的变量的正则表达式。
|
|
|
+ * SQL_STR_VAR_LIST: 存储SQL语句中书名号变量与对应值的列表。
|
|
|
+ * sqlStrNewSQL: 存储更换了书名号变量后的可执行SQL语句的映射。
|
|
|
+ */
|
|
|
+ private static final List<String> EVENTS = new ArrayList<>();
|
|
|
+ private static final Pattern REG_EXPRESSION = Pattern.compile("(?<=《)([^》]+)?(?=》)");//提取书名号变量的正则表达式
|
|
|
+ private static final Map<String, List<String>> SQL_STR_VAR_LIST = new HashMap<>();//SQL语句的书名号变量列表
|
|
|
public static Map<String, String> sqlStrNewSQL = new HashMap<>();//SQL语句更换书名号变量后的可执行SQL
|
|
|
|
|
|
+ // 初始化EVENTS列表
|
|
|
static {
|
|
|
- events.add("0");
|
|
|
- events.add("1");
|
|
|
- events.add("2");
|
|
|
- events.add("3");
|
|
|
- events.add("6");
|
|
|
- events.add("7");
|
|
|
+ EVENTS.add("0");
|
|
|
+ EVENTS.add("1");
|
|
|
+ EVENTS.add("2");
|
|
|
+ EVENTS.add("3");
|
|
|
+ EVENTS.add("6");
|
|
|
+ EVENTS.add("7");
|
|
|
}
|
|
|
|
|
|
+ // 数据库操作对象
|
|
|
private final DataBase DATABASE = new DataBase();
|
|
|
|
|
|
- public Pageable pageable(Map<String, Object> args) {
|
|
|
+ /**
|
|
|
+ * 根据提供的参数生成Pageable对象。
|
|
|
+ * 如果"event"参数非空且不等于"0",则返回null。
|
|
|
+ * 否则,从args中解析"page"和"size"参数来构造Pageable对象。
|
|
|
+ * 如果解析出的页码小于0,则默认为0;如果解析出的页面大小小于等于0,则默认为50。
|
|
|
+ *
|
|
|
+ * @param args 包含页面请求参数的映射
|
|
|
+ * @return 根据提供的参数构造的Pageable对象,或者在不满足条件时返回null
|
|
|
+ */
|
|
|
+ public Pageable pageable(Map<String, Object> args) {
|
|
|
+
|
|
|
+ // 解析页面和大小参数
|
|
|
Object page = args.get("page");
|
|
|
Object size = args.get("size");
|
|
|
Long pageNumber = 0L;
|
|
|
Long pageSizeNumber = 0L;
|
|
|
Object event = args.get("event");
|
|
|
+
|
|
|
+ // 如果事件参数非空且不等于"0",则返回null
|
|
|
if (event == null || !event.toString().equals("0")) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ // 处理页面号和页面大小
|
|
|
if (Objects.nonNull(page)) {
|
|
|
pageNumber = Long.parseLong(page.toString()) - 1;
|
|
|
}
|
|
@@ -52,52 +74,104 @@ public class DatabaseScriptUtil {
|
|
|
if (pageSizeNumber <= 0) {
|
|
|
pageSizeNumber = 50L;
|
|
|
}
|
|
|
+
|
|
|
+ // 返回构造的Pageable对象
|
|
|
return new Pageable(pageNumber, pageSizeNumber);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据过滤行列表生成SQL WHERE子句字符串。
|
|
|
+ * 过滤行列表中的每个条目应包含列名、比较器和值,可选地包括左右连接器和事件标志。
|
|
|
+ *
|
|
|
+ * @param filterLines 过滤条件的列表,每个过滤条件是一个包含列、比较器和值的映射
|
|
|
+ * @return 根据过滤行列表构造的WHERE子句字符串
|
|
|
+ */
|
|
|
public String getFilterLinesWhereSql(List<Map<String, Object>> filterLines) {
|
|
|
- if (Objects.isNull(filterLines)) filterLines = new ArrayList<>();
|
|
|
+ if(filterLines == null){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // 初始化WHERE条件字符串
|
|
|
String filterLineWhereStr = "";
|
|
|
for (Object f : filterLines) {
|
|
|
Map<String, Object> it = ((Map<String, Object>) f);
|
|
|
Object comparator = it.get("comparator").toString().toLowerCase();
|
|
|
+ // 格式化构造WHERE子句
|
|
|
filterLineWhereStr = " %s %s %s %s %s %s %s ".formatted(filterLineWhereStr,
|
|
|
it.getOrDefault("left", ""),
|
|
|
it.get("column"),
|
|
|
comparator,
|
|
|
- Objects.equals(comparator, " is null ") || Objects.equals(comparator, " is not null ") ? "" :"'"+ it.get("value")+"'",
|
|
|
+ Objects.equals(comparator, " is null ") || Objects.equals(comparator, " is not null ") ? "" : "'" + it.get("value") + "'",
|
|
|
it.getOrDefault("right", ""),
|
|
|
it.getOrDefault("connector", ""));
|
|
|
}
|
|
|
return filterLineWhereStr;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 从SQL字符串中提取变量列表,并更新SQL为使用占位符的形式。
|
|
|
+ * 初始版本的SQL字符串不会被修改,而是创建一个新的SQL字符串用于后续处理。
|
|
|
+ *
|
|
|
+ * @param sqlStr 原始SQL字符串
|
|
|
+ * @return SQL字符串中包含的变量列表
|
|
|
+ */
|
|
|
public List<String> getSQLVarList(String sqlStr) {
|
|
|
- String newSqlStr = sqlStr;//不破坏之前SQL语句
|
|
|
- List<String> sqlvarList = sqlStrVarList.containsKey(sqlStr) ? sqlStrVarList.get(sqlStr) : new ArrayList<>();//优先从缓存中取
|
|
|
- if (!sqlStrNewSQL.containsKey(sqlStr)) {//缓存不存在则重新获取
|
|
|
- Matcher matcher = regExpression.matcher(sqlStr);//正则提取
|
|
|
- while (matcher.find()) {//循环提取到的所有书名号变量
|
|
|
+ // 不修改原始SQL字符串
|
|
|
+ String newSqlStr = sqlStr;
|
|
|
+ // 从缓存中获取或创建新的变量列表
|
|
|
+ List<String> sqlvarList = SQL_STR_VAR_LIST.containsKey(sqlStr) ? SQL_STR_VAR_LIST.get(sqlStr) : new ArrayList<>();
|
|
|
+ // 如果缓存中不存在,则重新解析SQL
|
|
|
+ if (!sqlStrNewSQL.containsKey(sqlStr)) {
|
|
|
+ // 使用正则表达式提取变量
|
|
|
+ Matcher matcher = REG_EXPRESSION.matcher(sqlStr);
|
|
|
+ while (matcher.find()) {
|
|
|
String varName = matcher.group();
|
|
|
sqlvarList.add(varName.trim());
|
|
|
- newSqlStr = newSqlStr.replaceFirst("《".concat(varName).concat("》"), " ? "); //修订SQL
|
|
|
+ // 用占位符替换变量
|
|
|
+ newSqlStr = newSqlStr.replaceFirst("《".concat(varName).concat("》"), " ? ");
|
|
|
}
|
|
|
- sqlStrVarList.put(sqlStr, sqlvarList);//添加到缓存
|
|
|
- sqlStrNewSQL.put(sqlStr, newSqlStr);//添加到缓存
|
|
|
+ // 更新缓存
|
|
|
+ SQL_STR_VAR_LIST.put(sqlStr, sqlvarList);
|
|
|
+ sqlStrNewSQL.put(sqlStr, newSqlStr);
|
|
|
}
|
|
|
return sqlvarList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 检查数据库中是否存在匹配的记录。
|
|
|
+ * 使用提供的连接字符串、SQL语句和参数数组执行查询,判断是否存在符合条件的记录。
|
|
|
+ *
|
|
|
+ * @param connectionStr 数据库连接字符串
|
|
|
+ * @param sql SQL查询语句
|
|
|
+ * @param args SQL查询参数数组
|
|
|
+ * @return 如果存在匹配的记录,则返回true;否则返回false
|
|
|
+ * @throws Exception 如果查询过程中出现错误,则抛出异常
|
|
|
+ */
|
|
|
private boolean exists(String connectionStr, String sql, Object[] args) throws Exception {
|
|
|
+ // 执行查询
|
|
|
List<Map<String, Object>> mapList = DATABASE.queryBatch(connectionStr, sql, Collections.singletonList(args));
|
|
|
+ // 判断是否存在记录,并处理结果
|
|
|
if (mapList.isEmpty()) return false;
|
|
|
return !Objects.equals(mapList.get(0).get("existscount").toString(), "0");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据提供的SQL查询数据。
|
|
|
+ *
|
|
|
+ * @param connectionStr 数据源标识或连接字符串
|
|
|
+ * @param sql 查询的SQL语句
|
|
|
+ * @param argsList 查询参数列表
|
|
|
+ * @param filterColumns 需要过滤的列
|
|
|
+ * @param filterLines 过滤条件行
|
|
|
+ * @param pageable 分页信息
|
|
|
+ * @return 查询结果列表
|
|
|
+ * @throws Exception 查询过程中的任何异常
|
|
|
+ */
|
|
|
public List<Map<String, Object>> query(String connectionStr, String sql, List<Map<String, Object>> argsList, List<String> filterColumns, List<Map<String, Object>> filterLines, Pageable pageable) throws Exception {
|
|
|
|
|
|
+ // 替换SQL中的换行符
|
|
|
sql = sql.replaceAll("(\\r)?\\n", " ");
|
|
|
String filterLineWhereStr = null;
|
|
|
+ // 构建基于filterLines的where条件字符串
|
|
|
if (Objects.nonNull(filterLines)) {
|
|
|
for (Object f : filterLines) {
|
|
|
Map<String, Object> it = ((Map<String, Object>) f);
|
|
@@ -105,9 +179,11 @@ public class DatabaseScriptUtil {
|
|
|
filterLineWhereStr = " %s %s %s %s %s %s %s %s ".formatted(filterLineWhereStr, it.getOrDefault("left", ""), it.get("column"), comparator, Objects.equals(comparator, " is null ") ? "" : "?", !Objects.equals(comparator, " is null ") ? " " : it.get("value"), it.getOrDefault("right", ""), it.getOrDefault("connector", ""));
|
|
|
}
|
|
|
}
|
|
|
+ // 将构建的where条件附加到SQL上
|
|
|
if (Objects.nonNull(filterLineWhereStr)) {
|
|
|
sql = " %s %s and %s ".formatted(sql, sql.contains(" where ") ? " 1 = 1" : " where ", filterLineWhereStr);
|
|
|
}
|
|
|
+ // 如果指定过滤列,则将查询结果限定在这些列上
|
|
|
if (Objects.nonNull(filterColumns) && !filterColumns.isEmpty()) {
|
|
|
sql = "select %s from ( %s ) T".formatted(String.join(",", filterColumns), sql);
|
|
|
}
|
|
@@ -117,6 +193,7 @@ public class DatabaseScriptUtil {
|
|
|
List<String> names = getSQLVarList(sql);
|
|
|
String newSql = DatabaseScriptUtil.sqlStrNewSQL.get(sql);
|
|
|
|
|
|
+ // 处理whereStr变量替换
|
|
|
if (names.size() == 1 && names.contains("whereStr")) {
|
|
|
List<Map<String, Object>> list = new ArrayList<>();
|
|
|
for (Map<String, Object> stringObjectMap : argsList) {
|
|
@@ -134,6 +211,7 @@ public class DatabaseScriptUtil {
|
|
|
}
|
|
|
return list;
|
|
|
} else {
|
|
|
+ // 准备查询参数
|
|
|
for (Map<String, Object> map : argsList) {
|
|
|
List<Object> args = new ArrayList<>();
|
|
|
map = ((Map<String, Object>) map.getOrDefault("filter", map));
|
|
@@ -143,13 +221,24 @@ public class DatabaseScriptUtil {
|
|
|
result.add(args);
|
|
|
}
|
|
|
|
|
|
+ // 处理分页
|
|
|
if (pageable != null) {
|
|
|
newSql = "%s limit %d offset %d ".formatted(newSql, pageable.pageSize, pageable.page * pageable.pageSize);
|
|
|
}
|
|
|
+ // 执行查询并返回结果
|
|
|
return DATABASE.queryBatch(connectionStr, newSql, result.stream().map(List::toArray).toList());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据SQL表达式执行数据库操作。
|
|
|
+ *
|
|
|
+ * @param datasourceId 数据源标识
|
|
|
+ * @param expression SQL表达式
|
|
|
+ * @param args 查询参数
|
|
|
+ * @return 操作结果
|
|
|
+ * @throws Exception 操作过程中的任何异常
|
|
|
+ */
|
|
|
public Map<String, Object> execBySql(String datasourceId, String expression, Map<String, Object> args) throws Exception {
|
|
|
Object event = args.get("event");
|
|
|
Pageable pageable = pageable(args);
|
|
@@ -164,6 +253,7 @@ public class DatabaseScriptUtil {
|
|
|
if (!event.equals("0")) {
|
|
|
throw new RuntimeException("执行编号只能是 :0");
|
|
|
}
|
|
|
+ // 处理数据内容和过滤条件
|
|
|
List<Map<String, Object>> dataContent = DataFormatUtil.toList(args.get("datacontent")).stream().map(it -> ((Map<String, Object>) it)).toList();
|
|
|
Object filterColumnsTemp = args.get("filterColumns");
|
|
|
List<String> filterColumns = null;
|
|
@@ -178,24 +268,26 @@ public class DatabaseScriptUtil {
|
|
|
if (Objects.isNull(filterColumns)) filterColumns = new ArrayList<>();
|
|
|
|
|
|
if (filterColumns.isEmpty()) {
|
|
|
-// 没有列权限直接返回空数据
|
|
|
+ // 没有列权限直接返回空数据
|
|
|
return UniReturnUtil.success(new ArrayList<>());
|
|
|
}
|
|
|
|
|
|
String filterLineWhereStr = getFilterLinesWhereSql(filterLines);
|
|
|
|
|
|
String connectionStr = datasourceId;
|
|
|
+ // 根据数据源标识获取实际连接字符串
|
|
|
if (Pattern.compile("^\\d+$").matcher(connectionStr.trim()).matches()) {
|
|
|
connectionStr = DATABASE.queryConnectionStr(connectionStr.trim());
|
|
|
}
|
|
|
|
|
|
if (expression.contains("《whereStr》")) {
|
|
|
|
|
|
- String whereStr = " ";//初始化条件字符串
|
|
|
- List<Object> dbFilter = new ArrayList<>();//初始化条件执行参数
|
|
|
+ String whereStr = " ";// 初始化条件字符串
|
|
|
+ List<Object> dbFilter = new ArrayList<>();// 初始化条件执行参数
|
|
|
Map<String, Object> map = dataContent.get(0);
|
|
|
Object filter = map.get("filter");
|
|
|
- if (filter instanceof List filterList && !filterList.isEmpty()) {//如果存在上传的条件参数或者存在行权限
|
|
|
+ // 根据提供的过滤条件构建where字符串和执行参数
|
|
|
+ if (filter instanceof List filterList && !filterList.isEmpty()) {// 如果存在上传的条件参数或者存在行权限
|
|
|
Map<String, Object> filterMap = (Map<String, Object>) filterList.get(0);
|
|
|
if (filterMap.containsKey("column")) {
|
|
|
for (Object f : filterList) {
|
|
@@ -215,34 +307,41 @@ public class DatabaseScriptUtil {
|
|
|
whereStr += list.stream().map(it -> "%s = ?".formatted(it.getKey())).collect(Collectors.joining(" and "));
|
|
|
dbFilter.addAll(list.stream().map(Map.Entry::getValue).toList());
|
|
|
}
|
|
|
+ // 组合已有的where条件和行过滤条件
|
|
|
whereStr = " %s and %s".formatted(whereStr.trim().isEmpty() ? " 1=1 " : whereStr, filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr);
|
|
|
String sql = expression.replaceAll("《whereStr》", " " + whereStr);
|
|
|
|
|
|
+ // 处理查询列
|
|
|
sql = "select %s from (%s) as T".formatted(String.join(",", filterColumns), sql);
|
|
|
|
|
|
+ // 处理分页
|
|
|
if (pageable != null && !sql.contains(" limit ")) {
|
|
|
sql = "%s limit %d offset %d ".formatted(sql, pageable.pageSize, pageable.page * pageable.pageSize);
|
|
|
}
|
|
|
+ // 执行查询
|
|
|
List<Map<String, Object>> queryResult = DATABASE.queryBatch(connectionStr, sql, Collections.singletonList(dbFilter.toArray()));
|
|
|
return UniReturnUtil.success(queryResult);
|
|
|
|
|
|
} else {
|
|
|
if (Objects.equals(event, "0")) {
|
|
|
+ // 执行查询操作
|
|
|
List<Map<String, Object>> queryResult = query(connectionStr, expression, dataContent, filterColumns, filterLines, pageable);
|
|
|
return UniReturnUtil.success(queryResult);
|
|
|
} else if (Objects.equals(event, "1")) {
|
|
|
- if (sqlStrVarList.containsKey(expression)) {
|
|
|
+ // 执行更新操作
|
|
|
+ if (SQL_STR_VAR_LIST.containsKey(expression)) {
|
|
|
getSQLVarList(expression);
|
|
|
}
|
|
|
- List<String> names = sqlStrVarList.get(expression);
|
|
|
+ List<String> names = SQL_STR_VAR_LIST.get(expression);
|
|
|
String sql = sqlStrNewSQL.get(expression);
|
|
|
int[] updateResult = DATABASE.updateBatch(connectionStr, sql, dataContent.stream().map(it -> names.stream().map(it::get).toArray()).toList());
|
|
|
return UniReturnUtil.success(updateResult);
|
|
|
} else {
|
|
|
- if (sqlStrVarList.containsKey(expression)) {
|
|
|
+ // 执行更新操作,考虑行过滤条件
|
|
|
+ if (SQL_STR_VAR_LIST.containsKey(expression)) {
|
|
|
getSQLVarList(expression);
|
|
|
}
|
|
|
- List<String> names = sqlStrVarList.get(expression);
|
|
|
+ List<String> names = SQL_STR_VAR_LIST.get(expression);
|
|
|
String sql = " %s and %s".formatted(sqlStrNewSQL.get(expression), Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
int[] updateResult = DATABASE.updateBatch(connectionStr, sql, dataContent.stream().map(it -> names.stream().map(it::get).toArray()).toList());
|
|
|
return UniReturnUtil.success(updateResult);
|
|
@@ -250,26 +349,41 @@ public class DatabaseScriptUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Map<String, Object> execByTableName(String datasourceId, String table, Map<String, Object> args) throws Exception {
|
|
|
+ /**
|
|
|
+ * 根据提供的参数执行特定的数据操作。
|
|
|
+ *
|
|
|
+ * @param table 表名,不能为空且不能包含空格。
|
|
|
+ * @param args 包含执行所需各种参数的Map,包括事件标识、分页信息、数据内容、过滤列和过滤行等。
|
|
|
+ * @return 执行结果,这里未指定具体返回类型,因为代码片段未包含完整的函数体。
|
|
|
+ * @throws RuntimeException 当表名为空、包含空格、事件标识为空或不是预定义的事件编号之一时,抛出异常。
|
|
|
+ */
|
|
|
|
|
|
+ public Map<String, Object> execByTableName(String datasourceId, String table, Map<String, Object> args) throws Exception {
|
|
|
|
|
|
+ // 验证表名的合法性
|
|
|
if (Objects.isNull(table) || table.trim().contains(" ")) {
|
|
|
throw new RuntimeException("表名不合法: 表名不能为空");
|
|
|
}
|
|
|
String tableName = table;
|
|
|
+
|
|
|
+ // 从参数中获取事件标识并验证其合法性
|
|
|
Object event = args.get("event");
|
|
|
- Pageable pageable = pageable(args);
|
|
|
+ Pageable pageable = pageable(args); // 获取分页信息,函数pageable未在代码片段中定义
|
|
|
|
|
|
if (Objects.isNull(event) || !StringUtils.hasText(event.toString())) {
|
|
|
throw new RuntimeException("执行编号不能为空");
|
|
|
}
|
|
|
event = event.toString().trim();
|
|
|
|
|
|
- if (!events.contains(event)) {
|
|
|
-
|
|
|
+ // 检查事件是否在预定义的事件集合中
|
|
|
+ if (!EVENTS.contains(event)) {
|
|
|
throw new RuntimeException("执行编号只能是 :0,1,2,3,6,7");
|
|
|
}
|
|
|
+
|
|
|
+ // 处理数据内容参数
|
|
|
List<Map<String, Object>> dataContent = DataFormatUtil.toList(args.get("datacontent")).stream().map(it -> ((Map<String, Object>) it)).toList();
|
|
|
+
|
|
|
+ // 处理过滤列参数
|
|
|
Object filterColumnsTemp = args.get("filterColumns");
|
|
|
List<String> filterColumns;
|
|
|
if (Objects.nonNull(filterColumnsTemp))
|
|
@@ -277,46 +391,107 @@ public class DatabaseScriptUtil {
|
|
|
else {
|
|
|
filterColumns = new ArrayList<>();
|
|
|
}
|
|
|
+
|
|
|
+ // 处理过滤行参数,这里代码片段未完成
|
|
|
Object filterLinesTemp = args.get("filterLines");
|
|
|
- List<Map<String, Object>> filterLines = null;
|
|
|
+ List<Map<String, Object>> filterLines = new ArrayList<>();
|
|
|
+ // ... 这里应有进一步的代码来处理filterLinesTemp,但代码片段未给出
|
|
|
+ // 将可能为null的filterLinesTemp转换为非null的List<Map<String, Object>>类型
|
|
|
if (Objects.nonNull(filterLinesTemp))
|
|
|
filterLines = DataFormatUtil.toList(filterLinesTemp).stream().map(it -> ((Map<String, Object>) it)).toList();
|
|
|
|
|
|
-
|
|
|
+ // 初始化expression为table的值
|
|
|
String expression = table;
|
|
|
|
|
|
+ // 根据filterLines生成对应的SQL WHERE子句
|
|
|
String filterLineWhereStr = getFilterLinesWhereSql(filterLines);
|
|
|
|
|
|
+ // 初始化connectionStr为datasourceId的值,之后可能根据其是否为数字进行数据库连接字符串的查询
|
|
|
String connectionStr = datasourceId;
|
|
|
+ // 如果datasourceId是纯数字,则通过DATABASE查询对应的数据库连接字符串
|
|
|
if (Pattern.compile("^\\d+$").matcher(connectionStr.trim()).matches()) {
|
|
|
connectionStr = DATABASE.queryConnectionStr(connectionStr.trim());
|
|
|
}
|
|
|
+
|
|
|
+ // 初始化用于存储列名的列表
|
|
|
List<String> valueNames = null;
|
|
|
List<String> filterNames = null;
|
|
|
+
|
|
|
+ // 通过connectionStr和expression查询表的所有列名
|
|
|
List<String> allColumns = DATABASE.getColumnsByTableName(connectionStr, expression);
|
|
|
- if(filterColumns.size()==1 && !"*".equals(filterColumns.get(0))) {
|
|
|
- List<String> filterColumns2 = new ArrayList<>();
|
|
|
- for (String allColumn : allColumns) {
|
|
|
- if (filterColumns.contains(allColumn)) {
|
|
|
- filterColumns2.add(allColumn);
|
|
|
- }
|
|
|
- }
|
|
|
- filterColumns = filterColumns2;
|
|
|
|
|
|
+ // 如果filterColumns只包含一个元素且不为"*",则筛选出该元素在allColumns中存在的列名
|
|
|
+ if (filterColumns.size() == 1 && "*".equals(filterColumns.get(0))) {
|
|
|
+ filterColumns = allColumns;
|
|
|
}
|
|
|
-
|
|
|
// 查询
|
|
|
if (Objects.equals("0", event)) {
|
|
|
+ return queryByEventZero(args, filterColumns, dataContent, expression, allColumns, filterLineWhereStr, pageable, connectionStr, tableName);
|
|
|
+// 更新或新增
|
|
|
+ } else if (Objects.equals("6", event)) {
|
|
|
+// 新增或更新
|
|
|
+ return execByEventSix(args, dataContent, expression, connectionStr);
|
|
|
+ } else {
|
|
|
+// 新增修改删除
|
|
|
+ return execByModify(datasourceId, table, args, filterColumns, allColumns, event, dataContent, valueNames, expression, filterNames, filterLineWhereStr, connectionStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- if (filterColumns.isEmpty()) {
|
|
|
-// 列权限为空直接返回空数据
|
|
|
- return UniReturnUtil.success(new ArrayList<>());
|
|
|
+ /**
|
|
|
+ * 根据不同的事件类型执行数据库操作(新增、更新、删除)。
|
|
|
+ *
|
|
|
+ * @param datasourceId 数据源ID
|
|
|
+ * @param table 表名
|
|
|
+ * @param args 通用参数,用于不同操作中的额外信息传递
|
|
|
+ * @param filterColumns 过滤列,用于条件查询
|
|
|
+ * @param allColumns 所有列,包含表中的所有字段
|
|
|
+ * @param event 事件类型,决定执行的操作(1: 新增,2: 更新,3: 删除,7: 先删除后新增)
|
|
|
+ * @param dataContent 数据内容,包含要操作的数据信息
|
|
|
+ * @param valueNames 值名称列表,用于构建SQL语句中的值列表
|
|
|
+ * @param expression 基本的SQL表达式框架,用于构建最终的SQL语句
|
|
|
+ * @param filterNames 过滤名称列表,用于构建SQL语句中的过滤条件
|
|
|
+ * @param filterLineWhereStr 过滤行的WHERE子串,额外的过滤条件
|
|
|
+ * @param connectionStr 连接字符串,用于数据库连接
|
|
|
+ * @return 操作结果,通常为影响的行数
|
|
|
+ * @throws Exception 数据库操作异常或其他异常
|
|
|
+ */
|
|
|
+ private Map<String, Object> execByModify(String datasourceId, String table, Map<String, Object> args, List<String> filterColumns, List<String> allColumns, Object event, List<Map<String, Object>> dataContent, List<String> valueNames, String expression, List<String> filterNames, String filterLineWhereStr, String connectionStr) throws Exception {
|
|
|
+ // 根据过滤列更新allColumns
|
|
|
+ if (filterColumns.size() == 1 && !"*".equals(filterColumns.get(0))) {
|
|
|
+ for (String filterColumn : filterColumns) {
|
|
|
+ if (!allColumns.contains(filterColumn)) {
|
|
|
+ allColumns.remove(filterColumn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 根据不同的事件类型处理数据并构建SQL语句
|
|
|
+ if (Objects.equals("1", event)) { // 新增
|
|
|
+ Map<String, Object> value = dataContent.get(0);
|
|
|
+ Object valueObj = value.getOrDefault("value", value);
|
|
|
+ Map<?, ?> map = null;
|
|
|
+ if (valueObj instanceof ArrayNode v) {
|
|
|
+ map = ((Map<?, ?>) v.get(0));
|
|
|
+ } else if (valueObj instanceof List<?> v) {
|
|
|
+ map = ((Map<?, ?>) v.get(0));
|
|
|
+ } else if (valueObj instanceof Map<?, ?> v) {
|
|
|
+ map = v;
|
|
|
}
|
|
|
- Map<String, Object> argMap = Objects.isNull(args) || args.isEmpty() ? new HashMap<>() : dataContent.get(0);
|
|
|
+ valueNames = (List<String>) map.keySet().stream().filter(allColumns::contains).toList();
|
|
|
+ expression = "insert into %s ( %s) values(%s)".formatted(expression, String.join(",", valueNames), valueNames.stream().map(it -> "?").collect(Collectors.joining(",")));
|
|
|
+
|
|
|
+ } else if (Objects.equals("2", event)) { // 更新
|
|
|
+ Map<String, Object> map = dataContent.get(0);
|
|
|
+ Map<String, Object> value = ((Map<String, Object>) map.get("value"));
|
|
|
+ valueNames = value.keySet().stream().filter(allColumns::contains).toList();
|
|
|
+ Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
|
+ filterNames = filter.keySet().stream().filter(allColumns::contains).toList();
|
|
|
|
|
|
+ expression = "update %s set %s where %s and %s".formatted(expression, valueNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(",")), filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")), filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr);
|
|
|
+
|
|
|
+ } else if (Objects.equals("3", event)) { // 删除
|
|
|
+ Map<String, Object> argMap = dataContent.get(0);
|
|
|
Object filter = argMap.getOrDefault("filter", argMap);
|
|
|
- Map map = new HashMap<>();
|
|
|
+ Map<?, ?> map = null;
|
|
|
if (filter instanceof ArrayNode f) {
|
|
|
map = ((Map<?, ?>) f.get(0));
|
|
|
} else if (filter instanceof List<?> f) {
|
|
@@ -325,239 +500,257 @@ public class DatabaseScriptUtil {
|
|
|
} else if (filter instanceof Map<?, ?> f) {
|
|
|
map = f;
|
|
|
}
|
|
|
-
|
|
|
- String whereStr = "";
|
|
|
- List<Object[]> values = new ArrayList<>();
|
|
|
- if (map.containsKey("column") && map.containsKey("comparator")) {
|
|
|
-
|
|
|
- ArrayList<Object> objects = new ArrayList<>();
|
|
|
-// for (Object it : DataFormatUtil.toList(filter)) {
|
|
|
-//// filter = it.getOrDefault("filter", it);
|
|
|
-// if (it instanceof Map<?, ?> it) {
|
|
|
-// map = it;
|
|
|
-// }
|
|
|
-// Object comparator = map.get("comparator");
|
|
|
-// whereStr = " %s %s %s %s %s %s %s ".formatted(whereStr, map.getOrDefault("left", ""), map.get("column"), comparator, Objects.equals(comparator, " is null ") ? " " : "?", map.getOrDefault("right", ""), map.getOrDefault("connector", ""));
|
|
|
-// if (!Objects.equals(comparator, " is null ")) {
|
|
|
-// objects.add(map.get("value"));
|
|
|
-// }
|
|
|
-// }
|
|
|
- whereStr = getFilterLinesWhereSql((List<Map<String, Object>>) filter);
|
|
|
- expression = "select %s from %s where %s and %s".formatted(
|
|
|
- String.join(",", allColumns),
|
|
|
- expression,
|
|
|
- Objects.isNull(filterLineWhereStr) || filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr,
|
|
|
- whereStr.trim().isEmpty() ? " 1=? " : whereStr);
|
|
|
- if (whereStr.trim().isEmpty()) {
|
|
|
- values.add(new Object[]{1});
|
|
|
- } else {
|
|
|
- values.add(objects.toArray());
|
|
|
- }
|
|
|
- } else {
|
|
|
- filterNames = map.keySet().stream().toList();
|
|
|
-
|
|
|
- expression = "select %s from %s where %s %s".formatted(String.join(",", allColumns), expression, filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr,
|
|
|
- filterNames.isEmpty() ?
|
|
|
- "" : (" and " +
|
|
|
- filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and "))));
|
|
|
-
|
|
|
- for (Map<String, Object> arg : dataContent) {
|
|
|
- Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
- values.add(filterNames.stream().map(o1::get).toArray());
|
|
|
- }
|
|
|
+ filterNames = (List<String>) map.keySet().stream().filter(allColumns::contains).toList();
|
|
|
+ expression = "delete from %s where %s and %s".formatted(expression, filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")), filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr);
|
|
|
+ } else if (Objects.equals("7", event)) { // 先删除后新增
|
|
|
+ args.put("event", "3");
|
|
|
+ execByTableName(datasourceId, table, args); // 执行删除
|
|
|
+ args.put("event", "1");
|
|
|
+ return execByTableName(datasourceId, table, args); // 执行新增
|
|
|
+ }
|
|
|
+ List<Object[]> values = new ArrayList<>();
|
|
|
+ // 按照名称 和过滤条件取值,准备批量操作的数据
|
|
|
+ final List<String> finalFilterColumns = filterColumns;
|
|
|
+ for (Map<String, Object> arg : dataContent) {
|
|
|
+ List<Object> objects = new ArrayList<>();
|
|
|
+ if (Objects.nonNull(valueNames)) {
|
|
|
+ Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("value", arg));
|
|
|
+ objects.addAll(valueNames.stream().map(it -> {
|
|
|
+ // 根据是否为过滤列或是否包含所有列来决定是否取值
|
|
|
+ if (finalFilterColumns.contains(it) || finalFilterColumns.isEmpty() || (finalFilterColumns.get(0).equals("*") && finalFilterColumns.size() == 1)) {
|
|
|
+ return o1.get(it);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }).toList());
|
|
|
}
|
|
|
- if (pageable != null) {
|
|
|
- expression = "%s limit %d offset %d ".formatted(expression, pageable.pageSize, pageable.page * pageable.pageSize);
|
|
|
+ if (Objects.nonNull(filterNames)) {
|
|
|
+ Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
+ objects.addAll(filterNames.stream().map(o1::get).toList());
|
|
|
}
|
|
|
- expression = "select %s from (%s) as T".formatted(String.join(",", filterColumns), expression);
|
|
|
- List<Map<String, Object>> result = DATABASE.queryBatch(connectionStr, expression, values);
|
|
|
-
|
|
|
-// 查询关联项
|
|
|
-
|
|
|
- List<Map<String, Object>> tableJoins = DATABASE.query(Config.getCenterConnectionStr(), """
|
|
|
- select id, sourcetable, targettable, sourcejoincolumn, targetjoincolumn, targetshowcolumns, datasourceid
|
|
|
- from tablejoin where sourcetable=?""", tableName);
|
|
|
- if (!tableJoins.isEmpty()) {
|
|
|
- for (Map<String, Object> tableJoin : tableJoins) {
|
|
|
- Object targettable = tableJoin.get("targettable");
|
|
|
- String sourcejoincolumn = tableJoin.get("sourcejoincolumn").toString();
|
|
|
- String targetjoincolumn = tableJoin.get("targetjoincolumn").toString();
|
|
|
- String targetshowcolumns = tableJoin.get("targetshowcolumns").toString();
|
|
|
-
|
|
|
- String[] split = targetshowcolumns.split(",");
|
|
|
- Map<String, String> showColumns = new HashMap<>();
|
|
|
- for (String string : split) {
|
|
|
- if (string.contains(" as ")) {
|
|
|
- String[] split1 = string.split(" as ");
|
|
|
- showColumns.put(split1[0], split1[1]);
|
|
|
- } else {
|
|
|
- showColumns.put(string, string);
|
|
|
- }
|
|
|
- }
|
|
|
- Object joinConnectionDatasourceId = tableJoin.get("datasourceid");
|
|
|
-
|
|
|
- String joinTableConnectionStr = connectionStr;
|
|
|
- if (Objects.nonNull(joinConnectionDatasourceId)) {
|
|
|
- joinTableConnectionStr = DATABASE.queryConnectionStr(joinConnectionDatasourceId.toString());
|
|
|
- }
|
|
|
-
|
|
|
+ values.add(objects.toArray());
|
|
|
+ }
|
|
|
+ int[] ints = DATABASE.updateBatch(connectionStr, expression, values);
|
|
|
+ return UniReturnUtil.success(ints); // 返回操作结果
|
|
|
+ }
|
|
|
|
|
|
- List<Map<String, Object>> targetDatas = DATABASE.query(joinTableConnectionStr, "select * from %s".formatted(targettable));
|
|
|
|
|
|
- result.forEach(data -> targetDatas
|
|
|
- .stream()
|
|
|
- .filter(targetData -> Objects
|
|
|
- .equals(data
|
|
|
- .get(sourcejoincolumn), targetData.get(targetjoincolumn)))
|
|
|
- .findAny()
|
|
|
- .ifPresent(targetData -> {
|
|
|
- for (Map.Entry<String, String> stringStringEntry : showColumns.entrySet()) {
|
|
|
- data.put(stringStringEntry.getValue(), targetData.get(stringStringEntry.getKey()));
|
|
|
- }
|
|
|
- })
|
|
|
- );
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 根据事件执行操作(具体事件为六)。
|
|
|
+ * 该方法将输入的数据内容根据指定的过滤条件分割为需要插入和更新的数据集合,并分别执行插入和更新操作。
|
|
|
+ *
|
|
|
+ * @param args 通用参数,用于传递给execByTableName方法。
|
|
|
+ * @param dataContent 待处理的数据内容列表,每个元素包含过滤条件等信息。
|
|
|
+ * @param expression 用于构建SQL查询的表达式,将被格式化以插入表名和条件。
|
|
|
+ * @param connectionStr 数据库连接字符串。
|
|
|
+ * @return 一个包含插入和更新操作结果的Map,其中插入结果和更新结果分别以"insert"和"update"为键。
|
|
|
+ * @throws Exception 如果执行数据库操作时出现错误,则抛出异常。
|
|
|
+ */
|
|
|
+ private Map<String, Object> execByEventSix(Map<String, Object> args, List<Map<String, Object>> dataContent, String expression, String connectionStr) throws Exception {
|
|
|
+ // 提取过滤器名称列表
|
|
|
+ List<String> filterNames;
|
|
|
+ Map<String, Object> map = dataContent.get(0);
|
|
|
+ Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
|
+ filterNames = filter.keySet().stream().toList();
|
|
|
+
|
|
|
+ // 初始化插入和更新值的列表
|
|
|
+ List<Map<String, Object>> insertValues = new ArrayList<>();
|
|
|
+ List<Map<String, Object>> updateValues = new ArrayList<>();
|
|
|
+
|
|
|
+ // 构建SQL查询语句
|
|
|
+ String sql = "select count(1) as existscount from %s where %s".formatted(expression, filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")));
|
|
|
+ for (Map<String, Object> arg : dataContent) {
|
|
|
+ // 准备条件参数并检查数据是否存在
|
|
|
+ Map<String, Object> where = ((Map<String, Object>) arg.get("filter"));
|
|
|
+ Object[] objects = filterNames.stream().map(where::get).toArray();
|
|
|
+ if (exists(connectionStr, sql, objects)) {
|
|
|
+ updateValues.add(arg);
|
|
|
+ } else {
|
|
|
+ insertValues.add(arg);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return UniReturnUtil.success(result);
|
|
|
-// 更新或新增
|
|
|
- } else if (Objects.equals("6", event)) {
|
|
|
- Map<String, Object> map = dataContent.get(0);
|
|
|
- Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
|
- filterNames = filter.keySet().stream().toList();
|
|
|
+ // 执行插入操作并获取结果
|
|
|
+ Map<String, Object> insertResult = null;
|
|
|
+ if (!insertValues.isEmpty()) {
|
|
|
+ HashMap<String, Object> insertArgs = new HashMap<>(args);
|
|
|
+ insertArgs.put("event", "1");
|
|
|
+ insertArgs.put("datacontent", insertValues);
|
|
|
+ insertResult = execByTableName(connectionStr, expression, insertArgs);
|
|
|
+ }
|
|
|
|
|
|
- List<Map<String, Object>> insertValues = new ArrayList<>();
|
|
|
- List<Map<String, Object>> updateValues = new ArrayList<>();
|
|
|
+ // 执行更新操作并获取结果
|
|
|
+ Map<String, Object> updateResult = null;
|
|
|
+ if (!updateValues.isEmpty()) {
|
|
|
+ HashMap<String, Object> updateArgs = new HashMap<>(args);
|
|
|
+ updateArgs.put("event", "1");
|
|
|
+ updateArgs.put("datacontent", updateValues);
|
|
|
+ updateResult = execByTableName(connectionStr, expression, updateArgs);
|
|
|
+ }
|
|
|
|
|
|
- String sql = "select count(1) as existscount from %s where %s".formatted(expression, filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")));
|
|
|
- for (Map<String, Object> arg : dataContent) {
|
|
|
+ // 综合插入和更新结果,返回
|
|
|
+ Map<String, Object> finalInsertResult = insertResult;
|
|
|
+ Map<String, Object> finalUpdateResult = updateResult;
|
|
|
+ HashMap<Object, Object> data = new HashMap<>();
|
|
|
+ data.put("insert", finalInsertResult);
|
|
|
+ data.put("update", finalUpdateResult);
|
|
|
|
|
|
- Map<String, Object> where = ((Map<String, Object>) arg.get("filter"));
|
|
|
- Object[] objects = filterNames.stream().map(where::get).toArray();
|
|
|
- if (exists(connectionStr, sql, objects)) {
|
|
|
- updateValues.add(arg);
|
|
|
- } else {
|
|
|
- insertValues.add(arg);
|
|
|
- }
|
|
|
- }
|
|
|
- Map<String, Object> insertResult = null;
|
|
|
+ return UniReturnUtil.success(data);
|
|
|
+ }
|
|
|
|
|
|
- if (!insertValues.isEmpty()) {
|
|
|
- HashMap<String, Object> insertArgs = new HashMap<>(args);
|
|
|
- insertArgs.put("event", "1");
|
|
|
- insertArgs.put("datacontent", insertValues);
|
|
|
- insertResult = execByTableName(connectionStr, expression, insertArgs);
|
|
|
- }
|
|
|
- Map<String, Object> updateResult = null;
|
|
|
- if (!updateValues.isEmpty()) {
|
|
|
+ /**
|
|
|
+ * 根据事件查询数据,支持过滤条件和分页。
|
|
|
+ *
|
|
|
+ * @param args 查询参数,可以包含过滤条件等信息
|
|
|
+ * @param filterColumns 需要过滤的列
|
|
|
+ * @param dataContent 数据内容列表
|
|
|
+ * @param expression 基本的SQL查询表达式
|
|
|
+ * @param allColumns 所有查询列
|
|
|
+ * @param filterLineWhereStr 过滤行的WHERE子串
|
|
|
+ * @param pageable 分页信息
|
|
|
+ * @param connectionStr 数据库连接字符串
|
|
|
+ * @param tableName 查询的表名
|
|
|
+ * @return 查询结果,封装在Map中
|
|
|
+ * @throws Exception 查询过程中可能出现的异常
|
|
|
+ */
|
|
|
+ private Map<String, Object> queryByEventZero(Map<String, Object> args, List<String> filterColumns, List<Map<String, Object>> dataContent, String expression, List<String> allColumns, String filterLineWhereStr, Pageable pageable, String connectionStr, String tableName) throws Exception {
|
|
|
+ List<String> filterNames;
|
|
|
+ if (filterColumns.isEmpty()) {
|
|
|
+ // 列权限为空直接返回空数据
|
|
|
+ return UniReturnUtil.success(new ArrayList<>());
|
|
|
+ }
|
|
|
|
|
|
- HashMap<String, Object> updateArgs = new HashMap<>(args);
|
|
|
- updateArgs.put("event", "1");
|
|
|
- updateArgs.put("datacontent", updateValues);
|
|
|
- updateResult = execByTableName(connectionStr, expression, updateArgs);
|
|
|
+ // 处理查询参数
|
|
|
+ Map<String, Object> argMap = Objects.isNull(args) || args.isEmpty() ? new HashMap<>() : dataContent.get(0);
|
|
|
+ // 解析过滤条件
|
|
|
+ Object filter = argMap.getOrDefault("filter", argMap);
|
|
|
+ Map<?, ?> map = null;
|
|
|
+ if (filter instanceof ArrayNode) {
|
|
|
+ map = ((Map<?, ?>) ((ArrayNode) filter).get(0));
|
|
|
+ } else if (filter instanceof List<?>) {
|
|
|
+ map = ((Map<?, ?>) ((List<?>) filter).get(0));
|
|
|
+ } else if (filter instanceof Map<?, ?>) {
|
|
|
+ map = (Map<?, ?>) filter;
|
|
|
+ }
|
|
|
+ // 构造WHERE子句和参数值列表
|
|
|
+ String whereStr = "";
|
|
|
+ List<Object[]> values = new ArrayList<>();
|
|
|
+ if (map.containsKey("column") && map.containsKey("comparator")) {
|
|
|
+ whereStr = getFilterLinesWhereSql((List<Map<String, Object>>) filter);
|
|
|
+ expression = String.format("select %s from %s where %s and %s",
|
|
|
+ String.join(",", allColumns),
|
|
|
+ expression,
|
|
|
+ Objects.isNull(filterLineWhereStr) || filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr,
|
|
|
+ whereStr.trim().isEmpty() ? " 1=? " : whereStr);
|
|
|
+ if (whereStr.trim().isEmpty()) {
|
|
|
+ values.add(new Object[]{1});
|
|
|
+ } else {
|
|
|
+ values.add(new Object[0]); // 优化点:这里可以根据whereStr的实际内容填充参数
|
|
|
}
|
|
|
+ } else {
|
|
|
+ filterNames = (List<String>) map.keySet().stream().toList();
|
|
|
+ expression = String.format("select %s from %s where %s %s",
|
|
|
+ String.join(",", allColumns), expression,
|
|
|
+ filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr,
|
|
|
+ filterNames.isEmpty() ?
|
|
|
+ "" : (" and " +
|
|
|
+ filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and "))));
|
|
|
|
|
|
- Map<String, Object> finalInsertResult = insertResult;
|
|
|
- Map<String, Object> finalUpdateResult = updateResult;
|
|
|
- HashMap<Object, Object> data = new HashMap<>();
|
|
|
- data.put("insert", finalInsertResult);
|
|
|
- data.put("update", finalUpdateResult);
|
|
|
+ for (Map<String, Object> arg : dataContent) {
|
|
|
+ Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
+ values.add(filterNames.stream().map(o1::get).toArray());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 处理分页
|
|
|
+ if (pageable != null) {
|
|
|
+ expression = String.format("%s limit %d offset %d ",
|
|
|
+ expression, pageable.pageSize, pageable.page * pageable.pageSize);
|
|
|
+ }
|
|
|
|
|
|
- return UniReturnUtil.success(data);
|
|
|
- } else {
|
|
|
-// 新增
|
|
|
- if(filterColumns.size()==1 && !"*".equals(filterColumns.get(0))) {
|
|
|
- for (String filterColumn : filterColumns) {
|
|
|
- if (!allColumns.contains(filterColumn)) {
|
|
|
- allColumns.remove(filterColumn);
|
|
|
+ // 优化点:这里可以考虑是否需要对filterColumns进行处理
|
|
|
+
|
|
|
+ expression = String.format("select %s from (%s) as T", String.join(",", filterColumns), expression);
|
|
|
+
|
|
|
+ // 执行查询
|
|
|
+ List<Map<String, Object>> result = DATABASE.queryBatch(connectionStr, expression, values);
|
|
|
+ // 处理表关联查询
|
|
|
+ List<Map<String, Object>> tableJoins = DATABASE.query(Config.getCenterConnectionStr(), """
|
|
|
+ select id, sourcetable, targettable, sourcejoincolumn, targetjoincolumn, targetshowcolumns, datasourceid
|
|
|
+ from tablejoin where sourcetable=?""", tableName);
|
|
|
+ if (!tableJoins.isEmpty()) {
|
|
|
+ for (Map<String, Object> tableJoin : tableJoins) {
|
|
|
+ // 进行表关联查询,合并结果
|
|
|
+ String targetTable = tableJoin.get("targettable").toString();
|
|
|
+ String sourceJoinColumn = tableJoin.get("sourcejoincolumn").toString();
|
|
|
+ String targetJoinColumn = tableJoin.get("targetjoincolumn").toString();
|
|
|
+ String targetShowColumns = tableJoin.get("targetshowcolumns").toString();
|
|
|
+
|
|
|
+ String[] split = targetShowColumns.split(",");
|
|
|
+ Map<String, String> showColumns = new HashMap<>();
|
|
|
+ for (String string : split) {
|
|
|
+ if (string.contains(" as ")) {
|
|
|
+ String[] split1 = string.split(" as ");
|
|
|
+ showColumns.put(split1[0], split1[1]);
|
|
|
+ } else {
|
|
|
+ showColumns.put(string, string);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- if (Objects.equals("1", event)) {
|
|
|
- Map<String, Object> value = dataContent.get(0);
|
|
|
- Object valueObj = value.getOrDefault("value", value);
|
|
|
- Map map = null;
|
|
|
- if (valueObj instanceof ArrayNode v) {
|
|
|
- map = ((Map<?, ?>) v.get(0));
|
|
|
- } else if (valueObj instanceof List<?> v) {
|
|
|
- map = ((Map<?, ?>) v.get(0));
|
|
|
- } else if (valueObj instanceof Map<?, ?> v) {
|
|
|
- map = v;
|
|
|
- }
|
|
|
-// Map<String, Object> value = ((Map<String, Object>) map.getOrDefault("value", map));
|
|
|
- valueNames = map.keySet().stream().filter(allColumns::contains).toList();
|
|
|
- expression = "insert into %s ( %s) values(%s)".formatted(expression, String.join(",", valueNames), valueNames.stream().map(it -> "?").collect(Collectors.joining(",")));
|
|
|
-// 更新
|
|
|
- } else if (Objects.equals("2", event)) {
|
|
|
- Map<String, Object> map = dataContent.get(0);
|
|
|
- Map<String, Object> value = ((Map<String, Object>) map.get("value"));
|
|
|
- valueNames = value.keySet().stream().filter(allColumns::contains).toList();
|
|
|
- Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
|
- filterNames = filter.keySet().stream().filter(allColumns::contains).toList();
|
|
|
-
|
|
|
- expression = "update %s set %s where %s and %s".formatted(expression, valueNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(",")), filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")), filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr);
|
|
|
-// 删除
|
|
|
- } else if (Objects.equals("3", event)) {
|
|
|
-
|
|
|
-
|
|
|
- Map<String, Object> argMap = dataContent.get(0);
|
|
|
- Object filter = argMap.getOrDefault("filter", argMap);
|
|
|
- Map map = null;
|
|
|
- if (filter instanceof ArrayNode f) {
|
|
|
- map = ((Map<?, ?>) f.get(0));
|
|
|
- } else if (filter instanceof List<?> f) {
|
|
|
- map = ((Map<?, ?>) f.get(0));
|
|
|
-
|
|
|
- } else if (filter instanceof Map<?, ?> f) {
|
|
|
- map = f;
|
|
|
- }
|
|
|
- filterNames = map.keySet().stream().filter(allColumns::contains).toList();
|
|
|
- expression = "delete from %s where %s and %s".formatted(expression, filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")), filterLineWhereStr.trim().isEmpty() ? " 1=1 " : filterLineWhereStr);
|
|
|
- } else if (Objects.equals("7", event)) {
|
|
|
-// 先删除
|
|
|
- args.put("event", "3");
|
|
|
- execByTableName(datasourceId, table, args);
|
|
|
-// 再新增
|
|
|
- args.put("event", "1");
|
|
|
- return execByTableName(datasourceId, table, args);
|
|
|
- }
|
|
|
- List<Object[]> values = new ArrayList<>();
|
|
|
-// 按照名称 和过滤条件取值
|
|
|
- final List<String> finalFilterColumns = filterColumns;
|
|
|
- for (Map<String, Object> arg : dataContent) {
|
|
|
- List<Object> objects = new ArrayList<>();
|
|
|
- if (Objects.nonNull(valueNames)) {
|
|
|
- Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("value", arg));
|
|
|
- objects.addAll(valueNames.stream().map(it -> {
|
|
|
-
|
|
|
-// 新增 更新 对列过滤
|
|
|
- if (finalFilterColumns.contains(it) || finalFilterColumns.isEmpty() || (finalFilterColumns.get(0).equals("*") && finalFilterColumns.size() == 1)) {
|
|
|
- return o1.get(it);
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }).toList());
|
|
|
+
|
|
|
+ Object joinConnectionDatasourceId = tableJoin.get("datasourceid");
|
|
|
+ String joinTableConnectionStr = connectionStr;
|
|
|
+ if (Objects.nonNull(joinConnectionDatasourceId)) {
|
|
|
+ joinTableConnectionStr = DATABASE.queryConnectionStr(joinConnectionDatasourceId.toString());
|
|
|
}
|
|
|
- if (Objects.nonNull(filterNames)) {
|
|
|
- Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
- objects.addAll(filterNames.stream().map(o1::get).toList());
|
|
|
+
|
|
|
+ List<Map<String, Object>> targetDatas = DATABASE.query(joinTableConnectionStr, String.format("select * from %s", targetTable));
|
|
|
+
|
|
|
+ for (Map<String, Object> data : result) {
|
|
|
+ targetDatas.stream()
|
|
|
+ .filter(targetData -> Objects.equals(data.get(sourceJoinColumn), targetData.get(targetJoinColumn)))
|
|
|
+ .findAny()
|
|
|
+ .ifPresent(targetData -> {
|
|
|
+ for (Map.Entry<String, String> stringStringEntry : showColumns.entrySet()) {
|
|
|
+ data.put(stringStringEntry.getValue(), targetData.get(stringStringEntry.getKey()));
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
- values.add(objects.toArray());
|
|
|
}
|
|
|
- int[] ints = DATABASE.updateBatch(connectionStr, expression, values);
|
|
|
- return UniReturnUtil.success(ints);
|
|
|
}
|
|
|
+ return UniReturnUtil.success(result);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据动态SQL执行方法。
|
|
|
+ * 通过传入的数据源ID、SQL表达式以及参数映射,执行相应的SQL查询,并返回查询结果的映射。
|
|
|
+ *
|
|
|
+ * @param datasourceId 数据源ID,用于指定执行SQL查询的数据源。
|
|
|
+ * @param expression SQL表达式,可以是动态生成的SQL字符串。
|
|
|
+ * @param args 参数映射,包含SQL查询中需要的参数。
|
|
|
+ * @return 返回一个映射,包含查询结果的相关信息。
|
|
|
+ * @throws Exception 如果执行过程中出现错误,则抛出异常。
|
|
|
+ */
|
|
|
public Map<String, Object> execByDynamicSql(String datasourceId, String expression, Map<String, Object> args) throws Exception {
|
|
|
return execBySql(datasourceId, expression, args);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据动态表名执行方法。
|
|
|
+ * 通过传入的数据源ID、表名以及参数映射,执行相应的SQL查询,并返回查询结果的映射。
|
|
|
+ *
|
|
|
+ * @param datasourceId 数据源ID,用于指定执行SQL查询的数据源。
|
|
|
+ * @param table 表名,可以是动态确定的表名。
|
|
|
+ * @param args 参数映射,包含SQL查询中需要的参数。
|
|
|
+ * @return 返回一个映射,包含查询结果的相关信息。
|
|
|
+ * @throws Exception 如果执行过程中出现错误,则抛出异常。
|
|
|
+ */
|
|
|
public Map<String, Object> execByDynamicTableName(String datasourceId, String table, Map<String, Object> args) throws Exception {
|
|
|
return execByTableName(datasourceId, table, args);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 分页信息的记录类。
|
|
|
+ * 用于封装分页查询时的页码和每页大小。
|
|
|
+ */
|
|
|
public record Pageable(Long page, Long pageSize) {
|
|
|
}
|
|
|
}
|