Skip to content

Commit 7628857

Browse files
authored
Merge pull request Tencent#126 from zhoulingfengofcd/master
增加row关键字、解决PUT方法的BUG
2 parents 0109acc + cd817ff commit 7628857

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoSQLConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public String getUserIdKey(String database, String schema, String table) {
7373
// return null; // return null 则不生成 id,一般用于数据库自增 id
7474
// }
7575
};
76+
77+
// 自定义where条件拼接
78+
//RAW_MAP.put("commentWhereItem1","`Comment`.`userId` = 38710 and `Comment`.`momentId` = 470");
79+
//RAW_MAP.put("commentWhereItem2","`Comment`.`toId` = 0");
7680
}
7781

7882

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: 34 additions & 3 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 {
@@ -2041,7 +2070,7 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
20412070
String quote = getQuote();
20422071

20432072
boolean isFirst = true;
2044-
int keyType = 0;// 0 - =; 1 - +, 2 - -
2073+
int keyType;// 0 - =; 1 - +, 2 - -
20452074
Object value;
20462075

20472076
String idKey = getIdKey();
@@ -2055,6 +2084,8 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
20552084
keyType = 1;
20562085
} else if (key.endsWith("-")) {
20572086
keyType = 2;
2087+
} else {
2088+
keyType = 0; //注意重置类型,不然不该加减的字段会跟着加减
20582089
}
20592090
value = content.get(key);
20602091
key = getRealKey(method, key, false, true, verifyName, quote);

0 commit comments

Comments
 (0)