21
21
import static zuo .biao .apijson .JSONObject .KEY_HAVING ;
22
22
import static zuo .biao .apijson .JSONObject .KEY_ID ;
23
23
import static zuo .biao .apijson .JSONObject .KEY_ID_IN ;
24
+ import static zuo .biao .apijson .JSONObject .KEY_USER_ID ;
25
+ import static zuo .biao .apijson .JSONObject .KEY_USER_ID_IN ;
24
26
import static zuo .biao .apijson .JSONObject .KEY_ORDER ;
25
27
import static zuo .biao .apijson .JSONObject .KEY_ROLE ;
26
28
import static zuo .biao .apijson .JSONObject .KEY_SCHEMA ;
39
41
import java .util .LinkedHashMap ;
40
42
import java .util .List ;
41
43
import java .util .Map ;
44
+ import java .util .Map .Entry ;
42
45
import java .util .Set ;
43
46
44
47
import com .alibaba .fastjson .JSON ;
@@ -511,16 +514,35 @@ public String getWhereString() throws Exception {
511
514
* @throws Exception
512
515
*/
513
516
public static String getWhereString (RequestMethod method , Map <String , Object > where , boolean verifyName ) throws Exception {
514
- Set <String > set = where == null ? null : where . keySet ();
515
- if (set == null || set . isEmpty () ) {
517
+ Map <String , Object > where2 = where == null || where . isEmpty () ? null : new LinkedHashMap < String , Object > ();
518
+ if (where2 == null ) {
516
519
return "" ;
517
520
}
518
- String whereString = "" ;
521
+
522
+ //强制排序,把id,id{},userId,userId{}放最前面,保证安全、优化性能
523
+ Object id = where .remove (KEY_ID );
524
+ Object idIn = where .remove (KEY_ID_IN );
525
+ Object userId = where .remove (KEY_USER_ID );
526
+ Object userIdIn = where .remove (KEY_USER_ID_IN );
527
+
528
+ where2 .put (KEY_ID , id );
529
+ where2 .put (KEY_ID_IN , idIn );
530
+ where2 .put (KEY_USER_ID , userId );
531
+ where2 .put (KEY_USER_ID_IN , userIdIn );
532
+ where2 .putAll (where );
533
+
534
+
535
+ Set <Entry <String , Object >> set = where2 .entrySet ();
536
+
519
537
boolean isFirst = true ;
520
-
521
538
String condition ;
522
- for (String key : set ) {
523
- condition = getWhereItem (key , where .get (key ), method , verifyName );
539
+ String whereString = "" ;
540
+
541
+ for (Entry <String , Object > entry : set ) {
542
+ if (entry == null ) {
543
+ continue ;
544
+ }
545
+ condition = getWhereItem (entry .getKey (), entry .getValue (), method , verifyName );
524
546
525
547
if (StringUtil .isEmpty (condition , true )) {//避免SQL条件连接错误
526
548
continue ;
@@ -530,6 +552,12 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
530
552
531
553
isFirst = false ;
532
554
}
555
+
556
+ //还原where,后续可能用到
557
+ where .put (KEY_ID , id );
558
+ where .put (KEY_ID_IN , idIn );
559
+ where .put (KEY_USER_ID , userId );
560
+ where .put (KEY_USER_ID_IN , userIdIn );
533
561
534
562
String s = whereString .isEmpty () ? "" : " WHERE " + whereString ;
535
563
@@ -1041,8 +1069,7 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
1041
1069
}
1042
1070
AbstractSQLConfig config = callback .getSQLConfig (method , table );
1043
1071
1044
- boolean isEmpty = request .isEmpty ();
1045
- if (isEmpty ) { // User:{} 这种空内容在查询时也有效
1072
+ if (request .isEmpty ()) { // User:{} 这种空内容在查询时也有效
1046
1073
return config ; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
1047
1074
}
1048
1075
0 commit comments