Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
36b9166f
Unverified
提交
36b9166f
authored
10月 05, 2018
作者:
Evgenij Ryazanov
提交者:
GitHub
10月 05, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1497 from katzyn/mergeUsing
Fix UPDATE with DELETE in MERGE USING
上级
7e2ec608
abc0e082
全部展开
显示空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
458 行增加
和
313 行删除
+458
-313
help.csv
h2/src/docsrc/help/help.csv
+54
-32
changelog.html
h2/src/docsrc/html/changelog.html
+4
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+46
-45
Delete.java
h2/src/main/org/h2/command/dml/Delete.java
+25
-10
MergeUsing.java
h2/src/main/org/h2/command/dml/MergeUsing.java
+221
-214
Update.java
h2/src/main/org/h2/command/dml/Update.java
+17
-1
TableFilter.java
h2/src/main/org/h2/table/TableFilter.java
+15
-1
TestMergeUsing.java
h2/src/test/org/h2/test/db/TestMergeUsing.java
+14
-9
mergeUsing.sql
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
+62
-1
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
36b9166f
...
@@ -82,7 +82,7 @@ UPDATE PERSON P SET NAME=(SELECT A.NAME FROM ADDRESS A WHERE A.ID=P.ID);
...
@@ -82,7 +82,7 @@ UPDATE PERSON P SET NAME=(SELECT A.NAME FROM ADDRESS A WHERE A.ID=P.ID);
"
"
"Commands (DML)","DELETE","
"Commands (DML)","DELETE","
DELETE [ TOP term ] FROM tableName
deleteSearchCondition
DELETE [ TOP term ] FROM tableName
[ WHERE expression ] [ LIMIT term ]
","
","
Deletes rows form a table.
Deletes rows form a table.
If TOP or LIMIT is specified, at most the specified number of rows are deleted (no limit if null or smaller than zero).
If TOP or LIMIT is specified, at most the specified number of rows are deleted (no limit if null or smaller than zero).
...
@@ -137,36 +137,34 @@ MERGE INTO TEST KEY(ID) VALUES(2, 'World')
...
@@ -137,36 +137,34 @@ MERGE INTO TEST KEY(ID) VALUES(2, 'World')
MERGE INTO targetTableName [ [AS] targetAlias]
MERGE INTO targetTableName [ [AS] targetAlias]
USING { ( select ) | sourceTableName }[ [AS] sourceAlias ]
USING { ( select ) | sourceTableName }[ [AS] sourceAlias ]
ON expression
ON expression
[ WHEN MATCHED THEN
mergeWhenClause [,...]
[ UPDATE SET setClauseList ] [ DELETE deleteSearchCondition ] ]
","
[ WHEN NOT MATCHED THEN INSERT insertColumnsAndSource ]
Updates or deletes existing rows, and insert rows that don't exist.
","
Updates or deletes existing rows, and insert rows that don't exist. The ON clause
The ON clause specifies the matching column expression.
specifies the matching column expression and must be specified. If more than one row
Different rows from a source table may not match with the same target row,
is updated per input row, an exception is thrown.
but one source row may be matched with multiple target rows.
If the source data contains duplicate rows (specifically those columns used in the
row matching ON clause), then an exception is thrown to prevent two updates applying
to the same target row.
WHEN MATCHED THEN or WHEN NOT MATCHED THEN clauses or both of them in any order should be specified.
If WHEN MATCHED THEN is specified it should contain UPDATE or DELETE clauses of both of them.
If statement doesn't need a source table a DUAL table can be substituted.
If statement doesn't need a source table a DUAL table can be substituted.
","
","
MERGE INTO TARGET_TABLE AS T USING SOURCE_TABLE AS S
MERGE INTO TARGET_TABLE AS T USING SOURCE_TABLE AS S
ON T.ID = S.ID
ON T.ID = S.ID
WHEN MATCHED THEN
WHEN MATCHED AND T.COL2 <> 'FINAL' THEN
UPDATE SET T.COL1 = S.COL1 WHERE T.COL2<>'FINAL'
UPDATE SET T.COL1 = S.COL1
DELETE WHERE T.COL2='FINAL'
WHEN MATCHED AND T.COL2 = 'FINAL' THEN
DELETE
WHEN NOT MATCHED THEN
WHEN NOT MATCHED THEN
INSERT (ID,
COL1,COL2) VALUES(S.ID,S.COL1,
S.COL2)
INSERT (ID,
COL1, COL2) VALUES(S.ID, S.COL1,
S.COL2)
MERGE INTO TARGET_TABLE AS T USING (SELECT * FROM SOURCE_TABLE) AS S
MERGE INTO TARGET_TABLE AS T USING (SELECT * FROM SOURCE_TABLE) AS S
ON T.ID = S.ID
ON T.ID = S.ID
WHEN MATCHED THEN
WHEN MATCHED AND T.COL2 <> 'FINAL' THEN
UPDATE SET T.COL1 = S.COL1 WHERE T.COL2<>'FINAL'
UPDATE SET T.COL1 = S.COL1
DELETE WHERE T.COL2='FINAL'
WHEN MATCHED AND T.COL2 = 'FINAL' THEN
DELETE
WHEN NOT MATCHED THEN
WHEN NOT MATCHED THEN
INSERT
(ID,COL1,COL2) VALUES(S.ID,S.COL1,
S.COL2)
INSERT
VALUES (S.ID, S.COL1,
S.COL2)
MERGE INTO TARGET_TABLE USING DUAL ON ID = 1
MERGE INTO TARGET_TABLE USING DUAL ON ID = 1
WHEN NOT MATCHED THEN INSERT
(ID, NAME)
VALUES (1, 'Test')
WHEN NOT MATCHED THEN INSERT VALUES (1, 'Test')
WHEN MATCHED THEN UPDATE SET NAME = 'Test'
WHEN MATCHED THEN UPDATE SET NAME = 'Test'
"
"
...
@@ -2277,14 +2275,6 @@ SELECT CAST(0 AS DOUBLE)
...
@@ -2277,14 +2275,6 @@ SELECT CAST(0 AS DOUBLE)
SELECT -1.4e-10
SELECT -1.4e-10
"
"
"Other Grammar","Delete search condition","
[ WHERE expression ] [ LIMIT term ]
","
Search condition for DELETE statement.
","
WHERE ID = 2
"
"Other Grammar","Digit","
"Other Grammar","Digit","
0-9
0-9
","
","
...
@@ -2373,6 +2363,38 @@ Long numbers are between -9223372036854775808 and 9223372036854775807.
...
@@ -2373,6 +2363,38 @@ Long numbers are between -9223372036854775808 and 9223372036854775807.
100000
100000
"
"
"Other Grammar","Merge when clause","
mergeWhenMatchedClause|mergeWhenNotMatchedClause
","
WHEN MATCHED or WHEN NOT MATCHED clause for MERGE USING command.
","
WHEN MATCHED THEN DELETE
"
"Other Grammar","Merge when matched clause","
WHEN MATCHED [ AND expression ] THEN
UPDATE SET setClauseList
| DELETE
| {UPDATE SET setClauseList [ WHERE expression ] DELETE [ WHERE expression ]}
","
WHEN MATCHED clause for MERGE USING command.
If both UPDATE and DELETE are specified, DELETE can delete only rows that were updated,
WHERE condition in DELETE clause can be used to specify which updated rows should be deleted.
This condition checks values in updated row.
","
WHEN MATCHED THEN UPDATE SET VALUE = S.VALUE
WHEN MATCHED THEN DELETE
"
"Other Grammar","Merge when not matched clause","
WHEN NOT MATCHED [ AND expression ] THEN INSERT insertColumnsAndSource
","
WHEN NOT MATCHED clause for MERGE USING command.
","
WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)
"
"Other Grammar","Name","
"Other Grammar","Name","
{ { A-Z|_ } [ { A-Z|_|0-9 } [...] ] } | quotedName
{ { A-Z|_ } [ { A-Z|_|0-9 } [...] ] } | quotedName
","
","
...
@@ -2496,8 +2518,8 @@ START WITH 1
...
@@ -2496,8 +2518,8 @@ START WITH 1
"
"
"Other Grammar","Set clause list","
"Other Grammar","Set clause list","
{ { columnName = { DEFAULT | expression } } [,...] }
|
{ { columnName = { DEFAULT | expression } } [,...] }
{ ( columnName [,...] ) = ( select ) }
|
{ ( columnName [,...] ) = ( select ) }
","
","
List of SET clauses.
List of SET clauses.
","
","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
36b9166f
...
@@ -21,6 +21,8 @@ Change Log
...
@@ -21,6 +21,8 @@ Change Log
<h2>
Next Version (unreleased)
</h2>
<h2>
Next Version (unreleased)
</h2>
<ul>
<ul>
<li>
Issue #1495: MERGE statement doesn't affect any rows when Oracle UPDATE .. WHERE .. DELETE .. WHERE is used
</li>
<li>
Issue #1493: MERGE statement fails when it updates more than one row
<li>
Issue #1493: MERGE statement fails when it updates more than one row
</li>
</li>
<li>
Issue #1492: Unnecessary restriction on MERGE USING statement when ON clause doesn't reference any target table columns
<li>
Issue #1492: Unnecessary restriction on MERGE USING statement when ON clause doesn't reference any target table columns
...
@@ -29,6 +31,8 @@ Change Log
...
@@ -29,6 +31,8 @@ Change Log
</li>
</li>
<li>
Issue #1490: NullPointerException when running invalid MERGE statement
<li>
Issue #1490: NullPointerException when running invalid MERGE statement
</li>
</li>
<li>
Issue #1489: MERGE USING documentation has misleading railroad diagram
</li>
<li>
Issue #1488: Improve documentation of window and some other functions
<li>
Issue #1488: Improve documentation of window and some other functions
</li>
</li>
<li>
Issue #1485: Default window frame in presence of ORDER BY is RANGE .., not ROWS
<li>
Issue #1485: Default window frame in presence of ORDER BY is RANGE .., not ROWS
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
36b9166f
...
@@ -1116,11 +1116,11 @@ public class Parser {
...
@@ -1116,11 +1116,11 @@ public class Parser {
int
start
=
lastParseIndex
;
int
start
=
lastParseIndex
;
TableFilter
filter
=
readSimpleTableFilter
(
0
,
null
);
TableFilter
filter
=
readSimpleTableFilter
(
0
,
null
);
command
.
setTableFilter
(
filter
);
command
.
setTableFilter
(
filter
);
parseUpdateSetClause
(
command
,
filter
,
start
);
parseUpdateSetClause
(
command
,
filter
,
start
,
true
);
return
command
;
return
command
;
}
}
private
void
parseUpdateSetClause
(
Update
command
,
TableFilter
filter
,
int
start
)
{
private
void
parseUpdateSetClause
(
Update
command
,
TableFilter
filter
,
int
start
,
boolean
allowExtensions
)
{
read
(
"SET"
);
read
(
"SET"
);
if
(
readIf
(
OPEN_PAREN
))
{
if
(
readIf
(
OPEN_PAREN
))
{
ArrayList
<
Column
>
columns
=
Utils
.
newSmallArrayList
();
ArrayList
<
Column
>
columns
=
Utils
.
newSmallArrayList
();
...
@@ -1154,6 +1154,7 @@ public class Parser {
...
@@ -1154,6 +1154,7 @@ public class Parser {
Expression
condition
=
readExpression
();
Expression
condition
=
readExpression
();
command
.
setCondition
(
condition
);
command
.
setCondition
(
condition
);
}
}
if
(
allowExtensions
)
{
if
(
readIf
(
ORDER
))
{
if
(
readIf
(
ORDER
))
{
// for MySQL compatibility
// for MySQL compatibility
// (this syntax is supported, but ignored)
// (this syntax is supported, but ignored)
...
@@ -1164,6 +1165,7 @@ public class Parser {
...
@@ -1164,6 +1165,7 @@ public class Parser {
Expression
limit
=
readTerm
().
optimize
(
session
);
Expression
limit
=
readTerm
().
optimize
(
session
);
command
.
setLimit
(
limit
);
command
.
setLimit
(
limit
);
}
}
}
setSQL
(
command
,
"UPDATE"
,
start
);
setSQL
(
command
,
"UPDATE"
,
start
);
}
}
...
@@ -1197,20 +1199,15 @@ public class Parser {
...
@@ -1197,20 +1199,15 @@ public class Parser {
}
}
TableFilter
filter
=
readSimpleTableFilter
(
0
,
null
);
TableFilter
filter
=
readSimpleTableFilter
(
0
,
null
);
command
.
setTableFilter
(
filter
);
command
.
setTableFilter
(
filter
);
parseDeleteGivenTable
(
command
,
limit
,
start
);
return
command
;
}
private
void
parseDeleteGivenTable
(
Delete
command
,
Expression
limit
,
int
start
)
{
if
(
readIf
(
WHERE
))
{
if
(
readIf
(
WHERE
))
{
Expression
condition
=
readExpression
();
command
.
setCondition
(
readExpression
());
command
.
setCondition
(
condition
);
}
}
if
(
readIf
(
LIMIT
)
&&
limit
==
null
)
{
if
(
limit
==
null
&&
readIf
(
LIMIT
)
)
{
limit
=
readTerm
().
optimize
(
session
);
limit
=
readTerm
().
optimize
(
session
);
}
}
command
.
setLimit
(
limit
);
command
.
setLimit
(
limit
);
setSQL
(
command
,
"DELETE"
,
start
);
setSQL
(
command
,
"DELETE"
,
start
);
return
command
;
}
}
private
IndexColumn
[]
parseIndexColumnList
()
{
private
IndexColumn
[]
parseIndexColumnList
()
{
...
@@ -1496,47 +1493,47 @@ public class Parser {
...
@@ -1496,47 +1493,47 @@ public class Parser {
command
.
setOnCondition
(
condition
);
command
.
setOnCondition
(
condition
);
read
(
"WHEN"
);
read
(
"WHEN"
);
do
{
boolean
matched
=
readIf
(
"MATCHED"
);
boolean
matched
=
readIf
(
"MATCHED"
);
if
(
matched
)
{
if
(
matched
)
{
parseWhenMatched
(
command
);
parseWhenMatched
(
command
);
}
else
{
}
else
{
parseWhenNotMatched
(
command
);
parseWhenNotMatched
(
command
);
}
}
if
(
readIf
(
"WHEN"
))
{
}
while
(
readIf
(
"WHEN"
));
if
(
matched
)
{
parseWhenNotMatched
(
command
);
}
else
{
read
(
"MATCHED"
);
parseWhenMatched
(
command
);
}
}
setSQL
(
command
,
"MERGE"
,
start
);
setSQL
(
command
,
"MERGE"
,
start
);
return
command
;
return
command
;
}
}
private
void
parseWhenMatched
(
MergeUsing
command
)
{
private
void
parseWhenMatched
(
MergeUsing
command
)
{
Expression
and
=
readIf
(
"AND"
)
?
readExpression
()
:
null
;
read
(
"THEN"
);
read
(
"THEN"
);
int
startMatched
=
lastParseIndex
;
int
startMatched
=
lastParseIndex
;
boolean
ok
=
false
;
Update
updateCommand
=
null
;
if
(
readIf
(
"UPDATE"
))
{
if
(
readIf
(
"UPDATE"
))
{
Update
updateCommand
=
new
Update
(
session
);
updateCommand
=
new
Update
(
session
);
TableFilter
filter
=
command
.
getTargetTableFilter
();
TableFilter
filter
=
command
.
getTargetTableFilter
();
updateCommand
.
setTableFilter
(
filter
);
updateCommand
.
setTableFilter
(
filter
);
parseUpdateSetClause
(
updateCommand
,
filter
,
startMatched
);
parseUpdateSetClause
(
updateCommand
,
filter
,
startMatched
,
false
);
command
.
setUpdateCommand
(
updateCommand
);
ok
=
true
;
}
startMatched
=
lastParseIndex
;
startMatched
=
lastParseIndex
;
}
Delete
deleteCommand
=
null
;
if
(
readIf
(
"DELETE"
))
{
if
(
readIf
(
"DELETE"
))
{
Delete
deleteCommand
=
new
Delete
(
session
);
deleteCommand
=
new
Delete
(
session
);
TableFilter
filter
=
command
.
getTargetTableFilter
();
deleteCommand
.
setTableFilter
(
command
.
getTargetTableFilter
());
deleteCommand
.
setTableFilter
(
filter
);
if
(
readIf
(
WHERE
))
{
parseDeleteGivenTable
(
deleteCommand
,
null
,
startMatched
);
deleteCommand
.
setCondition
(
readExpression
());
command
.
setDeleteCommand
(
deleteCommand
);
ok
=
true
;
}
}
if
(!
ok
)
{
setSQL
(
deleteCommand
,
"DELETE"
,
startMatched
);
}
if
(
updateCommand
!=
null
||
deleteCommand
!=
null
)
{
MergeUsing
.
WhenMatched
when
=
new
MergeUsing
.
WhenMatched
(
command
);
when
.
setAndCondition
(
and
);
when
.
setUpdateCommand
(
updateCommand
);
when
.
setDeleteCommand
(
deleteCommand
);
command
.
addWhen
(
when
);
}
else
{
throw
getSyntaxError
();
throw
getSyntaxError
();
}
}
}
}
...
@@ -1544,12 +1541,16 @@ public class Parser {
...
@@ -1544,12 +1541,16 @@ public class Parser {
private
void
parseWhenNotMatched
(
MergeUsing
command
)
{
private
void
parseWhenNotMatched
(
MergeUsing
command
)
{
read
(
NOT
);
read
(
NOT
);
read
(
"MATCHED"
);
read
(
"MATCHED"
);
Expression
and
=
readIf
(
"AND"
)
?
readExpression
()
:
null
;
read
(
"THEN"
);
read
(
"THEN"
);
if
(
readIf
(
"INSERT"
))
{
if
(
readIf
(
"INSERT"
))
{
Insert
insertCommand
=
new
Insert
(
session
);
Insert
insertCommand
=
new
Insert
(
session
);
insertCommand
.
setTable
(
command
.
getTargetTable
());
insertCommand
.
setTable
(
command
.
getTargetTable
());
parseInsertGivenTable
(
insertCommand
,
command
.
getTargetTable
());
parseInsertGivenTable
(
insertCommand
,
command
.
getTargetTable
());
command
.
setInsertCommand
(
insertCommand
);
MergeUsing
.
WhenNotMatched
when
=
new
MergeUsing
.
WhenNotMatched
(
command
);
when
.
setAndCondition
(
and
);
when
.
setInsertCommand
(
insertCommand
);
command
.
addWhen
(
when
);
}
else
{
}
else
{
throw
getSyntaxError
();
throw
getSyntaxError
();
}
}
...
...
h2/src/main/org/h2/command/dml/Delete.java
浏览文件 @
36b9166f
...
@@ -5,6 +5,8 @@
...
@@ -5,6 +5,8 @@
*/
*/
package
org
.
h2
.
command
.
dml
;
package
org
.
h2
.
command
.
dml
;
import
java.util.HashSet
;
import
org.h2.api.Trigger
;
import
org.h2.api.Trigger
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.Prepared
;
import
org.h2.command.Prepared
;
...
@@ -40,6 +42,8 @@ public class Delete extends Prepared {
...
@@ -40,6 +42,8 @@ public class Delete extends Prepared {
*/
*/
private
TableFilter
sourceTableFilter
;
private
TableFilter
sourceTableFilter
;
private
HashSet
<
Long
>
keysFilter
;
public
Delete
(
Session
session
)
{
public
Delete
(
Session
session
)
{
super
(
session
);
super
(
session
);
}
}
...
@@ -56,6 +60,15 @@ public class Delete extends Prepared {
...
@@ -56,6 +60,15 @@ public class Delete extends Prepared {
return
this
.
condition
;
return
this
.
condition
;
}
}
/**
* Sets the keys filter.
*
* @param keysFilter the keys filter
*/
public
void
setKeysFilter
(
HashSet
<
Long
>
keysFilter
)
{
this
.
keysFilter
=
keysFilter
;
}
@Override
@Override
public
int
update
()
{
public
int
update
()
{
targetTableFilter
.
startQuery
(
session
);
targetTableFilter
.
startQuery
(
session
);
...
@@ -79,6 +92,7 @@ public class Delete extends Prepared {
...
@@ -79,6 +92,7 @@ public class Delete extends Prepared {
setCurrentRowNumber
(
rows
.
size
()
+
1
);
setCurrentRowNumber
(
rows
.
size
()
+
1
);
if
(
condition
==
null
||
condition
.
getBooleanValue
(
session
))
{
if
(
condition
==
null
||
condition
.
getBooleanValue
(
session
))
{
Row
row
=
targetTableFilter
.
get
();
Row
row
=
targetTableFilter
.
get
();
if
(
keysFilter
==
null
||
keysFilter
.
contains
(
row
.
getKey
()))
{
boolean
done
=
false
;
boolean
done
=
false
;
if
(
table
.
fireRow
())
{
if
(
table
.
fireRow
())
{
done
=
table
.
fireBeforeRow
(
session
,
row
,
null
);
done
=
table
.
fireBeforeRow
(
session
,
row
,
null
);
...
@@ -92,6 +106,7 @@ public class Delete extends Prepared {
...
@@ -92,6 +106,7 @@ public class Delete extends Prepared {
}
}
}
}
}
}
}
int
rowScanCount
=
0
;
int
rowScanCount
=
0
;
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
if
((++
rowScanCount
&
127
)
==
0
)
{
if
((++
rowScanCount
&
127
)
==
0
)
{
...
...
h2/src/main/org/h2/command/dml/MergeUsing.java
浏览文件 @
36b9166f
差异被折叠。
点击展开。
h2/src/main/org/h2/command/dml/Update.java
浏览文件 @
36b9166f
...
@@ -7,6 +7,7 @@ package org.h2.command.dml;
...
@@ -7,6 +7,7 @@ package org.h2.command.dml;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Objects
;
import
java.util.Objects
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
...
@@ -53,6 +54,8 @@ public class Update extends Prepared {
...
@@ -53,6 +54,8 @@ public class Update extends Prepared {
private
final
ArrayList
<
Column
>
columns
=
Utils
.
newSmallArrayList
();
private
final
ArrayList
<
Column
>
columns
=
Utils
.
newSmallArrayList
();
private
final
HashMap
<
Column
,
Expression
>
expressionMap
=
new
HashMap
<>();
private
final
HashMap
<
Column
,
Expression
>
expressionMap
=
new
HashMap
<>();
private
HashSet
<
Long
>
updatedKeysCollector
;
public
Update
(
Session
session
)
{
public
Update
(
Session
session
)
{
super
(
session
);
super
(
session
);
}
}
...
@@ -88,6 +91,15 @@ public class Update extends Prepared {
...
@@ -88,6 +91,15 @@ public class Update extends Prepared {
}
}
}
}
/**
* Sets the collector of updated keys.
*
* @param updatedKeysCollector the collector of updated keys
*/
public
void
setUpdatedKeysCollector
(
HashSet
<
Long
>
updatedKeysCollector
)
{
this
.
updatedKeysCollector
=
updatedKeysCollector
;
}
@Override
@Override
public
int
update
()
{
public
int
update
()
{
targetTableFilter
.
startQuery
(
session
);
targetTableFilter
.
startQuery
(
session
);
...
@@ -135,7 +147,8 @@ public class Update extends Prepared {
...
@@ -135,7 +147,8 @@ public class Update extends Prepared {
}
}
newRow
.
setValue
(
i
,
newValue
);
newRow
.
setValue
(
i
,
newValue
);
}
}
newRow
.
setKey
(
oldRow
.
getKey
());
long
key
=
oldRow
.
getKey
();
newRow
.
setKey
(
key
);
if
(
setOnUpdate
||
updateToCurrentValuesReturnsZero
)
{
if
(
setOnUpdate
||
updateToCurrentValuesReturnsZero
)
{
setOnUpdate
=
false
;
setOnUpdate
=
false
;
for
(
int
i
=
0
;
i
<
columnCount
;
i
++)
{
for
(
int
i
=
0
;
i
<
columnCount
;
i
++)
{
...
@@ -166,6 +179,9 @@ public class Update extends Prepared {
...
@@ -166,6 +179,9 @@ public class Update extends Prepared {
if
(!
done
)
{
if
(!
done
)
{
rows
.
add
(
oldRow
);
rows
.
add
(
oldRow
);
rows
.
add
(
newRow
);
rows
.
add
(
newRow
);
if
(
updatedKeysCollector
!=
null
)
{
updatedKeysCollector
.
add
(
key
);
}
}
}
count
++;
count
++;
}
}
...
...
h2/src/main/org/h2/table/TableFilter.java
浏览文件 @
36b9166f
...
@@ -82,6 +82,11 @@ public class TableFilter implements ColumnResolver {
...
@@ -82,6 +82,11 @@ public class TableFilter implements ColumnResolver {
*/
*/
private
final
ArrayList
<
IndexCondition
>
indexConditions
=
Utils
.
newSmallArrayList
();
private
final
ArrayList
<
IndexCondition
>
indexConditions
=
Utils
.
newSmallArrayList
();
/**
* Whether new window conditions should not be accepted.
*/
private
boolean
doneWithIndexConditions
;
/**
/**
* Additional conditions that can't be used for index lookup, but for row
* Additional conditions that can't be used for index lookup, but for row
* filter for this table (ID=ID, NAME LIKE '%X%')
* filter for this table (ID=ID, NAME LIKE '%X%')
...
@@ -627,8 +632,17 @@ public class TableFilter implements ColumnResolver {
...
@@ -627,8 +632,17 @@ public class TableFilter implements ColumnResolver {
* @param condition the index condition
* @param condition the index condition
*/
*/
public
void
addIndexCondition
(
IndexCondition
condition
)
{
public
void
addIndexCondition
(
IndexCondition
condition
)
{
if
(!
doneWithIndexConditions
)
{
indexConditions
.
add
(
condition
);
indexConditions
.
add
(
condition
);
}
}
}
/**
* Used to reject all additional index conditions.
*/
public
void
doneWithIndexConditions
()
{
this
.
doneWithIndexConditions
=
true
;
}
/**
/**
* Add a filter condition.
* Add a filter condition.
...
...
h2/src/test/org/h2/test/db/TestMergeUsing.java
浏览文件 @
36b9166f
...
@@ -96,7 +96,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -96,7 +96,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
"MERGE INTO PARENT AS P USING ("
+
"MERGE INTO PARENT AS P USING ("
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) ) AS S ON (P.ID = S.ID) "
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) ) AS S ON (P.ID = S.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"
WHEN MATCHED THEN
DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
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||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
@@ -116,8 +116,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -116,8 +116,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT AS P USING SOURCE AS S ON (P.ID = S.ID) WHEN MATCHED THEN "
+
"MERGE INTO PARENT AS P USING SOURCE AS S ON (P.ID = S.ID) "
+
"UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
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||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
@@ -128,8 +129,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -128,8 +129,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT AS P USING SOURCE ON (P.ID = SOURCE.ID) WHEN MATCHED THEN "
+
"MERGE INTO PARENT AS P USING SOURCE ON (P.ID = SOURCE.ID) "
+
"UPDATE SET P.NAME = SOURCE.NAME||SOURCE.ID WHERE P.ID = 2 DELETE WHERE P.ID = 1 "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = SOURCE.NAME||SOURCE.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
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||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
@@ -140,9 +142,10 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -140,9 +142,10 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT USING SOURCE ON (PARENT.ID = SOURCE.ID) WHEN MATCHED THEN "
+
"MERGE INTO PARENT USING SOURCE ON (PARENT.ID = SOURCE.ID) "
+
"UPDATE SET PARENT.NAME = SOURCE.NAME||SOURCE.ID WHERE PARENT.ID = 2 "
+
"WHEN MATCHED THEN UPDATE SET PARENT.NAME = SOURCE.NAME||SOURCE.ID WHERE PARENT.ID = 2 "
+
"DELETE WHERE PARENT.ID = 1 WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
"WHEN MATCHED THEN DELETE WHERE PARENT.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
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||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)"
,
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)"
,
...
@@ -185,6 +188,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -185,6 +188,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
// it's considered different - with respect to to ROWID - so no error
// it's considered different - with respect to to ROWID - so no error
// One insert, one update one delete happens (on same row) , target
// One insert, one update one delete happens (on same row) , target
// table missing PK, no source or target alias
// table missing PK, no source or target alias
if
(
false
)
// TODO
testMergeUsing
(
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) );"
+
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) );"
+
"CREATE TABLE SOURCE AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
,
"CREATE TABLE SOURCE AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
,
...
@@ -205,7 +209,8 @@ public class TestMergeUsing extends TestDb implements Trigger {
...
@@ -205,7 +209,8 @@ public class TestMergeUsing extends TestDb implements Trigger {
"MERGE INTO PARENT AS P USING "
+
"MERGE INTO PARENT AS P USING "
+
"(SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,4) ) AS S ON (P.ID = S.ID) "
+
"(SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,4) ) AS S ON (P.ID = S.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT 2 AS ID, 'Marcy22-updated2' AS NAME UNION ALL "
+
"SELECT 2 AS ID, 'Marcy22-updated2' AS NAME UNION ALL "
+
"SELECT X AS ID, 'Marcy'||X||'-inserted'||X AS NAME FROM SYSTEM_RANGE(3,4)"
,
"SELECT X AS ID, 'Marcy'||X||'-inserted'||X AS NAME FROM SYSTEM_RANGE(3,4)"
,
...
...
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
浏览文件 @
36b9166f
...
@@ -29,7 +29,7 @@ EXPLAIN PLAN
...
@@ -29,7 +29,7 @@ EXPLAIN PLAN
UPDATE
SET
P
.
NAME
=
S
.
NAME
WHERE
2
=
2
WHEN
NOT
UPDATE
SET
P
.
NAME
=
S
.
NAME
WHERE
2
=
2
WHEN
NOT
MATCHED
THEN
MATCHED
THEN
INSERT
(
ID
,
NAME
)
VALUES
(
S
.
ID
,
S
.
NAME
);
INSERT
(
ID
,
NAME
)
VALUES
(
S
.
ID
,
S
.
NAME
);
>>
MERGE
INTO
PUBLIC
.
PARENT
(
ID
,
NAME
)
SELECT
X
AS
ID
,
(
'Coco'
||
X
)
AS
NAME
FROM
SYSTEM_RANGE
(
1
,
2
)
/* PUBLIC.RANGE_INDEX */
>>
MERGE
INTO
PUBLIC
.
PARENT
USING
SELECT
X
AS
ID
,
(
'Coco'
||
X
)
AS
NAME
FROM
SYSTEM_RANGE
(
1
,
2
)
/* PUBLIC.RANGE_INDEX */
DROP
TABLE
PARENT
;
DROP
TABLE
PARENT
;
>
ok
>
ok
...
@@ -259,5 +259,66 @@ SELECT * FROM TEST;
...
@@ -259,5 +259,66 @@ SELECT * FROM TEST;
>
3
50
>
3
50
>
rows
:
3
>
rows
:
3
MERGE
INTO
TEST
USING
(
SELECT
1
)
ON
1
=
1
WHEN
MATCHED
THEN
UPDATE
SET
VALUE
=
60
WHERE
ID
=
3
DELETE
WHERE
ID
=
2
;
>
update
count
:
1
SELECT
*
FROM
TEST
;
>
ID
VALUE
>
-- -----
>
1
50
>
2
50
>
3
60
>
rows
:
3
MERGE
INTO
TEST
USING
(
SELECT
1
)
ON
1
=
1
WHEN
MATCHED
THEN
DELETE
WHERE
ID
=
2
;
>
update
count
:
1
SELECT
*
FROM
TEST
;
>
ID
VALUE
>
-- -----
>
1
50
>
3
60
>
rows
:
2
MERGE
INTO
TEST
USING
(
SELECT
1
)
ON
1
=
1
WHEN
MATCHED
THEN
UPDATE
SET
VALUE
=
70
WHERE
ID
=
3
DELETE
WHERE
VALUE
=
70
;
>
update
count
:
2
SELECT
*
FROM
TEST
;
>
ID
VALUE
>
-- -----
>
1
50
>
rows
:
1
DROP
TABLE
TEST
;
DROP
TABLE
TEST
;
>
ok
>
ok
CREATE
TABLE
T
(
ID
INT
,
F
BOOLEAN
,
VALUE
INT
);
>
ok
INSERT
INTO
T
VALUES
(
1
,
FALSE
,
10
),
(
2
,
TRUE
,
20
);
>
update
count
:
2
CREATE
TABLE
S
(
S_ID
INT
,
S_F
BOOLEAN
,
S_VALUE
INT
);
>
ok
INSERT
INTO
S
VALUES
(
1
,
FALSE
,
100
),
(
2
,
TRUE
,
200
),
(
3
,
FALSE
,
300
),
(
4
,
TRUE
,
400
);
>
update
count
:
4
MERGE
INTO
T
USING
S
ON
ID
=
S_ID
WHEN
MATCHED
AND
F
THEN
UPDATE
SET
VALUE
=
S_VALUE
WHEN
MATCHED
AND
NOT
F
THEN
DELETE
WHEN
NOT
MATCHED
AND
S_F
THEN
INSERT
VALUES
(
S_ID
,
S_F
,
S_VALUE
);
>
update
count
:
3
SELECT
*
FROM
T
;
>
ID
F
VALUE
>
-- ---- -----
>
2
TRUE
200
>
4
TRUE
400
>
rows
:
2
DROP
TABLE
T
,
S
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论