Skip to content

Commit 1f3b59b

Browse files
committed
新增支持 key[ 表示 length(key) 和 key{ 表示 json_length(key),可与 与或非逻辑符、其它各种功能符 组合使用
1 parent 4f6066c commit 1f3b59b

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

APIJSONORM/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.Tencent</groupId>
77
<artifactId>APIJSON</artifactId>
8-
<version>7.0.3</version>
8+
<version>7.0.5</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONORM</name>

APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,31 @@ public JSONObject putsEmpty(String key, boolean isEmpty, boolean trim) {
480480
public JSONObject putsLength(String key, String compare) {
481481
return puts(key+"{}", SQL.length(key) + compare);
482482
}
483+
/**
484+
* @param key
485+
* @param compare <=, > ...
486+
* @param value 1, 5, 3.14, -99 ...
487+
* @return {@link #puts(String, Object)}
488+
*/
489+
public JSONObject putsLength(String key, String compare, Object value) {
490+
return puts(key+"["+(StringUtil.isEmpty(compare) || "=".equals(compare) ? "" : ("!=".equals(compare) ? "!" : compare)), value);
491+
}
492+
/**
493+
* @param key
494+
* @param compare <=0, >5 ...
495+
* @return {@link #puts(String, Object)}
496+
*/
497+
public JSONObject putsJSONLength(String key, String compare) {
498+
return puts(key+"{}", SQL.json_length(key) + compare);
499+
}
500+
/**
501+
* @param key
502+
* @param compare <=0, >5 ...
503+
* @return {@link #puts(String, Object)}
504+
*/
505+
public JSONObject putsJSONLength(String key, String compare, Object value) {
506+
return puts(key + "{" + (StringUtil.isEmpty(compare) || "=".equals(compare) ? "" : ("!=".equals(compare) ? "!" : compare)), value);
507+
}
483508

484509
/**设置搜索
485510
* type = SEARCH_TYPE_CONTAIN_FULL

APIJSONORM/src/main/java/apijson/Log.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Log {
1414

1515
public static boolean DEBUG = true;
1616

17-
public static final String VERSION = "7.0.3";
17+
public static final String VERSION = "7.0.5";
1818
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
1919

2020
public static final String OS_NAME;

APIJSONORM/src/main/java/apijson/SQL.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ public static String lengthCompare(String s, String compare) {
116116
public static String length(String s) {
117117
return "length(" + s + ")";
118118
}
119+
/**
120+
* @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
121+
* @return "json_length(" + s + ")"
122+
*/
123+
public static String json_length(String s) {
124+
return "json_length(" + s + ")";
125+
}
119126
/**
120127
* @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
121128
* @return "char_length(" + s + ")"

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,7 +3585,9 @@ public String getCompareString(String key, String column, Object value, String t
35853585
if (value != null && JSON.isBooleanOrNumberOrString(value) == false && value instanceof Subquery == false) {
35863586
throw new IllegalArgumentException(key + ":value 中 value 不合法!比较运算 [>, <, >=, <=] 只支持 [Boolean, Number, String] 内的类型 !");
35873587
}
3588-
if (StringUtil.isName(column) == false) {
3588+
3589+
String rc = column.endsWith("[") || column.endsWith("{") ? column.substring(0, column.length() - 1) : column;
3590+
if ( ! StringUtil.isName(rc)) {
35893591
throw new IllegalArgumentException(key + ":value 中 key 不合法!比较运算 [>, <, >=, <=] 不支持 [&, !, |] 中任何逻辑运算符 !");
35903592
}
35913593

@@ -3594,7 +3596,16 @@ public String getCompareString(String key, String column, Object value, String t
35943596
}
35953597

35963598
public String getKey(String key) {
3597-
if (isTest()) {
3599+
String lenFun = "";
3600+
if (key.endsWith("[")) {
3601+
lenFun = isSQLServer() ? "datalength" : "length";
3602+
key = key.substring(0, key.length() - 1);
3603+
}
3604+
else if (key.endsWith("{")) {
3605+
lenFun = "json_length";
3606+
key = key.substring(0, key.length() - 1);
3607+
}
3608+
else if (isTest()) {
35983609
if (key.contains("'")) { // || key.contains("#") || key.contains("--")) {
35993610
throw new IllegalArgumentException("参数 " + key + " 不合法!key 中不允许有单引号 ' !");
36003611
}
@@ -3606,13 +3617,18 @@ public String getKey(String key) {
36063617
if (expression == null) {
36073618
expression = COLUMN_KEY_MAP == null ? null : COLUMN_KEY_MAP.get(key);
36083619
}
3620+
3621+
String sqlKey;
36093622
if (expression == null) {
3610-
return getSQLKey(key);
3623+
sqlKey = getSQLKey(key);
3624+
}
3625+
else {
3626+
// (name,tag) left(date,4) 等
3627+
List<String> raw = getRaw();
3628+
sqlKey = parseSQLExpression(KEY_KEY, expression, raw != null && raw.contains(KEY_KEY), false);
36113629
}
36123630

3613-
// (name,tag) left(date,4) 等
3614-
List<String> raw = getRaw();
3615-
return parseSQLExpression(KEY_KEY, expression, raw != null && raw.contains(KEY_KEY), false);
3631+
return lenFun.isEmpty() ? sqlKey : lenFun + "(" + sqlKey + ")";
36163632
}
36173633
public String getSQLKey(String key) {
36183634
String q = getQuote();
@@ -6016,28 +6032,37 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
60166032
}
60176033
}
60186034

6019-
//TODO if (key.endsWith("-")) { // 表示 key 和 value 顺序反过来: value LIKE key
6035+
String len = "";
6036+
if (key.endsWith("[") || key.endsWith("{")) {
6037+
len = key.substring(key.length() - 1);
6038+
key = key.substring(0, key.length() - 1);
6039+
}
6040+
6041+
// TODO if (key.endsWith("-")) { // 表示 key 和 value 顺序反过来: value LIKE key ?
60206042

6021-
//不用Logic优化代码,否则 key 可能变为 key| 导致 key=value 变成 key|=value 而出错
6043+
// 不用Logic优化代码,否则 key 可能变为 key| 导致 key=value 变成 key|=value 而出错
60226044
String last = key.isEmpty() ? "" : key.substring(key.length() - 1);
60236045
if ("&".equals(last) || "|".equals(last) || "!".equals(last)) {
60246046
key = key.substring(0, key.length() - 1);
60256047
} else {
60266048
last = null;//避免key + StringUtil.getString(last)错误延长
60276049
}
60286050

6029-
//"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好
6030-
if (isTableKey) {//不允许在column key中使用Type:key形式
6031-
key = Pair.parseEntry(key, true).getKey();//table以左边为准
6051+
// "User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好
6052+
if (isTableKey) { // 不允许在column key中使用Type:key形式
6053+
key = Pair.parseEntry(key, true).getKey(); // table以左边为准
60326054
} else {
6033-
key = Pair.parseEntry(key).getValue();//column以右边为准
6055+
key = Pair.parseEntry(key).getValue();// column 以右边为准
60346056
}
60356057

60366058
if (verifyName && StringUtil.isName(key.startsWith("@") ? key.substring(1) : key) == false) {
60376059
throw new IllegalArgumentException(method + "请求,字符 " + originKey + " 不合法!"
6038-
+ " key:value 中的key只能关键词 '@key' 或 'key[逻辑符][条件符]' 或 PUT请求下的 'key+' / 'key-' !");
6060+
+ " key:value 中的 key 只能关键词 '@key' 或 'key[长度符][逻辑符][条件符]' 或 PUT 请求下的 'key+' / 'key-' !"
6061+
+ "长度符 只能为 [ - length 和 { - json_length,逻辑符 只能是 & - 与、| - 或、! - 非 !");
60396062
}
60406063

6064+
key += len;
6065+
60416066
if (saveLogic && last != null) {
60426067
key = key + last;
60436068
}

0 commit comments

Comments
 (0)