Skip to content

Commit a3cd772

Browse files
committed
Merge branch 'master' of https://github.com/Tencent/APIJSON
* 'master' of https://github.com/Tencent/APIJSON: Update AbstractSQLExecutor.java Update AbstractSQLExecutor.java Update AbstractSQLExecutor.java
2 parents bb58a25 + 6e73efb commit a3cd772

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

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

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.Time;
2121
import java.sql.Timestamp;
2222
import java.util.ArrayList;
23+
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.HashMap;
2526
import java.util.List;
@@ -312,9 +313,57 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
312313
}
313314
else {
314315
// 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());
317316
// 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+
}
318367

319368
int index = -1;
320369

@@ -785,16 +834,16 @@ protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNu
785834
sqlResultDuration += System.currentTimeMillis() - startTime;
786835

787836
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]+$";
793841
boolean isMatch = Pattern.matches(pattern, key);
794842
if (isMatch) {
795843
key = key.split("\\.")[1];
796844
}
797845
}
846+
798847
return key;
799848
}
800849

@@ -912,9 +961,13 @@ public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exceptio
912961
if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id
913962
statement = getConnection(config).prepareStatement(config.getSQL(config.isPrepared()), Statement.RETURN_GENERATED_KEYS);
914963
}
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+
}
915967
else {
916968
statement = getConnection(config).prepareStatement(config.getSQL(config.isPrepared()));
917969
}
970+
918971
List<Object> valueList = config.isPrepared() ? config.getPreparedValueList() : null;
919972

920973
if (valueList != null && valueList.isEmpty() == false) {

0 commit comments

Comments
 (0)