Skip to content

Commit 5c989a2

Browse files
committed
Server:新增支持EXISTS,例如 "id}{@":{ "Comment": {} }
1 parent fd80bb9 commit 5c989a2

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
686686

687687
//不能改变,因为后面可能继续用到,导致1以上都改变 []:{0:{Comment[]:{0:{Comment:{}},1:{...},...}},1:{...},...}
688688
final int query = request.getIntValue(JSONRequest.KEY_QUERY);
689-
final Integer count = request.getInteger(JSONRequest.KEY_COUNT);
689+
final Integer count = request.getInteger(JSONRequest.KEY_COUNT); //TODO 如果不想用默认数量可以改成 getIntValue(JSONRequest.KEY_COUNT);
690690
final int page = request.getIntValue(JSONRequest.KEY_PAGE);
691691
final String join = request.getString(JSONRequest.KEY_JOIN);
692692
request.remove(JSONRequest.KEY_QUERY);

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,9 +1113,12 @@ else if (key.endsWith("%")) {
11131113
else if (key.endsWith("{}")) {
11141114
keyType = 4;
11151115
}
1116-
else if (key.endsWith("<>")) {
1116+
else if (key.endsWith("}{")) {
11171117
keyType = 5;
11181118
}
1119+
else if (key.endsWith("<>")) {
1120+
keyType = 6;
1121+
}
11191122
else { //else绝对不能省,避免再次踩坑! keyType = 0; 写在for循环外面都没注意!
11201123
keyType = 0;
11211124
}
@@ -1132,6 +1135,8 @@ else if (key.endsWith("<>")) {
11321135
case 4:
11331136
return getRangeString(key, value);
11341137
case 5:
1138+
return getExistsString(key, value);
1139+
case 6:
11351140
return getContainString(key, value);
11361141
default: //TODO MySQL JSON类型的字段对比 key='[]' 会无结果! key LIKE '[1, 2, 3]' //TODO MySQL , 后面有空格!
11371142
return getEqualString(key, value);
@@ -1441,7 +1446,7 @@ else if (range instanceof Subquery) { //如果在 Parser 解析成 SQL 字符串
14411446
}
14421447

14431448
throw new IllegalArgumentException(key + "{}:range 类型为" + range.getClass().getSimpleName()
1444-
+ "!range只能是 用','分隔条件的字符串 或者 可取选项JSONArray!");
1449+
+ "!range 只能是 用','分隔条件的字符串 或者 可取选项JSONArray!");
14451450
}
14461451
/**WHERE key IN ('key0', 'key1', ... )
14471452
* @param in
@@ -1465,6 +1470,32 @@ public String getInString(String key, Object[] in, boolean not) throws NotExistE
14651470
//{} range >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
14661471

14671472

1473+
//}{ exists <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1474+
/**WHERE EXISTS subquery
1475+
* 如果合并到 getRangeString,一方面支持不了 [1,2,2] 和 ">1" (转成 EXISTS(SELECT IN ) 需要static newSQLConfig,但它不能传入子类实例,除非不是 static),另一方面多了子查询临时表性能会比 IN 差
1476+
* @param key
1477+
* @param value
1478+
* @return EXISTS ALL(SELECT ...)
1479+
* @throws NotExistException
1480+
*/
1481+
@JSONField(serialize = false)
1482+
public String getExistsString(String key, Object value) throws Exception {
1483+
if (value == null) {
1484+
return "";
1485+
}
1486+
if (value instanceof Subquery == false) {
1487+
throw new IllegalArgumentException(key + "}{:subquery 类型为" + value.getClass().getSimpleName()
1488+
+ "!subquery 只能是 子查询JSONObejct!");
1489+
}
1490+
1491+
Logic logic = new Logic(key);
1492+
key = logic.getKey();
1493+
Log.i(TAG, "getExistsString key = " + key);
1494+
1495+
return (logic.isNot() ? NOT : "") + " EXISTS " + getSubqueryString((Subquery) value);
1496+
}
1497+
//}{ exists >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1498+
14681499
//<> contain <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
14691500
/**WHERE key contains value
14701501
* @param key
@@ -1473,14 +1504,14 @@ public String getInString(String key, Object[] in, boolean not) throws NotExistE
14731504
* @throws NotExistException
14741505
*/
14751506
@JSONField(serialize = false)
1476-
public String getContainString(String key, Object value) throws NotExistException {
1507+
public String getContainString(String key, Object value) throws IllegalArgumentException {
14771508
if (value == null) {
14781509
return "";
14791510
}
14801511

14811512
Logic logic = new Logic(key);
14821513
key = logic.getKey();
1483-
Log.i(TAG, "getRangeString key = " + key);
1514+
Log.i(TAG, "getContainString key = " + key);
14841515

14851516
return getContainString(key, newJSONArray(value).toArray(), logic.getType());
14861517
}
@@ -1506,7 +1537,7 @@ public String getContainString(String key, Object[] childs, int type) throws Ill
15061537
childs[i] = "\"" + childs[i] + "\"";
15071538
}
15081539
condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR))
1509-
+ "JSON_CONTAINS(" + getKey(key) + ", " + getValue(childs[i]) + ")";
1540+
+ "json_contains(" + getKey(key) + ", " + getValue(childs[i]) + ")";
15101541
}
15111542
}
15121543
if (condition.isEmpty()) {
@@ -1533,9 +1564,9 @@ public String getSubqueryString(Subquery subquery) throws Exception {
15331564

15341565
cfg.setPreparedValueList(new ArrayList<>());
15351566
String sql = (range == null || range.isEmpty() ? "" : range) + "(" + cfg.getSQL(isPrepared()) + ") ";
1536-
1567+
15371568
preparedValueList.addAll(cfg.getPreparedValueList());
1538-
1569+
15391570
return sql;
15401571
}
15411572

@@ -2202,22 +2233,25 @@ public static String getRealKey(RequestMethod method, String originKey
22022233
}
22032234

22042235
String key = new String(originKey);
2205-
if (key.endsWith("$")) {//搜索,查询时处理
2236+
if (key.endsWith("$")) {//搜索 LIKE,查询时处理
22062237
key = key.substring(0, key.length() - 1);
22072238
}
2208-
else if (key.endsWith("~") || key.endsWith("?")) {//匹配正则表达式,查询时处理 TODO ?可能以后会被废弃,全用 ~ 和 *~ 替代,更接近 PostgreSQL 语法
2239+
else if (key.endsWith("~") || key.endsWith("?")) {//匹配正则表达式 REGEXP,查询时处理 TODO ?可能以后会被废弃,全用 ~ 和 *~ 替代,更接近 PostgreSQL 语法
22092240
key = key.substring(0, key.length() - 1);
22102241
if (key.endsWith("*")) {//忽略大小写
22112242
key = key.substring(0, key.length() - 1);
22122243
}
22132244
}
2214-
else if (key.endsWith("%")) {//数字、文本、日期范围BETWEEN AND
2245+
else if (key.endsWith("%")) {//数字、文本、日期范围 BETWEEN AND
22152246
key = key.substring(0, key.length() - 1);
22162247
}
2217-
else if (key.endsWith("{}")) {//被包含,或者说key对应值处于value的范围内。查询时处理
2248+
else if (key.endsWith("{}")) {//被包含 IN,或者说key对应值处于value的范围内。查询时处理
2249+
key = key.substring(0, key.length() - 2);
2250+
}
2251+
else if (key.endsWith("}{")) {//被包含 EXISTS,或者说key对应值处于value的范围内。查询时处理
22182252
key = key.substring(0, key.length() - 2);
22192253
}
2220-
else if (key.endsWith("<>")) {//包含,或者说value处于key对应值的范围内。查询时处理
2254+
else if (key.endsWith("<>")) {//包含 json_contains,或者说value处于key对应值的范围内。查询时处理
22212255
key = key.substring(0, key.length() - 2);
22222256
}
22232257
else if (key.endsWith("()")) {//方法,查询完后处理,先用一个Map<key,function>保存?

0 commit comments

Comments
 (0)