Skip to content

Commit 718a5bb

Browse files
committed
新增支持对指定的方法忽略空字符串作为条件值或写入值等
1 parent 0f1b105 commit 718a5bb

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ public abstract class AbstractSQLConfig implements SQLConfig {
9898
*/
9999
public static boolean ENABLE_WITH_AS = false;
100100

101+
/**
102+
* 对指定的方法,忽略空字符串,不作为 GET 条件,PUT 值等。可取值 new RequestMethod[]{ RequestMethod.GET, RequestMethod.POST ... }
103+
*/
104+
public static List<RequestMethod> IGNORE_EMPTY_STRING_METHOD_LIST = null;
105+
/**
106+
* 对指定的方法,忽略空白字符串。即首尾 trim 去掉所有不可见字符后,仍然为空的,就忽略,不作为 GET 条件,PUT 值等。
107+
* 可取值 new RequestMethod[]{ RequestMethod.GET, RequestMethod.POST ... }
108+
*/
109+
public static List<RequestMethod> IGNORE_BLANK_STRING_METHOD_LIST = null;
101110
public static int MAX_HAVING_COUNT = 5;
102111
public static int MAX_WHERE_COUNT = 10;
103112
public static int MAX_COMBINE_DEPTH = 2;
@@ -5056,11 +5065,17 @@ else if (userId instanceof Subquery) {}
50565065
String[] rawArr = StringUtil.split(raw);
50575066
config.setRaw(rawArr == null || rawArr.length <= 0 ? null : new ArrayList<>(Arrays.asList(rawArr)));
50585067

5059-
Map<String, Object> tableWhere = new LinkedHashMap<String, Object>();//保证顺序好优化 WHERE id > 1 AND name LIKE...
5068+
Map<String, Object> tableWhere = new LinkedHashMap<String, Object>(); // 保证顺序好优化 WHERE id > 1 AND name LIKE...
5069+
5070+
boolean ignoreBlankStr = IGNORE_BLANK_STRING_METHOD_LIST != null && IGNORE_BLANK_STRING_METHOD_LIST.contains(method);
5071+
boolean ignoreEmptyStr = ignoreBlankStr || (IGNORE_EMPTY_STRING_METHOD_LIST != null && IGNORE_EMPTY_STRING_METHOD_LIST.contains(method));
5072+
boolean ignoreEmptyOrBlankStr = ignoreEmptyStr || ignoreBlankStr;
50605073

5061-
//已经remove了id和id{},以及@key
5062-
Set<String> set = request.keySet(); //前面已经判断request是否为空
5063-
if (method == POST) { //POST操作
5074+
boolean enableFakeDelete = config.isFakeDelete();
5075+
5076+
// 已经 remove了 id 和 id{},以及 @key
5077+
Set<String> set = request.keySet(); // 前面已经判断 request 是否为空
5078+
if (method == POST) { // POST操作
50645079
if (idIn != null) {
50655080
throw new IllegalArgumentException(table + ":{" + idInKey + ": value} 里的 key 不合法!POST 请求中不允许传 " + idInKey
50665081
+ " 这种非字段命名 key !必须为 英文字母 开头且只包含 英文字母、数字、下划线的 字段命名!"); }
@@ -5237,25 +5252,29 @@ else if (w.startsWith("!")) {
52375252
//条件>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
52385253

52395254
Map<String, Object> tableContent = new LinkedHashMap<String, Object>();
5240-
Object value;
52415255
for (String key : set) {
5242-
value = request.get(key);
5256+
Object value = request.get(key);
5257+
if (ignoreEmptyOrBlankStr && value instanceof String && StringUtil.isEmpty(value, ignoreBlankStr)) {
5258+
continue;
5259+
}
52435260

5244-
if (key.endsWith("<>") == false && value instanceof Map) {//只允许常规Object
5245-
throw new IllegalArgumentException(table + ":{ " + key + ":value } 中 value 类型错误!除了 key<>:{} 外,不允许 " + key + " 等其它任何 key 对应 value 的类型为 JSONObject {} !");
5261+
if (key.endsWith("<>") == false && value instanceof Map) { // 只允许常规 Object
5262+
throw new IllegalArgumentException(table + ":{ " + key + ":value } 中 value 类型错误!除了 key<>:{} 外,不允许 "
5263+
+ key + " 等其它任何 key 对应 value 的类型为 JSONObject {} !");
52465264
}
52475265

5248-
//兼容 PUT @combine
5249-
//解决AccessVerifier新增userId没有作为条件,而是作为内容,导致PUT,DELETE出错
5250-
if ((isWhere || (StringUtil.isName(key.replaceFirst("[+-]$", "")) == false)) || (isWhere == false && StringUtil.isNotEmpty(combineExpr, true) && keyInCombineExpr(combineExpr, key))) {
5266+
// 兼容 PUT @combine
5267+
// 解决AccessVerifier新增userId没有作为条件,而是作为内容,导致PUT,DELETE出错
5268+
if ((isWhere || (StringUtil.isName(key.replaceFirst("[+-]$", "")) == false))
5269+
|| (isWhere == false && StringUtil.isNotEmpty(combineExpr, true) && keyInCombineExpr(combineExpr, key))) {
52515270
tableWhere.put(key, value);
52525271
if (whereList.contains(key) == false) {
52535272
andList.add(key);
52545273
}
52555274
} else if (whereList.contains(key)) {
52565275
tableWhere.put(key, value);
52575276
} else {
5258-
tableContent.put(key, value); //一样 instanceof JSONArray ? JSON.toJSONString(value) : value);
5277+
tableContent.put(key, value); // 一样 instanceof JSONArray ? JSON.toJSONString(value) : value);
52595278
}
52605279
}
52615280

@@ -5305,11 +5324,11 @@ else if (w.startsWith("!")) {
53055324
}
53065325
}
53075326

5308-
boolean distinct = column == null || rawColumnSQL != null ? false : column.startsWith(PREFIX_DISTINCT);
5327+
boolean distinct = rawColumnSQL == null && column != null && column.startsWith(PREFIX_DISTINCT);
53095328
if (rawColumnSQL == null) {
5310-
String[] fks = StringUtil.split(distinct ? column.substring(PREFIX_DISTINCT.length()) : column, ";"); // key0,key1;fun0(key0,...);fun1(key0,...);key3;fun2(key0,...)
5329+
// key0,key1;fun0(key0,...);fun1(key0,...);key3;fun2(key0,...)
5330+
String[] fks = StringUtil.split(distinct ? column.substring(PREFIX_DISTINCT.length()) : column, ";");
53115331
if (fks != null) {
5312-
String[] ks;
53135332
for (String fk : fks) {
53145333
if (containColumnRaw) {
53155334
String rawSQL = config.getRawSQL(KEY_COLUMN, fk);
@@ -5322,8 +5341,8 @@ else if (w.startsWith("!")) {
53225341
if (fk.contains("(")) { // fun0(key0,...)
53235342
cs.add(fk);
53245343
}
5325-
else { //key0,key1...
5326-
ks = StringUtil.split(fk);
5344+
else { // key0,key1...
5345+
String[] ks = StringUtil.split(fk);
53275346
if (ks != null && ks.length > 0) {
53285347
cs.addAll(Arrays.asList(ks));
53295348
}
@@ -5403,6 +5422,9 @@ else if (newHaving instanceof JSONObject) {
54035422
throw new IllegalArgumentException(table + ":{ " + havingKey + ":{ " + k + ":value } } 里的"
54045423
+ " value 不合法!类型只能是 String,且不允许为空!");
54055424
}
5425+
if (ignoreEmptyOrBlankStr && StringUtil.isEmpty(v, ignoreBlankStr)) {
5426+
continue;
5427+
}
54065428

54075429
if (KEY_COMBINE.equals(k)) {
54085430
havingCombine = (String) v;

0 commit comments

Comments
 (0)