Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4617c019
提交
4617c019
authored
7 年前
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add partial support for INSERT IGNORE in MySQL mode
Only duplicate key errors are ignored.
上级
01397c72
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
73 行增加
和
4 行删除
+73
-4
Parser.java
h2/src/main/org/h2/command/Parser.java
+3
-0
Insert.java
h2/src/main/org/h2/command/dml/Insert.java
+27
-2
Mode.java
h2/src/main/org/h2/engine/Mode.java
+1
-1
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
insertIgnore.sql
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
+41
-0
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
4617c019
...
@@ -1220,6 +1220,9 @@ public class Parser {
...
@@ -1220,6 +1220,9 @@ public class Parser {
private
Insert
parseInsert
()
{
private
Insert
parseInsert
()
{
Insert
command
=
new
Insert
(
session
);
Insert
command
=
new
Insert
(
session
);
currentPrepared
=
command
;
currentPrepared
=
command
;
if
(
database
.
getMode
().
onDuplicateKeyUpdate
&&
readIf
(
"IGNORE"
))
{
command
.
setIgnore
(
true
);
}
read
(
"INTO"
);
read
(
"INTO"
);
Table
table
=
readTableOrView
();
Table
table
=
readTableOrView
();
command
.
setTable
(
table
);
command
.
setTable
(
table
);
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/command/dml/Insert.java
浏览文件 @
4617c019
...
@@ -57,6 +57,11 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -57,6 +57,11 @@ public class Insert extends Prepared implements ResultTarget {
*/
*/
private
HashMap
<
Column
,
Expression
>
duplicateKeyAssignmentMap
;
private
HashMap
<
Column
,
Expression
>
duplicateKeyAssignmentMap
;
/**
* For MySQL-style INSERT IGNORE
*/
private
boolean
ignore
;
public
Insert
(
Session
session
)
{
public
Insert
(
Session
session
)
{
super
(
session
);
super
(
session
);
}
}
...
@@ -77,6 +82,14 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -77,6 +82,14 @@ public class Insert extends Prepared implements ResultTarget {
this
.
columns
=
columns
;
this
.
columns
=
columns
;
}
}
/**
* Sets MySQL-style INSERT IGNORE mode
* @param ignore ignore errors
*/
public
void
setIgnore
(
boolean
ignore
)
{
this
.
ignore
=
ignore
;
}
public
void
setQuery
(
Query
query
)
{
public
void
setQuery
(
Query
query
)
{
this
.
query
=
query
;
this
.
query
=
query
;
}
}
...
@@ -160,7 +173,11 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -160,7 +173,11 @@ public class Insert extends Prepared implements ResultTarget {
try
{
try
{
table
.
addRow
(
session
,
newRow
);
table
.
addRow
(
session
,
newRow
);
}
catch
(
DbException
de
)
{
}
catch
(
DbException
de
)
{
handleOnDuplicate
(
de
);
if
(!
handleOnDuplicate
(
de
))
{
// INSERT IGNORE case
rowNumber
--;
continue
;
}
}
}
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
...
@@ -321,12 +338,19 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -321,12 +338,19 @@ public class Insert extends Prepared implements ResultTarget {
duplicateKeyAssignmentMap
.
isEmpty
();
duplicateKeyAssignmentMap
.
isEmpty
();
}
}
private
void
handleOnDuplicate
(
DbException
de
)
{
/**
* @param de duplicate key exception
* @return {@code true} if row was updated, {@code false} if row was ignored
*/
private
boolean
handleOnDuplicate
(
DbException
de
)
{
if
(
de
.
getErrorCode
()
!=
ErrorCode
.
DUPLICATE_KEY_1
)
{
if
(
de
.
getErrorCode
()
!=
ErrorCode
.
DUPLICATE_KEY_1
)
{
throw
de
;
throw
de
;
}
}
if
(
duplicateKeyAssignmentMap
==
null
||
if
(
duplicateKeyAssignmentMap
==
null
||
duplicateKeyAssignmentMap
.
isEmpty
())
{
duplicateKeyAssignmentMap
.
isEmpty
())
{
if
(
ignore
)
{
return
false
;
}
throw
de
;
throw
de
;
}
}
...
@@ -364,6 +388,7 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -364,6 +388,7 @@ public class Insert extends Prepared implements ResultTarget {
for
(
String
variableName
:
variableNames
)
{
for
(
String
variableName
:
variableNames
)
{
session
.
setVariable
(
variableName
,
ValueNull
.
INSTANCE
);
session
.
setVariable
(
variableName
,
ValueNull
.
INSTANCE
);
}
}
return
true
;
}
}
private
Expression
prepareUpdateCondition
(
Index
foundIndex
)
{
private
Expression
prepareUpdateCondition
(
Index
foundIndex
)
{
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/engine/Mode.java
浏览文件 @
4617c019
...
@@ -158,7 +158,7 @@ public class Mode {
...
@@ -158,7 +158,7 @@ public class Mode {
public
boolean
isolationLevelInSelectOrInsertStatement
;
public
boolean
isolationLevelInSelectOrInsertStatement
;
/**
/**
* MySQL style INSERT ... ON DUPLICATE KEY UPDATE ...
* MySQL style INSERT ... ON DUPLICATE KEY UPDATE ...
and INSERT IGNORE
*/
*/
public
boolean
onDuplicateKeyUpdate
;
public
boolean
onDuplicateKeyUpdate
;
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
4617c019
...
@@ -141,7 +141,7 @@ public class TestScript extends TestBase {
...
@@ -141,7 +141,7 @@ public class TestScript extends TestBase {
"parsedatetime"
,
"quarter"
,
"second"
,
"week"
,
"year"
})
{
"parsedatetime"
,
"quarter"
,
"second"
,
"week"
,
"year"
})
{
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
}
}
for
(
String
s
:
new
String
[]
{
"
with"
,
"mergeUsing
"
})
{
for
(
String
s
:
new
String
[]
{
"
insertIgnore"
,
"mergeUsing"
,
"with
"
})
{
testScript
(
"dml/"
+
s
+
".sql"
);
testScript
(
"dml/"
+
s
+
".sql"
);
}
}
deleteDb
(
"script"
);
deleteDb
(
"script"
);
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
0 → 100644
浏览文件 @
4617c019
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
SET
MODE
MySQL
;
>
ok
CREATE
TABLE
TEST
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
>
ok
INSERT
INTO
TEST
VALUES
(
1
,
10
),
(
2
,
20
),
(
3
,
30
),
(
4
,
40
);
>
update
count
:
4
INSERT
INTO
TEST
VALUES
(
3
,
31
),
(
5
,
51
);
>
exception
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
-- -----
>
1
10
>
2
20
>
3
30
>
4
40
>
rows
(
ordered
):
4
INSERT
IGNORE
INTO
TEST
VALUES
(
3
,
32
),
(
5
,
52
);
>
update
count
:
1
INSERT
IGNORE
INTO
TEST
VALUES
(
4
,
43
);
>
ok
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
-- -----
>
1
10
>
2
20
>
3
30
>
4
40
>
5
52
>
rows
(
ordered
):
5
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论