|
@@ -2,19 +2,15 @@ package com.scbfkj.uni.process;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.scbfkj.uni.library.DataFormatUtil;
|
|
|
-import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
+import com.scbfkj.uni.library.script.DatabaseScriptUtil;
|
|
|
import com.scbfkj.uni.system.Config;
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
|
import com.zaxxer.hikari.pool.HikariPool;
|
|
|
import jakarta.annotation.Nonnull;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.sql.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
public class DataBase {
|
|
|
|
|
@@ -114,255 +110,7 @@ public class DataBase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static Map<String, Object> exec(String connectionStr, String expression, List<Map<String, Object>> args, Object executionNumber, List<String> filterColumns, List<Map<String, Object>> filterLines) throws Exception {
|
|
|
|
|
|
- if (Objects.isNull(executionNumber) || !StringUtils.hasText(executionNumber.toString())) {
|
|
|
- throw new RuntimeException("执行编号不能为空");
|
|
|
- }
|
|
|
- expression = expression.replaceAll("\\s*(\\r)?\\n\\s*", " ").trim();
|
|
|
- boolean isTableName = !expression.contains(" ");
|
|
|
-
|
|
|
- if (Objects.isNull(filterColumns)) filterColumns = new ArrayList<>();
|
|
|
- if (Objects.isNull(filterLines)) filterLines = new ArrayList<>();
|
|
|
- String filterLineWhereStr =null;
|
|
|
- for (Object f : filterLines) {
|
|
|
- Map<String, Object> it = ((Map<String, Object>) f);
|
|
|
- filterLineWhereStr = " %s %s %s %s %s %s %s %s ".formatted(
|
|
|
- filterLineWhereStr,
|
|
|
- it.getOrDefault("left", ""),
|
|
|
- it.get("column"),
|
|
|
- it.get("comparator"),
|
|
|
- Objects.equals(it.get("comparator"), " is null ") ? "" : "?",
|
|
|
- !Objects.equals(it.get("comparator"), " is null ")?" ":it.get("value"),
|
|
|
- it.getOrDefault("right", ""),
|
|
|
- it.getOrDefault("connector", "")
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
-// 只有表名
|
|
|
- if (isTableName) {
|
|
|
- List<String> valueNames = null;
|
|
|
- List<String> filterNames = null;
|
|
|
-// 查询
|
|
|
- if (Objects.equals("0", executionNumber)) {
|
|
|
- Map<String, Object> map = args.get(0);
|
|
|
- Map<String, Object> filter = ((Map<String, Object>) map.getOrDefault("filter", map));
|
|
|
- filterNames = filter.keySet().stream().toList();
|
|
|
- String whereStr =null;
|
|
|
- for (Object f : filterLines) {
|
|
|
- Map<String, Object> it = ((Map<String, Object>) f);
|
|
|
- whereStr = " %s %s %s %s %s %s %s %s ".formatted(
|
|
|
- whereStr,
|
|
|
- it.getOrDefault("left", ""),
|
|
|
- it.get("column"),
|
|
|
- it.get("comparator"),
|
|
|
- Objects.equals(it.get("comparator"), " is null ") ? "" : "?",
|
|
|
- !Objects.equals(it.get("comparator"), " is null ")?" ":it.get("value"),
|
|
|
- it.getOrDefault("right", ""),
|
|
|
- it.getOrDefault("connector", "")
|
|
|
- );
|
|
|
- }
|
|
|
- expression = "select * from %s where %s and %s".formatted(
|
|
|
- expression,
|
|
|
- filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
|
- Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
-
|
|
|
- if( !filterColumns.isEmpty()){
|
|
|
- expression= "select %s from (%s) as T".formatted(String.join(",", filterColumns),expression);
|
|
|
- }
|
|
|
-
|
|
|
- List<Object[]> values = new ArrayList<>();
|
|
|
- for (Map<String, Object> arg : args) {
|
|
|
- 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(o1::get).toList());
|
|
|
- }
|
|
|
- if (Objects.nonNull(filterNames)) {
|
|
|
- Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
- objects.addAll(filterNames.stream().map(o1::get).toList());
|
|
|
- }
|
|
|
- values.add(objects.toArray());
|
|
|
- }
|
|
|
- List<Map<String, Object>> result = query(connectionStr, expression, values);
|
|
|
- return UniReturnUtil.success(result);
|
|
|
-// 更新或新增
|
|
|
- } else if (Objects.equals("6", executionNumber)) {
|
|
|
- Map<String, Object> map = args.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<>();
|
|
|
-
|
|
|
- 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 : args) {
|
|
|
-
|
|
|
- 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 = exec(connectionStr, expression, insertValues, "1", filterColumns, filterLines);
|
|
|
- Map<String, Object> updateResult = exec(connectionStr, expression, updateValues, "2", filterColumns, filterLines);
|
|
|
-
|
|
|
- return UniReturnUtil.success(new HashMap<>() {{
|
|
|
- put("insert", insertResult);
|
|
|
- put("update", updateResult);
|
|
|
- }});
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
-// 新增
|
|
|
- if (Objects.equals("1", executionNumber)) {
|
|
|
- Map<String, Object> map = args.get(0);
|
|
|
- Map<String, Object> value = ((Map<String, Object>) map.getOrDefault("value", map));
|
|
|
- valueNames = value.keySet().stream().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", executionNumber)) {
|
|
|
- Map<String, Object> map = args.get(0);
|
|
|
- Map<String, Object> value = ((Map<String, Object>) map.get("value"));
|
|
|
- valueNames = value.keySet().stream().toList();
|
|
|
- Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
|
- filterNames = filter.keySet().stream().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 ")),
|
|
|
- Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
-// 删除
|
|
|
- } else if (Objects.equals("3", executionNumber)) {
|
|
|
- Map<String, Object> map = args.get(0);
|
|
|
- Map<String, Object> filter = ((Map<String, Object>) map.getOrDefault("filter", map));
|
|
|
- filterNames = filter.keySet().stream().toList();
|
|
|
- expression = "delete from %s where %s and %s".formatted(expression,
|
|
|
- filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
|
- Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
- }
|
|
|
- List<Object[]> values = new ArrayList<>();
|
|
|
-// 按照名称 和过滤条件取值
|
|
|
- final List<String> finalFilterColumns = filterColumns;
|
|
|
- for (Map<String, Object> arg : args) {
|
|
|
- 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()) {
|
|
|
- return o1.get(it);
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }).toList());
|
|
|
- }
|
|
|
- if (Objects.nonNull(filterNames)) {
|
|
|
- Map<String, Object> o1 = ((Map<String, Object>) arg.getOrDefault("filter", arg));
|
|
|
- objects.addAll(filterNames.stream().map(o1::get).toList());
|
|
|
- }
|
|
|
- values.add(objects.toArray());
|
|
|
- }
|
|
|
- int[] ints = updateBatch(connectionStr, expression, values);
|
|
|
- return UniReturnUtil.success(ints);
|
|
|
- }
|
|
|
-
|
|
|
-// 目前只支持查询
|
|
|
- } else if (expression.contains("《whereStr》")) {
|
|
|
-
|
|
|
- String whereStr = " ";//初始化条件字符串
|
|
|
- List<Object> dbFilter = new ArrayList<>();//初始化条件执行参数
|
|
|
- Map<String, Object> map = args.get(0);
|
|
|
- Object filter = map.get("filter");
|
|
|
- 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) {
|
|
|
- Map<String, Object> it = ((Map<String, Object>) f);
|
|
|
- whereStr = " %s %s %s %s %s %s %s".formatted(
|
|
|
- whereStr,
|
|
|
- it.getOrDefault("left", ""),
|
|
|
- it.get("column"),
|
|
|
- it.get("comparator"),
|
|
|
- Objects.equals(it.get("comparator"), " is null ") ? "" : "?",
|
|
|
- it.getOrDefault("right", ""),
|
|
|
- it.getOrDefault("connector", "")
|
|
|
- );
|
|
|
- if (!Objects.equals(it.get("comparator"), " is null ")) {
|
|
|
- dbFilter.add(it.get("value"));
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- List<Map.Entry<String, Object>> list = filterMap.entrySet().stream().toList();
|
|
|
- whereStr += list.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and "));
|
|
|
- dbFilter.addAll(list.stream().map(Map.Entry::getValue).toList());
|
|
|
- }
|
|
|
- }else if(filter instanceof Map<?,?> filterMap && !filterMap.isEmpty()){
|
|
|
- List<? extends Map.Entry<?, ?>> list = filterMap.entrySet().stream().toList();
|
|
|
- whereStr += list.stream().map(it->"%s = ?".formatted(it.getKey())).collect(Collectors.joining(" and "));
|
|
|
- dbFilter.addAll(list.stream().map(Map.Entry::getValue).toList());
|
|
|
- }
|
|
|
- whereStr = " %s and %s".formatted(whereStr, Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
- String sql = expression.replaceAll("《whereStr》", " " + whereStr);
|
|
|
-
|
|
|
- if( !filterColumns.isEmpty()){
|
|
|
- sql= "select %s from (%s) as T".formatted(String.join(",", filterColumns),sql);
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> queryResult = query(connectionStr, sql, Collections.singletonList(dbFilter.toArray()));
|
|
|
- return UniReturnUtil.success(queryResult);
|
|
|
-
|
|
|
- } else {
|
|
|
- if (Objects.equals(executionNumber, "0")) {
|
|
|
- List<Map<String, Object>> queryResult = query(connectionStr, expression, args, filterColumns, filterLines);
|
|
|
- return UniReturnUtil.success(queryResult);
|
|
|
- } else if (Objects.equals(executionNumber, "1")) {
|
|
|
- if (sqlStrVarList.containsKey(expression)) {
|
|
|
- getSQLVarList(expression);
|
|
|
- }
|
|
|
- List<String> names = sqlStrVarList.get(expression);
|
|
|
- String sql = sqlStrNewSQL.get(expression);
|
|
|
- int[] updateResult = updateBatch(connectionStr, sql, args.stream().map(it -> names.stream().map(it::get).toArray()).toList());
|
|
|
- return UniReturnUtil.success(updateResult);
|
|
|
- } else {
|
|
|
- if (sqlStrVarList.containsKey(expression)) {
|
|
|
- getSQLVarList(expression);
|
|
|
- }
|
|
|
- List<String> names = sqlStrVarList.get(expression);
|
|
|
- String sql = " %s and %s".formatted(sqlStrNewSQL.get(expression), Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
- int[] updateResult = updateBatch(connectionStr, sql, args.stream().map(it -> names.stream().map(it::get).toArray()).toList());
|
|
|
- return UniReturnUtil.success(updateResult);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static Pattern regExpression = Pattern.compile("(?<=《)([^》]+)?(?=》)");//提取书名号变量的正则表达式
|
|
|
- private static Map<String, List<String>> sqlStrVarList = new HashMap<>();//SQL语句的书名号变量列表
|
|
|
- private static Map<String, String> sqlStrNewSQL = new HashMap<>();//SQL语句更换书名号变量后的可执行SQL
|
|
|
-
|
|
|
- private static 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()) {//循环提取到的所有书名号变量
|
|
|
- String varName = matcher.group();
|
|
|
- sqlvarList.add(varName.trim());
|
|
|
- newSqlStr = newSqlStr.replaceFirst("《".concat(varName).concat("》"), " ? "); //修订SQL
|
|
|
- }
|
|
|
- sqlStrVarList.put(sqlStr, sqlvarList);//添加到缓存
|
|
|
- sqlStrNewSQL.put(sqlStr, newSqlStr);//添加到缓存
|
|
|
- }
|
|
|
- return sqlvarList;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean exists(String connectionStr, String sql, Object[] args) throws Exception {
|
|
|
- List<Map<String, Object>> mapList = query(connectionStr, sql, Collections.singletonList(args));
|
|
|
- if (mapList.isEmpty()) return false;
|
|
|
- return !Objects.equals(mapList.get(0).get("existscount").toString(), "0");
|
|
|
- }
|
|
|
|
|
|
public static List<Map<String, Object>> query(String connectionStr, String sql, List<Map<String, Object>> argsList, List<String> filterColumns, List<Map<String, Object>> filterLines) throws Exception {
|
|
|
|
|
@@ -389,18 +137,9 @@ public class DataBase {
|
|
|
}
|
|
|
|
|
|
List<List<Object>> result = new ArrayList<>();
|
|
|
-// List<String> names = new ArrayList<>();
|
|
|
-// Matcher matcher = regExpression.matcher(sql);
|
|
|
-// if (matcher.find()) {
|
|
|
-// int index = 0;
|
|
|
-// while (matcher.find()) {
|
|
|
-// String name = matcher.group(index);
|
|
|
-// names.add(name);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- List<String> names = getSQLVarList(sql);
|
|
|
- String newSql = sqlStrNewSQL.get(sql);
|
|
|
+
|
|
|
+ List<String> names = DatabaseScriptUtil.getSQLVarList(sql);
|
|
|
+ String newSql = DatabaseScriptUtil.sqlStrNewSQL.get(sql);
|
|
|
|
|
|
if (names.size() == 1 && names.contains("whereStr")) {
|
|
|
List<Map<String, Object>> list = new ArrayList<>();
|