Skip to content

Commit 7677158

Browse files
committed
Java:MultiDataSource 中 SQLAuto 新增支持 Document.sqlauto 中的 ${var} 来匹配传参 SQL 中的 ?
1 parent 8b5624d commit 7677158

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
<!-- </dependency>-->
8080
<!-- fastjson2, jackson, gson 等 JSON 库插件选其中一个即可 >>>>>>>>>>>>>>> -->
8181

82+
<dependency>
83+
<groupId>org.apache.commons</groupId>
84+
<artifactId>commons-text</artifactId>
85+
<version>1.11.0</version>
86+
</dependency>
87+
8288
<dependency>
8389
<groupId>com.databend</groupId>
8490
<artifactId>databend-jdbc</artifactId>

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import jakarta.servlet.AsyncContext;
2929
import jakarta.servlet.ServletResponse;
30+
import org.apache.commons.text.StringSubstitutor;
3031
import org.jetbrains.annotations.NotNull;
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.http.*;
@@ -2234,11 +2235,34 @@ public static void init(boolean shutdownWhenServerError, APIJSONCreator<?, ? ext
22342235
// onServerError("服务器内部错误,Document 表中的 id=" + JSON.getString(item, "id") + ", name=" + JSON.getString(item, "name") + ", url=" + url + " 对应 url 值错误!只允许合法的 URL 格式!", shutdownWhenServerError);
22352236
//}
22362237
} else {
2237-
sqlauto = StringUtil.trim(sqlauto);
2238-
List<Map<String, Object>> mapList = newMap.get(sqlauto);
2238+
String sql = StringUtil.trim(sqlauto);
2239+
2240+
String header = JSON.getString(item, "header");
2241+
String[] args = StringUtil.split(header, "\n");
2242+
if (args != null) {
2243+
Map<String, String> valuesMap = new LinkedHashMap<>();
2244+
2245+
for (int j = 0; j < args.length; j++) {
2246+
String arg = StringUtil.trim(args[j]);
2247+
int ind = arg.startsWith("//") || arg.startsWith("#") || arg.startsWith("--") ? -1 : arg.indexOf(": ");
2248+
String key = ind <= 0 ? null : arg.substring(0, ind);
2249+
if (StringUtil.isEmpty(key)) {
2250+
continue;
2251+
}
2252+
2253+
valuesMap.put(key, "?");
2254+
}
2255+
2256+
if (! valuesMap.isEmpty()) {
2257+
StringSubstitutor sub = new StringSubstitutor(valuesMap);
2258+
sql = sub.replace(sqlauto);
2259+
}
2260+
}
2261+
2262+
List<Map<String, Object>> mapList = newMap.get(sql);
22392263
if (mapList == null) {
22402264
mapList = new ArrayList<>();
2241-
newMap.put(sqlauto, mapList);
2265+
newMap.put(sql, mapList);
22422266
}
22432267
mapList.add(item);
22442268
}
@@ -2329,9 +2353,9 @@ public String execute(@RequestBody String request, HttpSession session) {
23292353
}
23302354
}
23312355

2332-
String trimmedSQL = sql == null ? null : sql.trim();
2356+
String trimmedSQL = sql.trim();
23332357

2334-
String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
2358+
String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
23352359
boolean isInsert = sqlPrefix.startsWith("INSERT ");
23362360
boolean isWrite = isInsert || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
23372361
if (WRITE_STRICTLY && isWrite && ! isInsert) {
@@ -2349,7 +2373,7 @@ public String execute(@RequestBody String request, HttpSession session) {
23492373
List<Map<String, Object>> list = sqlauto.equalsIgnoreCase(trimmedSQL) ? entry.getValue() : null;
23502374
if (list != null && ! list.isEmpty()) {
23512375
find = list;
2352-
Log.d(TAG, "execute find DELETE/UPDATE rows = " + JSON.toJSONString(find, true));
2376+
Log.d(TAG, "execute " + sql + " : find match rows = " + JSON.toJSONString(find, true));
23532377
break;
23542378
}
23552379
}

0 commit comments

Comments
 (0)