Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增datasource关键字 #251

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions APIJSONORM/src/main/java/apijson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public JSONObject setUserIdIn(List<Object> list) {

public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
public static final String KEY_DATASOURCE = "@datasource"; //数据源
public static final String KEY_EXPLAIN = "@explain"; //分析 true/false
public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
public static final String KEY_FROM = "@from"; //FROM语句
public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
Expand All @@ -154,9 +155,10 @@ public JSONObject setUserIdIn(List<Object> list) {
TABLE_KEY_LIST = new ArrayList<String>();
TABLE_KEY_LIST.add(KEY_ROLE);
TABLE_KEY_LIST.add(KEY_DATABASE);
TABLE_KEY_LIST.add(KEY_SCHEMA);
TABLE_KEY_LIST.add(KEY_DATASOURCE);
TABLE_KEY_LIST.add(KEY_EXPLAIN);
TABLE_KEY_LIST.add(KEY_CACHE);
TABLE_KEY_LIST.add(KEY_SCHEMA);
TABLE_KEY_LIST.add(KEY_COLUMN);
TABLE_KEY_LIST.add(KEY_FROM);
TABLE_KEY_LIST.add(KEY_COMBINE);
Expand Down Expand Up @@ -216,6 +218,20 @@ public JSONObject setRole(String role) {
public JSONObject setDatabase(String database) {
return puts(KEY_DATABASE, database);
}
/**set schema where table was puts
* @param schema
* @return this
*/
public JSONObject setSchema(String schema) {
return puts(KEY_SCHEMA, schema);
}
/**set datasource where table was puts
* @param datasource
* @return this
*/
public JSONObject setDatasource(String datasource) {
return puts(KEY_DATASOURCE, datasource);
}
/**set if return explain informations
* @param explain
* @return
Expand Down Expand Up @@ -243,13 +259,6 @@ public JSONObject setCache(Integer cache) {
public JSONObject setCache(String cache) {
return puts(KEY_CACHE, cache);
}
/**set schema where table was puts
* @param schema
* @return this
*/
public JSONObject setSchema(String schema) {
return puts(KEY_SCHEMA, schema);
}

/**set keys need to be returned
* @param keys key0, key1, key2 ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ else if (method == PUT && value instanceof JSONArray
if (parser.getGlobleSchema() != null && sqlRequest.get(JSONRequest.KEY_SCHEMA) == null) {
sqlRequest.put(JSONRequest.KEY_SCHEMA, parser.getGlobleSchema());
}
if (parser.getGlobleDatasource() != null && sqlRequest.get(JSONRequest.KEY_DATASOURCE) == null) {
sqlRequest.put(JSONRequest.KEY_DATASOURCE, parser.getGlobleDatasource());
}

if (isSubquery == false) { //解决 SQL 语法报错,子查询不能 EXPLAIN
if (parser.getGlobleExplain() != null && sqlRequest.get(JSONRequest.KEY_EXPLAIN) == null) {
sqlRequest.put(JSONRequest.KEY_EXPLAIN, parser.getGlobleExplain());
Expand Down
15 changes: 14 additions & 1 deletion APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ public AbstractParser<T> setGlobleSchema(String globleSchema) {
public String getGlobleSchema() {
return globleSchema;
}
protected String globleDatasource;
@Override
public String getGlobleDatasource() {
return globleDatasource;
}
public AbstractParser<T> setGlobleDatasource(String globleDatasource) {
this.globleDatasource = globleDatasource;
return this;
}

protected Boolean globleExplain;
public AbstractParser<T> setGlobleExplain(Boolean globleExplain) {
this.globleExplain = globleExplain;
Expand Down Expand Up @@ -361,12 +371,14 @@ public JSONObject parseResponse(JSONObject request) {
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
setGlobleDatasource(requestObject.getString(JSONRequest.KEY_DATASOURCE));
setGlobleExplain(requestObject.getBoolean(JSONRequest.KEY_EXPLAIN));
setGlobleCache(requestObject.getString(JSONRequest.KEY_CACHE));

requestObject.remove(JSONRequest.KEY_FORMAT);
requestObject.remove(JSONRequest.KEY_DATABASE);
requestObject.remove(JSONRequest.KEY_SCHEMA);
requestObject.remove(JSONRequest.KEY_DATASOURCE);
requestObject.remove(JSONRequest.KEY_EXPLAIN);
requestObject.remove(JSONRequest.KEY_CACHE);
} catch (Exception e) {
Expand Down Expand Up @@ -1245,6 +1257,7 @@ else if (join != null){
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_ROLE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATABASE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_SCHEMA);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATASOURCE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COLUMN);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COMBINE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_GROUP);
Expand Down Expand Up @@ -1464,7 +1477,7 @@ public Object getValueByPath(String valuePath) {
//取出key被valuePath包含的result,再从里面获取key对应的value
JSONObject parent = null;
String[] keys = null;
for (Map.Entry<String,Object> entry : queryResultMap.entrySet()){
for (Entry<String,Object> entry : queryResultMap.entrySet()){
String path = entry.getKey();
if (valuePath.startsWith(path + "/")) {
try {
Expand Down
51 changes: 47 additions & 4 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static apijson.JSONObject.KEY_COLUMN;
import static apijson.JSONObject.KEY_COMBINE;
import static apijson.JSONObject.KEY_DATABASE;
import static apijson.JSONObject.KEY_DATASOURCE;
import static apijson.JSONObject.KEY_EXPLAIN;
import static apijson.JSONObject.KEY_FROM;
import static apijson.JSONObject.KEY_GROUP;
Expand Down Expand Up @@ -335,6 +336,7 @@ public String getUserIdKey() {
private boolean distinct = false;
private String database; //表所在的数据库类型
private String schema; //表所在的数据库名
private String datasource; //数据源
private String table; //表名
private String alias; //表别名
private String group; //分组方式的字符串数组,','分隔
Expand Down Expand Up @@ -549,6 +551,17 @@ public AbstractSQLConfig setSchema(String schema) {
this.schema = schema;
return this;
}

@Override
public String getDatasource() {
return datasource;
}
@Override
public SQLConfig setDatasource(String datasource) {
this.datasource = datasource;
return this;
}

/**请求传进来的Table名
* @return
* @see {@link #getSQLTable()}
Expand Down Expand Up @@ -1547,7 +1560,7 @@ public Object getWhere(String key, boolean exactMatch) {
synchronized (where) {
if (where != null) {
int index;
for (Map.Entry<String,Object> entry : where.entrySet()) {
for (Entry<String,Object> entry : where.entrySet()) {
String k = entry.getKey();
index = k.indexOf(key);
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
Expand Down Expand Up @@ -2502,7 +2515,7 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
Object value;

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

String schema = request.getString(KEY_SCHEMA);
String datasource = request.getString(KEY_DATASOURCE);

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

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

if (isProcedure) {
return config;
Expand Down Expand Up @@ -3387,21 +3402,39 @@ public static interface IdCallback {
*/
Object newId(RequestMethod method, String database, String schema, String table);

/**获取主键名
/**已废弃,最早 5.0.0 移除,改用 {@link #getIdKey(String, String, String, String)}
* @param database
* @param schema
* @param table
* @return
*/
@Deprecated
String getIdKey(String database, String schema, String table);

/**获取主键名
* @param database
* @param schema
* @param table
* @return
*/
String getIdKey(String database, String schema, String datasource, String table);

/**获取 User 的主键名
/**已废弃,最早 5.0.0 移除,改用 {@link #getUserIdKey(String, String, String, String)}
* @param database
* @param schema
* @param table
* @return
*/
@Deprecated
String getUserIdKey(String database, String schema, String table);

/**获取 User 的主键名
* @param database
* @param schema
* @param table
* @return
*/
String getUserIdKey(String database, String schema, String datasource, String table);
}

public static interface Callback extends IdCallback {
Expand Down Expand Up @@ -3434,11 +3467,21 @@ public Object newId(RequestMethod method, String database, String schema, String
public String getIdKey(String database, String schema, String table) {
return KEY_ID;
}

@Override
public String getIdKey(String database, String schema, String datasource, String table) {
return getIdKey(database, schema, table);
}

@Override
public String getUserIdKey(String database, String schema, String table) {
return KEY_USER_ID;
}

@Override
public String getUserIdKey(String database, String schema, String datasource, String table) {
return getUserIdKey(database, schema, table);
}

@Override
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
Expand Down
Loading