Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
cc0f21ec
Unverified
提交
cc0f21ec
authored
5月 02, 2018
作者:
Evgenij Ryazanov
提交者:
GitHub
5月 02, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1115 from manuhode/manuh-issue#1095
Add support for INSERT IGNORE from SELECT in MySQL Mode
上级
3ab43d18
49f0cff9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
87 行增加
和
11 行删除
+87
-11
Insert.java
h2/src/main/org/h2/command/dml/Insert.java
+30
-11
insertIgnore.sql
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
+57
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Insert.java
浏览文件 @
cc0f21ec
...
...
@@ -23,6 +23,7 @@ import org.h2.expression.Expression;
import
org.h2.expression.ExpressionColumn
;
import
org.h2.expression.Parameter
;
import
org.h2.expression.SequenceValue
;
import
org.h2.expression.ValueExpression
;
import
org.h2.index.Index
;
import
org.h2.index.PageDataIndex
;
import
org.h2.message.DbException
;
...
...
@@ -183,7 +184,7 @@ public class Insert extends Prepared implements ResultTarget {
try
{
table
.
addRow
(
session
,
newRow
);
}
catch
(
DbException
de
)
{
if
(
handleOnDuplicate
(
de
))
{
if
(
handleOnDuplicate
(
de
,
null
))
{
// MySQL returns 2 for updated row
// TODO: detect no-op change
rowNumber
++;
...
...
@@ -207,9 +208,20 @@ public class Insert extends Prepared implements ResultTarget {
while
(
rows
.
next
())
{
generatedKeys
.
nextRow
();
Value
[]
r
=
rows
.
currentRow
();
Row
newRow
=
addRowImpl
(
r
);
if
(
newRow
!=
null
)
{
generatedKeys
.
confirmRow
(
newRow
);
try
{
Row
newRow
=
addRowImpl
(
r
);
if
(
newRow
!=
null
)
{
generatedKeys
.
confirmRow
(
newRow
);
}
}
catch
(
DbException
de
)
{
if
(
handleOnDuplicate
(
de
,
r
))
{
// MySQL returns 2 for updated row
// TODO: detect no-op change
rowNumber
++;
}
else
{
// INSERT IGNORE case
rowNumber
--;
}
}
}
rows
.
close
();
...
...
@@ -365,9 +377,10 @@ public class Insert extends Prepared implements ResultTarget {
/**
* @param de duplicate key exception
* @param currentRow current row values (optional)
* @return {@code true} if row was updated, {@code false} if row was ignored
*/
private
boolean
handleOnDuplicate
(
DbException
de
)
{
private
boolean
handleOnDuplicate
(
DbException
de
,
Value
[]
currentRow
)
{
if
(
de
.
getErrorCode
()
!=
ErrorCode
.
DUPLICATE_KEY_1
)
{
throw
de
;
}
...
...
@@ -381,13 +394,20 @@ public class Insert extends Prepared implements ResultTarget {
ArrayList
<
String
>
variableNames
=
new
ArrayList
<>(
duplicateKeyAssignmentMap
.
size
());
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
Expression
[]
row
=
(
currentRow
==
null
)
?
list
.
get
(
getCurrentRowNumber
()
-
1
)
:
new
Expression
[
columns
.
length
];
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
String
key
=
table
.
getSchema
().
getName
()
+
"."
+
table
.
getName
()
+
"."
+
columns
[
i
].
getName
();
variableNames
.
add
(
key
);
session
.
setVariable
(
key
,
row
[
i
].
getValue
(
session
));
Value
value
;
if
(
currentRow
!=
null
)
{
value
=
currentRow
[
i
];
row
[
i
]
=
ValueExpression
.
get
(
value
);
}
else
{
value
=
row
[
i
].
getValue
(
session
);
}
session
.
setVariable
(
key
,
value
);
}
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
...
...
@@ -403,7 +423,7 @@ public class Insert extends Prepared implements ResultTarget {
throw
DbException
.
getUnsupportedException
(
"Unable to apply ON DUPLICATE KEY UPDATE, no index found!"
);
}
buff
.
append
(
prepareUpdateCondition
(
foundIndex
).
getSQL
());
buff
.
append
(
prepareUpdateCondition
(
foundIndex
,
row
).
getSQL
());
String
sql
=
buff
.
toString
();
Update
command
=
(
Update
)
session
.
prepare
(
sql
);
command
.
setUpdateToCurrentValuesReturnsZero
(
true
);
...
...
@@ -418,7 +438,7 @@ public class Insert extends Prepared implements ResultTarget {
return
result
;
}
private
Expression
prepareUpdateCondition
(
Index
foundIndex
)
{
private
Expression
prepareUpdateCondition
(
Index
foundIndex
,
Expression
[]
row
)
{
// MVPrimaryIndex is playing fast and loose with it's implementation of
// the Index interface.
// It returns all of the columns in the table when we call
...
...
@@ -440,7 +460,6 @@ public class Insert extends Prepared implements ResultTarget {
indexedColumns
=
foundIndex
.
getColumns
();
}
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
Expression
condition
=
null
;
for
(
Column
column
:
indexedColumns
)
{
ExpressionColumn
expr
=
new
ExpressionColumn
(
session
.
getDatabase
(),
...
...
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
浏览文件 @
cc0f21ec
...
...
@@ -39,3 +39,60 @@ SELECT * FROM TEST ORDER BY ID;
>
4
40
>
5
52
>
rows
(
ordered
):
5
CREATE
TABLE
TESTREF
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
>
ok
INSERT
INTO
TESTREF
VALUES
(
1
,
11
),
(
2
,
21
),
(
6
,
61
),
(
7
,
71
);
>
update
count
:
4
INSERT
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
exception
DUPLICATE_KEY_1
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
-- -----
>
1
10
>
2
20
>
3
30
>
4
40
>
5
52
>
rows
(
ordered
):
5
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
update
count
:
2
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
ok
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
-- -----
>
1
10
>
2
20
>
3
30
>
4
40
>
5
52
>
6
61
>
7
71
>
rows
(
ordered
):
7
INSERT
INTO
TESTREF
VALUES
(
8
,
81
),
(
9
,
91
);
>
update
count
:
2
INSERT
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
ON
DUPLICATE
KEY
UPDATE
VALUE
=
83
;
>
update
count
:
10
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
-- -----
>
1
83
>
2
83
>
3
30
>
4
40
>
5
52
>
6
83
>
7
83
>
8
81
>
9
91
>
rows
(
ordered
):
9
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论