Unverified 提交 83e4f3bd authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1011 from katzyn/mergeUsing

Handle tables with specified schema in Parser.parseMergeUsing()
......@@ -21,6 +21,10 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Issue #1010: MERGE USING table not found with qualified table
</li>
<li>PR #1009: Fix ARRAY_AGG with ORDER BY and refactor aggregates
</li>
<li>Issue #1006: "Empty enums are not allowed" in 1.4.197 (mode=MYSQL)
</li>
<li>PR #1007: Copy also SRID in ValueGeometry.getGeometry()
......
......@@ -1150,11 +1150,8 @@ public class Parser {
TableFilter sourceTableFilter = readSimpleTableFilter(0, excludeIdentifiers);
command.setSourceTableFilter(sourceTableFilter);
StringBuilder buff = new StringBuilder("SELECT * FROM ")
.append(sourceTableFilter.getTable().getName());
if (sourceTableFilter.getTableAlias() != null) {
buff.append(" AS ").append(sourceTableFilter.getTableAlias());
}
StringBuilder buff = new StringBuilder("SELECT * FROM ");
appendTableWithSchemaAndAlias(buff, sourceTableFilter.getTable(), sourceTableFilter.getTableAlias());
Prepared preparedQuery = prepare(session, buff.toString(), null/*paramValues*/);
command.setQuery((Select) preparedQuery);
......@@ -1197,11 +1194,9 @@ public class Parser {
// 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 " + command.getTargetTable().getName());
if (command.getTargetTableFilter().getTableAlias() != null) {
targetMatchQuerySQL.append(" AS ").append(command.getTargetTableFilter().getTableAlias());
}
StringBuilder targetMatchQuerySQL = new StringBuilder("SELECT _ROWID_ FROM ");
appendTableWithSchemaAndAlias(targetMatchQuerySQL, command.getTargetTable(),
command.getTargetTableFilter().getTableAlias());
targetMatchQuerySQL
.append(" WHERE ").append(command.getOnCondition().getSQL());
command.setTargetMatchQuery(
......@@ -1210,6 +1205,14 @@ public class Parser {
return command;
}
private static void appendTableWithSchemaAndAlias(StringBuilder buff, Table table, String alias) {
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;
......
......@@ -31,3 +31,44 @@ EXPLAIN PLAN
> PLAN
> ---------------------------------------------------------------------------------------------------------------------------------
> MERGE INTO PUBLIC.PARENT(ID, NAME) KEY(ID) SELECT X AS ID, ('Coco' || X) AS NAME FROM SYSTEM_RANGE(1, 2) /* PUBLIC.RANGE_INDEX */
DROP TABLE PARENT;
> ok
CREATE SCHEMA SOURCESCHEMA;
> ok
CREATE TABLE SOURCESCHEMA.SOURCE(ID INT PRIMARY KEY, VALUE INT);
> ok
INSERT INTO SOURCESCHEMA.SOURCE VALUES (1, 10), (3, 30), (5, 50);
> update count: 3
CREATE SCHEMA DESTSCHEMA;
> ok
CREATE TABLE DESTSCHEMA.DESTINATION(ID INT PRIMARY KEY, VALUE INT);
> ok
INSERT INTO DESTSCHEMA.DESTINATION VALUES (3, 300), (6, 600);
> update count: 2
MERGE INTO DESTSCHEMA.DESTINATION USING SOURCESCHEMA.SOURCE ON (DESTSCHEMA.DESTINATION.ID = SOURCESCHEMA.SOURCE.ID)
WHEN MATCHED THEN UPDATE SET VALUE = SOURCESCHEMA.SOURCE.VALUE
WHEN NOT MATCHED THEN INSERT (ID, VALUE) VALUES (SOURCESCHEMA.SOURCE.ID, SOURCESCHEMA.SOURCE.VALUE);
> update count: 3
SELECT * FROM DESTSCHEMA.DESTINATION;
> ID VALUE
> -- -----
> 1 10
> 3 30
> 5 50
> 6 600
> rows: 4
DROP SCHEMA SOURCESCHEMA CASCADE;
> ok
DROP SCHEMA DESTSCHEMA CASCADE;
> ok
......@@ -771,4 +771,4 @@ openoffice organize libre systemtables gmane sea borders announced millennium al
opti excessively
iterators tech enums incompatibilities loses reimplement readme reorganize milli subdirectory linkplain inspections
geometries
geometries sourceschema destschema
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论