@@ -103,22 +103,21 @@ public abstract class AbstractSQLConfig implements SQLConfig {
103
103
public static String PREFFIX_DISTINCT = "DISTINCT " ;
104
104
105
105
// * 和 / 不能同时出现,防止 /* */ 段注释! # 和 -- 不能出现,防止行注释! ; 不能出现,防止隔断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 ;
108
108
109
109
/**
110
110
* 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
111
111
*/
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 ;
115
115
116
116
// 自定义原始 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 ;
118
118
// 允许调用的 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 ;
122
121
123
122
static { // 凡是 SQL 边界符、分隔符、注释符 都不允许,例如 ' " ` ( ) ; # -- /**/ ,以免拼接 SQL 时被注入意外可执行指令
124
123
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
4339
4338
* @return
4340
4339
* @throws Exception
4341
4340
*/
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 {
4343
4342
if (request == null ) { // User:{} 这种空内容在查询时也有效
4344
4343
throw new NullPointerException (TAG + ": newSQLConfig request == null!" );
4345
4344
}
@@ -4357,7 +4356,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
4357
4356
String schema = request .getString (KEY_SCHEMA );
4358
4357
String datasource = request .getString (KEY_DATASOURCE );
4359
4358
4360
- SQLConfig config = callback .getSQLConfig (method , database , schema , table );
4359
+ SQLConfig config = callback .getSQLConfig (method , database , schema , datasource , table );
4361
4360
config .setAlias (alias );
4362
4361
4363
4362
config .setDatabase (database ); //不删,后面表对象还要用的,必须放在 parseJoin 前
@@ -4404,7 +4403,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
4404
4403
4405
4404
Object id = request .get (idKey );
4406
4405
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
4408
4407
}
4409
4408
4410
4409
if (id != null ) { //null无效
@@ -4992,7 +4991,7 @@ else if (newHaving != null) {
4992
4991
* @return
4993
4992
* @throws Exception
4994
4993
*/
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 {
4996
4995
boolean isQuery = RequestMethod .isQueryMethod (method );
4997
4996
config .setKeyPrefix (isQuery && config .isMain () == false );
4998
4997
@@ -5203,15 +5202,15 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
5203
5202
}
5204
5203
5205
5204
5206
- public static interface IdCallback {
5205
+ public static interface IdCallback < T extends Object > {
5207
5206
/**为 post 请求新建 id, 只能是 Long 或 String
5208
5207
* @param method
5209
5208
* @param database
5210
5209
* @param schema
5211
5210
* @param table
5212
5211
* @return
5213
5212
*/
5214
- Object newId (RequestMethod method , String database , String schema , String table );
5213
+ T newId (RequestMethod method , String database , String schema , String datasource , String table );
5215
5214
5216
5215
5217
5216
/**获取主键名
@@ -5231,15 +5230,15 @@ public static interface IdCallback {
5231
5230
String getUserIdKey (String database , String schema , String datasource , String table );
5232
5231
}
5233
5232
5234
- public static interface Callback extends IdCallback {
5233
+ public static interface Callback < T extends Object > extends IdCallback < T > {
5235
5234
/**获取 SQLConfig 的实例
5236
5235
* @param method
5237
5236
* @param database
5238
5237
* @param schema
5239
5238
* @param table
5240
5239
* @return
5241
5240
*/
5242
- SQLConfig getSQLConfig (RequestMethod method , String database , String schema , String table );
5241
+ SQLConfig getSQLConfig (RequestMethod method , String database , String schema , String datasource , String table );
5243
5242
5244
5243
/**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value
5245
5244
* @param combine
@@ -5249,12 +5248,23 @@ public static interface Callback extends IdCallback {
5249
5248
public void onMissingKey4Combine (String name , JSONObject request , String combine , String item , String key ) throws Exception ;
5250
5249
}
5251
5250
5252
- public static abstract class SimpleCallback implements Callback {
5251
+ public static Long LAST_ID ;
5252
+ static {
5253
+ LAST_ID = System .currentTimeMillis ();
5254
+ }
5253
5255
5256
+ public static abstract class SimpleCallback <T extends Object > implements Callback <T > {
5254
5257
5258
+ @ SuppressWarnings ("unchecked" )
5255
5259
@ 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 ;
5258
5268
}
5259
5269
5260
5270
@ Override
0 commit comments