Skip to content

Commit c5ea2fa

Browse files
committed
Server:新增支持SQL语句(is null,各种函数等);Client:去除SQL类拼接SQL语句两端的空格
1 parent efe4e0d commit c5ea2fa

File tree

2 files changed

+34
-24
lines changed
  • APIJSON(Android)/APIJSON(ADT)/APIJSONLibrary/src/zuo/biao/apijson
  • APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server

2 files changed

+34
-24
lines changed

APIJSON(Android)/APIJSON(ADT)/APIJSONLibrary/src/zuo/biao/apijson/SQL.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class SQL {
2424
* @return
2525
*/
2626
public static String isNull(boolean isNull) {
27-
return " is " + (isNull ? "" : " not ") + " null ";
27+
return "is" + (isNull ? "" : " not") + " null";
2828
}
2929
/**
3030
* trim = false
@@ -45,14 +45,14 @@ public static String isEmpty(String s, boolean isEmpty, boolean trim) {
4545
if (trim) {
4646
s = trim(s);
4747
}
48-
return lengthCompare(s, (isEmpty ? ">" : "<=") + "0 ");
48+
return lengthCompare(s, (isEmpty ? ">" : "<=") + "0");
4949
}
5050
/**
5151
* @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
5252
* @return
5353
*/
5454
public static String lengthCompare(String s, String compare) {
55-
return length(s) + compare + " ";
55+
return length(s) + compare;
5656
}
5757

5858

@@ -61,36 +61,36 @@ public static String lengthCompare(String s, String compare) {
6161
* @return
6262
*/
6363
public static String length(String s) {
64-
return " length(" + s + ") ";
64+
return "length(" + s + ")";
6565
}
6666
/**
6767
* @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
6868
* @return
6969
*/
7070
public static String charLength(String s) {
71-
return " char_length(" + s + ") ";
71+
return "char_length(" + s + ")";
7272
}
7373

7474
/**
7575
* @param s
7676
* @return
7777
*/
7878
public static String trim(String s) {
79-
return " trim(" + s + ") ";
79+
return "trim(" + s + ")";
8080
}
8181
/**
8282
* @param s
8383
* @return
8484
*/
8585
public static String trimLeft(String s) {
86-
return " ltrim(" + s + ") ";
86+
return "ltrim(" + s + ")";
8787
}
8888
/**
8989
* @param s
9090
* @return
9191
*/
9292
public static String trimRight(String s) {
93-
return " rtrim(" + s + ") ";
93+
return "rtrim(" + s + ")";
9494
}
9595

9696
/**
@@ -99,15 +99,15 @@ public static String trimRight(String s) {
9999
* @return
100100
*/
101101
public static String left(String s, int n) {
102-
return " left(" + s + "," + n + ") ";
102+
return "left(" + s + "," + n + ")";
103103
}
104104
/**
105105
* @param s
106106
* @param n
107107
* @return
108108
*/
109109
public static String right(String s, int n) {
110-
return " right(" + s + "," + n + ") ";
110+
return "right(" + s + "," + n + ")";
111111
}
112112

113113
/**
@@ -117,7 +117,7 @@ public static String right(String s, int n) {
117117
* @return
118118
*/
119119
public static String subString(String s, int start, int end) {
120-
return " substring(" + s + "," + start + "," + (end-start) + ") ";
120+
return "substring(" + s + "," + start + "," + (end-start) + ")";
121121
}
122122

123123
/**
@@ -126,7 +126,7 @@ public static String subString(String s, int start, int end) {
126126
* @return
127127
*/
128128
public static String indexOf(String s, String c) {
129-
return " instr(" + s + "," + c + ") ";
129+
return "instr(" + s + "," + c + ")";
130130
}
131131

132132
/**
@@ -136,7 +136,7 @@ public static String indexOf(String s, String c) {
136136
* @return
137137
*/
138138
public static String replace(String s, String c1, String c2) {
139-
return " replace(" + s + "," + c1 + "," + c2 + ") ";
139+
return "replace(" + s + "," + c1 + "," + c2 + ")";
140140
}
141141

142142
/**
@@ -145,22 +145,22 @@ public static String replace(String s, String c1, String c2) {
145145
* @return
146146
*/
147147
public static String equals(String s1, String s2) {
148-
return " strcmp(" + s1 + "," + s2 + ") ";
148+
return "strcmp(" + s1 + "," + s2 + ")";
149149
}
150150

151151
/**
152152
* @param s
153153
* @return
154154
*/
155155
public static String toUpperCase(String s) {
156-
return " upper(" + s + ") ";
156+
return "upper(" + s + ")";
157157
}
158158
/**
159159
* @param s
160160
* @return
161161
*/
162162
public static String toLowerCase(String s) {
163-
return " lower(" + s + ") ";
163+
return "lower(" + s + ")";
164164
}
165165

166166

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,9 @@ public static String getRangeString(String key, Object range) {
300300
Log.i(TAG, "getRangeString last = " + last);
301301
if ("|".equals(last)) {
302302
type = 0;
303-
}
304-
if ("&".equals(last)) {
303+
} else if ("&".equals(last)) {
305304
type = 1;
306-
}
307-
if ("!".equals(last)) {
305+
} else if ("!".equals(last)) {
308306
type = 2;
309307
}
310308
if (type >= 0 && type <= 2) {
@@ -322,8 +320,19 @@ public static String getRangeString(String key, Object range) {
322320
return key + getInString(type == 2, ((JSONArray) range).toArray());
323321
}
324322
if (range instanceof String) {//非Number类型需要客户端拼接成 < 'value0', >= 'value1'这种
325-
range = key + ((String) range).replaceAll(",", (type == 1 ? " AND " : " OR ") + key);
326-
return type != 2 ? (String) range : " NOT (" + range + ")";
323+
String[] conditions = StringUtil.split((String) range);
324+
String condition = "(";
325+
if (conditions != null) {
326+
int index;
327+
for (int i = 0; i < conditions.length; i++) {//对函数条件length(key)<=5这种不再在开头加key
328+
index = conditions[i] == null ? -1 : conditions[i].indexOf("(");
329+
condition += ((i <= 0 ? "" : (type == 1 ? " AND " : " OR "))//连接方式
330+
+ (index >= 0 && conditions[i].substring(index).contains(")") ? "" : key + " ")//函数和非函数条件
331+
+ conditions[i]);//单个条件
332+
}
333+
}
334+
condition += ")";
335+
return type != 2 ? condition : " NOT " + condition;
327336
}
328337

329338
throw new IllegalArgumentException("\"key{}\":range 中range只能是 用','分隔条件的字符串 或者 可取选项JSONArray!");
@@ -333,13 +342,14 @@ public static String getRangeString(String key, Object range) {
333342
* @return IN ('key0', 'key1', ... )
334343
*/
335344
public static String getInString(boolean not, Object[] in) {
336-
String inString = "";
345+
String inString = "(";
337346
if (in != null) {//返回 "" 会导致 id:[] 空值时效果和没有筛选id一样!
338347
for (int i = 0; i < in.length; i++) {
339348
inString += ((i > 0 ? "," : "") + "'" + in[i] + "'");
340349
}
341350
}
342-
return (not ? " NOT " : "") + " IN (" + inString + ") ";
351+
inString += ") ";
352+
return (not ? " NOT " : "") + " IN " + inString;
343353
}
344354
//WHERE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
345355

0 commit comments

Comments
 (0)