Skip to content

Commit

Permalink
optimize: repeat pk in after image sql for INSERT_ON_DUPLICATE_UPDATE (
Browse files Browse the repository at this point in the history
  • Loading branch information
renliangyu857 authored Nov 1, 2022
1 parent 84e8756 commit 19a41a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#4974](https://github.com/seata/seata/pull/4974)] optimize cancel the limit on the number of globalStatus queries in Redis mode
- [[#4981](https://github.com/seata/seata/pull/4981)] optimize tcc fence record not exists errMessage
- [[#4985](https://github.com/seata/seata/pull/4985)] fix undo_log id repeat
- [[#4995](https://github.com/seata/seata/pull/4995)] fix mysql InsertOnDuplicateUpdate duplicate pk condition in after image query sql


### test:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- [[#4962](https://github.com/seata/seata/pull/4962)] 优化构建配置,并修正docker镜像的基础镜像
- [[#4974](https://github.com/seata/seata/pull/4974)] 取消redis模式下,查询globalStatus条数的限制
- [[#4981](https://github.com/seata/seata/pull/4981)] 优化当tcc栅栏记录查不到时的错误提示
- [[#4995](https://github.com/seata/seata/pull/4995)] 修复mysql InsertOnDuplicateUpdate后置镜像查询SQL中重复的主键查询条件

### test:
- [[#4794](https://github.com/seata/seata/pull/4794)] 重构代码,尝试修复单元测试 `DataSourceProxyTest.getResourceIdTest()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Collections;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -84,6 +86,11 @@ public ArrayList<List<Object>> getParamAppenderList() {
*/
private ArrayList<List<Object>> paramAppenderList;

/**
* the primary keys in before image sql. if the primary key is auto increment,the set is empty
*/
private Set<String> primaryKeysInBeforeImageSql = new HashSet<>(4);

public MySQLInsertOnDuplicateUpdateExecutor(StatementProxy statementProxy, StatementCallback statementCallback, SQLRecognizer sqlRecognizer) {
super(statementProxy, statementCallback, sqlRecognizer);
}
Expand Down Expand Up @@ -224,8 +231,10 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException
for (int i = 0; i < rows.size(); i++) {
List<String> wherePrimaryList = new ArrayList<>();
primaryValueMap.forEach((k, v) -> {
wherePrimaryList.add(k + " = ? ");
primaryValues.add(v);
if (!primaryKeysInBeforeImageSql.contains(k)) {
wherePrimaryList.add(k + " = ? ");
primaryValues.add(v);
}
});
afterImageSql.append(" OR (").append(Joiner.on(" and ").join(wherePrimaryList)).append(") ");
}
Expand Down Expand Up @@ -305,6 +314,9 @@ public String buildImageSQL(TableMeta tableMeta) {
List<Object> imageParameters = imageParameterMap.get(columnName);
if (imageParameters == null && m.getColumnDef() != null) {
uniqueList.add(columnName + " = DEFAULT(" + columnName + ") ");
if ("PRIMARY".equalsIgnoreCase(k)) {
primaryKeysInBeforeImageSql.add(columnName);
}
columnIsNull = false;
continue;
}
Expand All @@ -317,6 +329,9 @@ public String buildImageSQL(TableMeta tableMeta) {
}
break;
}
if ("PRIMARY".equalsIgnoreCase(k)) {
primaryKeysInBeforeImageSql.add(columnName);
}
columnIsNull = false;
uniqueList.add(columnName + " = ? ");
paramAppenderTempList.add(imageParameters.get(finalI));
Expand Down

0 comments on commit 19a41a0

Please sign in to comment.