| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,7 +255,7 @@ public AbstractSQLConfig setGroup(String group) { | |||
| 255 | 255 | @JSONField(serialize = false) | |
| 256 | 256 | public String getGroupString() { | |
| 257 | 257 | //TODO 加上子表的group | |
| 258 | - | ||
| 258 | + | ||
| 259 | 259 | group = StringUtil.getTrimedString(group); | |
| 260 | 260 | if (group.isEmpty()) { | |
| 261 | 261 | return ""; | |
@@ -291,16 +291,68 @@ public AbstractSQLConfig setHaving(String having) { | |||
| 291 | 291 | this.having = having; | |
| 292 | 292 | return this; | |
| 293 | 293 | } | |
| 294 | + /** | ||
| 295 | + * @return HAVING conditoin0 AND condition1 OR condition2 ... | ||
| 296 | + */ | ||
| 294 | 297 | @JSONField(serialize = false) | |
| 295 | 298 | public String getHavingString() { | |
| 296 | 299 | having = StringUtil.getTrimedString(having); | |
| 297 | 300 | if(having.isEmpty()) { | |
| 298 | 301 | return ""; | |
| 299 | 302 | } | |
| 300 | - if (isPrepared()) { | ||
| 301 | - throw new UnsupportedOperationException("字符串 " + having + " 不合法!预编译模式下不允许传 @having:\"condition\" !"); | ||
| 303 | + | ||
| 304 | + String[] keys = StringUtil.split(having, ";"); | ||
| 305 | + if (keys == null || keys.length <= 0) { | ||
| 306 | + return ""; | ||
| 307 | + } | ||
| 308 | + | ||
| 309 | + String expression; | ||
| 310 | + String method; | ||
| 311 | + //暂时不允许 String prefix; | ||
| 312 | + String suffix; | ||
| 313 | + | ||
| 314 | + //fun0(arg0,arg1,...);fun1(arg0,arg1,...) | ||
| 315 | + for (int i = 0; i < keys.length; i++) { | ||
| 316 | + | ||
| 317 | + //fun(arg0,arg1,...) | ||
| 318 | + expression = keys[i]; | ||
| 319 | + | ||
| 320 | + //TODO 支持 maxId>=100 这种没括号的 | ||
| 321 | + int start = expression.indexOf("("); | ||
| 322 | + int end = expression.indexOf(")"); | ||
| 323 | + if (start >= end) { | ||
| 324 | + throw new IllegalArgumentException("字符 " + expression + " 不合法!@having:value 中 value 里的 SQL函数必须为 function(arg0,arg1,...) 这种格式!"); | ||
| 325 | + } | ||
| 326 | + | ||
| 327 | + method = expression.substring(0, start); | ||
| 328 | + | ||
| 329 | + if (StringUtil.isName(method) == false) { | ||
| 330 | + throw new IllegalArgumentException("字符 " + method + " 不合法!@having:\"function0(...)condition0;function1(...)condition1...\"" | ||
| 331 | + + " 中SQL函数名 function 必须符合正则表达式 ^[0-9a-zA-Z_]+$ !"); | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + suffix = expression.substring(end + 1, expression.length()); | ||
| 335 | + | ||
| 336 | + if (isPrepared() && PATTERN_RANGE.matcher((String) suffix).matches() == false) { | ||
| 337 | + throw new UnsupportedOperationException("字符串 " + suffix + " 不合法!预编译模式下 @having:\"function0(...)condition0;function1(...)condition1...\"" | ||
| 338 | + + " 中 condition 必须符合正则表达式 ^[0-9%!=<>,]+$ !不允许空格!"); | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + String[] ckeys = StringUtil.split(expression.substring(start + 1, end)); | ||
| 342 | + | ||
| 343 | + for (int j = 0; j < ckeys.length; j++) { | ||
| 344 | + | ||
| 345 | + if (isPrepared() && StringUtil.isName(ckeys[j]) == false) { | ||
| 346 | + throw new IllegalArgumentException("@having:'function0(arg0,arg1,...);function1(arg0,arg1,...)' 中所有 arg 都必须是1个单词!并且不要有空格!"); | ||
| 347 | + } | ||
| 348 | + | ||
| 349 | + ckeys[j] = getKey(ckeys[j]); | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | + keys[i] = method + "(" + StringUtil.getString(ckeys) + ")" + suffix; | ||
| 302 | 353 | } | |
| 303 | - return " HAVING " + having; | ||
| 354 | + | ||
| 355 | + return " HAVING " + StringUtil.getString(keys, AND); //TODO 支持 OR, NOT 参考 @combine:"&key0,|key1,!key2" | ||
| 304 | 356 | } | |
| 305 | 357 | ||
| 306 | 358 | @Override | |
@@ -329,7 +381,7 @@ public String getOrderString() { | |||
| 329 | 381 | if (order.contains("-")) { | |
| 330 | 382 | order = order.replaceAll("-", " DESC "); | |
| 331 | 383 | } | |
| 332 | - | ||
| 384 | + | ||
| 333 | 385 | //TODO column, order, group 都改用 List<String> 存储!!!,并且每个字段都要加 Table. 前缀! | |
| 334 | 386 | String[] keys = StringUtil.split(order); | |
| 335 | 387 | if (keys == null || keys.length <= 0) { | |
@@ -358,7 +410,7 @@ public String getOrderString() { | |||
| 358 | 410 | } | |
| 359 | 411 | keys[i] = getKey(origin) + sort; | |
| 360 | 412 | } | |
| 361 | - | ||
| 413 | + | ||
| 362 | 414 | return " ORDER BY " + StringUtil.getString(keys); | |
| 363 | 415 | } | |
| 364 | 416 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments