Skip to content

Commit 8b3ee34

Browse files
committed
Server:支持 @order 排序值中 rand() 可与 id+ 等其它排序方式组合
1 parent 1c421bc commit 8b3ee34

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,10 @@ public String getOrderString(boolean hasPrefix) {
618618

619619

620620
String order = StringUtil.getTrimedString(getOrder());
621-
if ("rand()".equals(order)) {
622-
return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
623-
}
621+
// SELECT * FROM sys.Moment ORDER BY userId ASC, rand(); 前面的 userId ASC 和后面的 rand() 都有效
622+
// if ("rand()".equals(order)) {
623+
// return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
624+
// }
624625

625626
if (getCount() > 0 && (isOracle() || isSQLServer() || isDb2())) { // Oracle, SQL Server, DB2 的 OFFSET 必须加 ORDER BY
626627

@@ -653,38 +654,37 @@ public String getOrderString(boolean hasPrefix) {
653654
}
654655

655656

656-
if (order.contains("+")) { //replace 没有包含 的replacement 会崩溃
657-
order = order.replaceAll("\\+", " ASC ");
658-
}
659-
if (order.contains("-")) {
660-
order = order.replaceAll("-", " DESC ");
661-
}
662-
663657
String[] keys = StringUtil.split(order);
664658
if (keys == null || keys.length <= 0) {
665659
return StringUtil.isEmpty(joinOrder, true) ? "" : (hasPrefix ? " ORDER BY " : "") + joinOrder;
666660
}
667661

668-
String origin;
669-
String sort;
670-
int index;
671662
for (int i = 0; i < keys.length; i++) {
672-
index = keys[i].trim().endsWith(" ASC") ? keys[i].lastIndexOf(" ASC") : -1; //StringUtil.split返回数组中,子项不会有null
663+
String item = keys[i];
664+
if ("rand()".equals(item)) {
665+
continue;
666+
}
667+
668+
int index = item.endsWith("+") ? item.length() - 1 : -1; //StringUtil.split返回数组中,子项不会有null
669+
String sort;
673670
if (index < 0) {
674-
index = keys[i].trim().endsWith(" DESC") ? keys[i].lastIndexOf(" DESC") : -1;
671+
index = item.endsWith("-") ? item.length() - 1 : -1;
675672
sort = index <= 0 ? "" : " DESC ";
676-
} else {
673+
}
674+
else {
677675
sort = " ASC ";
678676
}
679-
origin = index < 0 ? keys[i] : keys[i].substring(0, index);
677+
678+
String origin = index < 0 ? item : item.substring(0, index);
680679

681680
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
682681
//这里既不对origin trim,也不对 ASC/DESC ignoreCase,希望前端严格传没有任何空格的字符串过来,减少传输数据量,节约服务器性能
683682
if (StringUtil.isName(origin) == false) {
684-
throw new IllegalArgumentException("预编译模式下 @order:value 中 value 只能是 rand() 或 里面用 , 分割的每一项"
685-
+ " column+ / column- 中 column必须是1个单词!并且不要有多余的空格!");
683+
throw new IllegalArgumentException("预编译模式下 @order:value 中 " + item + " 不合法! value 里面用 , 分割的"
684+
+ "每一项必须是 随机函数 rand() 或 column+ / column- 且其中 column 必须是 1 个单词!并且不要有多余的空格!");
686685
}
687686
}
687+
688688
keys[i] = getKey(origin) + sort;
689689
}
690690

0 commit comments

Comments
 (0)