@@ -103,22 +103,21 @@ public abstract class AbstractSQLConfig implements SQLConfig {
103103 public static String PREFFIX_DISTINCT = "DISTINCT " ;
104104
105105 // * 和 / 不能同时出现,防止 /* */ 段注释! # 和 -- 不能出现,防止行注释! ; 不能出现,防止隔断SQL语句!空格不能出现,防止 CRUD,DROP,SHOW TABLES等语句!
106- private static final Pattern PATTERN_RANGE ;
107- private static final Pattern PATTERN_FUNCTION ;
106+ private static Pattern PATTERN_RANGE ;
107+ private static Pattern PATTERN_FUNCTION ;
108108
109109 /**
110110 * 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
111111 */
112- public static final Map <String , String > TABLE_KEY_MAP ;
113- public static final List <String > CONFIG_TABLE_LIST ;
114- public static final List <String > DATABASE_LIST ;
112+ public static Map <String , String > TABLE_KEY_MAP ;
113+ public static List <String > CONFIG_TABLE_LIST ;
114+ public static List <String > DATABASE_LIST ;
115115
116116 // 自定义原始 SQL 片段 Map<key, substring>:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL
117- public static final Map <String , String > RAW_MAP ;
117+ public static Map <String , String > RAW_MAP ;
118118 // 允许调用的 SQL 函数:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL
119- public static final Map <String , String > SQL_AGGREGATE_FUNCTION_MAP ;
120- public static final Map <String , String > SQL_FUNCTION_MAP ;
121-
119+ public static Map <String , String > SQL_AGGREGATE_FUNCTION_MAP ;
120+ public static Map <String , String > SQL_FUNCTION_MAP ;
122121
123122 static { // 凡是 SQL 边界符、分隔符、注释符 都不允许,例如 ' " ` ( ) ; # -- /**/ ,以免拼接 SQL 时被注入意外可执行指令
124123 PATTERN_RANGE = Pattern .compile ("^[0-9%,!=\\ <\\ >/\\ .\\ +\\ -\\ *\\ ^]+$" ); // ^[a-zA-Z0-9_*%!=<>(),"]+$ 导致 exists(select*from(Comment)) 通过!
@@ -4339,7 +4338,7 @@ protected void onGetCrossJoinString(Join j) throws UnsupportedOperationException
43394338 * @return
43404339 * @throws Exception
43414340 */
4342- public static SQLConfig newSQLConfig (RequestMethod method , String table , String alias , JSONObject request , List <Join > joinList , boolean isProcedure , Callback callback ) throws Exception {
4341+ public static < T extends Object > SQLConfig newSQLConfig (RequestMethod method , String table , String alias , JSONObject request , List <Join > joinList , boolean isProcedure , Callback < T > callback ) throws Exception {
43434342 if (request == null ) { // User:{} 这种空内容在查询时也有效
43444343 throw new NullPointerException (TAG + ": newSQLConfig request == null!" );
43454344 }
@@ -4357,7 +4356,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
43574356 String schema = request .getString (KEY_SCHEMA );
43584357 String datasource = request .getString (KEY_DATASOURCE );
43594358
4360- SQLConfig config = callback .getSQLConfig (method , database , schema , table );
4359+ SQLConfig config = callback .getSQLConfig (method , database , schema , datasource , table );
43614360 config .setAlias (alias );
43624361
43634362 config .setDatabase (database ); //不删,后面表对象还要用的,必须放在 parseJoin 前
@@ -4404,7 +4403,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
44044403
44054404 Object id = request .get (idKey );
44064405 if (id == null && method == POST ) {
4407- id = callback .newId (method , database , schema , table ); // null 表示数据库自增 id
4406+ id = callback .newId (method , database , schema , datasource , table ); // null 表示数据库自增 id
44084407 }
44094408
44104409 if (id != null ) { //null无效
@@ -4992,7 +4991,7 @@ else if (newHaving != null) {
49924991 * @return
49934992 * @throws Exception
49944993 */
4995- public static SQLConfig parseJoin (RequestMethod method , SQLConfig config , List <Join > joinList , Callback callback ) throws Exception {
4994+ public static < T extends Object > SQLConfig parseJoin (RequestMethod method , SQLConfig config , List <Join > joinList , Callback < T > callback ) throws Exception {
49964995 boolean isQuery = RequestMethod .isQueryMethod (method );
49974996 config .setKeyPrefix (isQuery && config .isMain () == false );
49984997
@@ -5203,15 +5202,15 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
52035202 }
52045203
52055204
5206- public static interface IdCallback {
5205+ public static interface IdCallback < T extends Object > {
52075206 /**为 post 请求新建 id, 只能是 Long 或 String
52085207 * @param method
52095208 * @param database
52105209 * @param schema
52115210 * @param table
52125211 * @return
52135212 */
5214- Object newId (RequestMethod method , String database , String schema , String table );
5213+ T newId (RequestMethod method , String database , String schema , String datasource , String table );
52155214
52165215
52175216 /**获取主键名
@@ -5231,15 +5230,15 @@ public static interface IdCallback {
52315230 String getUserIdKey (String database , String schema , String datasource , String table );
52325231 }
52335232
5234- public static interface Callback extends IdCallback {
5233+ public static interface Callback < T extends Object > extends IdCallback < T > {
52355234 /**获取 SQLConfig 的实例
52365235 * @param method
52375236 * @param database
52385237 * @param schema
52395238 * @param table
52405239 * @return
52415240 */
5242- SQLConfig getSQLConfig (RequestMethod method , String database , String schema , String table );
5241+ SQLConfig getSQLConfig (RequestMethod method , String database , String schema , String datasource , String table );
52435242
52445243 /**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value
52455244 * @param combine
@@ -5249,12 +5248,23 @@ public static interface Callback extends IdCallback {
52495248 public void onMissingKey4Combine (String name , JSONObject request , String combine , String item , String key ) throws Exception ;
52505249 }
52515250
5252- public static abstract class SimpleCallback implements Callback {
5251+ public static Long LAST_ID ;
5252+ static {
5253+ LAST_ID = System .currentTimeMillis ();
5254+ }
52535255
5256+ public static abstract class SimpleCallback <T extends Object > implements Callback <T > {
52545257
5258+ @ SuppressWarnings ("unchecked" )
52555259 @ Override
5256- public Object newId (RequestMethod method , String database , String schema , String table ) {
5257- return System .currentTimeMillis ();
5260+ public T newId (RequestMethod method , String database , String schema , String datasource , String table ) {
5261+ Long id = System .currentTimeMillis ();
5262+ if (id <= LAST_ID ) {
5263+ id = LAST_ID + 1 ; // 解决高并发下 id 冲突导致新增记录失败
5264+ }
5265+ LAST_ID = id ;
5266+
5267+ return (T ) id ;
52585268 }
52595269
52605270 @ Override
0 commit comments