2121import static zuo .biao .apijson .JSONObject .KEY_GROUP ;
2222import static zuo .biao .apijson .JSONObject .KEY_HAVING ;
2323import static zuo .biao .apijson .JSONObject .KEY_ID ;
24- import static zuo .biao .apijson .JSONObject .KEY_ID_IN ;
2524import static zuo .biao .apijson .JSONObject .KEY_ORDER ;
2625import static zuo .biao .apijson .JSONObject .KEY_ROLE ;
2726import static zuo .biao .apijson .JSONObject .KEY_SCHEMA ;
2827import static zuo .biao .apijson .JSONObject .KEY_USER_ID ;
29- import static zuo .biao .apijson .JSONObject .KEY_USER_ID_IN ;
3028import static zuo .biao .apijson .RequestMethod .DELETE ;
3129import static zuo .biao .apijson .RequestMethod .GET ;
3230import static zuo .biao .apijson .RequestMethod .GETS ;
@@ -80,6 +78,16 @@ public abstract class AbstractSQLConfig implements SQLConfig {
8078 TABLE_KEY_MAP .put (Column .class .getSimpleName (), Column .TAG );
8179 }
8280
81+ @ NotNull
82+ @ Override
83+ public String getIdKey () {
84+ return KEY_ID ;
85+ }
86+ @ NotNull
87+ @ Override
88+ public String getUserIdKey () {
89+ return KEY_USER_ID ;
90+ }
8391
8492
8593 private Object id ; //Table的id
@@ -914,16 +922,22 @@ else if (andList == null || andList.contains(key) == false) {
914922 andList = new ArrayList <>();
915923 }
916924 else if (prior && andList .isEmpty () == false ) {
917- if (andList .contains (KEY_ID )) {
925+
926+ String idKey = getIdKey ();
927+ String idInKey = idKey + "{}" ;
928+ String userIdKey = getUserIdKey ();
929+ String userIdInKey = userIdKey + "{}" ;
930+
931+ if (andList .contains (idKey )) {
918932 i ++;
919933 }
920- if (andList .contains (KEY_ID_IN )) {
934+ if (andList .contains (idInKey )) {
921935 i ++;
922936 }
923- if (andList .contains (KEY_USER_ID )) {
937+ if (andList .contains (userIdKey )) {
924938 i ++;
925939 }
926- if (andList .contains (KEY_USER_ID_IN )) {
940+ if (andList .contains (userIdInKey )) {
927941 i ++;
928942 }
929943 }
@@ -1689,9 +1703,10 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
16891703 int keyType = 0 ;// 0 - =; 1 - +, 2 - -
16901704 Object value ;
16911705
1706+ String idKey = getIdKey ();
16921707 for (String key : set ) {
16931708 //避免筛选到全部 value = key == null ? null : content.get(key);
1694- if (key == null || KEY_ID .equals (key )) {
1709+ if (key == null || idKey .equals (key )) {
16951710 continue ;
16961711 }
16971712
@@ -1951,21 +1966,28 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
19511966 if (request .isEmpty ()) { // User:{} 这种空内容在查询时也有效
19521967 return config ; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
19531968 }
1969+
1970+ String schema = request .getString (KEY_SCHEMA );
1971+
1972+ String idKey = callback .getIdKey (schema , table );
1973+ String idInKey = idKey + "{}" ;
1974+ String userIdKey = callback .getUserIdKey (schema , table );
1975+ String userIdInKey = userIdKey + "{}" ;
19541976
1955- Object idIn = request .get (KEY_ID_IN ); //可能是 id{}:">0"
1977+ Object idIn = request .get (idInKey ); //可能是 id{}:">0"
19561978
19571979 if (method == POST ) {
19581980 if (idIn != null ) { //不能在这里确定[]的长度,只能在外面传进来
19591981 if ((idIn instanceof List == false ) || ((List <?>)idIn ).isEmpty ()) { // id{}:[] 表示同时插入多条记录
19601982 throw new IllegalArgumentException ("POST请求,生成多条记录请用 id{}:[] ! [] 类型为JSONArray且不能为空!" );
19611983 }
1962- } else if (request .get (KEY_ID ) == null ) {
1963- request .put (KEY_ID , callback .newId (method , table ));
1984+ } else if (request .get (idKey ) == null ) {
1985+ request .put (idKey , callback .newId (method , table ));
19641986 }
19651987 }
19661988
19671989 //对id和id{}处理,这两个一定会作为条件
1968- Object id = request .get (KEY_ID );
1990+ Object id = request .get (idKey );
19691991 if (id != null ) { //null无效
19701992 if (id instanceof Number ) {
19711993 if (((Number ) id ).longValue () <= 0 ) { //一定没有值
@@ -1979,7 +2001,7 @@ else if (id instanceof String) {
19792001 }
19802002 else if (id instanceof Subquery ) {}
19812003 else {
1982- throw new IllegalArgumentException (KEY_ID + ":value 中 value 的类型只能是 Long , String 或 Subquery !" );
2004+ throw new IllegalArgumentException (idKey + ":value 中 value 的类型只能是 Long , String 或 Subquery !" );
19832005 }
19842006
19852007 if (idIn instanceof List ) { //共用idIn场景少性能差
@@ -1992,7 +2014,6 @@ else if (id instanceof Subquery) {}
19922014
19932015 String role = request .getString (KEY_ROLE );
19942016 String database = request .getString (KEY_DATABASE );
1995- String schema = request .getString (KEY_SCHEMA );
19962017 String combine = request .getString (KEY_COMBINE );
19972018 Subquery from = (Subquery ) request .get (KEY_FROM );
19982019 String column = request .getString (KEY_COLUMN );
@@ -2001,8 +2022,8 @@ else if (id instanceof Subquery) {}
20012022 String order = request .getString (KEY_ORDER );
20022023
20032024 //强制作为条件且放在最前面优化性能
2004- request .remove (KEY_ID );
2005- request .remove (KEY_ID_IN );
2025+ request .remove (idKey );
2026+ request .remove (idInKey );
20062027 //关键词
20072028 request .remove (KEY_ROLE );
20082029 request .remove (KEY_DATABASE );
@@ -2043,7 +2064,7 @@ else if (id instanceof Subquery) {}
20432064 throw new Exception ("服务器内部错误:\n " + TAG
20442065 + " newSQLConfig values == null || values.length != columns.length !" );
20452066 }
2046- column = KEY_ID + "," + StringUtil .getString (columns ); //set已经判断过不为空
2067+ column = idKey + "," + StringUtil .getString (columns ); //set已经判断过不为空
20472068 final int size = columns .length + 1 ; //以key数量为准
20482069
20492070 List <List <Object >> valuess = new ArrayList <>(idList .size ()); // [idList.size()][]
@@ -2072,12 +2093,12 @@ else if (id instanceof Subquery) {}
20722093
20732094 //强制作为条件且放在最前面优化性能
20742095 if (id != null ) {
2075- tableWhere .put (KEY_ID , id );
2076- andList .add (KEY_ID );
2096+ tableWhere .put (idKey , id );
2097+ andList .add (idKey );
20772098 }
20782099 if (idIn != null ) {
2079- tableWhere .put (KEY_ID_IN , idIn );
2080- andList .add (KEY_ID_IN );
2100+ tableWhere .put (idInKey , idIn );
2101+ andList .add (idInKey );
20812102 }
20822103
20832104 String [] ws = StringUtil .split (combine );
@@ -2119,9 +2140,9 @@ else if (w.startsWith("!")) {
21192140 throw new IllegalArgumentException (table + ":{} 里的 @combine:value 中的value里条件 " + ws [i ] + " 不合法!不允许为空值!" );
21202141 }
21212142 else {
2122- if (KEY_ID .equals (w ) || KEY_ID_IN .equals (w ) || KEY_USER_ID .equals (w ) || KEY_USER_ID_IN .equals (w )) {
2143+ if (idKey .equals (w ) || idInKey .equals (w ) || userIdKey .equals (w ) || userIdInKey .equals (w )) {
21232144 throw new UnsupportedOperationException (table + ":{} 里的 @combine:value 中的value里 " + ws [i ] + " 不合法!"
2124- + "不允许传 [" + KEY_ID + ", " + KEY_ID_IN + ", " + KEY_USER_ID + ", " + KEY_USER_ID_IN + "] 其中任何一个!" );
2145+ + "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!" );
21252146 }
21262147 }
21272148
@@ -2203,8 +2224,8 @@ else if (whereList != null && whereList.contains(key)) {
22032224
22042225 //后面还可能用到,要还原
22052226 //id或id{}条件
2206- request .put (KEY_ID , id );
2207- request .put (KEY_ID_IN , idIn );
2227+ request .put (idKey , id );
2228+ request .put (idInKey , idIn );
22082229 //关键词
22092230 request .put (KEY_ROLE , role );
22102231 request .put (KEY_DATABASE , database );
@@ -2385,20 +2406,55 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
23852406 }
23862407
23872408
2388- public interface Callback {
2409+ public static interface Callback {
23892410 /**获取 SQLConfig 的实例
23902411 * @param method
23912412 * @param table
23922413 * @return
23932414 */
23942415 AbstractSQLConfig getSQLConfig (RequestMethod method , String table );
23952416
2417+
23962418 /**为 post 请求新建 id, 只能是 Long 或 String
23972419 * @param method
23982420 * @param table
23992421 * @return
24002422 */
24012423 Object newId (RequestMethod method , String table );
2424+
2425+ /**获取主键名
2426+ * @param schema
2427+ * @param table
2428+ * @return
2429+ */
2430+ String getIdKey (String schema , String table );
2431+
2432+ /**获取 User 的主键名
2433+ * @param schema
2434+ * @param table
2435+ * @return
2436+ */
2437+ String getUserIdKey (String schema , String table );
2438+ }
2439+
2440+ public static abstract class SimpleCallback implements Callback {
2441+
2442+
2443+ @ Override
2444+ public Object newId (RequestMethod method , String table ) {
2445+ return System .currentTimeMillis ();
2446+ }
2447+
2448+ @ Override
2449+ public String getIdKey (String schema , String table ) {
2450+ return KEY_ID ;
2451+ }
2452+
2453+ @ Override
2454+ public String getUserIdKey (String schema , String table ) {
2455+ return KEY_USER_ID ;
2456+ }
2457+
24022458 }
24032459
24042460}
0 commit comments