Skip to content

Commit 92a9cf1

Browse files
增加了@raw关键字,支持自定义where条件拼接
1 parent a5c895e commit 92a9cf1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public JSONObject setUserIdIn(List<Object> list) {
155155
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
156156
public static final String KEY_ORDER = "@order"; //排序方式
157157
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
158+
public static final String KEY_RAW = "@raw"; //自定义where条件拼接
158159

159160
public static final List<String> TABLE_KEY_LIST;
160161
static {
@@ -171,6 +172,7 @@ public JSONObject setUserIdIn(List<Object> list) {
171172
TABLE_KEY_LIST.add(KEY_HAVING);
172173
TABLE_KEY_LIST.add(KEY_ORDER);
173174
TABLE_KEY_LIST.add(KEY_JSON);
175+
TABLE_KEY_LIST.add(KEY_RAW);
174176
}
175177

176178
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public abstract class AbstractSQLConfig implements SQLConfig {
8686
*/
8787
public static final Map<String, String> TABLE_KEY_MAP;
8888
public static final List<String> DATABASE_LIST;
89+
// 自定义where条件拼接
90+
public static final Map<String, String> RAW_MAP;
8991
static {
9092
TABLE_KEY_MAP = new HashMap<String, String>();
9193
TABLE_KEY_MAP.put(Table.class.getSimpleName(), Table.TABLE_NAME);
@@ -101,6 +103,8 @@ public abstract class AbstractSQLConfig implements SQLConfig {
101103
DATABASE_LIST.add(DATABASE_POSTGRESQL);
102104
DATABASE_LIST.add(DATABASE_SQLSERVER);
103105
DATABASE_LIST.add(DATABASE_ORACLE);
106+
107+
RAW_MAP = new HashMap<>();
104108
}
105109

106110
@Override
@@ -1442,7 +1446,10 @@ private String getWhereItem(String key, Object value
14421446
, RequestMethod method, boolean verifyName) throws Exception {
14431447
Log.d(TAG, "getWhereItem key = " + key);
14441448
//避免筛选到全部 value = key == null ? null : where.get(key);
1445-
if (key == null || value == null || key.startsWith("@") || key.endsWith("()")) {//关键字||方法, +或-直接报错
1449+
if(key.equals("@raw")){
1450+
Log.d(TAG, "getWhereItem key startsWith @ = @raw ");
1451+
// 自定义where条件拼接,直接通过,放行
1452+
}else if (key == null || value == null || key.startsWith("@") || key.endsWith("()")) {//关键字||方法, +或-直接报错
14461453
Log.d(TAG, "getWhereItem key == null || value == null"
14471454
+ " || key.startsWith(@) || key.endsWith(()) >> continue;");
14481455
return null;
@@ -1483,7 +1490,9 @@ else if (key.endsWith(">")) {
14831490
else if (key.endsWith("<")) {
14841491
keyType = 10;
14851492
}
1486-
else { //else绝对不能省,避免再次踩坑! keyType = 0; 写在for循环外面都没注意!
1493+
else if (key.startsWith("@")) {
1494+
keyType = 11;
1495+
} else { //else绝对不能省,避免再次踩坑! keyType = 0; 写在for循环外面都没注意!
14871496
keyType = 0;
14881497
}
14891498
key = getRealKey(method, key, false, true, verifyName, getQuote());
@@ -1510,11 +1519,31 @@ else if (key.endsWith("<")) {
15101519
return getCompareString(key, value, ">");
15111520
case 10:
15121521
return getCompareString(key, value, "<");
1522+
case 11:
1523+
return getRaw(key,value);
15131524
default: //TODO MySQL JSON类型的字段对比 key='[]' 会无结果! key LIKE '[1, 2, 3]' //TODO MySQL , 后面有空格!
15141525
return getEqualString(key, value);
15151526
}
15161527
}
15171528

1529+
@JSONField(serialize = false)
1530+
public String getRaw(String key, Object value) throws Exception {
1531+
if (JSON.isBooleanOrNumberOrString(value) == false && value instanceof Subquery == false) {
1532+
throw new IllegalArgumentException(key + ":value 中value不合法!非PUT请求只支持 [Boolean, Number, String] 内的类型 !");
1533+
}
1534+
1535+
String[] rawList = ((String)value).split(",");
1536+
String whereItem = "";
1537+
for (int i = 0; i < rawList.length; i++) {
1538+
if(rawList.length>1&& i!=0){
1539+
whereItem += " and " + RAW_MAP.get(rawList[i]);
1540+
}else{
1541+
whereItem += RAW_MAP.get(rawList[i]);
1542+
}
1543+
}
1544+
1545+
return whereItem;
1546+
}
15181547

15191548
@JSONField(serialize = false)
15201549
public String getEqualString(String key, Object value) throws Exception {

0 commit comments

Comments
 (0)