Skip to content

Commit 1edf5e4

Browse files
committed
Server:支持String类型的id
1 parent 295fc0a commit 1edf5e4

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
8181

8282

8383

84-
private long id; //Table的id
84+
private Object id; //Table的id
8585
private RequestMethod method; //操作方法
8686
private boolean prepared = true; //预编译
8787
private boolean main = true;
@@ -161,11 +161,11 @@ public AbstractSQLConfig setMain(boolean main) {
161161

162162

163163
@Override
164-
public long getId() {
164+
public Object getId() {
165165
return id;
166166
}
167167
@Override
168-
public AbstractSQLConfig setId(long id) {
168+
public AbstractSQLConfig setId(Object id) {
169169
this.id = id;
170170
return this;
171171
}
@@ -1739,7 +1739,7 @@ public String getJoinString() throws Exception {
17391739
return joinOns;
17401740
}
17411741

1742-
/**获取查询配置
1742+
/**新建SQL配置
17431743
* @param table
17441744
* @param request
17451745
* @return
@@ -1772,14 +1772,24 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
17721772
}
17731773

17741774
//对id和id{}处理,这两个一定会作为条件
1775-
Long id = request.getLong(KEY_ID);
1775+
Object id = request.get(KEY_ID);
17761776
if (id != null) { //null无效
1777-
if (id <= 0) { //一定没有值
1778-
throw new NotExistException(TAG + ": newSQLConfig " + table + ".id <= 0");
1777+
if (id instanceof Number) {
1778+
if (((Number) id).longValue() <= 0) { //一定没有值
1779+
throw new NotExistException(TAG + ": newSQLConfig " + table + ".id <= 0");
1780+
}
1781+
}
1782+
else if (id instanceof String) {
1783+
if (StringUtil.isEmpty(id, true)) { //一定没有值
1784+
throw new NotExistException(TAG + ": newSQLConfig StringUtil.isEmpty(" + table + ".id, true)");
1785+
}
1786+
}
1787+
else {
1788+
throw new IllegalArgumentException(KEY_ID + ":value 中 value 的类型只能是 Long 或 String !");
17791789
}
17801790

1781-
if (idIn != null && idIn instanceof List) { //共用idArr场景少性能差
1782-
if (idIn != null && ((List<?>) idIn).contains(id) == false) {//empty有效 BaseModel.isEmpty(idArr) == false) {
1791+
if (idIn != null && idIn instanceof List) { //共用idIn场景少性能差
1792+
if (idIn != null && ((List<?>) idIn).contains(id) == false) {//empty有效 BaseModel.isEmpty(idIn) == false) {
17831793
Log.w(TAG, "newSQLConfig id > 0 >> idInObj != null && idInObj.contains(id) == false >> return null;");
17841794
throw new NotExistException(TAG + ": newSQLConfig idIn != null && ((JSONArray) idIn).contains(id) == false");
17851795
}
@@ -1983,7 +1993,7 @@ else if (whereList != null && whereList.contains(key)) {
19831993
config.setColumn(cs);
19841994
config.setWhere(tableWhere);
19851995

1986-
config.setId(id == null ? 0 : id);
1996+
config.setId(id);
19871997
//在 tableWhere 第0个 config.setIdIn(idIn);
19881998

19891999
config.setRole(role);

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public JSONObject execute(SQLConfig config) throws Exception {
161161
, updateCount > 0 ? JSONResponse.MSG_SUCCEED : "没权限访问或对象不存在!");
162162

163163
//id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
164-
if (config.getId() > 0) {
164+
if (config.getId() != null) {
165165
result.put(JSONResponse.KEY_ID, config.getId());
166166
} else {
167167
result.put(JSONResponse.KEY_ID + "[]", config.getWhere(JSONResponse.KEY_ID_IN, true));

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/SQLConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public interface SQLConfig {
8787
RequestMethod getMethod();
8888
SQLConfig setMethod(RequestMethod method);
8989

90-
long getId();
91-
SQLConfig setId(long id);
90+
Object getId();
91+
SQLConfig setId(Object id);
9292

9393
RequestRole getRole();
9494
SQLConfig setRole(RequestRole role);

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
142142
private static void verifyId(@NotNull String method, @NotNull String name, @NotNull String key
143143
, @NotNull JSONObject robj, @NotNull String idKey, final int maxUpdateCount, boolean atLeastOne) {
144144
//单个修改或删除
145-
Object id = null;
146-
try {
147-
id = robj.getLong(idKey); //如果必须传 id ,可在Request表中配置NECESSARY
148-
} catch (Exception e) {
145+
Object id = robj.get(idKey); //如果必须传 id ,可在Request表中配置NECESSARY
146+
if (id != null && id instanceof Number == false && id instanceof String == false) {
149147
throw new IllegalArgumentException(method + "请求," + name + "/" + key
150-
+ " 里面的 " + idKey + ":value 中value的类型只能是 Long !");
148+
+ " 里面的 " + idKey + ":value 中value的类型只能是 Long 或 String !");
151149
}
150+
152151

153152
//批量修改或删除
154153
String idInKey = idKey + "{}";
@@ -173,11 +172,10 @@ private static void verifyId(@NotNull String method, @NotNull String name, @NotN
173172
//解决 id{}: ["1' OR 1='1'))--"] 绕过id{}限制
174173
//new ArrayList<Long>(idIn) 不能检查类型,Java泛型擦除问题,居然能把 ["a"] 赋值进去还不报错
175174
for (int i = 0; i < idIn.size(); i++) {
176-
try {
177-
idIn.getLong(i);
178-
} catch (Exception e) {
175+
Object o = idIn.get(i);
176+
if (o != null && o instanceof Number == false && o instanceof String == false) {
179177
throw new IllegalArgumentException(method + "请求," + name + "/" + key
180-
+ " 里面的 " + idInKey + ":[] 中所有项的类型都只能是Long!");
178+
+ " 里面的 " + idInKey + ":[] 中所有项的类型都只能是 Long 或 String !");
181179
}
182180
}
183181
}

0 commit comments

Comments
 (0)