Skip to content

Commit 4f4a197

Browse files
authored
Merge pull request #251 from fineday009/master
新增datasource关键字
2 parents 2a506fd + b39dda6 commit 4f4a197

File tree

7 files changed

+148
-24
lines changed

7 files changed

+148
-24
lines changed

APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ public JSONObject setUserIdIn(List<Object> list) {
137137

138138
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
139139
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
140+
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
141+
public static final String KEY_DATASOURCE = "@datasource"; //数据源
140142
public static final String KEY_EXPLAIN = "@explain"; //分析 true/false
141143
public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
142-
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
143144
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
144145
public static final String KEY_FROM = "@from"; //FROM语句
145146
public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
@@ -154,9 +155,10 @@ public JSONObject setUserIdIn(List<Object> list) {
154155
TABLE_KEY_LIST = new ArrayList<String>();
155156
TABLE_KEY_LIST.add(KEY_ROLE);
156157
TABLE_KEY_LIST.add(KEY_DATABASE);
158+
TABLE_KEY_LIST.add(KEY_SCHEMA);
159+
TABLE_KEY_LIST.add(KEY_DATASOURCE);
157160
TABLE_KEY_LIST.add(KEY_EXPLAIN);
158161
TABLE_KEY_LIST.add(KEY_CACHE);
159-
TABLE_KEY_LIST.add(KEY_SCHEMA);
160162
TABLE_KEY_LIST.add(KEY_COLUMN);
161163
TABLE_KEY_LIST.add(KEY_FROM);
162164
TABLE_KEY_LIST.add(KEY_COMBINE);
@@ -216,6 +218,20 @@ public JSONObject setRole(String role) {
216218
public JSONObject setDatabase(String database) {
217219
return puts(KEY_DATABASE, database);
218220
}
221+
/**set schema where table was puts
222+
* @param schema
223+
* @return this
224+
*/
225+
public JSONObject setSchema(String schema) {
226+
return puts(KEY_SCHEMA, schema);
227+
}
228+
/**set datasource where table was puts
229+
* @param datasource
230+
* @return this
231+
*/
232+
public JSONObject setDatasource(String datasource) {
233+
return puts(KEY_DATASOURCE, datasource);
234+
}
219235
/**set if return explain informations
220236
* @param explain
221237
* @return
@@ -243,13 +259,6 @@ public JSONObject setCache(Integer cache) {
243259
public JSONObject setCache(String cache) {
244260
return puts(KEY_CACHE, cache);
245261
}
246-
/**set schema where table was puts
247-
* @param schema
248-
* @return this
249-
*/
250-
public JSONObject setSchema(String schema) {
251-
return puts(KEY_SCHEMA, schema);
252-
}
253262

254263
/**set keys need to be returned
255264
* @param keys key0, key1, key2 ...

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ else if (method == PUT && value instanceof JSONArray
287287
if (parser.getGlobleSchema() != null && sqlRequest.get(JSONRequest.KEY_SCHEMA) == null) {
288288
sqlRequest.put(JSONRequest.KEY_SCHEMA, parser.getGlobleSchema());
289289
}
290+
if (parser.getGlobleDatasource() != null && sqlRequest.get(JSONRequest.KEY_DATASOURCE) == null) {
291+
sqlRequest.put(JSONRequest.KEY_DATASOURCE, parser.getGlobleDatasource());
292+
}
293+
290294
if (isSubquery == false) { //解决 SQL 语法报错,子查询不能 EXPLAIN
291295
if (parser.getGlobleExplain() != null && sqlRequest.get(JSONRequest.KEY_EXPLAIN) == null) {
292296
sqlRequest.put(JSONRequest.KEY_EXPLAIN, parser.getGlobleExplain());

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ public AbstractParser<T> setGlobleSchema(String globleSchema) {
199199
public String getGlobleSchema() {
200200
return globleSchema;
201201
}
202+
protected String globleDatasource;
203+
@Override
204+
public String getGlobleDatasource() {
205+
return globleDatasource;
206+
}
207+
public AbstractParser<T> setGlobleDatasource(String globleDatasource) {
208+
this.globleDatasource = globleDatasource;
209+
return this;
210+
}
211+
202212
protected Boolean globleExplain;
203213
public AbstractParser<T> setGlobleExplain(Boolean globleExplain) {
204214
this.globleExplain = globleExplain;
@@ -361,12 +371,14 @@ public JSONObject parseResponse(JSONObject request) {
361371
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
362372
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
363373
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
374+
setGlobleDatasource(requestObject.getString(JSONRequest.KEY_DATASOURCE));
364375
setGlobleExplain(requestObject.getBoolean(JSONRequest.KEY_EXPLAIN));
365376
setGlobleCache(requestObject.getString(JSONRequest.KEY_CACHE));
366377

367378
requestObject.remove(JSONRequest.KEY_FORMAT);
368379
requestObject.remove(JSONRequest.KEY_DATABASE);
369380
requestObject.remove(JSONRequest.KEY_SCHEMA);
381+
requestObject.remove(JSONRequest.KEY_DATASOURCE);
370382
requestObject.remove(JSONRequest.KEY_EXPLAIN);
371383
requestObject.remove(JSONRequest.KEY_CACHE);
372384
} catch (Exception e) {
@@ -1245,6 +1257,7 @@ else if (join != null){
12451257
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_ROLE);
12461258
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATABASE);
12471259
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_SCHEMA);
1260+
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATASOURCE);
12481261
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COLUMN);
12491262
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COMBINE);
12501263
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_GROUP);
@@ -1464,7 +1477,7 @@ public Object getValueByPath(String valuePath) {
14641477
//取出key被valuePath包含的result,再从里面获取key对应的value
14651478
JSONObject parent = null;
14661479
String[] keys = null;
1467-
for (Map.Entry<String,Object> entry : queryResultMap.entrySet()){
1480+
for (Entry<String,Object> entry : queryResultMap.entrySet()){
14681481
String path = entry.getKey();
14691482
if (valuePath.startsWith(path + "/")) {
14701483
try {

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static apijson.JSONObject.KEY_COLUMN;
1010
import static apijson.JSONObject.KEY_COMBINE;
1111
import static apijson.JSONObject.KEY_DATABASE;
12+
import static apijson.JSONObject.KEY_DATASOURCE;
1213
import static apijson.JSONObject.KEY_EXPLAIN;
1314
import static apijson.JSONObject.KEY_FROM;
1415
import static apijson.JSONObject.KEY_GROUP;
@@ -335,6 +336,7 @@ public String getUserIdKey() {
335336
private boolean distinct = false;
336337
private String database; //表所在的数据库类型
337338
private String schema; //表所在的数据库名
339+
private String datasource; //数据源
338340
private String table; //表名
339341
private String alias; //表别名
340342
private String group; //分组方式的字符串数组,','分隔
@@ -549,6 +551,17 @@ public AbstractSQLConfig setSchema(String schema) {
549551
this.schema = schema;
550552
return this;
551553
}
554+
555+
@Override
556+
public String getDatasource() {
557+
return datasource;
558+
}
559+
@Override
560+
public SQLConfig setDatasource(String datasource) {
561+
this.datasource = datasource;
562+
return this;
563+
}
564+
552565
/**请求传进来的Table名
553566
* @return
554567
* @see {@link #getSQLTable()}
@@ -1547,7 +1560,7 @@ public Object getWhere(String key, boolean exactMatch) {
15471560
synchronized (where) {
15481561
if (where != null) {
15491562
int index;
1550-
for (Map.Entry<String,Object> entry : where.entrySet()) {
1563+
for (Entry<String,Object> entry : where.entrySet()) {
15511564
String k = entry.getKey();
15521565
index = k.indexOf(key);
15531566
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
@@ -2502,7 +2515,7 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
25022515
Object value;
25032516

25042517
String idKey = getIdKey();
2505-
for (Map.Entry<String,Object> entry : content.entrySet()) {
2518+
for (Entry<String,Object> entry : content.entrySet()) {
25062519
String key = entry.getKey();
25072520
//避免筛选到全部 value = key == null ? null : content.get(key);
25082521
if (key == null || idKey.equals(key)) {
@@ -2811,12 +2824,14 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
28112824
}
28122825

28132826
String schema = request.getString(KEY_SCHEMA);
2827+
String datasource = request.getString(KEY_DATASOURCE);
28142828

28152829
SQLConfig config = callback.getSQLConfig(method, database, schema, table);
28162830
config.setAlias(alias);
28172831

28182832
config.setDatabase(database); //不删,后面表对象还要用的,必须放在 parseJoin 前
28192833
config.setSchema(schema); //不删,后面表对象还要用的
2834+
config.setDatasource(datasource); //不删,后面表对象还要用的
28202835

28212836
if (isProcedure) {
28222837
return config;
@@ -3387,21 +3402,39 @@ public static interface IdCallback {
33873402
*/
33883403
Object newId(RequestMethod method, String database, String schema, String table);
33893404

3390-
/**获取主键名
3405+
/**已废弃,最早 5.0.0 移除,改用 {@link #getIdKey(String, String, String, String)}
33913406
* @param database
33923407
* @param schema
33933408
* @param table
33943409
* @return
33953410
*/
3411+
@Deprecated
33963412
String getIdKey(String database, String schema, String table);
3413+
3414+
/**获取主键名
3415+
* @param database
3416+
* @param schema
3417+
* @param table
3418+
* @return
3419+
*/
3420+
String getIdKey(String database, String schema, String datasource, String table);
33973421

3398-
/**获取 User 的主键名
3422+
/**已废弃,最早 5.0.0 移除,改用 {@link #getUserIdKey(String, String, String, String)}
33993423
* @param database
34003424
* @param schema
34013425
* @param table
34023426
* @return
34033427
*/
3428+
@Deprecated
34043429
String getUserIdKey(String database, String schema, String table);
3430+
3431+
/**获取 User 的主键名
3432+
* @param database
3433+
* @param schema
3434+
* @param table
3435+
* @return
3436+
*/
3437+
String getUserIdKey(String database, String schema, String datasource, String table);
34053438
}
34063439

34073440
public static interface Callback extends IdCallback {
@@ -3434,11 +3467,21 @@ public Object newId(RequestMethod method, String database, String schema, String
34343467
public String getIdKey(String database, String schema, String table) {
34353468
return KEY_ID;
34363469
}
3470+
3471+
@Override
3472+
public String getIdKey(String database, String schema, String datasource, String table) {
3473+
return getIdKey(database, schema, table);
3474+
}
34373475

34383476
@Override
34393477
public String getUserIdKey(String database, String schema, String table) {
34403478
return KEY_USER_ID;
34413479
}
3480+
3481+
@Override
3482+
public String getUserIdKey(String database, String schema, String datasource, String table) {
3483+
return getUserIdKey(database, schema, table);
3484+
}
34423485

34433486
@Override
34443487
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {

0 commit comments

Comments
 (0)