4646import java .util .Set ;
4747import java .util .regex .Pattern ;
4848
49- import com .alibaba .fastjson .JSON ;
5049import com .alibaba .fastjson .JSONArray ;
5150import com .alibaba .fastjson .JSONObject ;
5251import com .alibaba .fastjson .annotation .JSONField ;
5352
53+ import zuo .biao .apijson .JSON ;
5454import zuo .biao .apijson .Log ;
5555import zuo .biao .apijson .RequestMethod ;
5656import zuo .biao .apijson .RequestRole ;
@@ -1080,15 +1080,18 @@ private String getWhereItem(String key, Object value
10801080 if (key .endsWith ("$" )) {
10811081 keyType = 1 ;
10821082 }
1083- else if (key .endsWith ("?" )) {
1084- keyType = 2 ;
1083+ else if (key .endsWith ("~" ) || key . endsWith ( " ?" )) { //TODO ?可能以后会被废弃,全用 ~ 和 *~ 替代,更接近 PostgreSQL 语法
1084+ keyType = key . charAt ( key . length () - 2 ) == '*' ? - 2 : 2 ; //FIXME StringIndexOutOfBoundsException
10851085 }
1086- else if (key .endsWith ("{} " )) {
1086+ else if (key .endsWith ("% " )) {
10871087 keyType = 3 ;
10881088 }
1089- else if (key .endsWith ("<> " )) {
1089+ else if (key .endsWith ("{} " )) {
10901090 keyType = 4 ;
10911091 }
1092+ else if (key .endsWith ("<>" )) {
1093+ keyType = 5 ;
1094+ }
10921095 else { //else绝对不能省,避免再次踩坑! keyType = 0; 写在for循环外面都没注意!
10931096 keyType = 0 ;
10941097 }
@@ -1097,11 +1100,14 @@ else if (key.endsWith("<>")) {
10971100 switch (keyType ) {
10981101 case 1 :
10991102 return getSearchString (key , value );
1103+ case -2 :
11001104 case 2 :
1101- return getRegExpString (key , value );
1105+ return getRegExpString (key , value , keyType < 0 );
11021106 case 3 :
1103- return getRangeString (key , value );
1107+ return getBetweenString (key , value );
11041108 case 4 :
1109+ return getRangeString (key , value );
1110+ case 5 :
11051111 return getContainString (key , value );
11061112 default : //TODO MySQL JSON类型的字段对比 key='[]' 会无结果! key LIKE '[1, 2, 3]' //TODO MySQL , 后面有空格!
11071113 return getEqualString (key , value );
@@ -1111,7 +1117,14 @@ else if (key.endsWith("<>")) {
11111117
11121118 @ JSONField (serialize = false )
11131119 public String getEqualString (String key , Object value ) {
1114- return getKey (key ) + "=" + getValue (value );
1120+ boolean not = key .endsWith ("!" ); // & | 没有任何意义,写法多了不好控制
1121+ if (not ) {
1122+ key = key .substring (0 , key .length () - 1 );
1123+ }
1124+ if (StringUtil .isName (key ) == false ) {
1125+ throw new IllegalArgumentException ("\" " + key + "\" :value 中key不合法!不支持 ! 以外的逻辑符 !" );
1126+ }
1127+ return getKey (key ) + (not ? "!=" : "=" ) + getValue (value );
11151128 }
11161129
11171130 public String getKey (String key ) {
@@ -1198,14 +1211,16 @@ public String getLikeString(String key, Object value) {
11981211
11991212
12001213
1201- //$ search <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1202- /**search key match RegExps value
1203- * @param in
1204- * @return {@link #getRegExpString(String, Object[], int)}
1214+ //~ regexp <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1215+ /**search key match RegExp values
1216+ * @param key
1217+ * @param value
1218+ * @param ignoreCase
1219+ * @return {@link #getRegExpString(String, Object[], int, boolean)}
12051220 * @throws IllegalArgumentException
12061221 */
12071222 @ JSONField (serialize = false )
1208- public String getRegExpString (String key , Object value ) throws IllegalArgumentException {
1223+ public String getRegExpString (String key , Object value , boolean ignoreCase ) throws IllegalArgumentException {
12091224 if (value == null ) {
12101225 return "" ;
12111226 }
@@ -1218,15 +1233,18 @@ public String getRegExpString(String key, Object value) throws IllegalArgumentEx
12181233 if (arr .isEmpty ()) {
12191234 return "" ;
12201235 }
1221- return getRegExpString (key , arr .toArray (), logic .getType ());
1236+ return getRegExpString (key , arr .toArray (), logic .getType (), ignoreCase );
12221237 }
12231238 /**search key match RegExp values
1224- * @param in
1239+ * @param key
1240+ * @param values
1241+ * @param type
1242+ * @param ignoreCase
12251243 * @return LOGIC [ key REGEXP 'values[i]' ]
12261244 * @throws IllegalArgumentException
12271245 */
12281246 @ JSONField (serialize = false )
1229- public String getRegExpString (String key , Object [] values , int type ) throws IllegalArgumentException {
1247+ public String getRegExpString (String key , Object [] values , int type , boolean ignoreCase ) throws IllegalArgumentException {
12301248 if (values == null || values .length <= 0 ) {
12311249 return "" ;
12321250 }
@@ -1236,7 +1254,7 @@ public String getRegExpString(String key, Object[] values, int type) throws Ille
12361254 if (values [i ] instanceof String == false ) {
12371255 throw new IllegalArgumentException (key + "$\" :value 中value的类型只能为String或String[]!" );
12381256 }
1239- condition += (i <= 0 ? "" : (Logic .isAnd (type ) ? AND : OR )) + getRegExpString (key , (String ) values [i ]);
1257+ condition += (i <= 0 ? "" : (Logic .isAnd (type ) ? AND : OR )) + getRegExpString (key , (String ) values [i ], ignoreCase );
12401258 }
12411259
12421260 return getCondition (Logic .isNot (type ), condition );
@@ -1245,13 +1263,65 @@ public String getRegExpString(String key, Object[] values, int type) throws Ille
12451263 /**WHERE key REGEXP 'value'
12461264 * @param key
12471265 * @param value
1266+ * @param ignoreCase
12481267 * @return key REGEXP 'value'
12491268 */
12501269 @ JSONField (serialize = false )
1251- public String getRegExpString (String key , String value ) {
1252- return getKey (key ) + " REGEXP " + getValue (value );
1270+ public String getRegExpString (String key , String value , boolean ignoreCase ) {
1271+ return getKey (key ) + " REGEXP " + ( ignoreCase ? "" : "BINARY " ) + getValue (value );
12531272 }
1254- //$ search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1273+ //~ regexp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1274+
1275+
1276+
1277+ //% between <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1278+
1279+ /**WHERE key BETWEEN 'value0' AND 'value1'
1280+ * @param key
1281+ * @param value
1282+ * @return key BETWEEN 'value0' AND 'value1'
1283+ */
1284+ @ JSONField (serialize = false )
1285+ public String getBetweenString (String key , Object value ) {
1286+ boolean not = key .endsWith ("!" ); //不能用 new Logic(key) 因为默认是 | ,而 BETWEEN 只能接 AND
1287+ if (not ) {
1288+ key = key .substring (0 , key .length () - 1 );
1289+ }
1290+ if (StringUtil .isName (key ) == false ) {
1291+ throw new IllegalArgumentException (key + "%\" :value 中key不合法!不支持 ! 以外的逻辑符 !" );
1292+ }
1293+
1294+ Object [] vs ;
1295+ if (value instanceof String ) {
1296+ vs = StringUtil .split ((String ) value );
1297+ // int index = ((String) value).indexOf(",");
1298+ // if (index < 0) {
1299+ // throw new IllegalArgumentException(key + "%\":value 中value的类型为 String 时必须包括逗号 , !前面缺省为 min(key) ,后面缺省为 max(key)");
1300+ // }
1301+ // if (index == 0) {
1302+ // start = "(SELECT min(key) FROM getSQLTable())"
1303+ // }
1304+ }
1305+ else if (value instanceof Collection <?>) {
1306+ vs = ((Collection <?>) value ).toArray ();
1307+ }
1308+ else {
1309+ throw new IllegalArgumentException (key + "%\" :value 中value不合法!类型只能为 1个逗号分隔的String 或者 只有Boolean[2]或Number[2]或String[2] !" );
1310+ }
1311+
1312+ if (vs == null || vs .length != 2 ) {
1313+ throw new IllegalArgumentException (key + "%\" :value 中value不合法!类型为 String 时必须包括1个逗号 , 且左右两侧都有值!类型为 JSONArray 时只能是 Boolean[2]或Number[2]或String[2] !" );
1314+ }
1315+
1316+ Object start = vs [0 ];
1317+ Object end = vs [1 ];
1318+ if (JSON .isBooleanOrNumberOrString (start ) == false || JSON .isBooleanOrNumberOrString (end ) == false ) {
1319+ throw new IllegalArgumentException (key + "%\" :value 中value不合法!类型为 String 时必须包括1个逗号 , 且左右两侧都有值!类型为 JSONArray 时只能是 Boolean[2]或Number[2]或String[2] !" );
1320+ }
1321+
1322+ return getKey (key ) + (not ? NOT : "" ) + " BETWEEN " + getValue (start ) + AND + getValue (end );
1323+ }
1324+ //% between >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
12551325
12561326
12571327
@@ -1997,7 +2067,13 @@ public static String getRealKey(RequestMethod method, String originKey
19972067 if (key .endsWith ("$" )) {//搜索,查询时处理
19982068 key = key .substring (0 , key .length () - 1 );
19992069 }
2000- else if (key .endsWith ("?" )) {//匹配正则表达式,查询时处理
2070+ else if (key .endsWith ("~" ) || key .endsWith ("?" )) {//匹配正则表达式,查询时处理 TODO ?可能以后会被废弃,全用 ~ 和 *~ 替代,更接近 PostgreSQL 语法
2071+ key = key .substring (0 , key .length () - 1 );
2072+ if (key .endsWith ("*" )) {//忽略大小写
2073+ key = key .substring (0 , key .length () - 1 );
2074+ }
2075+ }
2076+ else if (key .endsWith ("%" )) {//数字、文本、日期范围,BETWEEN AND
20012077 key = key .substring (0 , key .length () - 1 );
20022078 }
20032079 else if (key .endsWith ("{}" )) {//被包含,或者说key对应值处于value的范围内。查询时处理
0 commit comments