Skip to content

Commit 1c421bc

Browse files
committed
Server:新增支持随机函数 rand() 来排序
1 parent f89ebba commit 1c421bc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ 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+
}
621624

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

@@ -650,7 +653,7 @@ public String getOrderString(boolean hasPrefix) {
650653
}
651654

652655

653-
if (order.contains("+")) {//replace没有包含的replacement会崩溃
656+
if (order.contains("+")) { //replace 没有包含 的replacement 会崩溃
654657
order = order.replaceAll("\\+", " ASC ");
655658
}
656659
if (order.contains("-")) {
@@ -678,7 +681,7 @@ public String getOrderString(boolean hasPrefix) {
678681
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
679682
//这里既不对origin trim,也不对 ASC/DESC ignoreCase,希望前端严格传没有任何空格的字符串过来,减少传输数据量,节约服务器性能
680683
if (StringUtil.isName(origin) == false) {
681-
throw new IllegalArgumentException("预编译模式下 @order:value 中 value里面用 , 分割的每一项"
684+
throw new IllegalArgumentException("预编译模式下 @order:value 中 value 只能是 rand() 或 里面用 , 分割的每一项"
682685
+ " column+ / column- 中 column必须是1个单词!并且不要有多余的空格!");
683686
}
684687
}

0 commit comments

Comments
 (0)