Skip to content

Commit fb9c267

Browse files
committed
Server:AbstractSQLConfig.Callback 新增 database 和 schema 参数来区分同名表;JOIN 相同的表名给副表加 AS 解决重名语法错误
1 parent f10f3ea commit fb9c267

File tree

7 files changed

+86
-43
lines changed

7 files changed

+86
-43
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/DemoObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public DemoObjectParser setParser(Parser<?> parser) {
6363

6464
@Override
6565
public SQLConfig newSQLConfig(boolean isProcedure) throws Exception {
66-
return DemoSQLConfig.newSQLConfig(method, table, sqlRequest, joinList, isProcedure);
66+
return DemoSQLConfig.newSQLConfig(method, table, alias, sqlRequest, joinList, isProcedure);
6767
}
6868

6969

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/DemoSQLConfig.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ public class DemoSQLConfig extends AbstractSQLConfig {
5151
SIMPLE_CALLBACK = new SimpleCallback() {
5252

5353
@Override
54-
public DemoSQLConfig getSQLConfig(RequestMethod method, String table) {
54+
public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table) {
5555
return new DemoSQLConfig(method, table);
5656
}
5757

5858
//取消注释来实现自定义各个表的主键名
5959
// @Override
60-
// public String getIdKey(String schema, String table) {
60+
// public String getIdKey(String database, String schema, String table) {
6161
// return StringUtil.firstCase(table + "Id"); // userId, comemntId ...
6262
// // return StringUtil.toLowerCase(t) + "_id"; // user_id, comemnt_id ...
6363
// // return StringUtil.toUpperCase(t) + "_ID"; // USER_ID, COMMENT_ID ...
6464
// }
6565

6666
@Override
67-
public String getUserIdKey(String schema, String table) {
67+
public String getUserIdKey(String database, String schema, String table) {
6868
return Controller.USER_.equals(table) || Controller.PRIVACY_.equals(table) ? KEY_ID : KEY_USER_ID; // id / userId
6969
}
7070

@@ -100,12 +100,12 @@ public String getDBPassword() {
100100

101101
@Override
102102
public String getIdKey() {
103-
return SIMPLE_CALLBACK.getIdKey(getSchema(), getTable());
103+
return SIMPLE_CALLBACK.getIdKey(getDatabase(), getSchema(), getTable());
104104
}
105105

106106
@Override
107107
public String getUserIdKey() {
108-
return SIMPLE_CALLBACK.getUserIdKey(getSchema(), getTable());
108+
return SIMPLE_CALLBACK.getUserIdKey(getDatabase(), getSchema(), getTable());
109109
}
110110

111111

@@ -126,13 +126,14 @@ public DemoSQLConfig(RequestMethod method, int count, int page) {
126126

127127
/**获取SQL配置
128128
* @param table
129+
* @param alias
129130
* @param request
130131
* @param isProcedure
131132
* @return
132133
* @throws Exception
133134
*/
134-
public static SQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
135-
return newSQLConfig(method, table, request, joinList, isProcedure, SIMPLE_CALLBACK);
135+
public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
136+
return newSQLConfig(method, table, alias, request, joinList, isProcedure, SIMPLE_CALLBACK);
136137
}
137138

138139

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public AbstractObjectParser setParser(Parser<?> parser) {
7070
protected final boolean isTable;
7171
protected final String path;
7272
protected final String table;
73+
protected final String alias;
7374

7475

7576
protected final boolean tri;
@@ -97,7 +98,10 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
9798
this.type = arrayConfig == null ? 0 : arrayConfig.getType();
9899
this.joinList = arrayConfig == null ? null : arrayConfig.getJoinList();
99100
this.path = AbstractParser.getAbsPath(parentPath, name);
100-
this.table = Pair.parseEntry(name, true).getKey();
101+
102+
zuo.biao.apijson.server.Entry<String, String> entry = Pair.parseEntry(name, true);
103+
this.table = entry.getKey();
104+
this.alias = entry.getValue();
101105
this.isTable = zuo.biao.apijson.JSONObject.isTableKey(table);
102106

103107
this.objectCount = 0;
@@ -884,6 +888,10 @@ public String getTable() {
884888
return table;
885889
}
886890
@Override
891+
public String getAlias() {
892+
return alias;
893+
}
894+
@Override
887895
public SQLConfig getArrayConfig() {
888896
return arrayConfig;
889897
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,9 @@ else if (join != null){
960960

961961
index = path.indexOf("/");
962962
String tableKey = index < 0 ? null : path.substring(0, index); //User:owner
963-
String table = Pair.parseEntry(tableKey, true).getKey(); //User
963+
zuo.biao.apijson.server.Entry<String, String> entry = Pair.parseEntry(tableKey, true);
964+
String table = entry.getKey(); //User
965+
String alias = entry.getValue(); //owner
964966
String key = StringUtil.isEmpty(table, true) ? null : path.substring(index + 1);//id@
965967
if (StringUtil.isEmpty(key, true)) {
966968
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":value 中value不合法!"
@@ -1001,6 +1003,7 @@ else if (join != null){
10011003
j.setOriginValue(targetPath);
10021004
j.setJoinType(joinType);
10031005
j.setName(table);
1006+
j.setAlias(alias);
10041007
j.setTargetName(targetTable);
10051008
j.setTargetKey(targetKey);
10061009
j.setKeyAndType(key);

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

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -298,28 +298,33 @@ public String getTablePath() {
298298
String sqlTable = getSQLTable();
299299
String sch = getSQLSchema(sqlTable);
300300

301-
return (StringUtil.isEmpty(sch, true) ? "" : q + sch + q + ".") + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
301+
return (StringUtil.isEmpty(sch, true) ? "" : q + sch + q + ".") + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAliasWithQuote() : "");
302302
}
303303
@Override
304304
public AbstractSQLConfig setTable(String table) { //Table已经在Parser中校验,所以这里不用防SQL注入
305305
this.table = table;
306306
return this;
307307
}
308+
308309
@Override
309310
public String getAlias() {
310-
if (StringUtil.isEmpty(alias, true)) {
311-
alias = getTable();
312-
}
313-
String q = getQuote();
314-
//getTable 不能小写,因为Verifier用大小写敏感的名称判断权限
315-
//如果要强制小写,则可在子类重写这个方法再 toLowerCase return q + (DATABASE_POSTGRESQL.equals(getDatabase()) ? alias.toLowerCase() : alias) + q;
316-
return q + alias + q;
311+
return alias;
317312
}
318313
@Override
319314
public AbstractSQLConfig setAlias(String alias) {
320315
this.alias = alias;
321316
return this;
322317
}
318+
public String getAliasWithQuote() {
319+
String a = getAlias();
320+
if (StringUtil.isEmpty(a, true)) {
321+
a = getTable();
322+
}
323+
String q = getQuote();
324+
//getTable 不能小写,因为Verifier用大小写敏感的名称判断权限
325+
//如果要强制小写,则可在子类重写这个方法再 toLowerCase return q + (DATABASE_POSTGRESQL.equals(getDatabase()) ? a.toLowerCase() : a) + q;
326+
return q + a + q;
327+
}
323328

324329
@Override
325330
public String getGroup() {
@@ -347,7 +352,9 @@ public String getGroupString(boolean hasPrefix) {
347352
}
348353

349354
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
350-
cfg.setAlias(cfg.getTable());
355+
if (StringUtil.isEmpty(cfg.getAlias(), true)) {
356+
cfg.setAlias(cfg.getTable());
357+
}
351358

352359
c = ((AbstractSQLConfig) cfg).getGroupString(false);
353360
if (StringUtil.isEmpty(c, true) == false) {
@@ -407,7 +414,9 @@ public String getHavingString(boolean hasPrefix) {
407414
}
408415

409416
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
410-
cfg.setAlias(cfg.getTable());
417+
if (StringUtil.isEmpty(cfg.getAlias(), true)) {
418+
cfg.setAlias(cfg.getTable());
419+
}
411420

412421
c = ((AbstractSQLConfig) cfg).getHavingString(false);
413422
if (StringUtil.isEmpty(c, true) == false) {
@@ -515,7 +524,9 @@ public String getOrderString(boolean hasPrefix) {
515524
}
516525

517526
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
518-
cfg.setAlias(cfg.getTable());
527+
if (StringUtil.isEmpty(cfg.getAlias(), true)) {
528+
cfg.setAlias(cfg.getTable());
529+
}
519530

520531
c = ((AbstractSQLConfig) cfg).getOrderString(false);
521532
if (StringUtil.isEmpty(c, true) == false) {
@@ -648,8 +659,10 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
648659
else {
649660
cfg = j.getJoinConfig();
650661
}
651-
652-
cfg.setAlias(cfg.getTable());
662+
663+
if (StringUtil.isEmpty(cfg.getAlias(), true)) {
664+
cfg.setAlias(cfg.getTable());
665+
}
653666

654667
c = ((AbstractSQLConfig) cfg).getColumnString(true);
655668
if (StringUtil.isEmpty(c, true) == false) {
@@ -661,7 +674,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
661674
}
662675
}
663676

664-
String tableAlias = getAlias();
677+
String tableAlias = getAliasWithQuote();
665678

666679
// String c = StringUtil.getString(column); //id,name;json_length(contactIdList):contactCount;...
667680

@@ -1399,7 +1412,7 @@ public String getCompareString(String key, Object value, String type) throws Exc
13991412

14001413
public String getKey(String key) {
14011414
String q = getQuote();
1402-
return (isKeyPrefix() ? getAlias() + "." : "") + q + key + q;
1415+
return (isKeyPrefix() ? getAliasWithQuote() + "." : "") + q + key + q;
14031416
}
14041417

14051418
/**
@@ -2001,7 +2014,7 @@ private static String getConditionString(String column, String table, AbstractSQ
20012014

20022015
Subquery from = config.getFrom();
20032016
if (from != null) {
2004-
table = config.getSubqueryString(from) + " AS " + config.getAlias() + " "; //TODO Comment:c 转为 AS `Comment:c`
2017+
table = config.getSubqueryString(from) + " AS " + config.getAliasWithQuote() + " ";
20052018
}
20062019

20072020
String condition = table + config.getJoinString() + where + (
@@ -2081,7 +2094,7 @@ public String getJoinString() throws Exception {
20812094
jc = j.getJoinConfig();
20822095
jc.setPrepared(isPrepared());
20832096

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

20872100
//如果要强制小写,则可在子类重写这个方法再 toLowerCase
@@ -2144,14 +2157,16 @@ public String getJoinString() throws Exception {
21442157
* @return
21452158
* @throws Exception
21462159
*/
2147-
public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Join> joinList, boolean isProcedure, Callback callback) throws Exception {
2160+
public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback callback) throws Exception {
21482161
if (request == null) { // User:{} 这种空内容在查询时也有效
21492162
throw new NullPointerException(TAG + ": newSQLConfig request == null!");
21502163
}
2151-
AbstractSQLConfig config = callback.getSQLConfig(method, table);
2152-
21532164
String database = request.getString(KEY_DATABASE);
21542165
String schema = request.getString(KEY_SCHEMA);
2166+
2167+
AbstractSQLConfig config = callback.getSQLConfig(method, database, schema, table);
2168+
config.setAlias(alias);
2169+
21552170
config.setDatabase(database); //不删,后面表对象还要用的,必须放在 parseJoin 前
21562171
config.setSchema(schema); //不删,后面表对象还要用的
21572172

@@ -2168,9 +2183,9 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
21682183
return config;
21692184
}
21702185

2171-
String idKey = callback.getIdKey(schema, table);
2186+
String idKey = callback.getIdKey(database, schema, table);
21722187
String idInKey = idKey + "{}";
2173-
String userIdKey = callback.getUserIdKey(schema, table);
2188+
String userIdKey = callback.getUserIdKey(database, schema, table);
21742189
String userIdInKey = userIdKey + "{}";
21752190

21762191
Object idIn = request.get(idInKey); //可能是 id{}:">0"
@@ -2181,7 +2196,7 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
21812196
throw new IllegalArgumentException("POST请求,生成多条记录请用 id{}:[] ! [] 类型为JSONArray且不能为空!");
21822197
}
21832198
} else if (request.get(idKey) == null) {
2184-
request.put(idKey, callback.newId(method, table));
2199+
request.put(idKey, callback.newId(method, database, schema, table));
21852200
}
21862201
}
21872202

@@ -2478,11 +2493,13 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
24782493

24792494

24802495
String name;
2496+
String alias;
24812497
for (Join j : joinList) {
24822498
name = j.getName();
2499+
alias = j.getAlias();
24832500
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
2484-
SQLConfig joinConfig = newSQLConfig(method, name, j.getTable(), null, false, callback);
2485-
SQLConfig cacheConfig = newSQLConfig(method, name, j.getTable(), null, false, callback).setCount(1);
2501+
SQLConfig joinConfig = newSQLConfig(method, name, alias, j.getTable(), null, false, callback);
2502+
SQLConfig cacheConfig = newSQLConfig(method, name, alias, j.getTable(), null, false, callback).setCount(1);
24862503

24872504
if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
24882505
if (joinConfig.getDatabase() == null) {
@@ -2504,7 +2521,7 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
25042521
joinConfig.setMain(false).setKeyPrefix(true);
25052522

25062523
if (j.isLeftOrRightJoin()) {
2507-
SQLConfig outterConfig = newSQLConfig(method, name, j.getOutter(), null, false, callback);
2524+
SQLConfig outterConfig = newSQLConfig(method, name, alias, j.getOutter(), null, false, callback);
25082525
outterConfig.setMain(false).setKeyPrefix(true).setDatabase(joinConfig.getDatabase()).setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
25092526
j.setOutterConfig(outterConfig);
25102527
}
@@ -2645,49 +2662,55 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
26452662
public static interface Callback {
26462663
/**获取 SQLConfig 的实例
26472664
* @param method
2665+
* @param database
2666+
* @param schema
26482667
* @param table
26492668
* @return
26502669
*/
2651-
AbstractSQLConfig getSQLConfig(RequestMethod method, String table);
2670+
AbstractSQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table);
26522671

26532672

26542673
/**为 post 请求新建 id, 只能是 Long 或 String
26552674
* @param method
2675+
* @param database
2676+
* @param schema
26562677
* @param table
26572678
* @return
26582679
*/
2659-
Object newId(RequestMethod method, String table);
2680+
Object newId(RequestMethod method, String database, String schema, String table);
26602681

26612682
/**获取主键名
2683+
* @param database
26622684
* @param schema
26632685
* @param table
26642686
* @return
26652687
*/
2666-
String getIdKey(String schema, String table);
2688+
String getIdKey(String database, String schema, String table);
26672689

26682690
/**获取 User 的主键名
2691+
* @param database
26692692
* @param schema
26702693
* @param table
26712694
* @return
26722695
*/
2673-
String getUserIdKey(String schema, String table);
2696+
String getUserIdKey(String database, String schema, String table);
26742697
}
26752698

26762699
public static abstract class SimpleCallback implements Callback {
26772700

26782701

26792702
@Override
2680-
public Object newId(RequestMethod method, String table) {
2703+
public Object newId(RequestMethod method, String database, String schema, String table) {
26812704
return System.currentTimeMillis();
26822705
}
26832706

26842707
@Override
2685-
public String getIdKey(String schema, String table) {
2708+
public String getIdKey(String database, String schema, String table) {
26862709
return KEY_ID;
26872710
}
26882711

26892712
@Override
2690-
public String getUserIdKey(String schema, String table) {
2713+
public String getUserIdKey(String database, String schema, String table) {
26912714
return KEY_USER_ID;
26922715
}
26932716

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class Join {
3232
private String relateType; // "" - 一对一, "{}" - 一对多, "<>" - 多对一
3333
private JSONObject table; // { "id@":"/Moment/userId" }
3434
private String name; //User
35+
private String alias; //owner
3536
private String key; //id
3637
private String targetName; // Moment
3738
private String targetKey; // userId
@@ -81,6 +82,12 @@ public String getName() {
8182
public void setName(String name) {
8283
this.name = name;
8384
}
85+
public String getAlias() {
86+
return alias;
87+
}
88+
public void setAlias(String alias) {
89+
this.alias = alias;
90+
}
8491
public JSONObject getTable() {
8592
return table;
8693
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public interface ObjectParser {
140140
boolean isTable();
141141
String getPath();
142142
String getTable();
143+
String getAlias();
143144
SQLConfig getArrayConfig();
144145

145146
SQLConfig getSQLConfig();

0 commit comments

Comments
 (0)