Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
a778d6ee
Unverified
提交
a778d6ee
authored
5月 22, 2018
作者:
Evgenij Ryazanov
提交者:
GitHub
5月 22, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1155 from katzyn/merge_using
Fix different issues with MERGE USING
上级
3b989b35
a53d48d0
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
85 行增加
和
44 行删除
+85
-44
Parser.java
h2/src/main/org/h2/command/Parser.java
+49
-32
MergeUsing.java
h2/src/main/org/h2/command/dml/MergeUsing.java
+0
-11
TestMergeUsing.java
h2/src/test/org/h2/test/db/TestMergeUsing.java
+1
-1
mergeUsing.sql
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
+35
-0
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
a778d6ee
...
...
@@ -1168,11 +1168,33 @@ public class Parser {
command
.
setOnCondition
(
condition
);
read
(
")"
);
if
(
readIfAll
(
"WHEN"
,
"MATCHED"
,
"THEN"
))
{
boolean
matched
=
parseWhenMatched
(
command
);
if
(
parseWhenNotMatched
(
command
)
&&
!
matched
)
{
parseWhenMatched
(
command
);
}
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
;
}
private
boolean
parseWhenMatched
(
MergeUsing
command
)
{
if
(!
readIfAll
(
"WHEN"
,
"MATCHED"
,
"THEN"
))
{
return
false
;
}
int
startMatched
=
lastParseIndex
;
if
(
readIf
(
"UPDATE"
))
{
Update
updateCommand
=
new
Update
(
session
);
//currentPrepared = updateCommand;
TableFilter
filter
=
command
.
getTargetTableFilter
();
updateCommand
.
setTableFilter
(
filter
);
parseUpdateSetClause
(
updateCommand
,
filter
,
startMatched
);
...
...
@@ -1186,34 +1208,29 @@ public class Parser {
parseDeleteGivenTable
(
deleteCommand
,
null
,
startMatched
);
command
.
setDeleteCommand
(
deleteCommand
);
}
return
true
;
}
private
boolean
parseWhenNotMatched
(
MergeUsing
command
)
{
if
(!
readIfAll
(
"WHEN"
,
"NOT"
,
"MATCHED"
,
"THEN"
))
{
return
false
;
}
if
(
readIfAll
(
"WHEN"
,
"NOT"
,
"MATCHED"
,
"THEN"
))
{
if
(
readIf
(
"INSERT"
))
{
Insert
insertCommand
=
new
Insert
(
session
);
insertCommand
.
setTable
(
command
.
getTargetTable
());
parseInsertGivenTable
(
insertCommand
,
command
.
getTargetTable
());
command
.
setInsertCommand
(
insertCommand
);
}
}
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
;
return
true
;
}
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
));
}
...
...
@@ -1460,7 +1477,7 @@ public class Parser {
table
=
new
FunctionTable
(
mainSchema
,
session
,
expr
,
call
);
}
}
else
{
table
=
readTableOrView
(
tableName
,
true
);
table
=
readTableOrView
(
tableName
);
}
}
ArrayList
<
String
>
derivedColumnNames
=
null
;
...
...
@@ -5908,10 +5925,10 @@ public class Parser {
}
private
Table
readTableOrView
()
{
return
readTableOrView
(
readIdentifierWithSchema
(
null
)
,
false
);
return
readTableOrView
(
readIdentifierWithSchema
(
null
));
}
private
Table
readTableOrView
(
String
tableName
,
boolean
allowDual
)
{
private
Table
readTableOrView
(
String
tableName
)
{
if
(
schemaName
!=
null
)
{
Table
table
=
getSchema
().
resolveTableOrView
(
session
,
tableName
);
if
(
table
!=
null
)
{
...
...
@@ -5934,7 +5951,7 @@ public class Parser {
}
}
}
if
(
allowDual
&&
isDualTable
(
tableName
))
{
if
(
isDualTable
(
tableName
))
{
return
getDualTable
(
false
);
}
throw
DbException
.
get
(
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
,
tableName
);
...
...
h2/src/main/org/h2/command/dml/MergeUsing.java
浏览文件 @
a778d6ee
...
...
@@ -113,7 +113,6 @@ public class MergeUsing extends Prepared {
private
Insert
insertCommand
;
private
String
queryAlias
;
private
int
countUpdatedRows
;
private
Column
[]
sourceKeys
;
private
Select
targetMatchQuery
;
private
final
HashMap
<
Value
,
Integer
>
targetRowidsRemembered
=
new
HashMap
<>();
private
int
sourceQueryRowNumber
;
...
...
@@ -358,16 +357,6 @@ public class MergeUsing extends Prepared {
"No references to target columns found in ON clause:"
+
targetTableFilter
.
toString
());
}
if
(
sourceKeys
==
null
)
{
HashSet
<
Column
>
sourceColumns
=
buildColumnListFromOnCondition
(
sourceTableFilter
);
sourceKeys
=
sourceColumns
.
toArray
(
new
Column
[
0
]);
}
if
(
sourceKeys
.
length
==
0
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_NOT_FOUND_1
,
"No references to source columns found in ON clause:"
+
sourceTableFilter
.
toString
());
}
// only do the optimize now - before we have already gathered the
// unoptimized column data
...
...
h2/src/test/org/h2/test/db/TestMergeUsing.java
浏览文件 @
a778d6ee
...
...
@@ -245,7 +245,7 @@ public class TestMergeUsing extends TestBase implements Trigger {
GATHER_ORDERED_RESULTS_SQL
,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)"
,
3
,
"
No references to source columns found in ON clause
"
);
3
,
"
Duplicate key updated 3 rows at once, only 1 expected
"
);
// Insert does not insert correct values with respect to ON condition
// (inserts ID value above 100, instead)
testMergeUsingException
(
...
...
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
浏览文件 @
a778d6ee
...
...
@@ -114,3 +114,38 @@ DROP TABLE SOURCE_TABLE;
DROP
TABLE
DEST_TABLE
;
>
ok
CREATE
TABLE
TEST
(
C1
INT
,
C2
INT
,
C3
INT
);
>
ok
MERGE
INTO
TEST
USING
DUAL
ON
(
C1
=
11
AND
C2
=
21
)
WHEN
NOT
MATCHED
THEN
INSERT
(
C1
,
C2
,
C3
)
VALUES
(
11
,
21
,
31
)
WHEN
MATCHED
THEN
UPDATE
SET
C3
=
31
;
>
update
count
:
1
MERGE
INTO
TEST
USING
DUAL
ON
(
C1
=
11
AND
C2
=
22
)
WHEN
NOT
MATCHED
THEN
INSERT
(
C1
,
C2
,
C3
)
VALUES
(
11
,
22
,
32
)
WHEN
MATCHED
THEN
UPDATE
SET
C3
=
32
;
>
update
count
:
1
SELECT
*
FROM
TEST
ORDER
BY
C1
,
C2
;
>
C1
C2
C3
>
-- -- --
>
11
21
31
>
11
22
32
>
rows
(
ordered
):
2
MERGE
INTO
TEST
USING
DUAL
ON
(
C1
=
11
AND
C2
=
21
)
WHEN
NOT
MATCHED
THEN
INSERT
(
C1
,
C2
,
C3
)
VALUES
(
11
,
21
,
33
)
WHEN
MATCHED
THEN
UPDATE
SET
C3
=
33
;
>
update
count
:
1
SELECT
*
FROM
TEST
ORDER
BY
C1
,
C2
;
>
C1
C2
C3
>
-- -- --
>
11
21
33
>
11
22
32
>
rows
(
ordered
):
2
DROP
TABLE
TEST
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论