Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
7d1f4bcd
提交
7d1f4bcd
authored
5月 02, 2018
作者:
XEHA6284
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pass current row to handleOnDuplicate()
上级
c033f74c
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
14 行增加
和
18 行删除
+14
-18
Insert.java
h2/src/main/org/h2/command/dml/Insert.java
+14
-18
没有找到文件。
h2/src/main/org/h2/command/dml/Insert.java
浏览文件 @
7d1f4bcd
...
@@ -184,7 +184,7 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -184,7 +184,7 @@ public class Insert extends Prepared implements ResultTarget {
try
{
try
{
table
.
addRow
(
session
,
newRow
);
table
.
addRow
(
session
,
newRow
);
}
catch
(
DbException
de
)
{
}
catch
(
DbException
de
)
{
if
(
handleOnDuplicate
(
de
))
{
if
(
handleOnDuplicate
(
de
,
null
))
{
// MySQL returns 2 for updated row
// MySQL returns 2 for updated row
// TODO: detect no-op change
// TODO: detect no-op change
rowNumber
++;
rowNumber
++;
...
@@ -205,25 +205,19 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -205,25 +205,19 @@ public class Insert extends Prepared implements ResultTarget {
query
.
query
(
0
,
this
);
query
.
query
(
0
,
this
);
}
else
{
}
else
{
ResultInterface
rows
=
query
.
query
(
0
);
ResultInterface
rows
=
query
.
query
(
0
);
int
updatedRows
=
0
;
while
(
rows
.
next
())
{
while
(
rows
.
next
())
{
generatedKeys
.
nextRow
();
generatedKeys
.
nextRow
();
Value
[]
r
=
rows
.
currentRow
();
Value
[]
r
=
rows
.
currentRow
();
try
{
try
{
Expression
[]
exp
=
new
Expression
[
columns
.
length
];
for
(
int
j
=
0
,
len
=
columns
.
length
;
j
<
len
;
j
++)
{
exp
[
j
]
=
ValueExpression
.
get
(
r
[
j
]);
}
addRow
(
exp
);
Row
newRow
=
addRowImpl
(
r
);
Row
newRow
=
addRowImpl
(
r
);
if
(
newRow
!=
null
)
{
if
(
newRow
!=
null
)
{
generatedKeys
.
confirmRow
(
newRow
);
generatedKeys
.
confirmRow
(
newRow
);
}
}
}
catch
(
DbException
de
)
{
}
catch
(
DbException
de
)
{
if
(
handleOnDuplicate
(
de
))
{
if
(
handleOnDuplicate
(
de
,
r
))
{
// MySQL returns 2 for updated row
// MySQL returns 2 for updated row
// TODO: detect no-op change
// TODO: detect no-op change
updatedRows
++;
rowNumber
++;
}
else
{
}
else
{
// INSERT IGNORE case
// INSERT IGNORE case
rowNumber
--;
rowNumber
--;
...
@@ -231,8 +225,6 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -231,8 +225,6 @@ public class Insert extends Prepared implements ResultTarget {
}
}
}
}
rows
.
close
();
rows
.
close
();
list
.
clear
();
rowNumber
+=
updatedRows
;
}
}
}
}
table
.
fire
(
session
,
Trigger
.
INSERT
,
false
);
table
.
fire
(
session
,
Trigger
.
INSERT
,
false
);
...
@@ -385,9 +377,10 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -385,9 +377,10 @@ public class Insert extends Prepared implements ResultTarget {
/**
/**
* @param de duplicate key exception
* @param de duplicate key exception
* @param currentRow current row values (optional)
* @return {@code true} if row was updated, {@code false} if row was ignored
* @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
)
{
if
(
de
.
getErrorCode
()
!=
ErrorCode
.
DUPLICATE_KEY_1
)
{
throw
de
;
throw
de
;
}
}
...
@@ -401,13 +394,17 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -401,13 +394,17 @@ public class Insert extends Prepared implements ResultTarget {
ArrayList
<
String
>
variableNames
=
new
ArrayList
<>(
ArrayList
<
String
>
variableNames
=
new
ArrayList
<>(
duplicateKeyAssignmentMap
.
size
());
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
++)
{
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
String
key
=
table
.
getSchema
().
getName
()
+
"."
+
String
key
=
table
.
getSchema
().
getName
()
+
"."
+
table
.
getName
()
+
"."
+
columns
[
i
].
getName
();
table
.
getName
()
+
"."
+
columns
[
i
].
getName
();
variableNames
.
add
(
key
);
variableNames
.
add
(
key
);
if
(
currentRow
!=
null
)
{
row
[
i
]
=
ValueExpression
.
get
(
currentRow
[
i
]);
}
session
.
setVariable
(
key
,
session
.
setVariable
(
key
,
row
[
i
].
getValue
(
session
)
);
(
currentRow
==
null
)
?
row
[
i
].
getValue
(
session
)
:
currentRow
[
i
]
);
}
}
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
...
@@ -423,7 +420,7 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -423,7 +420,7 @@ public class Insert extends Prepared implements ResultTarget {
throw
DbException
.
getUnsupportedException
(
throw
DbException
.
getUnsupportedException
(
"Unable to apply ON DUPLICATE KEY UPDATE, no index found!"
);
"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
();
String
sql
=
buff
.
toString
();
Update
command
=
(
Update
)
session
.
prepare
(
sql
);
Update
command
=
(
Update
)
session
.
prepare
(
sql
);
command
.
setUpdateToCurrentValuesReturnsZero
(
true
);
command
.
setUpdateToCurrentValuesReturnsZero
(
true
);
...
@@ -438,7 +435,7 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -438,7 +435,7 @@ public class Insert extends Prepared implements ResultTarget {
return
result
;
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
// MVPrimaryIndex is playing fast and loose with it's implementation of
// the Index interface.
// the Index interface.
// It returns all of the columns in the table when we call
// It returns all of the columns in the table when we call
...
@@ -460,7 +457,6 @@ public class Insert extends Prepared implements ResultTarget {
...
@@ -460,7 +457,6 @@ public class Insert extends Prepared implements ResultTarget {
indexedColumns
=
foundIndex
.
getColumns
();
indexedColumns
=
foundIndex
.
getColumns
();
}
}
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
Expression
condition
=
null
;
Expression
condition
=
null
;
for
(
Column
column
:
indexedColumns
)
{
for
(
Column
column
:
indexedColumns
)
{
ExpressionColumn
expr
=
new
ExpressionColumn
(
session
.
getDatabase
(),
ExpressionColumn
expr
=
new
ExpressionColumn
(
session
.
getDatabase
(),
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论