提交 24e903c6 authored 作者: Owner's avatar Owner

Cleanup of MergeUsing useless stuff copied from Merge

上级 2ac7c44d
...@@ -19,7 +19,6 @@ import org.h2.engine.Session; ...@@ -19,7 +19,6 @@ import org.h2.engine.Session;
import org.h2.expression.ConditionAndOr; import org.h2.expression.ConditionAndOr;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.ExpressionVisitor; import org.h2.expression.ExpressionVisitor;
import org.h2.expression.Parameter;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.result.ResultInterface; import org.h2.result.ResultInterface;
import org.h2.result.Row; import org.h2.result.Row;
...@@ -44,7 +43,6 @@ public class MergeUsing extends Prepared { ...@@ -44,7 +43,6 @@ public class MergeUsing extends Prepared {
private Column[] keys; private Column[] keys;
private final ArrayList<Expression[]> valuesExpressionList = New.arrayList(); private final ArrayList<Expression[]> valuesExpressionList = New.arrayList();
private Query query; private Query query;
private Prepared update;
// MergeUsing fields // MergeUsing fields
private TableFilter sourceTableFilter; private TableFilter sourceTableFilter;
...@@ -68,6 +66,8 @@ public class MergeUsing extends Prepared { ...@@ -68,6 +66,8 @@ public class MergeUsing extends Prepared {
@Override @Override
public int update() { public int update() {
// clear list of source table keys we have processed already
sourceKeysRemembered.clear(); sourceKeysRemembered.clear();
if(targetTableFilter!=null){ if(targetTableFilter!=null){
...@@ -93,7 +93,6 @@ public class MergeUsing extends Prepared { ...@@ -93,7 +93,6 @@ public class MergeUsing extends Prepared {
countInputRows++; countInputRows++;
Value[] sourceRowValues = rows.currentRow(); Value[] sourceRowValues = rows.currentRow();
Row sourceRow = new RowImpl(sourceRowValues,0); Row sourceRow = new RowImpl(sourceRowValues,0);
Row newTargetRow = targetTable.getTemplateRow();
ArrayList<Value> sourceKeyValuesList = new ArrayList<Value>(); ArrayList<Value> sourceKeyValuesList = new ArrayList<Value>();
setCurrentRowNumber(countInputRows); setCurrentRowNumber(countInputRows);
...@@ -120,19 +119,7 @@ public class MergeUsing extends Prepared { ...@@ -120,19 +119,7 @@ public class MergeUsing extends Prepared {
sourceKeysRemembered.put(sourceKeyValuesList,countInputRows); sourceKeysRemembered.put(sourceKeyValuesList,countInputRows);
} }
merge(sourceRow, sourceRowValues);
// compute the new target row columns values
for (int j = 0; j < columns.length; j++) {
Column c = columns[j];
int index = c.getColumnId();
try {
Value v = c.convert(sourceRowValues[j]);
newTargetRow.setValue(index, v);
} catch (DbException ex) {
throw setRow(ex, countInputRows, getSQL(sourceRowValues));
}
}
merge(sourceRow, sourceRowValues, newTargetRow);
} }
rows.close(); rows.close();
targetTable.fire(session, evaluateTriggerMasks(), false); targetTable.fire(session, evaluateTriggerMasks(), false);
...@@ -164,12 +151,14 @@ public class MergeUsing extends Prepared { ...@@ -164,12 +151,14 @@ public class MergeUsing extends Prepared {
if(deleteCommand!=null){ if(deleteCommand!=null){
session.getUser().checkRight(targetTable, Right.DELETE); session.getUser().checkRight(targetTable, Right.DELETE);
} }
// check the underlying tables
session.getUser().checkRight(targetTable, Right.SELECT);
session.getUser().checkRight(sourceTableFilter.getTable(), Right.SELECT);
} }
protected void merge(Row sourceRow, Value[] sourceRowValues, Row newTargetRow) { protected void merge(Row sourceRow, Value[] sourceRowValues) {
configPreparedParameters(newTargetRow, update); // put the column values into the table filter
// put the column values into the table filter
sourceTableFilter.set(sourceRow); sourceTableFilter.set(sourceRow);
// try and perform an update // try and perform an update
...@@ -191,36 +180,16 @@ public class MergeUsing extends Prepared { ...@@ -191,36 +180,16 @@ public class MergeUsing extends Prepared {
// if either updates do nothing, try an insert // if either updates do nothing, try an insert
if (rowUpdateCount == 0) { if (rowUpdateCount == 0) {
rowUpdateCount+=addRowByCommandInsert(session,newTargetRow); rowUpdateCount+=addRowByCommandInsert(session);
} else if (rowUpdateCount != 1) { } else if (rowUpdateCount != 1) {
throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Duplicate key updated "+rowUpdateCount+" rows at once, only 1 expected:"+targetTable.getSQL()); throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Duplicate key updated "+rowUpdateCount+" rows at once, only 1 expected:"+targetTable.getSQL());
} }
countUpdatedRows+=rowUpdateCount; countUpdatedRows+=rowUpdateCount;
} }
private void configPreparedParameters(Row newTargetRow, Prepared updatePrepared) {
ArrayList<Parameter> k = updatePrepared.getParameters();
// set each parameter in the updatePrepared with the real value from the source column
// 0 to columns.length-1
for (int i = 0; i < columns.length; i++) {
Column col = columns[i];
Value v = newTargetRow.getValue(col.getColumnId());
Parameter p = k.get(i);
p.setValue(v);
}
// columns.length to columns.length+keys.length-1
for (int i = 0; i < keys.length; i++) {
Column col = keys[i];
Value v = newTargetRow.getValue(col.getColumnId());
if (v == null) {
throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
}
Parameter p = k.get(columns.length + i);
p.setValue(v);
}
}
private int addRowByCommandInsert(Session session, Row newTargetRow) { private int addRowByCommandInsert(Session session/*, Row newTargetRow*/) {
int localCount = 0; int localCount = 0;
if(insertCommand!=null){ if(insertCommand!=null){
localCount += insertCommand.update(); localCount += insertCommand.update();
...@@ -228,6 +197,7 @@ public class MergeUsing extends Prepared { ...@@ -228,6 +197,7 @@ public class MergeUsing extends Prepared {
return localCount; return localCount;
} }
// Use the regular merge syntax as our plan SQL
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("MERGE INTO "); StatementBuilder buff = new StatementBuilder("MERGE INTO ");
...@@ -276,10 +246,10 @@ public class MergeUsing extends Prepared { ...@@ -276,10 +246,10 @@ public class MergeUsing extends Prepared {
public void prepare() { public void prepare() {
onCondition.addFilterConditions(sourceTableFilter, true); onCondition.addFilterConditions(sourceTableFilter, true);
onCondition.addFilterConditions(targetTableFilter, true); onCondition.addFilterConditions(targetTableFilter, true);
onCondition.mapColumns(sourceTableFilter, 2); onCondition.mapColumns(sourceTableFilter, 2);
onCondition.mapColumns(targetTableFilter, 1); onCondition.mapColumns(targetTableFilter, 1);
if (keys == null) { if (keys == null) {
HashSet<Column> targetColumns = buildColumnListFromOnCondition(targetTableFilter); HashSet<Column> targetColumns = buildColumnListFromOnCondition(targetTableFilter);
keys = targetColumns.toArray(new Column[0]); keys = targetColumns.toArray(new Column[0]);
...@@ -328,11 +298,8 @@ public class MergeUsing extends Prepared { ...@@ -328,11 +298,8 @@ public class MergeUsing extends Prepared {
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
} }
} }
String sql = buildPreparedSQL();
update = session.prepare(sql);
// Not sure how these sub-prepares will work... // Prepare each of the sub-commands ready to aid in the MERGE collaboration
if(updateCommand!=null){ if(updateCommand!=null){
updateCommand.setSourceTableFilter(sourceTableFilter); updateCommand.setSourceTableFilter(sourceTableFilter);
updateCommand.setCondition(appendOnCondition(updateCommand)); updateCommand.setCondition(appendOnCondition(updateCommand));
...@@ -375,28 +342,7 @@ public class MergeUsing extends Prepared { ...@@ -375,28 +342,7 @@ public class MergeUsing extends Prepared {
} }
return new ConditionAndOr(ConditionAndOr.AND,deleteCommand.getCondition(),onCondition); return new ConditionAndOr(ConditionAndOr.AND,deleteCommand.getCondition(),onCondition);
} }
private String buildPreparedSQL() {
StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(targetTable.getSQL());
if(targetTableFilter.getTableAlias()!=null){
buff.append(" AS "+targetTableFilter.getTableAlias()+" ");
}
buff.append(" SET ");
for (Column c : columns) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append("=?");
}
buff.append(" WHERE ");
buff.resetCount();
for (Column c : keys) {
buff.appendExceptFirst(" AND ");
buff.append(c.getSQL()).append("=?");
}
String sql = buff.toString();
return sql;
}
public void setSourceTableFilter(TableFilter sourceTableFilter) { public void setSourceTableFilter(TableFilter sourceTableFilter) {
this.sourceTableFilter = sourceTableFilter; this.sourceTableFilter = sourceTableFilter;
} }
...@@ -457,8 +403,9 @@ public class MergeUsing extends Prepared { ...@@ -457,8 +403,9 @@ public class MergeUsing extends Prepared {
} }
public void setTargetTable(Table targetTable) { public void setTargetTable(Table targetTable) {
this.targetTable = targetTable; this.targetTable = targetTable;
} }
// Prepared interface
// Prepared interface implementations
@Override @Override
public boolean isTransactional() { public boolean isTransactional() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论