Skip to content

Commit 7680d5e

Browse files
committed
Structure新增支持通过id{}:[]修改和删除多条记录;JSONObject新增@Try,@drop,@Correct,@about等关键词;Structure新增支持校验 key?:"正则",key{}:"条件" ;新增支持查询数据库表属性(Table)及表字段属性(Column);完善及优化SQL;解决SQLExecutor中部分String类型被改变;日期时间在数据库用TimeStamp,Model用String解决转为Long问题;解决server.JSONRequest#put非JSON String失败;细分Parser内权限校验粒度;StringUtil中正则相关完善及优化
1 parent 15cf2a7 commit 7680d5e

File tree

19 files changed

+1081
-369
lines changed

19 files changed

+1081
-369
lines changed

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/apijson/demo/server/Controller.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public JSONObject headVerify(String phone, String vfy) {
314314
}
315315

316316
//验证码过期
317-
if (System.currentTimeMillis() > (60000 + BaseModel.value(verify.getDate()))) {
317+
if (System.currentTimeMillis() > (60000 + BaseModel.getTimeMillis(verify.getDate()))) {
318318
new Parser(DELETE, true).parseResponse(
319319
new JSONRequest(new Verify(phone)).setTag(VERIFY_)
320320
);
@@ -629,8 +629,12 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
629629

630630
JSONObject pwdObj = requestObject.getJSONObject(PASSWORD_);
631631
requestObject.remove(PASSWORD_);
632-
if (pwdObj == null || pwdObj.getIntValue(TYPE) != Password.TYPE_PAY) {
633-
return Parser.extendErrorResult(requestObject, new ConditionErrorException("Password type必须是支付类型!"));
632+
if (pwdObj == null) {
633+
pwdObj = new JSONRequest();
634+
}
635+
if (pwdObj.getIntValue(TYPE) != Password.TYPE_PAY) {
636+
// return Parser.extendErrorResult(requestObject, new ConditionErrorException("Password type必须是支付类型!"));
637+
pwdObj.put(TYPE, Password.TYPE_PAY);
634638
}
635639

636640
JSONResponse response = new JSONResponse(
@@ -649,7 +653,7 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
649653
//验证金额范围<<<<<<<<<<<<<<<<<<<<<<<
650654

651655
JSONObject wallet = requestObject.getJSONObject(WALLET_);
652-
long id = wallet == null ? null : wallet.getLong(ID);
656+
long id = wallet == null ? 0 : wallet.getLongValue(ID);
653657
if (id <= 0) {
654658
return Parser.extendErrorResult(requestObject, new ConditionErrorException("请设置Wallet及内部的id!"));
655659
}

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/apijson/demo/server/model/BaseModel.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package apijson.demo.server.model;
1616

1717
import java.io.Serializable;
18+
import java.sql.Timestamp;
1819
import java.util.Arrays;
1920
import java.util.Collection;
21+
import java.util.Date;
2022
import java.util.Map;
2123

2224
import com.alibaba.fastjson.JSON;
@@ -28,8 +30,9 @@
2830
public abstract class BaseModel implements Serializable {
2931
private static final long serialVersionUID = 1L;
3032

31-
private Long id;
32-
private Long date;
33+
private Long id; //主键,唯一标识
34+
private Long userId; //对应User表中的会员id,外键
35+
private String date; //创建时间,JSON没有Date,TimeStamp类型,都会被转成Long,不能用!
3336

3437
public Long getId() {
3538
return id;
@@ -38,10 +41,17 @@ public BaseModel setId(Long id) {
3841
this.id = id;
3942
return this;
4043
}
41-
public Long getDate() {
44+
public Long getUserId() {
45+
return userId;
46+
}
47+
public BaseModel setUserId(Long userId) {
48+
this.userId = userId;
49+
return this;
50+
}
51+
public String getDate() {
4252
return date;
4353
}
44-
public BaseModel setDate(Long date) {
54+
public BaseModel setDate(String date) {
4555
this.date = date;
4656
return this;
4757
}
@@ -52,7 +62,29 @@ public String toString() {
5262
return JSON.toJSONString(this);
5363
}
5464

55-
65+
66+
/**获取当前时间戳
67+
* @return
68+
*/
69+
public static Timestamp currentTimeStamp() {
70+
return new Timestamp(new Date().getTime());
71+
}
72+
/**获取时间戳 TODO 判空? 还是要报错?
73+
* @param time
74+
* @return
75+
*/
76+
public static Timestamp getTimeStamp(String time) {
77+
return Timestamp.valueOf(time);
78+
}
79+
/**获取时间毫秒值 TODO 判空? 还是要报错?
80+
* @param time
81+
* @return
82+
*/
83+
public static long getTimeMillis(String time) {
84+
return getTimeStamp(time).getTime();
85+
}
86+
87+
5688
//判断是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5789
/**判断array是否为空
5890
* @param array

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/apijson/demo/server/model/Comment.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class Comment extends BaseModel {
3333
private static final long serialVersionUID = 1L;
3434

3535
private Long toId;
36-
private Long userId;
3736
private Long momentId;
3837
private String content;
3938
public Comment() {
@@ -52,11 +51,8 @@ public Comment setToId(Long toId) {
5251
this.toId = toId;
5352
return this;
5453
}
55-
public Long getUserId() {
56-
return userId;
57-
}
5854
public Comment setUserId(Long userId) {
59-
this.userId = userId;
55+
super.setUserId(userId);
6056
return this;
6157
}
6258
public Long getMomentId() {

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/apijson/demo/server/model/Moment.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
public class Moment extends BaseModel {
5151
private static final long serialVersionUID = 1L;
5252

53-
private Long userId;
5453
private String content;
5554
private List<String> pictureList;
5655
private List<Long> praiseUserIdList;
@@ -64,11 +63,9 @@ public Moment(long id) {
6463
setId(id);
6564
}
6665

67-
public Long getUserId() {
68-
return userId;
69-
}
66+
7067
public Moment setUserId(Long userId) {
71-
this.userId = userId;
68+
super.setUserId(userId);
7269
return this;
7370
}
7471
public String getContent() {

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.UnsupportedEncodingException;
2020
import java.net.URLDecoder;
2121
import java.net.URLEncoder;
22+
import java.util.Map;
2223
import java.util.Set;
2324

2425
/**use this class instead of com.alibaba.fastjson.JSONObject, not encode in default cases
@@ -215,38 +216,26 @@ public static boolean isArrayKey(String key) {
215216
* @return
216217
*/
217218
public static boolean isTableKey(String key) {
218-
return StringUtil.isBigWord(key);
219+
return StringUtil.isBigName(key);
219220
}
220221
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
221222

222223

223224
//JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
224225

225226
//@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<<
226-
/**
227-
* 角色,拥有对某些数据的某些操作的权限
228-
*/
229-
public static final String KEY_ROLE = "@role";
230-
/**
231-
* 数据库,Table在非默认schema内时需要声明
232-
*/
233-
public static final String KEY_SCHEMA = "@schema";
234-
/**
235-
* 查询的Table字段或SQL函数
236-
*/
237-
public static final String KEY_COLUMN = "@column";
238-
/**
239-
* 分组方式
240-
*/
241-
public static final String KEY_GROUP = "@group";
242-
/**
243-
* 聚合函数条件,一般和@group一起用
244-
*/
245-
public static final String KEY_HAVING = "@having";
246-
/**
247-
* 排序方式
248-
*/
249-
public static final String KEY_ORDER = "@order";
227+
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
228+
public static final String KEY_CONDITION = "@condition"; //条件
229+
public static final String KEY_TRY = "@try"; //尝试,忽略异常
230+
public static final String KEY_DROP = "@drop"; //丢弃,不返回
231+
public static final String KEY_CORRECT = "@correct"; //字段校正
232+
233+
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
234+
public static final String KEY_ABOUT = "@about"; //关于,返回数据库表的信息,包括表说明和字段说明
235+
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
236+
public static final String KEY_GROUP = "@group"; //分组方式
237+
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
238+
public static final String KEY_ORDER = "@order"; //排序方式
250239
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>
251240

252241

@@ -258,6 +247,35 @@ public JSONObject setRole(String role) {
258247
put(KEY_ROLE, role);
259248
return this;
260249
}
250+
251+
/**set try, ignore exceptions
252+
* @param tri
253+
* @return this
254+
*/
255+
public JSONObject setTry(boolean tri) {
256+
put(KEY_TRY, tri);
257+
return this;
258+
}
259+
260+
/**set drop, data dropped will not return
261+
* @param drop
262+
* @return this
263+
*/
264+
public JSONObject setDrop(boolean drop) {
265+
put(KEY_DROP, drop);
266+
return this;
267+
}
268+
269+
/**set correct, correct keys to target ones
270+
* @param correct Map{originKey, [posibleKeys]}, posibleKey之间用 , 隔开
271+
* @return this
272+
*/
273+
public JSONObject setCorrect(Map<String, String> correct) {
274+
put(KEY_CORRECT, correct);
275+
return this;
276+
}
277+
278+
261279

262280
/**set schema where table was put
263281
* @param schema
@@ -268,6 +286,15 @@ public JSONObject setSchema(String schema) {
268286
return this;
269287
}
270288

289+
/**set about
290+
* @param about
291+
* @return this
292+
*/
293+
public JSONObject setAbout(boolean about) {
294+
put(KEY_ABOUT, about);
295+
return this;
296+
}
297+
271298
/**set keys need to be returned
272299
* @param keys key0, key1, key2 ...
273300
* @return {@link #setColumn(String)}

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/zuo/biao/apijson/JSONRequest.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import java.util.List;
18+
19+
import com.alibaba.fastjson.JSONArray;
20+
1721
/**encapsulator for request JSONObject, encode in default cases
1822
* @author Lemon
1923
* @see #toArray
@@ -68,9 +72,32 @@ public JSONRequest(String name, Object object, boolean encode) {
6872

6973

7074

75+
public static final String KEY_ID = "id";
76+
public static final String KEY_ID_IN = KEY_ID + "{}";
7177
public static final String KEY_TAG = "tag";//只在最外层,最外层用JSONRequest
7278

73-
public JSONObject setTag(String tag) {
79+
/**set "id":id in Table layer
80+
* @param id
81+
* @return
82+
*/
83+
public JSONRequest setId(Long id) {
84+
put(KEY_ID, id);
85+
return this;
86+
}
87+
/**set id{}:[] in Table layer
88+
* @param list
89+
* @return
90+
*/
91+
public JSONRequest setIdIn(List<Object> list) {
92+
put(KEY_ID_IN, list);
93+
return this;
94+
}
95+
/**set "tag":tag in outermost layer
96+
* for write operations
97+
* @param tag
98+
* @return
99+
*/
100+
public JSONRequest setTag(String tag) {
74101
put(KEY_TAG, tag);
75102
return this;
76103
}
@@ -86,24 +113,27 @@ public JSONObject setTag(String tag) {
86113
public static final String KEY_COUNT = "count";
87114
public static final String KEY_PAGE = "page";
88115

89-
/**
116+
/**set what to query in Array layer
90117
* @param query what need to query, Table,total,ALL?
91118
* @return
119+
* @see {@link #QUERY_TABLE}
120+
* @see {@link #QUERY_TOTAL}
121+
* @see {@link #QUERY_ALL}
92122
*/
93123
public JSONRequest setQuery(int query) {
94124
put(KEY_QUERY, query);
95125
return this;
96126
}
97-
/**
98-
* @param count
127+
/**set maximum count of Tables to query in Array layer
128+
* @param count <= 0 || >= max ? max : count
99129
* @return
100130
*/
101131
public JSONRequest setCount(int count) {
102132
put(KEY_COUNT, count);
103133
return this;
104134
}
105-
/**
106-
* @param page
135+
/**set page of Tables to query in Array layer
136+
* @param page <= 0 ? 0 : page
107137
* @return
108138
*/
109139
public JSONRequest setPage(int page) {

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/zuo/biao/apijson/JSONResponse.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ public JSONResponse(JSONObject object) {
4646

4747
//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
4848

49-
public static final int CODE_SUCCEED = 200;
50-
public static final int CODE_PARTIAL_SUCCEED = 206;
51-
public static final int CODE_UNSUPPORTED_ENCODING = 400;
52-
public static final int CODE_ILLEGAL_ACCESS = 401;
53-
public static final int CODE_UNSUPPORTED_OPERATION = 403;
54-
public static final int CODE_NOT_FOUND = 404;
55-
public static final int CODE_ILLEGAL_ARGUMENT = 406;
56-
public static final int CODE_NOT_LOGGED_IN = 407;
57-
public static final int CODE_TIME_OUT = 408;
58-
public static final int CODE_CONFLICT = 409;
59-
public static final int CODE_CONDITION_ERROR = 412;
60-
public static final int CODE_UNSUPPORTED_TYPE = 415;
61-
public static final int CODE_OUT_OF_RANGE = 416;
62-
public static final int CODE_NULL_POINTER = 417;
63-
public static final int CODE_SERVER_ERROR = 500;
49+
public static final int CODE_SUCCEED = 200; //成功
50+
public static final int CODE_PARTIAL_SUCCEED = 206; //部分成功
51+
public static final int CODE_UNSUPPORTED_ENCODING = 400; //编码错误
52+
public static final int CODE_ILLEGAL_ACCESS = 401; //权限错误
53+
public static final int CODE_UNSUPPORTED_OPERATION = 403; //禁止操作
54+
public static final int CODE_NOT_FOUND = 404; //未找到
55+
public static final int CODE_ILLEGAL_ARGUMENT = 406; //参数错误
56+
public static final int CODE_NOT_LOGGED_IN = 407; //未登录
57+
public static final int CODE_TIME_OUT = 408; //超时
58+
public static final int CODE_CONFLICT = 409; //重复,已存在
59+
public static final int CODE_CONDITION_ERROR = 412; //条件错误,如密码错误
60+
public static final int CODE_UNSUPPORTED_TYPE = 415; //类型错误
61+
public static final int CODE_OUT_OF_RANGE = 416; //超出范围
62+
public static final int CODE_NULL_POINTER = 417; //对象为空
63+
public static final int CODE_SERVER_ERROR = 500; //服务器内部错误
6464

6565

6666
public static final String KEY_CODE = "code";

0 commit comments

Comments
 (0)