提交 7f6eef7b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Remove dead code from MergeUsing and refactor its merge() method

上级 cef43b34
...@@ -202,41 +202,28 @@ public class MergeUsing extends Prepared { ...@@ -202,41 +202,28 @@ public class MergeUsing extends Prepared {
protected void merge(Row sourceRow) { protected void merge(Row sourceRow) {
// put the column values into the table filter // put the column values into the table filter
sourceTableFilter.set(sourceRow); sourceTableFilter.set(sourceRow);
if (isTargetRowFound()) {
// Is the target row there already ?
boolean rowFound = isTargetRowFound();
// try and perform an update
int rowUpdateCount = 0;
if (rowFound) {
if (updateCommand != null) { if (updateCommand != null) {
rowUpdateCount += updateCommand.update(); countUpdatedRows += updateCommand.update();
} }
if (deleteCommand != null) {
int deleteRowUpdateCount = deleteCommand.update();
// under oracle rules these updates & delete combinations are // under oracle rules these updates & delete combinations are
// allowed together // allowed together
if (rowUpdateCount == 1 && deleteRowUpdateCount == 1) { if (deleteCommand != null) {
countUpdatedRows += deleteRowUpdateCount; countUpdatedRows += deleteCommand.update();
deleteRowUpdateCount = 0;
} else {
rowUpdateCount += deleteRowUpdateCount;
}
} }
} else { } else {
// if either updates do nothing, try an insert if (insertCommand != null) {
if (rowUpdateCount == 0) { int count = insertCommand.update();
rowUpdateCount += addRowByCommandInsert(sourceRow); if (!isTargetRowFound()) {
} else if (rowUpdateCount != 1) { throw DbException.get(ErrorCode.GENERAL_ERROR_1,
throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Expected to find key after row inserted, but none found. "
"Duplicate key inserted " + rowUpdateCount + "Insert does not match ON condition.:"
+ " rows at once, only 1 expected:" + targetTable.getSQL() + ":source row="
+ targetTable.getSQL()); + Arrays.asList(sourceRow.getValueList()));
}
countUpdatedRows += count;
} }
} }
countUpdatedRows += rowUpdateCount;
} }
private boolean isTargetRowFound() { private boolean isTargetRowFound() {
...@@ -281,20 +268,6 @@ public class MergeUsing extends Prepared { ...@@ -281,20 +268,6 @@ public class MergeUsing extends Prepared {
} }
} }
private int addRowByCommandInsert(Row sourceRow) {
int localCount = 0;
if (insertCommand != null) {
localCount += insertCommand.update();
if (!isTargetRowFound()) {
throw DbException.get(ErrorCode.GENERAL_ERROR_1,
"Expected to find key after row inserted, but none found. Insert does not match ON condition.:"
+ targetTable.getSQL() + ":source row="
+ Arrays.asList(sourceRow.getValueList()));
}
}
return localCount;
}
// Use the regular merge syntax as our plan SQL // Use the regular merge syntax as our plan SQL
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论