|
33 | 33 | import java.util.Set; |
34 | 34 | import java.util.regex.Pattern; |
35 | 35 |
|
| 36 | +import javax.activation.UnsupportedDataTypeException; |
| 37 | + |
36 | 38 | import com.alibaba.fastjson.JSON; |
37 | 39 | import com.alibaba.fastjson.JSONArray; |
38 | 40 | import com.alibaba.fastjson.JSONObject; |
@@ -244,45 +246,13 @@ public AbstractObjectParser parse() throws Exception { |
244 | 246 | index ++; |
245 | 247 | } |
246 | 248 | } |
| 249 | + else if ((method == POST || method == PUT) && value instanceof JSONArray && JSONRequest.isTableArray(key)) { //JSONArray,批量新增或修改,往下一级提取 |
| 250 | + onTableArrayParse(key, (JSONArray) value); |
| 251 | + } |
247 | 252 | else if (method == PUT && value instanceof JSONArray |
248 | 253 | && (whereList == null || whereList.contains(key) == false)) { //PUT JSONArray |
249 | 254 | onPUTArrayParse(key, (JSONArray) value); |
250 | 255 | } |
251 | | - else if (method == POST && value instanceof JSONArray && key.endsWith("[]") && JSONRequest.isTableKey(key.substring(0, key.length() - 2))) { //JSONArray,批量新增,往下一级提取 |
252 | | - String childKey = key.substring(0, key.length() - 2); |
253 | | - JSONArray valueArray = (JSONArray) value; |
254 | | - |
255 | | - int allCount = 0; |
256 | | - JSONArray ids = new JSONArray(); |
257 | | - |
258 | | - int version = parser.getVersion(); |
259 | | - int maxUpdateCount = parser.getMaxUpdateCount(); |
260 | | - |
261 | | - for (int i = 0; i < valueArray.size(); i++) { //只要有一条失败,则抛出异常,全部失败 |
262 | | - //TODO 改成一条多 VALUES 的 SQL 性能更高,报错也更会更好处理,更人性化 |
263 | | - JSONRequest req = new JSONRequest(childKey, valueArray.getJSONObject(i)); |
264 | | - |
265 | | - //parser.getMaxSQLCount() ? 可能恶意调用接口,把数据库拖死 |
266 | | - JSONObject result = (JSONObject) onChildParse(0, "" + i, parser.parseCorrectRequest(method, childKey, version, "", req, maxUpdateCount, parser)); |
267 | | - result = result.getJSONObject(childKey); |
268 | | -// |
269 | | - boolean success = JSONResponse.isSuccess(result); |
270 | | - int count = result == null ? null : result.getIntValue(JSONResponse.KEY_COUNT); |
271 | | - |
272 | | - if (success == false || count != 1) { //如果 code = 200 但 count != 1,不能算成功,掩盖了错误为不好排查问题 |
273 | | - throw new ServerException("批量新增失败!" + key + "/" + i + ":" + (success ? "成功但 count != 1 !" : (result == null ? "null" : result.getString(JSONResponse.KEY_MSG)))); |
274 | | - } |
275 | | - |
276 | | - allCount += count; |
277 | | - ids.add(result.get(JSONResponse.KEY_ID)); |
278 | | - } |
279 | | - |
280 | | - JSONObject allResult = AbstractParser.newSuccessResult(); |
281 | | - allResult.put(JSONResponse.KEY_ID_IN, ids); |
282 | | - allResult.put(JSONResponse.KEY_COUNT, allCount); |
283 | | - |
284 | | - response.put(key, allResult); //不按原样返回,避免数据量过大 |
285 | | - } |
286 | 256 | else {//JSONArray或其它Object,直接填充 |
287 | 257 | if (onParse(key, value) == false) { |
288 | 258 | invalidate(); |
@@ -597,6 +567,52 @@ public void onPUTArrayParse(@NotNull String key, @NotNull JSONArray array) throw |
597 | 567 |
|
598 | 568 | } |
599 | 569 |
|
| 570 | + |
| 571 | + @Override |
| 572 | + public void onTableArrayParse(String key, JSONArray value) throws Exception { |
| 573 | + String childKey = key.substring(0, key.length() - JSONRequest.KEY_ARRAY.length()); |
| 574 | + JSONArray valueArray = (JSONArray) value; |
| 575 | + |
| 576 | + int allCount = 0; |
| 577 | + JSONArray ids = new JSONArray(); |
| 578 | + |
| 579 | + int version = parser.getVersion(); |
| 580 | + int maxUpdateCount = parser.getMaxUpdateCount(); |
| 581 | + |
| 582 | + for (int i = 0; i < valueArray.size(); i++) { //只要有一条失败,则抛出异常,全部失败 |
| 583 | + //TODO 改成一条多 VALUES 的 SQL 性能更高,报错也更会更好处理,更人性化 |
| 584 | + JSONObject item; |
| 585 | + try { |
| 586 | + item = valueArray.getJSONObject(i); |
| 587 | + } |
| 588 | + catch (Exception e) { |
| 589 | + throw new UnsupportedDataTypeException("批量新增/修改失败!" + key + "/" + i + ":value 中value不合法!类型必须是 OBJECT ,结构为 {} !"); |
| 590 | + } |
| 591 | + JSONRequest req = new JSONRequest(childKey, item); |
| 592 | + |
| 593 | + //parser.getMaxSQLCount() ? 可能恶意调用接口,把数据库拖死 |
| 594 | + JSONObject result = (JSONObject) onChildParse(0, "" + i, parser.parseCorrectRequest(method, childKey, version, "", req, maxUpdateCount, parser)); |
| 595 | + result = result.getJSONObject(childKey); |
| 596 | + // |
| 597 | + boolean success = JSONResponse.isSuccess(result); |
| 598 | + int count = result == null ? null : result.getIntValue(JSONResponse.KEY_COUNT); |
| 599 | + |
| 600 | + if (success == false || count != 1) { //如果 code = 200 但 count != 1,不能算成功,掩盖了错误为不好排查问题 |
| 601 | + throw new ServerException("批量新增/修改失败!" + key + "/" + i + ":" + (success ? "成功但 count != 1 !" : (result == null ? "null" : result.getString(JSONResponse.KEY_MSG)))); |
| 602 | + } |
| 603 | + |
| 604 | + allCount += count; |
| 605 | + ids.add(result.get(JSONResponse.KEY_ID)); |
| 606 | + } |
| 607 | + |
| 608 | + JSONObject allResult = AbstractParser.newSuccessResult(); |
| 609 | + allResult.put(JSONResponse.KEY_ID_IN, ids); |
| 610 | + allResult.put(JSONResponse.KEY_COUNT, allCount); |
| 611 | + |
| 612 | + response.put(key, allResult); //不按原样返回,避免数据量过大 |
| 613 | + } |
| 614 | + |
| 615 | + |
600 | 616 | @Override |
601 | 617 | public JSONObject parseResponse(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception { |
602 | 618 | SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure); |
|
0 commit comments