Skip to content

Commit b590dd9

Browse files
committed
Server:新增支持全局默认模式 @Schema
1 parent 867d808 commit b590dd9

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import apijson.demo.server.model.Privacy;
2525
import apijson.demo.server.model.User;
2626
import zuo.biao.apijson.RequestMethod;
27-
import zuo.biao.apijson.StringUtil;
2827
import zuo.biao.apijson.server.AbstractSQLConfig;
2928
import zuo.biao.apijson.server.Join;
3029
import zuo.biao.apijson.server.SQLConfig;
@@ -39,11 +38,16 @@ public class DemoSQLConfig extends AbstractSQLConfig {
3938

4039
public static final Callback SIMPLE_CALLBACK;
4140

42-
//表名映射,隐藏真实表名,对安全要求很高的表可以这么做
41+
4342
static {
43+
//默认模式名
44+
DEFAULT_SCHEMA = "sys";
45+
46+
//表名映射,隐藏真实表名,对安全要求很高的表可以这么做
4447
TABLE_KEY_MAP.put(User.class.getSimpleName(), "apijson_user");
4548
TABLE_KEY_MAP.put(Privacy.class.getSimpleName(), "apijson_privacy");
4649

50+
//主键名映射
4751
SIMPLE_CALLBACK = new SimpleCallback() {
4852

4953
@Override
@@ -87,11 +91,6 @@ public String getDBAccount() {
8791
public String getDBPassword() {
8892
return DATABASE_POSTGRESQL.equalsIgnoreCase(getDatabase()) ? null : "apijson"; //TODO 改成你自己的,TiDB 默认密码为空字符串 ""
8993
}
90-
@Override
91-
public String getSchema() {
92-
String s = super.getSchema();
93-
return StringUtil.isEmpty(s, true) ? "sys" : s; //TODO 改成你自己的
94-
}
9594

9695

9796
@Override

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ public AbstractParser<T> setGlobleDatabase(String globleDatabase) {
139139
this.globleDatabase = globleDatabase;
140140
return this;
141141
}
142+
protected String globleSchema;
143+
public AbstractParser<T> setGlobleSchema(String globleSchema) {
144+
this.globleSchema = globleSchema;
145+
return this;
146+
}
142147
protected boolean globleFormat;
143148
public AbstractParser<T> setGlobleFormat(Boolean globleFormat) {
144149
this.globleFormat = globleFormat;
@@ -273,9 +278,11 @@ public JSONObject parseResponse(JSONObject request) {
273278

274279
try {
275280
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
281+
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
276282
setGlobleFormat(requestObject.getBooleanValue(JSONRequest.KEY_FORMAT));
277283

278284
requestObject.remove(JSONRequest.KEY_DATABASE);
285+
requestObject.remove(JSONRequest.KEY_SCHEMA);
279286
requestObject.remove(JSONRequest.KEY_FORMAT);
280287
} catch (Exception e) {
281288
return extendErrorResult(requestObject, e);
@@ -335,10 +342,15 @@ public void onVerifyContent() throws Exception {
335342
*/
336343
@Override
337344
public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
338-
Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
345+
//居然导致 @JSONField(serialize = false) 的方法也被执行了,然后 getTablePath 里 对 sch 赋值了 Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
339346
if (config.getDatabase() == null && globleDatabase != null) {
340347
config.setDatabase(globleDatabase);
341348
}
349+
if (config.getSchema() == null && globleSchema != null) {
350+
config.setSchema(globleSchema);
351+
}
352+
353+
Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
342354

343355
if (noVerifyRole == false) {
344356
if (config.getRole() == null) {
@@ -696,10 +708,10 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
696708
query2 = JSONRequest.QUERY_TABLE;
697709
}
698710
else {
699-
// if (isSubquery) {
700-
// throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_QUERY + "!");
701-
// }
702-
711+
// if (isSubquery) {
712+
// throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_QUERY + "!");
713+
// }
714+
703715
switch (query) {
704716
case "0":
705717
case "TABLE":
@@ -730,7 +742,7 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
730742
if (count2 < 0 || count2 > max) {
731743
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_COUNT + ":value 中 value 的值不合法!必须在 0-" + max + " 内 !");
732744
}
733-
745+
734746
request.remove(JSONRequest.KEY_QUERY);
735747
request.remove(JSONRequest.KEY_COUNT);
736748
request.remove(JSONRequest.KEY_PAGE);

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
public abstract class AbstractSQLConfig implements SQLConfig {
6868
private static final String TAG = "AbstractSQLConfig";
6969

70+
public static String DEFAULT_SCHEMA = "sys";
7071

7172
/**
7273
* 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
@@ -211,10 +212,6 @@ public String getQuote() {
211212

212213
@Override
213214
public String getSchema() {
214-
String sqlTable = getSQLTable();
215-
if (StringUtil.isEmpty(schema, true) && (Table.TAG.equals(sqlTable) || Column.TAG.equals(sqlTable)) ) {
216-
return SCHEMA_INFORMATION;
217-
}
218215
return schema;
219216
}
220217
@Override
@@ -252,7 +249,18 @@ public String getSQLTable() {
252249
@Override
253250
public String getTablePath() {
254251
String q = getQuote();
255-
return q + getSchema() + q + "." + q + getSQLTable() + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
252+
253+
String sqlTable = getSQLTable();
254+
String sch = getSchema();
255+
if (StringUtil.isEmpty(sch, true)) {
256+
if ((Table.TAG.equals(sqlTable) || Column.TAG.equals(sqlTable)) ) {
257+
sch = SCHEMA_INFORMATION;
258+
} else {
259+
sch = DEFAULT_SCHEMA;
260+
}
261+
}
262+
263+
return q + sch + q + "." + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
256264
}
257265
@Override
258266
public AbstractSQLConfig setTable(String table) { //Table已经在Parser中校验,所以这里不用防SQL注入

0 commit comments

Comments
 (0)