|
20 | 20 | import java.sql.Time; |
21 | 21 | import java.sql.Timestamp; |
22 | 22 | import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
23 | 24 | import java.util.Collection; |
24 | 25 | import java.util.HashMap; |
25 | 26 | import java.util.List; |
@@ -312,9 +313,57 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws |
312 | 313 | } |
313 | 314 | else { |
314 | 315 | // final boolean cache = config.getCount() != 1; |
315 | | - // TODO 设置初始容量为查到的数据量,解决频繁扩容导致的延迟,貌似只有 rs.last 取 rs.getRow() ? 然后又得 rs.beforeFirst 重置位置以便下方取值 |
316 | | - resultList = new ArrayList<>(config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount()); |
317 | 316 | // Log.d(TAG, "select cache = " + cache + "; resultList" + (resultList == null ? "=" : "!=") + "null"); |
| 317 | + try { // 设置初始容量为查到的数据量,解决频繁扩容导致的延迟,貌似只有 rs.last 取 rs.getRow() ? 然后又得 rs.beforeFirst 重置位置以便下方取值 |
| 318 | + rs.last(); //移到最后一行 |
| 319 | + resultList = new ArrayList<>(rs.getRow()); |
| 320 | + rs.beforeFirst(); |
| 321 | + } |
| 322 | + catch (Throwable e) { |
| 323 | + Log.e(TAG, "try { rs.last(); resultList = new ArrayList<>(rs.getRow()); rs.beforeFirst(); >> } catch (Throwable e) = " + e.getMessage()); |
| 324 | + int capacity; |
| 325 | + if (config.getId() != null) { // id:Object 一定是 AND 条件,最终返回数据最多就这么多 |
| 326 | + capacity = 1; |
| 327 | + } |
| 328 | + else { |
| 329 | + Object idIn = config.getWhere(config.getIdKey() + "{}", true); |
| 330 | + if (idIn instanceof Collection<?>) { // id{}:[] 一定是 AND 条件,最终返回数据最多就这么多 |
| 331 | + capacity = ((Collection<?>) idIn).size(); |
| 332 | + } |
| 333 | + else { // 预估容量 |
| 334 | + capacity = config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount(); |
| 335 | + if (capacity > 100) { |
| 336 | + // 有条件,条件越多过滤数据越多 |
| 337 | + Map<String, List<String>> combine = config.getCombine(); |
| 338 | + |
| 339 | + List<String> andList = combine == null ? null : combine.get("&"); |
| 340 | + int andCondCount = andList == null ? (config.getWhere() == null ? 0 : config.getWhere().size()) : andList.size(); |
| 341 | + |
| 342 | + List<String> orList = combine == null ? null : combine.get("|"); |
| 343 | + int orCondCount = orList == null ? 0 : orList.size(); |
| 344 | + |
| 345 | + List<String> notList = combine == null ? null : combine.get("|"); |
| 346 | + int notCondCount = notList == null ? 0 : notList.size(); |
| 347 | + |
| 348 | + |
| 349 | + // 有分组,分组字段越少过滤数据越多 |
| 350 | + String[] group = StringUtil.split(config.getGroup()); |
| 351 | + int groupCount = group == null ? 0 : group.length; |
| 352 | + if (groupCount > 0 && Arrays.asList(group).contains(config.getIdKey())) { |
| 353 | + groupCount = 0; |
| 354 | + } |
| 355 | + |
| 356 | + capacity /= Math.pow(1.5, Math.log10(capacity) + andCondCount |
| 357 | + + (orCondCount <= 0 ? 0 : 2/orCondCount) // 1: 2.3, 2: 1.5, 3: 1.3, 4: 1.23, 5: 1.18 |
| 358 | + + (notCondCount/5) // 1: 1.08, 2: 1.18, 3: 1.28, 4: 1.38, 1.50 |
| 359 | + + (groupCount <= 0 ? 0 : 10/groupCount) // 1: 57.7, 7.6, 3: 3.9, 4: 2.8, 5: 2.3 |
| 360 | + ); |
| 361 | + capacity += 1; // 避免正好比需要容量少一点点导致多一次扩容,大量数据 System.arrayCopy |
| 362 | + } |
| 363 | + } |
| 364 | + } |
| 365 | + resultList = new ArrayList<>(capacity); |
| 366 | + } |
318 | 367 |
|
319 | 368 | int index = -1; |
320 | 369 |
|
@@ -785,16 +834,16 @@ protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNu |
785 | 834 | sqlResultDuration += System.currentTimeMillis() - startTime; |
786 | 835 |
|
787 | 836 | if (config.isHive()) { |
788 | | - String table_name = config.getTable(); |
789 | | - if (AbstractSQLConfig.TABLE_KEY_MAP.containsKey(table_name)) { |
790 | | - table_name = AbstractSQLConfig.TABLE_KEY_MAP.get(table_name); |
791 | | - } |
792 | | - String pattern = "^" + table_name + "\\." + "[a-zA-Z]+$"; |
| 837 | + String tableName = config.getTable(); |
| 838 | + String realTableName = AbstractSQLConfig.TABLE_KEY_MAP.get(tableName); |
| 839 | + |
| 840 | + String pattern = "^" + (StringUtil.isEmpty(realTableName, true) ? tableName : realTableName) + "\\." + "[a-zA-Z]+$"; |
793 | 841 | boolean isMatch = Pattern.matches(pattern, key); |
794 | 842 | if (isMatch) { |
795 | 843 | key = key.split("\\.")[1]; |
796 | 844 | } |
797 | 845 | } |
| 846 | + |
798 | 847 | return key; |
799 | 848 | } |
800 | 849 |
|
@@ -912,9 +961,13 @@ public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exceptio |
912 | 961 | if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id |
913 | 962 | statement = getConnection(config).prepareStatement(config.getSQL(config.isPrepared()), Statement.RETURN_GENERATED_KEYS); |
914 | 963 | } |
| 964 | + else if (RequestMethod.isGetMethod(config.getMethod(), true)) { |
| 965 | + statement = getConnection(config).prepareStatement(config.getSQL(config.isPrepared()), ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); |
| 966 | + } |
915 | 967 | else { |
916 | 968 | statement = getConnection(config).prepareStatement(config.getSQL(config.isPrepared())); |
917 | 969 | } |
| 970 | + |
918 | 971 | List<Object> valueList = config.isPrepared() ? config.getPreparedValueList() : null; |
919 | 972 |
|
920 | 973 | if (valueList != null && valueList.isEmpty() == false) { |
|
0 commit comments