Skip to content

Commit f10f3ea

Browse files
committed
Server:解决 JOIN 副表有 DISTINCT 时外层 SELECT 字段出现多余的 DISTINCT 导致语法错误
1 parent acd1b16 commit f10f3ea

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-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
@@ -106,6 +106,7 @@ public String getUserIdKey() {
106106
* TODO 被关联的表通过就忽略关联的表?(这个不行 User:{"sex@":"/Comment/toId"})
107107
*/
108108
private RequestRole role; //发送请求的用户的角色
109+
private boolean distinct = false;
109110
private String database; //表所在的数据库类型
110111
private String schema; //表所在的数据库名
111112
private String table; //表名
@@ -214,6 +215,16 @@ public AbstractSQLConfig setRole(RequestRole role) {
214215
return this;
215216
}
216217

218+
@Override
219+
public boolean isDistinct() {
220+
return distinct;
221+
}
222+
@Override
223+
public SQLConfig setDistinct(boolean distinct) {
224+
this.distinct = distinct;
225+
return this;
226+
}
227+
217228
@Override
218229
public String getDatabase() {
219230
return database;
@@ -683,7 +694,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
683694
}
684695

685696
method = expression.substring(0, start);
686-
boolean distinct = method.startsWith(PREFFIX_DISTINCT);
697+
boolean distinct = i <= 0 && method.startsWith(PREFFIX_DISTINCT);
687698
if (StringUtil.isName(distinct ? method.substring(PREFFIX_DISTINCT.length()) : method) == false) {
688699
throw new IllegalArgumentException("字符 " + method + " 不合法!"
689700
+ "预编译模式下 @column:\"column0,column1:alias;function0(arg0,arg1,...);function1(...):alias...\""
@@ -708,7 +719,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
708719
origin = index < 0 ? ckeys[j] : ckeys[j].substring(0, index);
709720
alias = index < 0 ? null : ckeys[j].substring(index + 1);
710721

711-
distinct = origin.startsWith(PREFFIX_DISTINCT);
722+
distinct = j <= 0 && origin.startsWith(PREFFIX_DISTINCT);
712723
if (distinct) {
713724
origin = origin.substring(PREFFIX_DISTINCT.length());
714725
}
@@ -786,9 +797,8 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
786797
}
787798

788799
String c = StringUtil.getString(keys);
789-
790-
return (c.contains(":") == false ? c : c.replaceAll(":", " AS ")) + (StringUtil.isEmpty(joinColumn, true) ? "" : ", " + joinColumn);//不能在这里改,后续还要用到:
791-
800+
c = (c.contains(":") == false ? c : c.replaceAll(":", " AS ")) + (StringUtil.isEmpty(joinColumn, true) ? "" : ", " + joinColumn);//不能在这里改,后续还要用到:
801+
return isMain() && isDistinct() ? PREFFIX_DISTINCT + c : c;
792802
default:
793803
throw new UnsupportedOperationException(
794804
"服务器内部错误:getColumnString 不支持 " + RequestMethod.getName(getMethod())
@@ -2071,7 +2081,7 @@ public String getJoinString() throws Exception {
20712081
jc = j.getJoinConfig();
20722082
jc.setPrepared(isPrepared());
20732083

2074-
jt = jc.getTable();
2084+
jt = jc.getTable();//FIXME getAlias 不能加 `` StringUtil.isEmpty(jc.getAlias(), true) ? jc.getTable() : jc.getAlias();
20752085
tn = j.getTargetName();
20762086

20772087
//如果要强制小写,则可在子类重写这个方法再 toLowerCase
@@ -2391,8 +2401,10 @@ else if (whereList != null && whereList.contains(key)) {
23912401
config.setContent(tableContent);
23922402
}
23932403

2404+
boolean distinct = column == null ? false : column.startsWith(PREFFIX_DISTINCT);
2405+
23942406
List<String> cs = new ArrayList<>();
2395-
String[] fks = StringUtil.split(column, ";"); // key0,key1;fun0(key0,...);fun1(key0,...);key3;fun2(key0,...)
2407+
String[] fks = StringUtil.split(distinct ? column.substring(PREFFIX_DISTINCT.length()) : column, ";"); // key0,key1;fun0(key0,...);fun1(key0,...);key3;fun2(key0,...)
23962408
if (fks != null) {
23972409
String[] ks;
23982410
for (String fk : fks) {
@@ -2411,6 +2423,7 @@ else if (whereList != null && whereList.contains(key)) {
24112423
config.setExplain(explain);
24122424
config.setCache(cache);
24132425
config.setFrom(from);
2426+
config.setDistinct(distinct);
24142427
config.setColumn(column == null ? null : cs); //解决总是 config.column != null,总是不能得到 *
24152428
config.setWhere(tableWhere);
24162429

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public interface SQLConfig {
107107
RequestRole getRole();
108108
SQLConfig setRole(RequestRole role);
109109

110+
public boolean isDistinct();
111+
public SQLConfig setDistinct(boolean distinct);
112+
110113
String getDatabase();
111114
SQLConfig setDatabase(String database);
112115

0 commit comments

Comments
 (0)