68
68
import static apijson .JSONObject .KEY_USER_ID ;
69
69
import static apijson .RequestMethod .DELETE ;
70
70
import static apijson .RequestMethod .GET ;
71
- import static apijson .RequestMethod .GETS ;
72
- import static apijson .RequestMethod .HEADS ;
73
71
import static apijson .RequestMethod .POST ;
74
72
import static apijson .RequestMethod .PUT ;
75
73
import static apijson .JSONObject .KEY_METHOD ;
@@ -171,7 +169,8 @@ public abstract class AbstractSQLConfig implements SQLConfig {
171
169
DATABASE_LIST .add (DATABASE_TRINO );
172
170
DATABASE_LIST .add (DATABASE_INFLUXDB );
173
171
DATABASE_LIST .add (DATABASE_TDENGINE );
174
-
172
+ DATABASE_LIST .add (DATABASE_REDIS );
173
+ DATABASE_LIST .add (DATABASE_MQ );
175
174
176
175
RAW_MAP = new LinkedHashMap <>(); // 保证顺序,避免配置冲突等意外情况
177
176
@@ -1116,6 +1115,21 @@ public static boolean isTDengine(String db) {
1116
1115
return DATABASE_TDENGINE .equals (db );
1117
1116
}
1118
1117
1118
+ @ Override
1119
+ public boolean isRedis () {
1120
+ return isRedis (getSQLDatabase ());
1121
+ }
1122
+ public static boolean isRedis (String db ) {
1123
+ return DATABASE_REDIS .equals (db );
1124
+ }
1125
+
1126
+ @ Override
1127
+ public boolean isMQ () {
1128
+ return isMQ (getSQLDatabase ());
1129
+ }
1130
+ public static boolean isMQ (String db ) {
1131
+ return DATABASE_MQ .equals (db );
1132
+ }
1119
1133
1120
1134
@ Override
1121
1135
public String getQuote () {
@@ -4956,10 +4970,6 @@ else if (userId instanceof Subquery) {}
4956
4970
4957
4971
//条件<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
4958
4972
String [] ws = StringUtil .split (combine );
4959
- if (ws != null && (method == DELETE || method == GETS || method == HEADS )) {
4960
- throw new IllegalArgumentException (table + ":{} 里的 @combine:value 不合法!DELETE,GETS,HEADS 请求不允许传 @combine:value !" );
4961
- }
4962
-
4963
4973
String combineExpr = ws == null || ws .length != 1 ? null : ws [0 ];
4964
4974
4965
4975
Map <String , List <String >> combineMap = new LinkedHashMap <>();
@@ -4991,28 +5001,44 @@ else if (userId instanceof Subquery) {}
4991
5001
whereList .add (userIdInKey );
4992
5002
}
4993
5003
5004
+ if (config .isFakeDelete ()) {
5005
+ // 查询Access假删除
5006
+ Map <String , Object > accessFakeDeleteMap = AbstractVerifier .ACCESS_FAKE_DELETE_MAP .get (config .getTable ());
5007
+ if (StringUtil .isNotEmpty (accessFakeDeleteMap .get ("deletedKey" ), true )) {
5008
+ Map <String , Object > fakeDeleteMap = new HashMap <>();
5009
+ boolean isFakeDelete = true ;
5010
+ if (method != DELETE ) {
5011
+ if (from != null ) {
5012
+ // 兼容 join 外层select 重复生成deletedKey
5013
+ if (StringUtil .equals (table , from .getConfig ().getTable ())) {
5014
+ isFakeDelete = false ;
5015
+ }
5016
+
5017
+ if (from .getConfig ().getJoinList () != null ) {
5018
+ for (Join join : from .getConfig ().getJoinList ()) {
5019
+ if (StringUtil .equals (table , join .getTable ())) {
5020
+ isFakeDelete = false ;
5021
+ break ;
5022
+ }
5023
+ }
5024
+ }
5025
+ }
5026
+ if (isFakeDelete ) {
5027
+ fakeDeleteMap .put (accessFakeDeleteMap .get ("deletedKey" ).toString ()+"!" , accessFakeDeleteMap .get ("deletedValue" ));
5028
+ tableWhere .putAll (fakeDeleteMap );
5029
+ andList .addAll (fakeDeleteMap .keySet ());
5030
+ whereList .addAll (fakeDeleteMap .keySet ());
5031
+ }
5032
+ }
5033
+ }
5034
+ }
5035
+
4994
5036
if (StringUtil .isNotEmpty (combineExpr , true )) {
4995
5037
List <String > banKeyList = Arrays .asList (idKey , idInKey , userIdKey , userIdInKey );
4996
5038
for (String key : banKeyList ) {
4997
- String str = combineExpr ;
4998
- while (str .isEmpty () == false ) {
4999
- int index = str .indexOf (key );
5000
- if (index < 0 ) {
5001
- break ;
5002
- }
5003
-
5004
- char left = index <= 0 ? ' ' : str .charAt (index - 1 );
5005
- char right = index >= str .length () - key .length () ? ' ' : str .charAt (index + key .length ());
5006
- if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!' ) && (right == ' ' || right == ')' )) {
5007
- throw new UnsupportedOperationException (table + ":{} 里的 @combine:value 中的 value 里 " + key + " 不合法!"
5008
- + "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!" );
5009
- }
5010
-
5011
- int newIndex = index + key .length () + 1 ;
5012
- if (str .length () <= newIndex ) {
5013
- break ;
5014
- }
5015
- str = str .substring (newIndex );
5039
+ if (keyInCombineExpr (combineExpr , key )) {
5040
+ throw new UnsupportedOperationException (table + ":{} 里的 @combine:value 中的 value 里 " + key + " 不合法!"
5041
+ + "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!" );
5016
5042
}
5017
5043
}
5018
5044
}
@@ -5066,7 +5092,7 @@ else if (w.startsWith("!")) {
5066
5092
}
5067
5093
5068
5094
//条件>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5069
-
5095
+
5070
5096
Map <String , Object > tableContent = new LinkedHashMap <String , Object >();
5071
5097
Object value ;
5072
5098
for (String key : set ) {
@@ -5076,18 +5102,17 @@ else if (w.startsWith("!")) {
5076
5102
throw new IllegalArgumentException (table + ":{ " + key + ":value } 中 value 类型错误!除了 key<>:{} 外,不允许 " + key + " 等其它任何 key 对应 value 的类型为 JSONObject {} !" );
5077
5103
}
5078
5104
5105
+ //兼容 PUT @combine
5079
5106
//解决AccessVerifier新增userId没有作为条件,而是作为内容,导致PUT,DELETE出错
5080
- if (isWhere || (StringUtil .isName (key .replaceFirst ("[+-]$" , "" )) == false )) {
5107
+ if (( isWhere || (StringUtil .isName (key .replaceFirst ("[+-]$" , "" )) == false )) || ( isWhere == false && StringUtil . isNotEmpty ( combineExpr , true ) && keyInCombineExpr ( combineExpr , key ) )) {
5081
5108
tableWhere .put (key , value );
5082
5109
if (whereList .contains (key ) == false ) {
5083
5110
andList .add (key );
5084
5111
}
5085
- }
5086
- else if (whereList .contains (key )) {
5112
+ } else if (whereList .contains (key )) {
5087
5113
tableWhere .put (key , value );
5088
- }
5089
- else {
5090
- tableContent .put (key , value ); //一样 instanceof JSONArray ? JSON.toJSONString(value) : value);
5114
+ } else {
5115
+ tableContent .put (key , value ); //一样 instanceof JSONArray ? JSON.toJSONString(value) : value);
5091
5116
}
5092
5117
}
5093
5118
@@ -5102,6 +5127,20 @@ else if (whereList.contains(key)) {
5102
5127
config .setContent (tableContent );
5103
5128
}
5104
5129
5130
+ if (method == DELETE ) {
5131
+ if (config .isFakeDelete ()) {
5132
+ //查询Access假删除
5133
+ Map <String , Object > accessFakeDeleteMap = AbstractVerifier .ACCESS_FAKE_DELETE_MAP .get (config .getTable ());
5134
+ if (StringUtil .isNotEmpty (accessFakeDeleteMap .get ("deletedKey" ), true )) {
5135
+ //假删除需要更新的其他字段,比如:删除时间 deletedTime 之类的
5136
+ Map <String , Object > fakeDeleteMap = new HashMap <>();
5137
+ config .onFakeDelete (fakeDeleteMap );
5138
+ fakeDeleteMap .put (accessFakeDeleteMap .get ("deletedKey" ).toString (), accessFakeDeleteMap .get ("deletedValue" ));
5139
+ config .setMethod (PUT );
5140
+ config .setContent (fakeDeleteMap );
5141
+ }
5142
+ }
5143
+ }
5105
5144
5106
5145
List <String > cs = new ArrayList <>();
5107
5146
@@ -5494,14 +5533,12 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
5494
5533
5495
5534
//TODO if (key.endsWith("-")) { // 表示 key 和 value 顺序反过来: value LIKE key
5496
5535
5497
- String last = null ;//不用Logic优化代码,否则 key 可能变为 key| 导致 key=value 变成 key|=value 而出错
5498
- if (RequestMethod .isQueryMethod (method )) {//逻辑运算符仅供GET,HEAD方法使用
5499
- last = key .isEmpty () ? "" : key .substring (key .length () - 1 );
5500
- if ("&" .equals (last ) || "|" .equals (last ) || "!" .equals (last )) {
5501
- key = key .substring (0 , key .length () - 1 );
5502
- } else {
5503
- last = null ;//避免key + StringUtil.getString(last)错误延长
5504
- }
5536
+ //不用Logic优化代码,否则 key 可能变为 key| 导致 key=value 变成 key|=value 而出错
5537
+ String last = key .isEmpty () ? "" : key .substring (key .length () - 1 );
5538
+ if ("&" .equals (last ) || "|" .equals (last ) || "!" .equals (last )) {
5539
+ key = key .substring (0 , key .length () - 1 );
5540
+ } else {
5541
+ last = null ;//避免key + StringUtil.getString(last)错误延长
5505
5542
}
5506
5543
5507
5544
//"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好
@@ -5606,6 +5643,27 @@ public void onMissingKey4Combine(String name, JSONObject request, String combine
5606
5643
5607
5644
}
5608
5645
5646
+ private static boolean keyInCombineExpr (String combineExpr , String key ) {
5647
+ while (combineExpr .isEmpty () == false ) {
5648
+ int index = combineExpr .indexOf (key );
5649
+ if (index < 0 ) {
5650
+ return false ;
5651
+ }
5652
+
5653
+ char left = index <= 0 ? ' ' : combineExpr .charAt (index - 1 );
5654
+ char right = index >= combineExpr .length () - key .length () ? ' ' : combineExpr .charAt (index + key .length ());
5655
+ if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!' ) && (right == ' ' || right == ')' )) {
5656
+ return true ;
5657
+ }
5658
+ int newIndex = index + key .length () + 1 ;
5659
+ if (combineExpr .length () <= newIndex ) {
5660
+ break ;
5661
+ }
5662
+ combineExpr = combineExpr .substring (newIndex );
5663
+ }
5664
+ return false ;
5665
+ }
5666
+
5609
5667
private void setWithAsExpreList () {
5610
5668
// mysql8版本以上,子查询支持with as表达式
5611
5669
if (this .isMySQL () && this .getDBVersionNums ()[0 ] >= 8 ) {
0 commit comments