1919 */
2020public class SQL {
2121
22+ public static final String OR = " OR " ;
23+ public static final String AND = " AND " ;
24+ public static final String NOT = " NOT " ;
25+ public static final String IS = " is " ;
26+ public static final String NULL = " null " ;
27+
28+ /**
29+ * isNull = true
30+ * @return {@link #isNull(boolean)}
31+ */
32+ public static String isNull () {
33+ return isNull (true );
34+ }
2235 /**
2336 * @param isNull
24- * @return
37+ * @return IS + (isNull ? "" : NOT) + NULL;
2538 */
2639 public static String isNull (boolean isNull ) {
27- return "is" + (isNull ? "" : " not" ) + " null" ;
40+ return IS + (isNull ? "" : NOT ) + NULL ;
41+ }
42+ /**
43+ * isNull = true
44+ * @param s
45+ * @return {@link #isNull(String, boolean)}
46+ */
47+ public static String isNull (String s ) {
48+ return isNull (s , true );
49+ }
50+ /**
51+ * @param s
52+ * @param isNull
53+ * @return s + {@link #isNull(boolean)}
54+ */
55+ public static String isNull (String s , boolean isNull ) {
56+ return s + isNull (isNull );
57+ }
58+
59+ /**
60+ * isEmpty = true
61+ * @param s
62+ * @return {@link #isEmpty(String, boolean)}
63+ */
64+ public static String isEmpty (String s ) {
65+ return isEmpty (s , true );
2866 }
2967 /**
3068 * trim = false
3169 * @param s
3270 * @param isEmpty
33- * @return
71+ * @return {@link #isEmpty(String, boolean, boolean)}
3472 */
3573 public static String isEmpty (String s , boolean isEmpty ) {
3674 return isEmpty (s , isEmpty , false );
3775 }
3876 /**
77+ * nullable = true
3978 * @param s
40- * @param isEmpty
41- * @param trim
42- * @return
79+ * @param isEmpty <=0
80+ * @param trim s = trim(s);
81+ * @return {@link #isEmpty(String, boolean, boolean, boolean)}
4382 */
4483 public static String isEmpty (String s , boolean isEmpty , boolean trim ) {
84+ return isEmpty (s , isEmpty , trim , true );
85+ }
86+ /**
87+ * @param s
88+ * @param isEmpty <=0
89+ * @param trim s = trim(s);
90+ * @param nullable isNull(s, true) + OR +
91+ * @return {@link #lengthCompare(String, String)}
92+ */
93+ public static String isEmpty (String s , boolean isEmpty , boolean trim , boolean nullable ) {
4594 if (trim ) {
4695 s = trim (s );
4796 }
48- return lengthCompare (s , (isEmpty ? "> " : "<= " ) + "0" );
97+ return ( nullable ? isNull ( s , true ) + OR : "" ) + lengthCompare (s , (isEmpty ? "<= " : "> " ) + "0" );
4998 }
5099 /**
51100 * @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
52- * @return
101+ * @return {@link #length(String)} + compare
53102 */
54103 public static String lengthCompare (String s , String compare ) {
55104 return length (s ) + compare ;
56105 }
57-
58-
106+
107+
59108 /**
60109 * @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
61- * @return
110+ * @return "length(" + s + ")"
62111 */
63112 public static String length (String s ) {
64113 return "length(" + s + ")" ;
65114 }
66115 /**
67116 * @param s 因为POWER(x,y)等函数含有不只一个key,所以需要客户端添加进去,服务端检测到条件中有'('和')'时就不转换,直接当SQL语句查询
68- * @return
117+ * @return "char_length(" + s + ")"
69118 */
70119 public static String charLength (String s ) {
71120 return "char_length(" + s + ")" ;
72121 }
73-
122+
74123 /**
75124 * @param s
76- * @return
125+ * @return "trim(" + s + ")"
77126 */
78127 public static String trim (String s ) {
79128 return "trim(" + s + ")" ;
80129 }
81130 /**
82131 * @param s
83- * @return
132+ * @return "ltrim(" + s + ")"
84133 */
85134 public static String trimLeft (String s ) {
86135 return "ltrim(" + s + ")" ;
87136 }
88137 /**
89138 * @param s
90- * @return
139+ * @return "rtrim(" + s + ")"
91140 */
92141 public static String trimRight (String s ) {
93142 return "rtrim(" + s + ")" ;
94143 }
95-
144+
96145 /**
97146 * @param s
98147 * @param n
99- * @return
148+ * @return "left(" + s + "," + n + ")"
100149 */
101150 public static String left (String s , int n ) {
102151 return "left(" + s + "," + n + ")" ;
103152 }
104153 /**
105154 * @param s
106155 * @param n
107- * @return
156+ * @return "right(" + s + "," + n + ")"
108157 */
109158 public static String right (String s , int n ) {
110159 return "right(" + s + "," + n + ")" ;
111160 }
112-
161+
113162 /**
114163 * @param s
115164 * @param start
116165 * @param end
117- * @return
166+ * @return "substring(" + s + "," + start + "," + (end-start) + ")"
118167 */
119168 public static String subString (String s , int start , int end ) {
120169 return "substring(" + s + "," + start + "," + (end -start ) + ")" ;
121170 }
122-
171+
123172 /**
124173 * @param s
125174 * @param c
126- * @return
175+ * @return "instr(" + s + "," + c + ")"
127176 */
128177 public static String indexOf (String s , String c ) {
129178 return "instr(" + s + "," + c + ")" ;
130179 }
131-
180+
132181 /**
133182 * @param s
134183 * @param c1
@@ -138,35 +187,35 @@ public static String indexOf(String s, String c) {
138187 public static String replace (String s , String c1 , String c2 ) {
139188 return "replace(" + s + "," + c1 + "," + c2 + ")" ;
140189 }
141-
190+
142191 /**
143192 * @param s1
144193 * @param s2
145- * @return
194+ * @return "strcmp(" + s1 + "," + s2 + ")"
146195 */
147196 public static String equals (String s1 , String s2 ) {
148197 return "strcmp(" + s1 + "," + s2 + ")" ;
149198 }
150-
199+
151200 /**
152201 * @param s
153- * @return
202+ * @return "upper(" + s + ")"
154203 */
155204 public static String toUpperCase (String s ) {
156205 return "upper(" + s + ")" ;
157206 }
158207 /**
159208 * @param s
160- * @return
209+ * @return "lower(" + s + ")"
161210 */
162211 public static String toLowerCase (String s ) {
163212 return "lower(" + s + ")" ;
164213 }
165-
166-
167-
168-
169-
214+
215+
216+
217+
218+
170219 public static final int SEARCH_TYPE_CONTAIN_FULL = 0 ;
171220 public static final int SEARCH_TYPE_CONTAIN_ORDER = 1 ;
172221 public static final int SEARCH_TYPE_CONTAIN_SINGLE = 2 ;
@@ -237,5 +286,5 @@ public static String search(String s, int type, boolean ignoreCase) {
237286 return "%" + s + "%" ;
238287 }
239288 }
240-
289+
241290}
0 commit comments