Skip to content

Commit 1e5e587

Browse files
committed
新增支持 @having:"match(arg0..)AGAINST(..)%2=1" 全文检索等函数后带数学表达式;对 key{}:">0;length(key)<=5" 新增支持部分为 RAW SQL;禁止 @having:"fun(arg0..):alias" 这样使用别名
1 parent 5340749 commit 1e5e587

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

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

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,18 +1230,12 @@ public String getHavingString(boolean hasPrefix) throws Exception {
12301230
return (hasPrefix ? " HAVING " : "") + StringUtil.concat(havingString, joinHaving, AND);
12311231
}
12321232

1233-
protected String getHavingItem(String quote, String table, String alias, String key, String expression, boolean containRaw) {
1233+
protected String getHavingItem(String quote, String table, String alias, String key, String expression, boolean containRaw) throws Exception {
12341234
//fun(arg0,arg1,...)
12351235
if (containRaw) {
1236-
try {
1237-
String rawSQL = getRawSQL(KEY_HAVING, expression);
1238-
if (rawSQL != null) {
1239-
return rawSQL;
1240-
}
1241-
} catch (Exception e) {
1242-
Log.e(TAG, "newSQLConfig rawColumnSQL == null >> try { "
1243-
+ " String rawSQL = ((AbstractSQLConfig) config).getRawSQL(KEY_COLUMN, fk); ... "
1244-
+ "} catch (Exception e) = " + e.getMessage());
1236+
String rawSQL = getRawSQL(KEY_HAVING, expression);
1237+
if (rawSQL != null) {
1238+
return rawSQL;
12451239
}
12461240
}
12471241

@@ -1460,6 +1454,17 @@ public SQLConfig setRaw(List<String> raw) {
14601454
*/
14611455
@Override
14621456
public String getRawSQL(String key, Object value) throws Exception {
1457+
return getRawSQL(key, value, false);
1458+
}
1459+
/**获取原始 SQL 片段
1460+
* @param key
1461+
* @param value
1462+
* @param throwWhenMissing
1463+
* @return
1464+
* @throws Exception
1465+
*/
1466+
@Override
1467+
public String getRawSQL(String key, Object value, boolean throwWhenMissing) throws Exception {
14631468
if (value == null) {
14641469
return null;
14651470
}
@@ -1474,11 +1479,12 @@ public String getRawSQL(String key, Object value) throws Exception {
14741479
String rawSQL = containRaw ? RAW_MAP.get(value) : null;
14751480
if (containRaw) {
14761481
if (rawSQL == null) {
1477-
throw new UnsupportedOperationException("@raw:value 的 value 中 " + key + " 不合法!"
1478-
+ "对应的 " + key + ":value 中 value 值 " + value + " 未在后端 RAW_MAP 中配置 !");
1482+
if (throwWhenMissing) {
1483+
throw new UnsupportedOperationException("@raw:value 的 value 中 " + key + " 不合法!"
1484+
+ "对应的 " + key + ":value 中 value 值 " + value + " 未在后端 RAW_MAP 中配置 !");
1485+
}
14791486
}
1480-
1481-
if (rawSQL.isEmpty()) {
1487+
else if (rawSQL.isEmpty()) {
14821488
return (String) value;
14831489
}
14841490
}
@@ -1824,14 +1830,14 @@ public String parseSQLExpression(String key, String expression, boolean containR
18241830
String s2 = expression.substring(keyIndex + 1); // OVER 后半部分
18251831

18261832
int index1 = s1.indexOf("("); // 函数 "(" 的起始位置
1827-
String fun = s1.substring(0, index1); // 函数名称
18281833
int end = s2.lastIndexOf(")"); // 后半部分 “)” 的位置
18291834

1830-
if (index1 >= end) {
1835+
if (index1 >= end + s1.length()) {
18311836
throw new IllegalArgumentException("字符 " + expression + " 不合法!"
18321837
+ key + ":value 中 value 里的 SQL 函数必须为 function(arg0,arg1,...) 这种格式!");
18331838
}
18341839

1840+
String fun = s1.substring(0, index1); // 函数名称
18351841
if (fun.isEmpty() == false) {
18361842
if (SQL_FUNCTION_MAP == null || SQL_FUNCTION_MAP.isEmpty()) {
18371843
if (StringUtil.isName(fun) == false) {
@@ -1851,11 +1857,26 @@ else if (SQL_FUNCTION_MAP.containsKey(fun) == false) {
18511857
int index2 = s2.indexOf("("); // 后半部分 “(”的起始位置
18521858
String argString2 = s2.substring(index2 + 1, end); // 后半部分的参数
18531859
// 别名
1854-
String alias = allowAlias == false || s2.lastIndexOf(":") < s2.lastIndexOf(")") ? null : s2.substring(s2.lastIndexOf(":") + 1);
1860+
int aliasIndex = allowAlias == false ? -1 : s2.lastIndexOf(":");
1861+
String alias = aliasIndex < 0 ? "" : s2.substring(aliasIndex + 1);
1862+
if (alias.isEmpty() == false && StringUtil.isName(alias) == false) {
1863+
throw new IllegalArgumentException("字符串 " + alias + " 不合法!预编译模式下 "
1864+
+ key + ":value 中 value里面用 ; 分割的每一项"
1865+
+ " function(arg0,arg1,...):alias 中 alias 必须是1个单词!并且不要有多余的空格!");
1866+
}
1867+
1868+
String suffix = s2.substring(end + 1, aliasIndex < 0 ? s2.length() : aliasIndex);
1869+
if (suffix.isEmpty() == false && (((String) suffix).contains("--") || ((String) suffix).contains("/*")
1870+
|| PATTERN_RANGE.matcher((String) suffix).matches() == false)) {
1871+
throw new UnsupportedOperationException("字符串 " + suffix + " 不合法!预编译模式下 " + key
1872+
+ ":\"column?value;function(arg0,arg1,...)?value...\""
1873+
+ " 中 ?value 必须符合正则表达式 " + PATTERN_RANGE + " 且不包含连续减号 -- 或注释符 /* !不允许多余的空格!");
1874+
}
1875+
18551876
// 获取后半部分的参数解析 (agr0 agr1 ...)
18561877
String argsString2[] = parseArgsSplitWithComma(argString2, false, containRaw, allowAlias);
18571878
expression = fun + "(" + StringUtil.getString(agrsString1) + (containOver ? ") OVER (" : ") AGAINST (") // 传参不传空格,拼接带空格
1858-
+ StringUtil.getString(argsString2) + ")" + (StringUtil.isEmpty(alias, true) ? "" : " AS " + quote + alias + quote); }
1879+
+ StringUtil.getString(argsString2) + ")" + suffix + (StringUtil.isEmpty(alias, true) ? "" : " AS " + quote + alias + quote); }
18591880
}
18601881

18611882
return expression;
@@ -3016,8 +3037,6 @@ protected String getWhereItem(String key, Object value, RequestMethod method, bo
30163037
throw new IllegalArgumentException(TAG + ".getWhereItem: 字符 " + key + " 不合法!");
30173038
}
30183039

3019-
// 原始 SQL 片段
3020-
String rawSQL = getRawSQL(key, value);
30213040

30223041
int keyType;
30233042
if (key.endsWith("$")) {
@@ -3054,6 +3073,9 @@ else if (key.endsWith("<")) {
30543073
}
30553074

30563075
String column = getRealKey(method, key, false, true, verifyName);
3076+
3077+
// 原始 SQL 片段
3078+
String rawSQL = getRawSQL(key, value, keyType != 4 || value instanceof String == false);
30573079

30583080
switch (keyType) {
30593081
case 1:
@@ -4747,15 +4769,9 @@ else if (whereList.contains(key)) {
47474769
boolean containColumnRaw = rawList != null && rawList.contains(KEY_COLUMN);
47484770
String rawColumnSQL = null;
47494771
if (containColumnRaw) {
4750-
try {
4751-
rawColumnSQL = config.getRawSQL(KEY_COLUMN, column);
4752-
if (rawColumnSQL != null) {
4753-
cs.add(rawColumnSQL);
4754-
}
4755-
} catch (Exception e) {
4756-
Log.e(TAG, "newSQLConfig config instanceof AbstractSQLConfig >> try { "
4757-
+ " rawColumnSQL = ((AbstractSQLConfig) config).getRawSQL(KEY_COLUMN, column); "
4758-
+ "} catch (Exception e) = " + e.getMessage());
4772+
rawColumnSQL = config.getRawSQL(KEY_COLUMN, column);
4773+
if (rawColumnSQL != null) {
4774+
cs.add(rawColumnSQL);
47594775
}
47604776
}
47614777

@@ -4766,16 +4782,10 @@ else if (whereList.contains(key)) {
47664782
String[] ks;
47674783
for (String fk : fks) {
47684784
if (containColumnRaw) {
4769-
try {
4770-
String rawSQL = config.getRawSQL(KEY_COLUMN, fk);
4771-
if (rawSQL != null) {
4772-
cs.add(rawSQL);
4773-
continue;
4774-
}
4775-
} catch (Exception e) {
4776-
Log.e(TAG, "newSQLConfig rawColumnSQL == null >> try { "
4777-
+ " String rawSQL = ((AbstractSQLConfig) config).getRawSQL(KEY_COLUMN, fk); ... "
4778-
+ "} catch (Exception e) = " + e.getMessage());
4785+
String rawSQL = config.getRawSQL(KEY_COLUMN, fk);
4786+
if (rawSQL != null) {
4787+
cs.add(rawSQL);
4788+
continue;
47794789
}
47804790
}
47814791

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public interface SQLConfig {
244244
String getWhereString(boolean hasPrefix) throws Exception;
245245

246246
String getRawSQL(String key, Object value) throws Exception;
247+
String getRawSQL(String key, Object value, boolean throwWhenMissing) throws Exception;
247248

248249
boolean isKeyPrefix();
249250

0 commit comments

Comments
 (0)