Skip to content

Commit 3d189dd

Browse files
committed
Server:同步eclipse版至idea版
1 parent 3dc3073 commit 3d189dd

File tree

3 files changed

+203
-122
lines changed

3 files changed

+203
-122
lines changed

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/StringUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ public static boolean isPath(String path) {
404404
* @return
405405
*/
406406
public static String[] splitPath(String path) {
407+
if (StringUtil.isNotEmpty(path, true) == false) {
408+
return null;
409+
}
407410
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
408411
}
409412
/**将s分割成String[]

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
224224
Set<String> set = where == null ? null : where.keySet();
225225
if (set != null && set.size() > 0) {
226226
if (RequestParser.isGetMethod(method) == false && method != RequestMethod.POST_GET
227-
&& where.containsKey(Table.ID) == false) {
227+
&& where.containsKey(Table.ID) == false) {//POST必须有id,否则不能INSERT后直接返回id
228228
throw new IllegalArgumentException("请设置" + Table.ID + "!");
229229
}
230230

@@ -249,10 +249,21 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
249249
keyType = 2;
250250
}
251251
value = where.get(key);
252-
key = RequestParser.getRealKey(method, key, false);
253-
254-
whereString += (isFirst ? "" : " AND ") + (key + (keyType == 1 ? " LIKE '" + value + "'" : (keyType == 2
255-
? getRangeString(key, value) : "='" + value + "'") ));
252+
key = RequestParser.getRealKey(method, key, false, true);
253+
254+
String condition = "";
255+
switch (keyType) {
256+
case 1:
257+
condition = getLikeString(key, value);
258+
break;
259+
case 2:
260+
condition = getRangeString(key, value);
261+
break;
262+
default:
263+
condition = (key + "='" + value + "'");
264+
break;
265+
}
266+
whereString += (isFirst ? "" : " AND ") + condition;
256267

257268
isFirst = false;
258269
}
@@ -263,18 +274,56 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
263274
}
264275
return "";
265276
}
266-
277+
278+
/**WHERE key LIKE 'value'
279+
* @param key endsWith("!") ? key = key.substring(0, key.length() - 1) + " NOT ";
280+
* @param value
281+
* @return key LIKE 'value'
282+
*/
283+
public static String getLikeString(String key, Object value) {
284+
String last = key.isEmpty() ? "" : key.substring(key.length() - 1);
285+
if ("!".equals(last)) {
286+
key = key.substring(0, key.length() - 1) + " NOT ";
287+
}
288+
return key + " LIKE '" + value + "'";
289+
}
267290
/**WHERE key > 'key0' AND key <= 'key1' AND ...
268291
* @param key
269292
* @param range "condition0,condition1..."
270293
* @return key condition0 AND key condition1 AND ...
271294
*/
272295
public static String getRangeString(String key, Object range) {
296+
Log.i(TAG, "getRangeString key = " + key);
297+
298+
String last = key.isEmpty() ? "" : key.substring(key.length() - 1);
299+
int type = -1;
300+
Log.i(TAG, "getRangeString last = " + last);
301+
if ("|".equals(last)) {
302+
type = 0;
303+
}
304+
if ("&".equals(last)) {
305+
type = 1;
306+
}
307+
if ("!".equals(last)) {
308+
type = 2;
309+
}
310+
if (type >= 0 && type <= 2) {
311+
key = key.substring(0, key.length() - 1);
312+
}
313+
if (type < 0) {
314+
type = 0;
315+
}
316+
Log.i(TAG, "getRangeString key = " + key);
317+
273318
if (range instanceof JSONArray) {
274-
return getInString(((JSONArray) range).toArray());
319+
if (type != 0 && type != 2) {
320+
throw new IllegalArgumentException("\"key{}\":[] 中key末尾的逻辑运算符只能用'|','!'中的一种 !");
321+
}
322+
return key + getInString(type == 2, ((JSONArray) range).toArray());
275323
}
276-
if (range instanceof String) {
277-
return ((String) range).replaceAll(",", " AND " + key);//非Number类型需要客户端拼接成 < 'value0', >= 'value1'这种
324+
if (range instanceof String) {//非Number类型需要客户端拼接成 < 'value0', >= 'value1'这种
325+
range = key + ((String) range).replaceAll(",", (type == 1 ? " AND " : " OR ") + key);
326+
return type != 2 ? (String) range : " NOT (" + range + ")";
278327
}
279328

280329
throw new IllegalArgumentException("\"key{}\":range 中range只能是 用','分隔条件的字符串 或者 可取选项JSONArray!");
@@ -283,14 +332,14 @@ public static String getRangeString(String key, Object range) {
283332
* @param in
284333
* @return IN ('key0', 'key1', ... )
285334
*/
286-
public static String getInString(Object[] in) {
335+
public static String getInString(boolean not, Object[] in) {
287336
String inString = "";
288337
if (in != null) {//返回 "" 会导致 id:[] 空值时效果和没有筛选id一样!
289338
for (int i = 0; i < in.length; i++) {
290339
inString += ((i > 0 ? "," : "") + "'" + in[i] + "'");
291340
}
292341
}
293-
return " IN (" + inString + ") ";
342+
return (not ? " NOT " : "") + " IN (" + inString + ") ";
294343
}
295344
//WHERE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
296345

@@ -331,7 +380,7 @@ public static String getSetString(RequestMethod method, Map<String, Object> wher
331380
keyType = 2;
332381
}
333382
value = where.get(key);
334-
key = RequestParser.getRealKey(method, key, false);
383+
key = RequestParser.getRealKey(method, key, false, true);
335384

336385
setString += (isFirst ? "" : ", ") + (key + "=" + (keyType == 1 ? getAddString(key, value) : (keyType == 2
337386
? getRemoveString(key, value) : "'" + value + "'") ) );
@@ -377,7 +426,7 @@ public static String getRemoveString(String key, Object value) throws IllegalArg
377426
throw new IllegalArgumentException(key + "- 对应的值 " + value + " 不是Number,String,Array中的任何一种!");
378427
}
379428
//SET >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
380-
429+
381430

382431
/**获取查询配置
383432
* @param table

0 commit comments

Comments
 (0)