Skip to content

Commit 247f149

Browse files
committed
新增支持在 @column: value 中传自定义表名,例如 Comment.toId, Moment.userId 等
1 parent f400f09 commit 247f149

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,7 @@ else if (SQL_FUNCTION_MAP.containsKey(fun) == false) {
21202120
private String[] parseArgsSplitWithComma(String param, boolean isColumn, boolean containRaw, boolean allowAlias) {
21212121
// 以"," 分割参数
21222122
String quote = getQuote();
2123+
boolean isKeyPrefix = isKeyPrefix();
21232124
String tableAlias = getAliasWithQuote();
21242125
String ckeys[] = StringUtil.split(param); // 以","分割参数
21252126
if (ckeys != null && ckeys.length > 0) {
@@ -2190,28 +2191,41 @@ else if ("!=null".equals(ck)) {
21902191
// 以空格分割参数
21912192
String[] mkes = containRaw ? StringUtil.split(ck, " ", true) : new String[]{ ck };
21922193

2193-
//如果参数中含有空格(少数情况) 比如 fun(arg1 arg2 arg3 ,arg4) 中的 arg1 arg2 arg3,比如 DISTINCT id
2194+
//如果参数中含有空格(少数情况) 比如 fun(arg1, arg2,arg3,arg4) 中的 arg1 arg2 arg3,比如 DISTINCT id
21942195
if (mkes != null && mkes.length >= 2) {
2195-
origin = praseArgsSplitWithSpace(mkes);
2196+
origin = parseArgsSplitWithSpace(mkes);
21962197
} else {
2197-
boolean isName = false;
2198-
21992198
String mk = RAW_MAP.get(origin);
22002199
if (mk != null) { // newSQLConfig 提前处理好的
22012200
if (mk.length() > 0) {
22022201
origin = mk;
22032202
}
22042203
} else if (StringUtil.isNumer(origin)) {
22052204
//do nothing
2206-
} else if (StringUtil.isName(origin)) {
2207-
origin = quote + origin + quote;
2208-
isName = true;
22092205
} else {
2210-
origin = getValue(origin).toString();
2211-
}
2206+
String[] keys = origin.split("[.]");
2207+
StringBuilder sb = new StringBuilder();
2208+
2209+
int len = keys == null ? 0 : keys.length;
2210+
if (len > 0) {
2211+
boolean first = true;
2212+
for (String k : keys) {
2213+
if (StringUtil.isName(k) == false) {
2214+
sb = null;
2215+
break;
2216+
}
2217+
2218+
sb.append(first ? "" : ".").append(quote).append(k).append(quote);
2219+
first = false;
2220+
}
2221+
}
22122222

2213-
if (isName && isKeyPrefix()) {
2214-
origin = tableAlias + "." + origin;
2223+
String s = sb == null ? null : sb.toString();
2224+
if (StringUtil.isNotEmpty(s, true)) {
2225+
origin = (len == 1 && isKeyPrefix ? tableAlias + "." : "") + s;
2226+
} else {
2227+
origin = getValue(origin).toString();
2228+
}
22152229
}
22162230

22172231
if (isColumn && StringUtil.isEmpty(alias, true) == false) {
@@ -2235,8 +2249,9 @@ else if ("!=null".equals(ck)) {
22352249
* @param mkes
22362250
* @return
22372251
*/
2238-
private String praseArgsSplitWithSpace(String mkes[]) {
2252+
private String parseArgsSplitWithSpace(String mkes[]) {
22392253
String quote = getQuote();
2254+
boolean isKeyPrefix = isKeyPrefix();
22402255
String tableAlias = getAliasWithQuote();
22412256

22422257
// 包含空格的参数 肯定不包含别名 不用处理别名
@@ -2264,7 +2279,7 @@ private String praseArgsSplitWithSpace(String mkes[]) {
22642279
+ " 中所有字符串 column 都必须必须为1个单词 !");
22652280
}
22662281

2267-
mkes[j] = getKey(origin).toString();
2282+
mkes[j] = getKey(origin);
22682283
continue;
22692284
}
22702285
else if (ck.startsWith("'") && ck.endsWith("'")) {
@@ -2285,18 +2300,32 @@ else if (ck.contains("`") || ck.contains("'") || origin.startsWith("_") || origi
22852300
+ " 中所有 arg 都必须是1个不以 _ 开头的单词 或者符合正则表达式 " + PATTERN_FUNCTION + " 且不包含连续减号 -- !DISTINCT 必须全大写,且后面必须有且只有 1 个空格!其它情况不允许空格!");
22862301
}
22872302

2288-
boolean isName = false;
22892303
if (StringUtil.isNumer(origin)) {
22902304
//do nothing
2291-
} else if (StringUtil.isName(origin)) {
2292-
origin = quote + origin + quote;
2293-
isName = true;
22942305
} else {
2295-
origin = getValue(origin).toString();
2296-
}
2306+
String[] keys = origin.split("[.]");
2307+
StringBuilder sb = new StringBuilder();
2308+
2309+
int len = keys == null ? 0 : keys.length;
2310+
if (len > 0) {
2311+
boolean first = true;
2312+
for (String k : keys) {
2313+
if (StringUtil.isName(k) == false) {
2314+
sb = null;
2315+
break;
2316+
}
22972317

2298-
if (isName && isKeyPrefix()) {
2299-
origin = tableAlias + "." + origin;
2318+
sb.append(first ? "" : ".").append(quote).append(k).append(quote);
2319+
first = false;
2320+
}
2321+
}
2322+
2323+
String s = sb == null ? null : sb.toString();
2324+
if (StringUtil.isNotEmpty(s, true)) {
2325+
origin = (len == 1 && isKeyPrefix ? tableAlias + "." : "") + s;
2326+
} else {
2327+
origin = getValue(origin).toString();
2328+
}
23002329
}
23012330

23022331
mkes[j] = origin;
@@ -2894,13 +2923,13 @@ protected String parseCombineExpression(RequestMethod method, String quote, Stri
28942923
} else {
28952924
wi = isHaving ? getHavingItem(quote, table, alias, column, (String) value, containRaw) : getWhereItem(column, value, method, verifyName);
28962925
}
2897-
2926+
28982927
if (1.0f*allCount/size > maxCombineRatio && maxCombineRatio > 0) {
28992928
throw new IllegalArgumentException(errPrefix + " 中字符 '" + s + "' 不合法!"
29002929
+ "其中 key 数量 " + allCount + " / 条件键值对数量 " + size + " = " + (1.0f*allCount/size)
29012930
+ " 已超过 最大倍数,必须在条件键值对数量 0-" + maxCombineRatio + " 倍内!");
29022931
}
2903-
2932+
29042933
if (StringUtil.isEmpty(wi, true)) { // 转成 1=1 ?
29052934
throw new IllegalArgumentException(errPrefix + " 中字符 '" + key
29062935
+ "' 对应的 " + column + ":value 不是有效条件键值对!");
@@ -5127,7 +5156,7 @@ else if (userId instanceof Subquery) {}
51275156
if(StringUtil.equals(table, from.getConfig().getTable())) {
51285157
isFakeDelete = false;
51295158
}
5130-
5159+
51315160
if(from.getConfig().getJoinList() != null) {
51325161
for(Join join : from.getConfig().getJoinList()) {
51335162
if(StringUtil.equals(table, join.getTable())) {
@@ -5144,9 +5173,9 @@ else if (userId instanceof Subquery) {}
51445173
whereList.addAll(fakeDeleteMap.keySet());
51455174
}
51465175
}
5147-
}
5176+
}
51485177
}
5149-
5178+
51505179
if (StringUtil.isNotEmpty(combineExpr, true)) {
51515180
List<String> banKeyList = Arrays.asList(idKey, idInKey, userIdKey, userIdInKey);
51525181
for (String key : banKeyList) {
@@ -5206,7 +5235,7 @@ else if (w.startsWith("!")) {
52065235
}
52075236

52085237
//条件>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5209-
5238+
52105239
Map<String, Object> tableContent = new LinkedHashMap<String, Object>();
52115240
Object value;
52125241
for (String key : set) {
@@ -5252,7 +5281,7 @@ else if (w.startsWith("!")) {
52525281
fakeDeleteMap.put(accessFakeDeleteMap.get("deletedKey").toString(), accessFakeDeleteMap.get("deletedValue"));
52535282
config.setMethod(PUT);
52545283
config.setContent(fakeDeleteMap);
5255-
}
5284+
}
52565285
}
52575286
}
52585287

0 commit comments

Comments
 (0)