提交 4ccb02d1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not use parse() in Parser.parseMergeUsing()

上级 bf312e9f
......@@ -1494,17 +1494,6 @@ public class Parser {
}
setSQL(command, "MERGE", start);
// build and prepare the targetMatchQuery ready to test each rows
// existence in the target table (using source row to match)
StringBuilder targetMatchQuerySQL = new StringBuilder("SELECT _ROWID_ FROM ");
appendTableWithSchemaAndAlias(targetMatchQuerySQL, command.getTargetTable(),
command.getTargetTableFilter().getTableAlias());
targetMatchQuerySQL
.append(" WHERE ").append(command.getOnCondition().getSQL());
command.setTargetMatchQuery(
(Select) parse(targetMatchQuerySQL.toString()));
return command;
}
......@@ -1548,18 +1537,6 @@ public class Parser {
}
}
private static void appendTableWithSchemaAndAlias(StringBuilder buff, Table table, String alias) {
if (table instanceof RangeTable) {
buff.append(table.getSQL());
} else {
buff.append(quoteIdentifier(table.getSchema().getName()))
.append('.').append(quoteIdentifier(table.getName()));
}
if (alias != null) {
buff.append(" AS ").append(quoteIdentifier(alias));
}
}
private Insert parseInsert() {
Insert command = new Insert(session);
currentPrepared = command;
......
......@@ -17,6 +17,7 @@ import org.h2.command.Prepared;
import org.h2.engine.Right;
import org.h2.expression.ConditionAndOr;
import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn;
import org.h2.expression.ExpressionVisitor;
import org.h2.message.DbException;
import org.h2.result.ResultInterface;
......@@ -381,11 +382,14 @@ public class MergeUsing extends Prepared {
}
// setup the targetMatchQuery - for detecting if the target row exists
Expression targetMatchCondition = targetMatchQuery.getCondition();
targetMatchCondition.addFilterConditions(sourceTableFilter, true);
targetMatchCondition.mapColumns(sourceTableFilter, 2);
targetMatchCondition = targetMatchCondition.optimize(session);
targetMatchCondition.createIndexConditions(session, sourceTableFilter);
targetMatchQuery = new Select(session);
ArrayList<Expression> expressions = new ArrayList<>(1);
expressions.add(new ExpressionColumn(session.getDatabase(), targetTable.getSchema().getName(),
targetTableFilter.getTableAlias(), "_ROWID_"));
targetMatchQuery.setExpressions(expressions);
targetMatchQuery.addTableFilter(targetTableFilter, true);
targetMatchQuery.addCondition(onCondition);
targetMatchQuery.init();
targetMatchQuery.prepare();
}
......@@ -486,14 +490,6 @@ public class MergeUsing extends Prepared {
this.targetTable = targetTable;
}
public Select getTargetMatchQuery() {
return targetMatchQuery;
}
public void setTargetMatchQuery(Select targetMatchQuery) {
this.targetMatchQuery = targetMatchQuery;
}
// Prepared interface implementations
@Override
......
......@@ -187,3 +187,26 @@ MERGE INTO TEST USING DUAL ON (ID = 1)
DROP TABLE TEST;
> ok
CREATE TABLE TEST(ID INT PRIMARY KEY);
> ok
MERGE INTO TEST USING (SELECT CAST(? AS INT) ID FROM DUAL) S ON (TEST.ID = S.ID)
WHEN NOT MATCHED THEN INSERT (ID) VALUES (S.ID);
{
10
20
30
};
> update count: 3
SELECT * FROM TEST;
> ID
> --
> 10
> 20
> 30
> rows: 3
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论