|
@@ -83,7 +83,7 @@ public class DataBase {
|
|
try (Connection connection = dataSourcePool.getConnection();
|
|
try (Connection connection = dataSourcePool.getConnection();
|
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)
|
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)
|
|
) {
|
|
) {
|
|
- int[] result = null;
|
|
|
|
|
|
+ int[] result;
|
|
int index = 0;
|
|
int index = 0;
|
|
try {
|
|
try {
|
|
while (argsList.size() > index) {
|
|
while (argsList.size() > index) {
|
|
@@ -121,7 +121,7 @@ public class DataBase {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static Map<String, Object> exec(String connectionStr, String expression, List<Map<String, Object>> args, Object executionNumber, List<String> filterColumns, Map<String, Object> filterLines) throws Exception {
|
|
|
|
|
|
+ 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())) {
|
|
if (Objects.isNull(executionNumber) || !StringUtils.hasText(executionNumber.toString())) {
|
|
throw new RuntimeException("执行编号不能为空");
|
|
throw new RuntimeException("执行编号不能为空");
|
|
@@ -130,7 +130,21 @@ public class DataBase {
|
|
boolean isTableName = !expression.contains(" ");
|
|
boolean isTableName = !expression.contains(" ");
|
|
|
|
|
|
if (Objects.isNull(filterColumns)) filterColumns = new ArrayList<>();
|
|
if (Objects.isNull(filterColumns)) filterColumns = new ArrayList<>();
|
|
- if (Objects.isNull(filterLines)) filterLines = new HashMap<>();
|
|
|
|
|
|
+ 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) {
|
|
if (isTableName) {
|
|
@@ -141,12 +155,26 @@ public class DataBase {
|
|
Map<String, Object> map = args.get(0);
|
|
Map<String, Object> map = args.get(0);
|
|
Map<String, Object> filter = ((Map<String, Object>) map.getOrDefault("filter", map));
|
|
Map<String, Object> filter = ((Map<String, Object>) map.getOrDefault("filter", map));
|
|
filterNames = filter.keySet().stream().toList();
|
|
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 = "select * from %s where %s and %s".formatted(
|
|
expression,
|
|
expression,
|
|
filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
- Objects.isNull(filterLines) || filterLines.isEmpty() ? " 1=1 " : filterLines.entrySet().stream().map(entry -> "%s = '%s'".formatted(entry.getKey(), entry.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
|
|
|
|
- if(Objects.nonNull(filterColumns) && !filterColumns.isEmpty()){
|
|
|
|
|
|
+ if( !filterColumns.isEmpty()){
|
|
expression= "select %s from (%s) as T".formatted(String.join(",", filterColumns),expression);
|
|
expression= "select %s from (%s) as T".formatted(String.join(",", filterColumns),expression);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -208,9 +236,10 @@ public class DataBase {
|
|
valueNames = value.keySet().stream().toList();
|
|
valueNames = value.keySet().stream().toList();
|
|
Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
Map<String, Object> filter = ((Map<String, Object>) map.get("filter"));
|
|
filterNames = filter.keySet().stream().toList();
|
|
filterNames = filter.keySet().stream().toList();
|
|
|
|
+
|
|
expression = "update %s set %s where %s and %s".formatted(expression, valueNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(",")),
|
|
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 ")),
|
|
filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
- Objects.isNull(filterLines) || filterLines.isEmpty() ? " 1=1 " : filterLines.entrySet().stream().map(entry -> "%s = '%s'".formatted(entry.getKey(), entry.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
// 删除
|
|
// 删除
|
|
} else if (Objects.equals("3", executionNumber)) {
|
|
} else if (Objects.equals("3", executionNumber)) {
|
|
Map<String, Object> map = args.get(0);
|
|
Map<String, Object> map = args.get(0);
|
|
@@ -218,7 +247,7 @@ public class DataBase {
|
|
filterNames = filter.keySet().stream().toList();
|
|
filterNames = filter.keySet().stream().toList();
|
|
expression = "delete from %s where %s and %s".formatted(expression,
|
|
expression = "delete from %s where %s and %s".formatted(expression,
|
|
filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
filterNames.stream().map("%s = ?"::formatted).collect(Collectors.joining(" and ")),
|
|
- Objects.isNull(filterLines) || filterLines.isEmpty() ? " 1=1 " : filterLines.entrySet().stream().map(entry -> "%s = '%s'".formatted(entry.getKey(), entry.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
}
|
|
}
|
|
List<Object[]> values = new ArrayList<>();
|
|
List<Object[]> values = new ArrayList<>();
|
|
// 按照名称 和过滤条件取值
|
|
// 按照名称 和过滤条件取值
|
|
@@ -230,7 +259,7 @@ public class DataBase {
|
|
objects.addAll(valueNames.stream().map(it -> {
|
|
objects.addAll(valueNames.stream().map(it -> {
|
|
|
|
|
|
// 新增 更新 对列过滤
|
|
// 新增 更新 对列过滤
|
|
- if (finalFilterColumns.contains(it)) {
|
|
|
|
|
|
+ if (finalFilterColumns.contains(it) || finalFilterColumns.isEmpty()) {
|
|
return o1.get(it);
|
|
return o1.get(it);
|
|
} else {
|
|
} else {
|
|
return null;
|
|
return null;
|
|
@@ -266,7 +295,7 @@ public class DataBase {
|
|
it.get("comparator"),
|
|
it.get("comparator"),
|
|
Objects.equals(it.get("comparator"), " is null ") ? "" : "?",
|
|
Objects.equals(it.get("comparator"), " is null ") ? "" : "?",
|
|
it.getOrDefault("right", ""),
|
|
it.getOrDefault("right", ""),
|
|
- it.getOrDefault("connector", " and ")
|
|
|
|
|
|
+ it.getOrDefault("connector", "")
|
|
);
|
|
);
|
|
if (!Objects.equals(it.get("comparator"), " is null ")) {
|
|
if (!Objects.equals(it.get("comparator"), " is null ")) {
|
|
dbFilter.add(it.get("value"));
|
|
dbFilter.add(it.get("value"));
|
|
@@ -282,10 +311,10 @@ public class DataBase {
|
|
whereStr += list.stream().map(it->"%s = ?".formatted(it.getKey())).collect(Collectors.joining(" and "));
|
|
whereStr += list.stream().map(it->"%s = ?".formatted(it.getKey())).collect(Collectors.joining(" and "));
|
|
dbFilter.addAll(list.stream().map(Map.Entry::getValue).toList());
|
|
dbFilter.addAll(list.stream().map(Map.Entry::getValue).toList());
|
|
}
|
|
}
|
|
- whereStr = " %s and %s".formatted(whereStr, Objects.isNull(filterLines) || filterLines.isEmpty() ? " 1=1 " : filterLines.entrySet().stream().map(entry -> "%s = '%s'".formatted(entry.getKey(), entry.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ whereStr = " %s and %s".formatted(whereStr, Objects.isNull(filterLineWhereStr) ? " 1=1 " : filterLineWhereStr);
|
|
String sql = expression.replaceAll("《whereStr》", " " + whereStr);
|
|
String sql = expression.replaceAll("《whereStr》", " " + whereStr);
|
|
|
|
|
|
- if(Objects.nonNull(filterColumns) && !filterColumns.isEmpty()){
|
|
|
|
|
|
+ if( !filterColumns.isEmpty()){
|
|
sql= "select %s from (%s) as T".formatted(String.join(",", filterColumns),sql);
|
|
sql= "select %s from (%s) as T".formatted(String.join(",", filterColumns),sql);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -309,7 +338,7 @@ public class DataBase {
|
|
getSQLVarList(expression);
|
|
getSQLVarList(expression);
|
|
}
|
|
}
|
|
List<String> names = sqlStrVarList.get(expression);
|
|
List<String> names = sqlStrVarList.get(expression);
|
|
- String sql = " %s %s".formatted(sqlStrNewSQL.get(expression), Objects.isNull(filterLines) || filterLines.isEmpty() ? "" : " and " + filterLines.entrySet().stream().map(entry -> "%s = '%s'".formatted(entry.getKey(), entry.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ 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());
|
|
int[] updateResult = updateBatch(connectionStr, sql, args.stream().map(it -> names.stream().map(it::get).toArray()).toList());
|
|
return UniReturnUtil.success(updateResult);
|
|
return UniReturnUtil.success(updateResult);
|
|
}
|
|
}
|
|
@@ -317,8 +346,8 @@ public class DataBase {
|
|
}
|
|
}
|
|
|
|
|
|
private static Pattern regExpression = Pattern.compile("(?<=《)([^》]+)?(?=》)");//提取书名号变量的正则表达式
|
|
private static Pattern regExpression = Pattern.compile("(?<=《)([^》]+)?(?=》)");//提取书名号变量的正则表达式
|
|
- public static Map<String, List<String>> sqlStrVarList = new HashMap<>();//SQL语句的书名号变量列表
|
|
|
|
- public static Map<String, String> sqlStrNewSQL = new HashMap<>();//SQL语句更换书名号变量后的可执行SQL
|
|
|
|
|
|
+ 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) {
|
|
private static List<String> getSQLVarList(String sqlStr) {
|
|
String newSqlStr = sqlStr;//不破坏之前SQL语句
|
|
String newSqlStr = sqlStr;//不破坏之前SQL语句
|
|
@@ -342,30 +371,44 @@ public class DataBase {
|
|
return !Objects.equals(mapList.get(0).get("existscount").toString(), "0");
|
|
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, Map<String, Object> filterLines) throws Exception {
|
|
|
|
|
|
+ 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 {
|
|
|
|
|
|
sql = sql.replaceAll("(\\r)?\\n", " ");
|
|
sql = sql.replaceAll("(\\r)?\\n", " ");
|
|
-
|
|
|
|
- if (Objects.nonNull(filterLines) && !filterLines.isEmpty()) {
|
|
|
|
- sql = " (%s %s %s) ".formatted(sql, sql.contains(" where ") ? " " : " where ", filterLines.entrySet().stream().map(it -> "%s = %s".formatted(it.getKey(), it.getValue())).collect(Collectors.joining(" and ")));
|
|
|
|
|
|
+ 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 (Objects.nonNull(filterLineWhereStr) ) {
|
|
|
|
+ sql = " %s %s and %s ".formatted(sql, sql.contains(" where ") ? " 1 = 1" : " where ", Objects.isNull(filterLineWhereStr)?" 1=1 ":filterLineWhereStr);
|
|
}
|
|
}
|
|
if (Objects.nonNull(filterColumns) && !filterColumns.isEmpty()) {
|
|
if (Objects.nonNull(filterColumns) && !filterColumns.isEmpty()) {
|
|
- sql = "select %s from %s".formatted(filterColumns.stream().collect(Collectors.joining(",")), sql);
|
|
|
|
- } else {
|
|
|
|
- sql = "select * from %s".formatted(sql);
|
|
|
|
|
|
+ sql = "select %s from ( %s ) T".formatted(String.join(",", filterColumns), sql);
|
|
}
|
|
}
|
|
|
|
|
|
List<List<Object>> result = new ArrayList<>();
|
|
List<List<Object>> result = new ArrayList<>();
|
|
- List<String> names = new ArrayList<>();
|
|
|
|
- if (sql.matches("《.+?》")) {
|
|
|
|
- Pattern compile = Pattern.compile("(?=《).*(?=》)");
|
|
|
|
- Matcher matcher = compile.matcher(sql);
|
|
|
|
- int index = 0;
|
|
|
|
- while (matcher.find()) {
|
|
|
|
- String name = matcher.group(index);
|
|
|
|
- names.add(name);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// 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);
|
|
|
|
+
|
|
if (names.size() == 1 && names.contains("whereStr")) {
|
|
if (names.size() == 1 && names.contains("whereStr")) {
|
|
List<Map<String, Object>> list = new ArrayList<>();
|
|
List<Map<String, Object>> list = new ArrayList<>();
|
|
for (Map<String, Object> stringObjectMap : argsList) {
|
|
for (Map<String, Object> stringObjectMap : argsList) {
|
|
@@ -385,17 +428,15 @@ public class DataBase {
|
|
}
|
|
}
|
|
return list;
|
|
return list;
|
|
} else {
|
|
} else {
|
|
- for (String it : names) {
|
|
|
|
- sql = sql.replace("《%s》".formatted(it), " ? ");
|
|
|
|
- }
|
|
|
|
for (Map<String, Object> map : argsList) {
|
|
for (Map<String, Object> map : argsList) {
|
|
List<Object> args = new ArrayList<>();
|
|
List<Object> args = new ArrayList<>();
|
|
|
|
+ map= ((Map<String,Object>) map.getOrDefault("filter", map));
|
|
for (String name : names) {
|
|
for (String name : names) {
|
|
args.add(map.get(name));
|
|
args.add(map.get(name));
|
|
}
|
|
}
|
|
result.add(args);
|
|
result.add(args);
|
|
}
|
|
}
|
|
- return query(connectionStr, sql, result.stream().map(List::toArray).toList());
|
|
|
|
|
|
+ return query(connectionStr, newSql, result.stream().map(List::toArray).toList());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -453,7 +494,7 @@ public class DataBase {
|
|
|
|
|
|
private static final Map<String, List<String>> columns = new HashMap<>();
|
|
private static final Map<String, List<String>> columns = new HashMap<>();
|
|
|
|
|
|
- public static List<String> getColumns(String connection, String sql, ResultSet resultSet) throws SQLException {
|
|
|
|
|
|
+ private static List<String> getColumns(String connection, String sql, ResultSet resultSet) throws SQLException {
|
|
if (columns.containsKey(connection + sql)) {
|
|
if (columns.containsKey(connection + sql)) {
|
|
return columns.get(connection + sql);
|
|
return columns.get(connection + sql);
|
|
}
|
|
}
|