Skip to content

Commit c997551

Browse files
committed
Server:新增支持去重关键词 DISTINCT
1 parent c925b03 commit c997551

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
7272
private static final String TAG = "AbstractSQLConfig";
7373

7474
public static String DEFAULT_SCHEMA = "sys";
75+
public static String PREFFIX_DISTINCT = "DISTINCT ";
7576

7677
/**
7778
* 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
@@ -682,8 +683,8 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
682683
}
683684

684685
method = expression.substring(0, start);
685-
686-
if (StringUtil.isName(method) == false) {
686+
boolean distinct = method.startsWith(PREFFIX_DISTINCT);
687+
if (StringUtil.isName(distinct ? method.substring(PREFFIX_DISTINCT.length()) : method) == false) {
687688
throw new IllegalArgumentException("字符 " + method + " 不合法!"
688689
+ "预编译模式下 @column:\"column0,column1:alias;function0(arg0,arg1,...);function1(...):alias...\""
689690
+ " 中SQL函数名 function 必须符合正则表达式 ^[0-9a-zA-Z_]+$ !");
@@ -698,26 +699,34 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
698699
// if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
699700
if (ckeys != null && ckeys.length > 0) {
700701

702+
boolean distinct;
701703
String origin;
702704
String alias;
703705
int index;
704706
for (int j = 0; j < ckeys.length; j++) {
705-
index = ckeys[j].lastIndexOf(":"); //StringUtil.split返回数组中,子项不会有null
707+
index = isColumn ? ckeys[j].lastIndexOf(":") : -1; //StringUtil.split返回数组中,子项不会有null
706708
origin = index < 0 ? ckeys[j] : ckeys[j].substring(0, index);
707709
alias = index < 0 ? null : ckeys[j].substring(index + 1);
710+
711+
distinct = origin.startsWith(PREFFIX_DISTINCT);
712+
if (distinct) {
713+
origin = origin.substring(PREFFIX_DISTINCT.length());
714+
}
708715

709716
if (isPrepared()) {
710717
if (isColumn) {
711718
if (StringUtil.isName(origin) == false || (alias != null && StringUtil.isName(alias) == false)) {
712-
throw new IllegalArgumentException("GET请求: 预编译模式下 @column:value 中 value里面用 , 分割的每一项"
713-
+ " column:alias 中 column 必须是1个单词!如果有alias,则alias也必须为1个单词!并且不要有多余的空格!");
719+
throw new IllegalArgumentException("字符 " + ckeys[j] + " 不合法!"
720+
+ "预编译模式下 @column:value 中 value里面用 , 分割的每一项"
721+
+ " column:alias 中 column 必须是1个单词!如果有alias,则alias也必须为1个单词!"
722+
+ "DISTINCT 必须全大写,且后面必须有且只有 1 个空格!其它情况不允许空格!");
714723
}
715724
}
716725
else {
717-
if ((StringUtil.isName(ckeys[j]) == false || ckeys[j].startsWith("_"))) {
726+
if ((StringUtil.isName(origin) == false || origin.startsWith("_"))) {
718727
throw new IllegalArgumentException("字符 " + ckeys[j] + " 不合法!"
719728
+ "预编译模式下 @column:\"column0,column1:alias;function0(arg0,arg1,...);function1(...):alias...\""
720-
+ " 中所有 arg 都必须是1个不以 _ 开头的单词!并且不要有空格!");
729+
+ " 中所有 arg 都必须是1个不以 _ 开头的单词!DISTINCT 必须全大写,且后面必须有且只有 1 个空格!其它情况不允许空格!");
721730
}
722731
}
723732
}
@@ -735,6 +744,10 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
735744
} else {
736745
ckeys[j] = origin + (StringUtil.isEmpty(alias, true) ? "" : " AS " + quote + alias + quote);
737746
}
747+
748+
if (distinct) {
749+
ckeys[j] = PREFFIX_DISTINCT + ckeys[j];
750+
}
738751
}
739752
// }
740753

0 commit comments

Comments
 (0)