Skip to content

Commit fa46fa4

Browse files
committed
Java:MultiDataSource 中 SQLAuto: 导出用例为 Markdown 文档:完善响应结果和注释;完善 rollback 和 close connection,解决 key =/IN val 对 val 判断错误导致 OR 条件没拦截到
1 parent 2adeb68 commit fa46fa4

3 files changed

Lines changed: 171 additions & 89 deletions

File tree

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

Lines changed: 106 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,8 @@ public String execute(@RequestBody String request, HttpSession session) {
24522452
char c = sqlRest.charAt(i);
24532453
if (c == '?' || c == '+' || c == '-' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
24542454
endInd ++;
2455+
} else {
2456+
break;
24552457
}
24562458
}
24572459
} else {
@@ -2568,103 +2570,134 @@ public String execute(@RequestBody String request, HttpSession session) {
25682570
long rsDuration = 0;
25692571
long executeStartTime = System.currentTimeMillis();
25702572

2571-
if (DemoSQLConfig.DATABASE_INFLUXDB.equals(database) || DemoSQLConfig.DATABASE_CASSANDRA.equals(database)) {
2572-
JSONObject result = executor.execute(config, false);
2573-
executeDuration = System.currentTimeMillis() - executeStartTime;
2573+
try {
2574+
if (DemoSQLConfig.DATABASE_INFLUXDB.equals(database) || DemoSQLConfig.DATABASE_CASSANDRA.equals(database)) {
2575+
JSONObject result = executor.execute(config, false);
2576+
executeDuration = System.currentTimeMillis() - executeStartTime;
25742577

2575-
if (isWrite) {
2576-
updateCount = result == null ? 0 : result.getIntValue(JSONResponse.KEY_COUNT);
2577-
} else {
2578-
arr = result == null ? null : result.getJSONArray(DemoSQLExecutor.KEY_RAW_LIST);
2579-
if (arr == null) {
2580-
arr = new JSONArray();
2581-
if (result != null) {
2582-
arr.add(result);
2578+
if (isWrite) {
2579+
updateCount = result == null ? 0 : result.getIntValue(JSONResponse.KEY_COUNT);
2580+
} else {
2581+
arr = result == null ? null : result.getJSONArray(DemoSQLExecutor.KEY_RAW_LIST);
2582+
if (arr == null) {
2583+
arr = new JSONArray();
2584+
if (result != null) {
2585+
arr.add(result);
2586+
}
25832587
}
25842588
}
2585-
}
2586-
} else {
2587-
Connection connection = executor.getConnection(config);
2588-
Statement statement = executor.getStatement(config, trimmedSQL);
2589-
if (statement instanceof PreparedStatement) {
2590-
if (EXECUTE_STRICTLY) {
2591-
if (isWrite) {
2592-
try {
2593-
connection.setAutoCommit(false);
2594-
// connection.beginRequest();
2595-
int rows = ((PreparedStatement) statement).executeUpdate();
2596-
if (rows > maxCount) {
2597-
throw new UnsupportedOperationException("实际影响数量 " + rows + " 超过上限 " + maxCount + " ! 已回滚变更");
2589+
} else {
2590+
Connection connection = executor.getConnection(config);
2591+
Statement statement = executor.getStatement(config, trimmedSQL);
2592+
if (statement instanceof PreparedStatement) {
2593+
if (EXECUTE_STRICTLY) {
2594+
if (isWrite) {
2595+
try {
2596+
connection.setAutoCommit(false);
2597+
// connection.beginRequest();
2598+
int rows = ((PreparedStatement) statement).executeUpdate();
2599+
if (rows > maxCount) {
2600+
throw new UnsupportedOperationException("实际影响数量 " + rows + " 超过上限 " + maxCount + " ! 已回滚变更");
2601+
}
2602+
} catch (Throwable e) {
2603+
connection.rollback();
2604+
throw e;
25982605
}
2599-
} catch (Throwable e) {
2600-
connection.rollback();
2601-
throw e;
2606+
} else {
2607+
((PreparedStatement) statement).executeQuery();
26022608
}
26032609
} else {
2604-
((PreparedStatement) statement).executeQuery();
2610+
((PreparedStatement) statement).execute();
26052611
}
26062612
} else {
2607-
((PreparedStatement) statement).execute();
2608-
}
2609-
} else {
2610-
if (arg != null && ! arg.isEmpty()) {
2611-
throw new UnsupportedOperationException("非预编译模式不允许传参 args !");
2612-
}
2613+
if (arg != null && ! arg.isEmpty()) {
2614+
throw new UnsupportedOperationException("非预编译模式不允许传参 args !");
2615+
}
26132616

2614-
if (EXECUTE_STRICTLY) {
2615-
if (isWrite) {
2616-
try {
2617-
connection.setAutoCommit(false);
2618-
// connection.beginRequest();
2619-
int rows = statement.executeUpdate(sql);
2620-
if (rows > maxCount) {
2621-
throw new UnsupportedOperationException("实际影响数量 " + rows + " 超过上限 " + maxCount + " ! 已回滚变更");
2617+
if (EXECUTE_STRICTLY) {
2618+
if (isWrite) {
2619+
try {
2620+
connection.setAutoCommit(false);
2621+
// connection.beginRequest();
2622+
int rows = statement.executeUpdate(sql);
2623+
if (rows > maxCount) {
2624+
throw new UnsupportedOperationException("实际影响数量 " + rows + " 超过上限 " + maxCount + " ! 已回滚变更");
2625+
}
2626+
} catch (Throwable e) {
2627+
connection.rollback();
2628+
throw e;
26222629
}
2623-
} catch (Throwable e) {
2624-
connection.rollback();
2625-
throw e;
2630+
} else {
2631+
statement.executeQuery(sql);
26262632
}
26272633
} else {
2628-
statement.executeQuery(sql);
2634+
statement.execute(sql);
26292635
}
2630-
} else {
2631-
statement.execute(sql);
26322636
}
2633-
}
26342637

2635-
executeDuration = System.currentTimeMillis() - executeStartTime;
2638+
executeDuration = System.currentTimeMillis() - executeStartTime;
2639+
2640+
arr = new JSONArray();
2641+
ResultSet rs = statement.getResultSet();
2642+
ResultSetMetaData rsmd = rs == null ? null : rs.getMetaData();
2643+
int length = rsmd == null ? 0 : rsmd.getColumnCount();
2644+
26362645

2637-
arr = new JSONArray();
2638-
ResultSet rs = statement.getResultSet();
2639-
ResultSetMetaData rsmd = rs == null ? null : rs.getMetaData();
2640-
int length = rsmd == null ? 0 : rsmd.getColumnCount();
2646+
long cursorStartTime = System.currentTimeMillis();
2647+
while (rs != null && rs.next()) {
2648+
cursorDuration += System.currentTimeMillis() - cursorStartTime;
26412649

2650+
JSONObject obj = JSON.newJSONObject();
2651+
for (int i = 1; i <= length; i++) {
2652+
long sqlStartTime = System.currentTimeMillis();
2653+
String name = rsmd.getColumnName(i); // rsmd.getColumnLable(i);
2654+
Object value = rs.getObject(i);
2655+
rsDuration += System.currentTimeMillis() - sqlStartTime;
26422656

2643-
long cursorStartTime = System.currentTimeMillis();
2644-
while (rs != null && rs.next()) {
2645-
cursorDuration += System.currentTimeMillis() - cursorStartTime;
2657+
obj.put(name, value);
2658+
}
2659+
2660+
arr.add(obj);
2661+
}
26462662

2647-
JSONObject obj = JSON.newJSONObject();
2648-
for (int i = 1; i <= length; i++) {
2649-
long sqlStartTime = System.currentTimeMillis();
2650-
String name = rsmd.getColumnName(i); // rsmd.getColumnLable(i);
2651-
Object value = rs.getObject(i);
2652-
rsDuration += System.currentTimeMillis() - sqlStartTime;
2663+
// try {
2664+
updateCount = statement.getUpdateCount();
2665+
if (WRITE_STRICTLY && isWrite && updateCount > maxCount) {
2666+
try {
2667+
connection.rollback();
2668+
} catch (Throwable e) {
2669+
e.printStackTrace();
2670+
}
26532671

2654-
obj.put(name, value);
2672+
throw new IllegalArgumentException("实际影响数量 " + updateCount + " 超过上限 " + maxCount + " ! 已回滚变更");
26552673
}
26562674

2657-
arr.add(obj);
2658-
}
2675+
try {
2676+
connection.commit();
2677+
} finally {
2678+
try {
2679+
statement.close();
2680+
} catch (Throwable e) {
2681+
e.printStackTrace();
2682+
}
26592683

2660-
// try {
2661-
updateCount = statement.getUpdateCount();
2662-
if (WRITE_STRICTLY && isWrite && updateCount > maxCount) {
2663-
throw new IllegalArgumentException("实际影响数量 " + updateCount + " 超过上限 " + maxCount + " ! 已回滚变更");
2684+
try {
2685+
connection.close();
2686+
} catch (Throwable e) {
2687+
e.printStackTrace();
2688+
}
2689+
}
2690+
2691+
// } catch (Throwable e) {
2692+
// e.printStackTrace();
2693+
// }
2694+
}
2695+
} finally {
2696+
try {
2697+
executor.close();
2698+
} catch (Throwable e) {
2699+
e.printStackTrace();
26642700
}
2665-
// } catch (Throwable e) {
2666-
// e.printStackTrace();
2667-
// }
26682701
}
26692702

26702703
JSONObject result = newSuccessResult();

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/sql/README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77

88
<p align="center" >
9+
<a href="https://deepwiki.com/TommyLemon/SQLAuto">English</a>
910
<a href="https://github.com/TommyLemon/SQLAuto#%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B">快速上手</a>
1011
<a href="https://space.bilibili.com/437134249/search/video?keyword=apiauto">视频教程</a>
1112
<a href="http://apijson.cn/sql">在线体验</a>
13+
<a href="https://deepwiki.com/TommyLemon/SQLAuto">AI 问答</a>
1214
</p>
1315

1416
<img width="1440" alt="018AD342-6A7B-4D4E-ACC2-523196580221" src="https://user-images.githubusercontent.com/5738175/189524723-a178d4e8-c2ef-4adb-ac0a-9d4b627f42ad.png">
@@ -51,7 +53,7 @@ name: 'Test ' + new Date().toLocaleTimeString() // 通过代码自定义
5153
<br />
5254

5355
**后端需要部署 APIJSON-Demo 5.2.5+ 的 APIJSONBoot-MultiDataSource** <br />
54-
DemoSQLConfig<T, M, L> 改下 getDBAccount, getDBUri 等返回值,具体见 <br />
56+
DemoSQLConfig 改下 getDBAccount, getDBUri 等返回值,具体见 <br />
5557
https://github.com/APIJSON/APIJSON-Demo/tree/master/APIJSON-Java-Server
5658

5759
<br />
@@ -155,15 +157,19 @@ https://github.com/TommyLemon/APIAuto/issues
155157
<br />
156158
<br />
157159

158-
### 其它项目
160+
### 生态项目
159161

160-
[APIJSON](https://github.com/Tencent/APIJSON) 🚀 腾讯零代码、全功能、强安全 ORM 库 🏆 后端接口和文档零代码,前端(客户端) 定制返回 JSON 的数据和结构
162+
[APIJSON](https://github.com/Tencent/APIJSON) 🏆 腾讯实时 零代码、全功能、强安全 ORM 库 🚀 后端接口和文档零代码,前端(客户端) 定制返回 JSON 的数据和结构
161163

162164
[APIAuto](https://github.com/TommyLemon/APIAuto) 敏捷开发最强大易用的 HTTP 接口工具,机器学习零代码测试、生成代码与静态检查、生成文档与光标悬浮注释,集 文档、测试、Mock、调试、管理 于一体的一站式体验
163165

164-
[UnitAuto](https://github.com/TommyLemon/UnitAuto) 机器学习单元测试平台,零代码、全方位、自动化 测试 方法/函数 的正确性和可用性
166+
[CVAuto](https://github.com/TommyLemon/CVAuto) 👁 零代码零标注 CV AI 自动化测试平台 🚀 免除大量人工画框和打标签等,直接快速测试 CV 计算机视觉 AI 图像识别算法
165167

166-
[UIGO](https://github.com/TommyLemon/UIGO) 📱 零代码快准稳 UI 智能录制回放平台 🚀 自动兼容任意宽高比分辨率屏幕,自动精准等待网络请求,录制回放快、准、稳!
168+
[UnitAuto](https://github.com/TommyLemon/UnitAuto) ☀️ 最先进、最省事、ROI 最高的单元测试,零代码、全方位、自动化 测试 方法/函数,用户包含腾讯、快手、某 500 强巨头等
169+
170+
[UIGO](https://github.com/TommyLemon/UIGO) 📱 零代码快准稳 UI 智能录制回放平台 🚀 3 像素内自动精准定位,2 毫秒内自动精准等待,用户包含腾讯,微信团队邀请分享
171+
172+
[UIGOX](https://github.com/TommyLemon/UIGOX) UIGO 的 AndroidX 版 📱 零代码快准稳 UI 智能录制回放平台 🚀 3 像素内自动精准定位,2 毫秒内自动精准等待
167173

168174
[apijson-doc](https://github.com/vincentCheng/apijson-doc) APIJSON 官方文档,提供排版清晰、搜索方便的文档内容展示,包括设计规范、图文教程等
169175

@@ -192,9 +198,10 @@ https://github.com/TommyLemon/APIAuto/issues
192198
[Android-ZBLibrary](https://github.com/TommyLemon/Android-ZBLibrary) Android MVP 快速开发框架,Demo 全面,注释详细,使用简单,代码严谨
193199

194200
### 持续更新
195-
https://github.com/TommyLemon/SQLAuto/commits/master
201+
https://github.com/TommyLemon/SQLAuto/commits
196202

197203

198204
### 我要赞赏
199-
创作不易,右上角点 ⭐Star 支持下本项目吧,谢谢 ^_^ <br />
205+
创作不易、坚持更难,右上角点亮 ⭐Star 支持下本项目吧,谢谢 ^_^ <br />
200206
https://gitee.com/TommyLemon/SQLAuto
207+

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/sql/js/main.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ https://github.com/Tencent/APIJSON/issues
20482048
saveTextAs('# ' + this.exTxt.name + '\n主页: https://github.com/Tencent/APIJSON'
20492049
+ '\n\nBASE_URL: ' + this.getBaseUrl()
20502050
+ '\n\n\n## 测试用例(Markdown格式,可用工具预览) \n\n' + this.getDoc4TestCase()
2051-
+ '\n\n\n\n\n\n\n\n## 文档(Markdown格式,可用工具预览) \n\n' + doc
2051+
+ (this.view != 'markdown' ? '' : '\n\n\n\n\n\n\n\n## 文档(Markdown格式,可用工具预览) \n\n' + doc)
20522052
, this.exTxt.name + '.txt')
20532053
}
20542054
else if (this.view == 'markdown' || this.view == 'output') { //model
@@ -6954,17 +6954,59 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
69546954

69556955
getDoc4TestCase: function () {
69566956
var list = this.remotes || []
6957-
var doc = ''
6958-
var item
6957+
var s = ''
69596958
for (var i = 0; i < list.length; i ++) {
6960-
item = list[i] == null ? null : list[i].Document
6961-
if (item == null || item.name == null) {
6959+
var item = list[i]
6960+
var doc = item == null ? null : item.Document
6961+
if (doc == null || doc.name == null) {
69626962
continue
69636963
}
6964-
doc += '\n\n#### ' + (item.version > 0 ? 'V' + item.version : 'V*') + ' ' + item.name + ' ' + item.url
6965-
doc += '\n```json\n' + item.request + '\n```\n'
6964+
var tr = item.TestRecord
6965+
6966+
var req = doc.sqlauto // || doc.request
6967+
var res = ''
6968+
try {
6969+
var m = this.getMethod(doc.url);
6970+
var standardObj = null;
6971+
try {
6972+
standardObj = parseJSON(doc.standard);
6973+
} catch (e3) {
6974+
log(e3)
6975+
}
6976+
6977+
var isAPIJSONRouter = false;
6978+
// try {
6979+
// var apijson = parseJSON(doc.apijson);
6980+
// isAPIJSONRouter = JSONResponse.isObject(apijson)
6981+
// } catch (e3) {
6982+
// log(e3)
6983+
// }
6984+
//
6985+
// req = StringUtil.trim(CodeUtil.parseComment(req, docObj == null ? null : docObj['[]'], m, this.database, this.language, true, standardObj, null, true, isAPIJSONRouter));
6986+
6987+
if (this.view != 'markdown') {
6988+
res = tr == null ? null : tr.response
6989+
var standardObj2 = null;
6990+
try {
6991+
standardObj2 = StringUtil.isEmpty(res) ? null : parseJSON(tr.standard);
6992+
} catch (e3) {
6993+
log(e3)
6994+
}
6995+
6996+
if (StringUtil.isNotEmpty(standardObj2)) {
6997+
res = StringUtil.trim(CodeUtil.parseComment(format(res), docObj == null ? null : docObj['[]'], m, this.database, this.language, false, standardObj2, null, true, isAPIJSONRouter));
6998+
}
6999+
}
7000+
} catch (e) {
7001+
log(e)
7002+
}
7003+
7004+
s += '\n\n#### ' + (doc.version > 0 ? 'V' + doc.version : 'V*') + ' ' + doc.name + ' ' + doc.url; // + (doc.method || '') + ' ' + (doc.type || '') + ' ' + doc.url;
7005+
s += '\n```sql\n' + req + '\n```\n';
7006+
s += StringUtil.isEmpty(res) ? '' : '\n=> Response:\n```json\n' + res + '\n```\n';
69667007
}
6967-
return doc
7008+
7009+
return s
69687010
},
69697011

69707012
enableCross: function (enable) {
@@ -7656,7 +7698,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
76567698
else {
76577699
configVal = val
76587700
}
7659-
constConfigLines[which] = p_k + ': ' + configVal;
7701+
constConfigLines[which] = StringUtil.isEmpty(p_k) ? configVal : p_k + ': ' + configVal;
76607702
// }
76617703

76627704
if (generateName) {

0 commit comments

Comments
 (0)