| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9e501d4 commit c32498b
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,7 @@ | |||
| 34 | 34 | import apijson.RequestMethod; | |
| 35 | 35 | import apijson.RequestRole; | |
| 36 | 36 | import apijson.StringUtil; | |
| 37 | + import apijson.orm.AbstractSQLConfig.IdCallback; | ||
| 37 | 38 | import apijson.orm.exception.ConditionErrorException; | |
| 38 | 39 | import apijson.orm.exception.ConflictException; | |
| 39 | 40 | import apijson.orm.exception.NotExistException; | |
@@ -43,7 +44,7 @@ | |||
| 43 | 44 | /**parser for parsing request to JSONObject | |
| 44 | 45 | * @author Lemon | |
| 45 | 46 | */ | |
| 46 | - public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator { | ||
| 47 | + public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator, IdCallback { | ||
| 47 | 48 | protected static final String TAG = "AbstractParser"; | |
| 48 | 49 | ||
| 49 | 50 | ||
@@ -490,11 +491,25 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers | |||
| 490 | 491 | ||
| 491 | 492 | //获取指定的JSON结构 >>>>>>>>>>>>>> | |
| 492 | 493 | ||
| 494 | + | ||
| 493 | 495 | //JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {} | |
| 494 | - return Structure.parseRequest(method, name, target, request, maxUpdateCount, creator); | ||
| 496 | + return Structure.parseRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), this, creator); | ||
| 495 | 497 | } | |
| 496 | 498 | ||
| 497 | - | ||
| 499 | + @Override | ||
| 500 | + public String getIdKey(String database, String schema, String table) { | ||
| 501 | + return apijson.JSONObject.KEY_ID; | ||
| 502 | + } | ||
| 503 | + @Override | ||
| 504 | + public String getUserIdKey(String database, String schema, String table) { | ||
| 505 | + return apijson.JSONObject.KEY_USER_ID; | ||
| 506 | + } | ||
| 507 | + @Override | ||
| 508 | + public Object newId(RequestMethod method, String database, String schema, String table) { | ||
| 509 | + return System.currentTimeMillis(); | ||
| 510 | + } | ||
| 511 | + | ||
| 512 | + | ||
| 498 | 513 | /**新建带状态内容的JSONObject | |
| 499 | 514 | * @param code | |
| 500 | 515 | * @param msg | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,8 +16,8 @@ | |||
| 16 | 16 | import static apijson.JSONObject.KEY_ID; | |
| 17 | 17 | import static apijson.JSONObject.KEY_JSON; | |
| 18 | 18 | import static apijson.JSONObject.KEY_ORDER; | |
| 19 | - import static apijson.JSONObject.KEY_ROLE; | ||
| 20 | 19 | import static apijson.JSONObject.KEY_RAW; | |
| 20 | + import static apijson.JSONObject.KEY_ROLE; | ||
| 21 | 21 | import static apijson.JSONObject.KEY_SCHEMA; | |
| 22 | 22 | import static apijson.JSONObject.KEY_USER_ID; | |
| 23 | 23 | import static apijson.RequestMethod.DELETE; | |
@@ -48,20 +48,28 @@ | |||
| 48 | 48 | import com.alibaba.fastjson.annotation.JSONField; | |
| 49 | 49 | ||
| 50 | 50 | import apijson.JSON; | |
| 51 | + import apijson.JSONResponse; | ||
| 51 | 52 | import apijson.Log; | |
| 52 | 53 | import apijson.NotNull; | |
| 53 | 54 | import apijson.RequestMethod; | |
| 54 | 55 | import apijson.RequestRole; | |
| 55 | 56 | import apijson.SQL; | |
| 56 | 57 | import apijson.StringUtil; | |
| 57 | 58 | import apijson.orm.exception.NotExistException; | |
| 59 | + import apijson.orm.model.Access; | ||
| 58 | 60 | import apijson.orm.model.Column; | |
| 61 | + import apijson.orm.model.Document; | ||
| 59 | 62 | import apijson.orm.model.ExtendedProperty; | |
| 63 | + import apijson.orm.model.Function; | ||
| 60 | 64 | import apijson.orm.model.PgAttribute; | |
| 61 | 65 | import apijson.orm.model.PgClass; | |
| 66 | + import apijson.orm.model.Request; | ||
| 67 | + import apijson.orm.model.Response; | ||
| 62 | 68 | import apijson.orm.model.SysColumn; | |
| 63 | 69 | import apijson.orm.model.SysTable; | |
| 64 | 70 | import apijson.orm.model.Table; | |
| 71 | + import apijson.orm.model.Test; | ||
| 72 | + import apijson.orm.model.TestRecord; | ||
| 65 | 73 | ||
| 66 | 74 | /**config sql for JSON Request | |
| 67 | 75 | * @author Lemon | |
@@ -77,6 +85,7 @@ public abstract class AbstractSQLConfig implements SQLConfig { | |||
| 77 | 85 | * 表名映射,隐藏真实表名,对安全要求很高的表可以这么做 | |
| 78 | 86 | */ | |
| 79 | 87 | public static final Map<String, String> TABLE_KEY_MAP; | |
| 88 | + public static final List<String> CONFIG_TABLE_LIST; | ||
| 80 | 89 | public static final List<String> DATABASE_LIST; | |
| 81 | 90 | // 自定义where条件拼接 | |
| 82 | 91 | public static final Map<String, String> RAW_MAP; | |
@@ -89,6 +98,16 @@ public abstract class AbstractSQLConfig implements SQLConfig { | |||
| 89 | 98 | TABLE_KEY_MAP.put(SysTable.class.getSimpleName(), SysTable.TABLE_NAME); | |
| 90 | 99 | TABLE_KEY_MAP.put(SysColumn.class.getSimpleName(), SysColumn.TABLE_NAME); | |
| 91 | 100 | TABLE_KEY_MAP.put(ExtendedProperty.class.getSimpleName(), ExtendedProperty.TABLE_NAME); | |
| 101 | + | ||
| 102 | + CONFIG_TABLE_LIST = new ArrayList<>(); // Table, Column 等是系统表 AbstractVerifier.SYSTEM_ACCESS_MAP.keySet()); | ||
| 103 | + CONFIG_TABLE_LIST.add(Function.class.getSimpleName()); | ||
| 104 | + CONFIG_TABLE_LIST.add(Request.class.getSimpleName()); | ||
| 105 | + CONFIG_TABLE_LIST.add(Response.class.getSimpleName()); | ||
| 106 | + CONFIG_TABLE_LIST.add(Test.class.getSimpleName()); | ||
| 107 | + CONFIG_TABLE_LIST.add(Access.class.getSimpleName()); | ||
| 108 | + CONFIG_TABLE_LIST.add(Document.class.getSimpleName()); | ||
| 109 | + CONFIG_TABLE_LIST.add(TestRecord.class.getSimpleName()); | ||
| 110 | + | ||
| 92 | 111 | ||
| 93 | 112 | DATABASE_LIST = new ArrayList<>(); | |
| 94 | 113 | DATABASE_LIST.add(DATABASE_MYSQL); | |
@@ -1623,8 +1642,18 @@ public String getCompareString(String key, Object value, String type) throws Exc | |||
| 1623 | 1642 | } | |
| 1624 | 1643 | ||
| 1625 | 1644 | public String getKey(String key) { | |
| 1645 | + if (isTest()) { | ||
| 1646 | + if (key.contains("'")) { // || key.contains("#") || key.contains("--")) { | ||
| 1647 | + throw new IllegalArgumentException("参数 " + key + " 不合法!key 中不允许有单引号 ' !"); | ||
| 1648 | + } | ||
| 1649 | + return getSQLValue(key).toString(); | ||
| 1650 | + } | ||
| 1651 | + | ||
| 1652 | + return getSQLKey(key); | ||
| 1653 | + } | ||
| 1654 | + public String getSQLKey(String key) { | ||
| 1626 | 1655 | String q = getQuote(); | |
| 1627 | - return (isKeyPrefix() ? getAliasWithQuote() + "." : "") + q + key + q; | ||
| 1656 | + return (isKeyPrefix() ? getAliasWithQuote() + "." : "") + q + key + q; | ||
| 1628 | 1657 | } | |
| 1629 | 1658 | ||
| 1630 | 1659 | /** | |
@@ -1636,6 +1665,9 @@ private Object getValue(@NotNull Object value) { | |||
| 1636 | 1665 | preparedValueList.add(value); | |
| 1637 | 1666 | return "?"; | |
| 1638 | 1667 | } | |
| 1668 | + return getSQLValue(value); | ||
| 1669 | + } | ||
| 1670 | + public Object getSQLValue(@NotNull Object value) { | ||
| 1639 | 1671 | // return (value instanceof Number || value instanceof Boolean) && DATABASE_POSTGRESQL.equals(getDatabase()) ? value : "'" + value + "'"; | |
| 1640 | 1672 | return (value instanceof Number || value instanceof Boolean) ? value : "'" + value + "'"; //MySQL 隐式转换用不了索引 | |
| 1641 | 1673 | } | |
@@ -2230,9 +2262,15 @@ public static String getSQL(AbstractSQLConfig config) throws Exception { | |||
| 2230 | 2262 | case DELETE: | |
| 2231 | 2263 | return "DELETE FROM " + tablePath + config.getWhereString(true); | |
| 2232 | 2264 | default: | |
| 2265 | + String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : ""); | ||
| 2266 | + if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) { | ||
| 2267 | + String q = config.getQuote(); // 生成 SELECT ( (24 >=0 AND 24 <3) ) AS `code` LIMIT 1 OFFSET 0 | ||
| 2268 | + return explain + "SELECT " + config.getWhereString(false) + " AS " + q + JSONResponse.KEY_CODE + q + config.getLimitString(); | ||
| 2269 | + } | ||
| 2270 | + | ||
| 2233 | 2271 | config.setPreparedValueList(new ArrayList<Object>()); | |
| 2234 | 2272 | String column = config.getColumnString(); | |
| 2235 | - return (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "") + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config); | ||
| 2273 | + return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config); | ||
| 2236 | 2274 | } | |
| 2237 | 2275 | } | |
| 2238 | 2276 | ||
@@ -2915,16 +2953,7 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理 | |||
| 2915 | 2953 | } | |
| 2916 | 2954 | ||
| 2917 | 2955 | ||
| 2918 | - public static interface Callback { | ||
| 2919 | - /**获取 SQLConfig 的实例 | ||
| 2920 | - * @param method | ||
| 2921 | - * @param database | ||
| 2922 | - * @param schema | ||
| 2923 | - * @param table | ||
| 2924 | - * @return | ||
| 2925 | - */ | ||
| 2926 | - SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table); | ||
| 2927 | - | ||
| 2956 | + public static interface IdCallback { | ||
| 2928 | 2957 | /**为 post 请求新建 id, 只能是 Long 或 String | |
| 2929 | 2958 | * @param method | |
| 2930 | 2959 | * @param database | |
@@ -2949,7 +2978,18 @@ public static interface Callback { | |||
| 2949 | 2978 | * @return | |
| 2950 | 2979 | */ | |
| 2951 | 2980 | String getUserIdKey(String database, String schema, String table); | |
| 2952 | - | ||
| 2981 | + } | ||
| 2982 | + | ||
| 2983 | + public static interface Callback extends IdCallback { | ||
| 2984 | + /**获取 SQLConfig 的实例 | ||
| 2985 | + * @param method | ||
| 2986 | + * @param database | ||
| 2987 | + * @param schema | ||
| 2988 | + * @param table | ||
| 2989 | + * @return | ||
| 2990 | + */ | ||
| 2991 | + SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table); | ||
| 2992 | + | ||
| 2953 | 2993 | /**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value | |
| 2954 | 2994 | * @param combine | |
| 2955 | 2995 | * @param key | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,21 +9,26 @@ | |||
| 9 | 9 | * @author Lemon | |
| 10 | 10 | */ | |
| 11 | 11 | public enum Operation { | |
| 12 | + /** | ||
| 13 | + * 必须传的字段,结构是 | ||
| 14 | + * "key0,key1,key2..." | ||
| 15 | + */ | ||
| 16 | + MUST, | ||
| 17 | + /** | ||
| 18 | + * @deprecated 用 MUST 代替,最早可能 4.5.0 移除 | ||
| 19 | + */ | ||
| 20 | + NECESSARY, | ||
| 12 | 21 | ||
| 13 | 22 | /** | |
| 14 | 23 | * 不允许传的字段,结构是 | |
| 15 | 24 | * "key0,key1,key2..." | |
| 16 | - * TODO 改成 MUST 减少长度 ? | ||
| 17 | 25 | */ | |
| 18 | - DISALLOW, | ||
| 19 | - | ||
| 26 | + REFUSE, | ||
| 20 | 27 | /** | |
| 21 | - * 必须传的字段,结构是 | ||
| 22 | - * "key0,key1,key2..." | ||
| 23 | - * TODO 改成 REFUSE 减少长度 ? | ||
| 28 | + * @deprecated 用 REFUSE 代替,最早可能 4.5.0 移除 | ||
| 24 | 29 | */ | |
| 25 | - NECESSARY, | ||
| 26 | - | ||
| 30 | + DISALLOW, | ||
| 31 | + | ||
| 27 | 32 | ||
| 28 | 33 | /**TODO 是否应该把数组类型写成 BOOLEANS, NUMBERS 等复数单词,以便抽取 enum ?扩展用 VERIFY 或 INSERT/UPDATE 远程函数等 | |
| 29 | 34 | * 验证是否符合预设的类型: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments