| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5d40217 commit 8df36e2
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,6 +45,7 @@ public enum RequestMethod { | |||
| 45 | 45 | */ | |
| 46 | 46 | DELETE; | |
| 47 | 47 | ||
| 48 | + public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, DELETE}; | ||
| 48 | 49 | ||
| 49 | 50 | /**是否为GET请求方法 | |
| 50 | 51 | * @param method | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,15 +178,15 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str | |||
| 178 | 178 | + "\n请检查函数名和参数数量是否与已定义的函数一致!" | |
| 179 | 179 | + "\n且必须为 function(key0,key1,...) 这种单函数格式!" | |
| 180 | 180 | + "\nfunction必须符合Java函数命名,key是用于在request内取值的键!" | |
| 181 | - + "\n调用时不要有空格!"); | ||
| 181 | + + "\n调用时不要有空格!" + e.getMessage()); | ||
| 182 | 182 | } | |
| 183 | 183 | if (e instanceof InvocationTargetException) { | |
| 184 | 184 | Throwable te = ((InvocationTargetException) e).getTargetException(); | |
| 185 | 185 | if (StringUtil.isEmpty(te.getMessage(), true) == false) { //到处把函数声明throws Exception改成throws Throwable挺麻烦 | |
| 186 | 186 | throw te instanceof Exception ? (Exception) te : new Exception(te.getMessage()); | |
| 187 | 187 | } | |
| 188 | 188 | throw new IllegalArgumentException("字符 " + function + " 对应的远程函数传参类型错误!" | |
| 189 | - + "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 的要求!"); | ||
| 189 | + + "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 的要求!" + e.getMessage()); | ||
| 190 | 190 | } | |
| 191 | 191 | throw e; | |
| 192 | 192 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -908,7 +908,7 @@ public JSONObject onSQLExecute() throws Exception { | |||
| 908 | 908 | && (table.equals(arrayTable)); | |
| 909 | 909 | ||
| 910 | 910 | // 提取并缓存数组主表的列表数据 | |
| 911 | - rawList = (List<JSONObject>) result.remove(SQLExecutor.KEY_RAW_LIST); | ||
| 911 | + rawList = (List<JSONObject>) result.remove(AbstractSQLExecutor.KEY_RAW_LIST); | ||
| 912 | 912 | if (rawList != null) { | |
| 913 | 913 | String arrayPath = parentPath.substring(0, parentPath.lastIndexOf("[]") + 2); | |
| 914 | 914 | ||
@@ -936,7 +936,7 @@ public JSONObject onSQLExecute() throws Exception { | |||
| 936 | 936 | parser.putQueryResult(path, result); // 解决获取关联数据时requestObject里不存在需要的关联数据 | |
| 937 | 937 | ||
| 938 | 938 | if (isSimpleArray && rawList != null) { | |
| 939 | - result.put(SQLExecutor.KEY_RAW_LIST, rawList); | ||
| 939 | + result.put(AbstractSQLExecutor.KEY_RAW_LIST, rawList); | ||
| 940 | 940 | } | |
| 941 | 941 | } | |
| 942 | 942 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,7 +50,7 @@ | |||
| 50 | 50 | /**parser for parsing request to JSONObject | |
| 51 | 51 | * @author Lemon | |
| 52 | 52 | */ | |
| 53 | - public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator { | ||
| 53 | + public abstract class AbstractParser<T extends Object> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator { | ||
| 54 | 54 | protected static final String TAG = "AbstractParser"; | |
| 55 | 55 | ||
| 56 | 56 | /** | |
@@ -72,6 +72,49 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, | |||
| 72 | 72 | public static boolean IS_PRINT_REQUEST_ENDTIME_LOG = false; | |
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | + public static int DEFAULT_QUERY_COUNT = 10; | ||
| 76 | + public static int MAX_QUERY_PAGE = 100; | ||
| 77 | + public static int MAX_QUERY_COUNT = 100; | ||
| 78 | + public static int MAX_UPDATE_COUNT = 10; | ||
| 79 | + public static int MAX_SQL_COUNT = 200; | ||
| 80 | + public static int MAX_OBJECT_COUNT = 5; | ||
| 81 | + public static int MAX_ARRAY_COUNT = 5; | ||
| 82 | + public static int MAX_QUERY_DEPTH = 5; | ||
| 83 | + | ||
| 84 | + @Override | ||
| 85 | + public int getDefaultQueryCount() { | ||
| 86 | + return DEFAULT_QUERY_COUNT; | ||
| 87 | + } | ||
| 88 | + @Override | ||
| 89 | + public int getMaxQueryPage() { | ||
| 90 | + return MAX_QUERY_PAGE; | ||
| 91 | + } | ||
| 92 | + @Override | ||
| 93 | + public int getMaxQueryCount() { | ||
| 94 | + return MAX_QUERY_COUNT; | ||
| 95 | + } | ||
| 96 | + @Override | ||
| 97 | + public int getMaxUpdateCount() { | ||
| 98 | + return MAX_UPDATE_COUNT; | ||
| 99 | + } | ||
| 100 | + @Override | ||
| 101 | + public int getMaxSQLCount() { | ||
| 102 | + return MAX_SQL_COUNT; | ||
| 103 | + } | ||
| 104 | + @Override | ||
| 105 | + public int getMaxObjectCount() { | ||
| 106 | + return MAX_OBJECT_COUNT; | ||
| 107 | + } | ||
| 108 | + @Override | ||
| 109 | + public int getMaxArrayCount() { | ||
| 110 | + return MAX_ARRAY_COUNT; | ||
| 111 | + } | ||
| 112 | + @Override | ||
| 113 | + public int getMaxQueryDepth() { | ||
| 114 | + return MAX_QUERY_DEPTH; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + | ||
| 75 | 118 | /** | |
| 76 | 119 | * method = null | |
| 77 | 120 | */ | |
@@ -1276,7 +1319,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { // | |||
| 1276 | 1319 | */ | |
| 1277 | 1320 | JSONObject fo = i != 0 || arrTableKey == null ? null : parent.getJSONObject(arrTableKey); | |
| 1278 | 1321 | @SuppressWarnings("unchecked") | |
| 1279 | - List<JSONObject> list = fo == null ? null : (List<JSONObject>) fo.remove(SQLExecutor.KEY_RAW_LIST); | ||
| 1322 | + List<JSONObject> list = fo == null ? null : (List<JSONObject>) fo.remove(AbstractSQLExecutor.KEY_RAW_LIST); | ||
| 1280 | 1323 | ||
| 1281 | 1324 | if (list != null && list.isEmpty() == false) { | |
| 1282 | 1325 | isExtract = false; | |
@@ -1629,43 +1672,6 @@ else if (join != null){ | |||
| 1629 | 1672 | return joinList; | |
| 1630 | 1673 | } | |
| 1631 | 1674 | ||
| 1632 | - | ||
| 1633 | - | ||
| 1634 | - | ||
| 1635 | - @Override | ||
| 1636 | - public int getDefaultQueryCount() { | ||
| 1637 | - return DEFAULT_QUERY_COUNT; | ||
| 1638 | - } | ||
| 1639 | - @Override | ||
| 1640 | - public int getMaxQueryPage() { | ||
| 1641 | - return MAX_QUERY_PAGE; | ||
| 1642 | - } | ||
| 1643 | - @Override | ||
| 1644 | - public int getMaxQueryCount() { | ||
| 1645 | - return MAX_QUERY_COUNT; | ||
| 1646 | - } | ||
| 1647 | - @Override | ||
| 1648 | - public int getMaxUpdateCount() { | ||
| 1649 | - return MAX_UPDATE_COUNT; | ||
| 1650 | - } | ||
| 1651 | - @Override | ||
| 1652 | - public int getMaxSQLCount() { | ||
| 1653 | - return MAX_SQL_COUNT; | ||
| 1654 | - } | ||
| 1655 | - @Override | ||
| 1656 | - public int getMaxObjectCount() { | ||
| 1657 | - return MAX_OBJECT_COUNT; | ||
| 1658 | - } | ||
| 1659 | - @Override | ||
| 1660 | - public int getMaxArrayCount() { | ||
| 1661 | - return MAX_ARRAY_COUNT; | ||
| 1662 | - } | ||
| 1663 | - @Override | ||
| 1664 | - public int getMaxQueryDepth() { | ||
| 1665 | - return MAX_QUERY_DEPTH; | ||
| 1666 | - } | ||
| 1667 | - | ||
| 1668 | - | ||
| 1669 | 1675 | /**根据路径取值 | |
| 1670 | 1676 | * @param parent | |
| 1671 | 1677 | * @param pathKeys | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -103,22 +103,21 @@ public abstract class AbstractSQLConfig implements SQLConfig { | |||
| 103 | 103 | public static String PREFFIX_DISTINCT = "DISTINCT "; | |
| 104 | 104 | ||
| 105 | 105 | // * 和 / 不能同时出现,防止 /* */ 段注释! # 和 -- 不能出现,防止行注释! ; 不能出现,防止隔断SQL语句!空格不能出现,防止 CRUD,DROP,SHOW TABLES等语句! | |
| 106 | - private static final Pattern PATTERN_RANGE; | ||
| 107 | - private static final Pattern PATTERN_FUNCTION; | ||
| 106 | + private static Pattern PATTERN_RANGE; | ||
| 107 | + private static Pattern PATTERN_FUNCTION; | ||
| 108 | 108 | ||
| 109 | 109 | /** | |
| 110 | 110 | * 表名映射,隐藏真实表名,对安全要求很高的表可以这么做 | |
| 111 | 111 | */ | |
| 112 | - public static final Map<String, String> TABLE_KEY_MAP; | ||
| 113 | - public static final List<String> CONFIG_TABLE_LIST; | ||
| 114 | - public static final List<String> DATABASE_LIST; | ||
| 112 | + public static Map<String, String> TABLE_KEY_MAP; | ||
| 113 | + public static List<String> CONFIG_TABLE_LIST; | ||
| 114 | + public static List<String> DATABASE_LIST; | ||
| 115 | 115 | ||
| 116 | 116 | // 自定义原始 SQL 片段 Map<key, substring>:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL | |
| 117 | - public static final Map<String, String> RAW_MAP; | ||
| 117 | + public static Map<String, String> RAW_MAP; | ||
| 118 | 118 | // 允许调用的 SQL 函数:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL | |
| 119 | - public static final Map<String, String> SQL_AGGREGATE_FUNCTION_MAP; | ||
| 120 | - public static final Map<String, String> SQL_FUNCTION_MAP; | ||
| 121 | - | ||
| 119 | + public static Map<String, String> SQL_AGGREGATE_FUNCTION_MAP; | ||
| 120 | + public static Map<String, String> SQL_FUNCTION_MAP; | ||
| 122 | 121 | ||
| 123 | 122 | static { // 凡是 SQL 边界符、分隔符、注释符 都不允许,例如 ' " ` ( ) ; # -- /**/ ,以免拼接 SQL 时被注入意外可执行指令 | |
| 124 | 123 | PATTERN_RANGE = Pattern.compile("^[0-9%,!=\\<\\>/\\.\\+\\-\\*\\^]+$"); // ^[a-zA-Z0-9_*%!=<>(),"]+$ 导致 exists(select*from(Comment)) 通过! | |
@@ -4339,7 +4338,7 @@ protected void onGetCrossJoinString(Join j) throws UnsupportedOperationException | |||
| 4339 | 4338 | * @return | |
| 4340 | 4339 | * @throws Exception | |
| 4341 | 4340 | */ | |
| 4342 | - public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback callback) throws Exception { | ||
| 4341 | + public static <T extends Object> SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback<T> callback) throws Exception { | ||
| 4343 | 4342 | if (request == null) { // User:{} 这种空内容在查询时也有效 | |
| 4344 | 4343 | throw new NullPointerException(TAG + ": newSQLConfig request == null!"); | |
| 4345 | 4344 | } | |
@@ -4357,7 +4356,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String | |||
| 4357 | 4356 | String schema = request.getString(KEY_SCHEMA); | |
| 4358 | 4357 | String datasource = request.getString(KEY_DATASOURCE); | |
| 4359 | 4358 | ||
| 4360 | - SQLConfig config = callback.getSQLConfig(method, database, schema, table); | ||
| 4359 | + SQLConfig config = callback.getSQLConfig(method, database, schema, datasource, table); | ||
| 4361 | 4360 | config.setAlias(alias); | |
| 4362 | 4361 | ||
| 4363 | 4362 | config.setDatabase(database); //不删,后面表对象还要用的,必须放在 parseJoin 前 | |
@@ -4404,7 +4403,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String | |||
| 4404 | 4403 | ||
| 4405 | 4404 | Object id = request.get(idKey); | |
| 4406 | 4405 | if (id == null && method == POST) { | |
| 4407 | - id = callback.newId(method, database, schema, table); // null 表示数据库自增 id | ||
| 4406 | + id = callback.newId(method, database, schema, datasource, table); // null 表示数据库自增 id | ||
| 4408 | 4407 | } | |
| 4409 | 4408 | ||
| 4410 | 4409 | if (id != null) { //null无效 | |
@@ -4992,7 +4991,7 @@ else if (newHaving != null) { | |||
| 4992 | 4991 | * @return | |
| 4993 | 4992 | * @throws Exception | |
| 4994 | 4993 | */ | |
| 4995 | - public static SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<Join> joinList, Callback callback) throws Exception { | ||
| 4994 | + public static <T extends Object> SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<Join> joinList, Callback<T> callback) throws Exception { | ||
| 4996 | 4995 | boolean isQuery = RequestMethod.isQueryMethod(method); | |
| 4997 | 4996 | config.setKeyPrefix(isQuery && config.isMain() == false); | |
| 4998 | 4997 | ||
@@ -5203,15 +5202,15 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理 | |||
| 5203 | 5202 | } | |
| 5204 | 5203 | ||
| 5205 | 5204 | ||
| 5206 | - public static interface IdCallback { | ||
| 5205 | + public static interface IdCallback<T extends Object> { | ||
| 5207 | 5206 | /**为 post 请求新建 id, 只能是 Long 或 String | |
| 5208 | 5207 | * @param method | |
| 5209 | 5208 | * @param database | |
| 5210 | 5209 | * @param schema | |
| 5211 | 5210 | * @param table | |
| 5212 | 5211 | * @return | |
| 5213 | 5212 | */ | |
| 5214 | - Object newId(RequestMethod method, String database, String schema, String table); | ||
| 5213 | + T newId(RequestMethod method, String database, String schema, String datasource, String table); | ||
| 5215 | 5214 | ||
| 5216 | 5215 | ||
| 5217 | 5216 | /**获取主键名 | |
@@ -5231,15 +5230,15 @@ public static interface IdCallback { | |||
| 5231 | 5230 | String getUserIdKey(String database, String schema, String datasource, String table); | |
| 5232 | 5231 | } | |
| 5233 | 5232 | ||
| 5234 | - public static interface Callback extends IdCallback { | ||
| 5233 | + public static interface Callback<T extends Object> extends IdCallback<T> { | ||
| 5235 | 5234 | /**获取 SQLConfig 的实例 | |
| 5236 | 5235 | * @param method | |
| 5237 | 5236 | * @param database | |
| 5238 | 5237 | * @param schema | |
| 5239 | 5238 | * @param table | |
| 5240 | 5239 | * @return | |
| 5241 | 5240 | */ | |
| 5242 | - SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table); | ||
| 5241 | + SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table); | ||
| 5243 | 5242 | ||
| 5244 | 5243 | /**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value | |
| 5245 | 5244 | * @param combine | |
@@ -5249,12 +5248,23 @@ public static interface Callback extends IdCallback { | |||
| 5249 | 5248 | public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception; | |
| 5250 | 5249 | } | |
| 5251 | 5250 | ||
| 5252 | - public static abstract class SimpleCallback implements Callback { | ||
| 5251 | + public static Long LAST_ID; | ||
| 5252 | + static { | ||
| 5253 | + LAST_ID = System.currentTimeMillis(); | ||
| 5254 | + } | ||
| 5253 | 5255 | ||
| 5256 | + public static abstract class SimpleCallback<T extends Object> implements Callback<T> { | ||
| 5254 | 5257 | ||
| 5258 | + @SuppressWarnings("unchecked") | ||
| 5255 | 5259 | @Override | |
| 5256 | - public Object newId(RequestMethod method, String database, String schema, String table) { | ||
| 5257 | - return System.currentTimeMillis(); | ||
| 5260 | + public T newId(RequestMethod method, String database, String schema, String datasource, String table) { | ||
| 5261 | + Long id = System.currentTimeMillis(); | ||
| 5262 | + if (id <= LAST_ID) { | ||
| 5263 | + id = LAST_ID + 1; // 解决高并发下 id 冲突导致新增记录失败 | ||
| 5264 | + } | ||
| 5265 | + LAST_ID = id; | ||
| 5266 | + | ||
| 5267 | + return (T) id; | ||
| 5258 | 5268 | } | |
| 5259 | 5269 | ||
| 5260 | 5270 | @Override | |
| Back | FazBrowse Home | New Git URL |
0 commit comments