Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
55869c8f
提交
55869c8f
authored
5月 02, 2018
作者:
XEHA6284
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for INSERT IGNORE from SELECT in MySQL Mode
上级
9f7ef4b1
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
585 行增加
和
561 行删除
+585
-561
Insert.java
h2/src/main/org/h2/command/dml/Insert.java
+487
-483
insertIgnore.sql
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
+98
-78
没有找到文件。
h2/src/main/org/h2/command/dml/Insert.java
浏览文件 @
55869c8f
/*
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Initial Developer: H2 Group
*/
*/
package
org
.
h2
.
command
.
dml
;
package
org
.
h2
.
command
.
dml
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.Trigger
;
import
org.h2.api.Trigger
;
import
org.h2.command.Command
;
import
org.h2.command.Command
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.Prepared
;
import
org.h2.command.Prepared
;
import
org.h2.engine.GeneratedKeys
;
import
org.h2.engine.GeneratedKeys
;
import
org.h2.engine.Right
;
import
org.h2.engine.Right
;
import
org.h2.engine.Session
;
import
org.h2.engine.Session
;
import
org.h2.engine.UndoLogRecord
;
import
org.h2.engine.UndoLogRecord
;
import
org.h2.expression.Comparison
;
import
org.h2.expression.Comparison
;
import
org.h2.expression.ConditionAndOr
;
import
org.h2.expression.ConditionAndOr
;
import
org.h2.expression.Expression
;
import
org.h2.expression.Expression
;
import
org.h2.expression.ExpressionColumn
;
import
org.h2.expression.ExpressionColumn
;
import
org.h2.expression.Parameter
;
import
org.h2.expression.Parameter
;
import
org.h2.expression.SequenceValue
;
import
org.h2.expression.SequenceValue
;
import
org.h2.expression.ValueExpression
;
import
org.h2.expression.ValueExpression
;
import
org.h2.index.Index
;
import
org.h2.index.Index
;
import
org.h2.index.PageDataIndex
;
import
org.h2.index.PageDataIndex
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.mvstore.db.MVPrimaryIndex
;
import
org.h2.mvstore.db.MVPrimaryIndex
;
import
org.h2.result.ResultInterface
;
import
org.h2.result.ResultInterface
;
import
org.h2.result.ResultTarget
;
import
org.h2.result.ResultTarget
;
import
org.h2.result.Row
;
import
org.h2.result.Row
;
import
org.h2.table.Column
;
import
org.h2.table.Column
;
import
org.h2.table.Table
;
import
org.h2.table.Table
;
import
org.h2.table.TableFilter
;
import
org.h2.table.TableFilter
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.Utils
;
import
org.h2.util.Utils
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.ValueNull
;
import
org.h2.value.ValueNull
;
/**
/**
* This class represents the statement
* This class represents the statement
* INSERT
* INSERT
*/
*/
public
class
Insert
extends
Prepared
implements
ResultTarget
{
public
class
Insert
extends
Prepared
implements
ResultTarget
{
private
Table
table
;
private
Table
table
;
private
Column
[]
columns
;
private
Column
[]
columns
;
private
final
ArrayList
<
Expression
[]>
list
=
Utils
.
newSmallArrayList
();
private
final
ArrayList
<
Expression
[]>
list
=
Utils
.
newSmallArrayList
();
private
Query
query
;
private
Query
query
;
private
boolean
sortedInsertMode
;
private
boolean
sortedInsertMode
;
private
int
rowNumber
;
private
int
rowNumber
;
private
boolean
insertFromSelect
;
private
boolean
insertFromSelect
;
/**
/**
* This table filter is for MERGE..USING support - not used in stand-alone DML
* This table filter is for MERGE..USING support - not used in stand-alone DML
*/
*/
private
TableFilter
sourceTableFilter
;
private
TableFilter
sourceTableFilter
;
/**
/**
* For MySQL-style INSERT ... ON DUPLICATE KEY UPDATE ....
* For MySQL-style INSERT ... ON DUPLICATE KEY UPDATE ....
*/
*/
private
HashMap
<
Column
,
Expression
>
duplicateKeyAssignmentMap
;
private
HashMap
<
Column
,
Expression
>
duplicateKeyAssignmentMap
;
/**
/**
* For MySQL-style INSERT IGNORE
* For MySQL-style INSERT IGNORE
*/
*/
private
boolean
ignore
;
private
boolean
ignore
;
public
Insert
(
Session
session
)
{
public
Insert
(
Session
session
)
{
super
(
session
);
super
(
session
);
}
}
@Override
@Override
public
void
setCommand
(
Command
command
)
{
public
void
setCommand
(
Command
command
)
{
super
.
setCommand
(
command
);
super
.
setCommand
(
command
);
if
(
query
!=
null
)
{
if
(
query
!=
null
)
{
query
.
setCommand
(
command
);
query
.
setCommand
(
command
);
}
}
}
}
public
void
setTable
(
Table
table
)
{
public
void
setTable
(
Table
table
)
{
this
.
table
=
table
;
this
.
table
=
table
;
}
}
public
void
setColumns
(
Column
[]
columns
)
{
public
void
setColumns
(
Column
[]
columns
)
{
this
.
columns
=
columns
;
this
.
columns
=
columns
;
}
}
/**
/**
* Sets MySQL-style INSERT IGNORE mode
* Sets MySQL-style INSERT IGNORE mode
* @param ignore ignore errors
* @param ignore ignore errors
*/
*/
public
void
setIgnore
(
boolean
ignore
)
{
public
void
setIgnore
(
boolean
ignore
)
{
this
.
ignore
=
ignore
;
this
.
ignore
=
ignore
;
}
}
public
void
setQuery
(
Query
query
)
{
public
void
setQuery
(
Query
query
)
{
this
.
query
=
query
;
this
.
query
=
query
;
}
}
/**
/**
* Keep a collection of the columns to pass to update if a duplicate key
* Keep a collection of the columns to pass to update if a duplicate key
* happens, for MySQL-style INSERT ... ON DUPLICATE KEY UPDATE ....
* happens, for MySQL-style INSERT ... ON DUPLICATE KEY UPDATE ....
*
*
* @param column the column
* @param column the column
* @param expression the expression
* @param expression the expression
*/
*/
public
void
addAssignmentForDuplicate
(
Column
column
,
Expression
expression
)
{
public
void
addAssignmentForDuplicate
(
Column
column
,
Expression
expression
)
{
if
(
duplicateKeyAssignmentMap
==
null
)
{
if
(
duplicateKeyAssignmentMap
==
null
)
{
duplicateKeyAssignmentMap
=
new
HashMap
<>();
duplicateKeyAssignmentMap
=
new
HashMap
<>();
}
}
if
(
duplicateKeyAssignmentMap
.
containsKey
(
column
))
{
if
(
duplicateKeyAssignmentMap
.
containsKey
(
column
))
{
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_COLUMN_NAME_1
,
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_COLUMN_NAME_1
,
column
.
getName
());
column
.
getName
());
}
}
duplicateKeyAssignmentMap
.
put
(
column
,
expression
);
duplicateKeyAssignmentMap
.
put
(
column
,
expression
);
}
}
/**
/**
* Add a row to this merge statement.
* Add a row to this merge statement.
*
*
* @param expr the list of values
* @param expr the list of values
*/
*/
public
void
addRow
(
Expression
[]
expr
)
{
public
void
addRow
(
Expression
[]
expr
)
{
list
.
add
(
expr
);
list
.
add
(
expr
);
}
}
@Override
@Override
public
int
update
()
{
public
int
update
()
{
Index
index
=
null
;
Index
index
=
null
;
if
(
sortedInsertMode
)
{
if
(
sortedInsertMode
)
{
index
=
table
.
getScanIndex
(
session
);
index
=
table
.
getScanIndex
(
session
);
index
.
setSortedInsertMode
(
true
);
index
.
setSortedInsertMode
(
true
);
}
}
try
{
try
{
return
insertRows
();
return
insertRows
();
}
finally
{
}
finally
{
if
(
index
!=
null
)
{
if
(
index
!=
null
)
{
index
.
setSortedInsertMode
(
false
);
index
.
setSortedInsertMode
(
false
);
}
}
}
}
}
}
private
int
insertRows
()
{
private
int
insertRows
()
{
session
.
getUser
().
checkRight
(
table
,
Right
.
INSERT
);
session
.
getUser
().
checkRight
(
table
,
Right
.
INSERT
);
setCurrentRowNumber
(
0
);
setCurrentRowNumber
(
0
);
table
.
fire
(
session
,
Trigger
.
INSERT
,
true
);
table
.
fire
(
session
,
Trigger
.
INSERT
,
true
);
rowNumber
=
0
;
rowNumber
=
0
;
GeneratedKeys
generatedKeys
=
session
.
getGeneratedKeys
();
GeneratedKeys
generatedKeys
=
session
.
getGeneratedKeys
();
generatedKeys
.
initialize
(
table
);
generatedKeys
.
initialize
(
table
);
int
listSize
=
list
.
size
();
int
listSize
=
list
.
size
();
if
(
listSize
>
0
)
{
if
(
listSize
>
0
)
{
int
columnLen
=
columns
.
length
;
int
columnLen
=
columns
.
length
;
for
(
int
x
=
0
;
x
<
listSize
;
x
++)
{
for
(
int
x
=
0
;
x
<
listSize
;
x
++)
{
session
.
startStatementWithinTransaction
();
session
.
startStatementWithinTransaction
();
generatedKeys
.
nextRow
();
generatedKeys
.
nextRow
();
Row
newRow
=
table
.
getTemplateRow
();
Row
newRow
=
table
.
getTemplateRow
();
Expression
[]
expr
=
list
.
get
(
x
);
Expression
[]
expr
=
list
.
get
(
x
);
setCurrentRowNumber
(
x
+
1
);
setCurrentRowNumber
(
x
+
1
);
for
(
int
i
=
0
;
i
<
columnLen
;
i
++)
{
for
(
int
i
=
0
;
i
<
columnLen
;
i
++)
{
Column
c
=
columns
[
i
];
Column
c
=
columns
[
i
];
int
index
=
c
.
getColumnId
();
int
index
=
c
.
getColumnId
();
Expression
e
=
expr
[
i
];
Expression
e
=
expr
[
i
];
if
(
e
!=
null
)
{
if
(
e
!=
null
)
{
// e can be null (DEFAULT)
// e can be null (DEFAULT)
e
=
e
.
optimize
(
session
);
e
=
e
.
optimize
(
session
);
try
{
try
{
Value
v
=
c
.
convert
(
e
.
getValue
(
session
),
session
.
getDatabase
().
getMode
());
Value
v
=
c
.
convert
(
e
.
getValue
(
session
),
session
.
getDatabase
().
getMode
());
newRow
.
setValue
(
index
,
v
);
newRow
.
setValue
(
index
,
v
);
if
(
e
instanceof
SequenceValue
)
{
if
(
e
instanceof
SequenceValue
)
{
generatedKeys
.
add
(
c
);
generatedKeys
.
add
(
c
);
}
}
}
catch
(
DbException
ex
)
{
}
catch
(
DbException
ex
)
{
throw
setRow
(
ex
,
x
,
getSQL
(
expr
));
throw
setRow
(
ex
,
x
,
getSQL
(
expr
));
}
}
}
}
}
}
rowNumber
++;
rowNumber
++;
table
.
validateConvertUpdateSequence
(
session
,
newRow
);
table
.
validateConvertUpdateSequence
(
session
,
newRow
);
boolean
done
=
table
.
fireBeforeRow
(
session
,
null
,
newRow
);
boolean
done
=
table
.
fireBeforeRow
(
session
,
null
,
newRow
);
if
(!
done
)
{
if
(!
done
)
{
table
.
lock
(
session
,
true
,
false
);
table
.
lock
(
session
,
true
,
false
);
try
{
try
{
table
.
addRow
(
session
,
newRow
);
table
.
addRow
(
session
,
newRow
);
}
catch
(
DbException
de
)
{
}
catch
(
DbException
de
)
{
if
(
handleOnDuplicate
(
de
))
{
if
(
handleOnDuplicate
(
de
))
{
// MySQL returns 2 for updated row
// MySQL returns 2 for updated row
// TODO: detect no-op change
// TODO: detect no-op change
rowNumber
++;
rowNumber
++;
}
else
{
}
else
{
// INSERT IGNORE case
// INSERT IGNORE case
rowNumber
--;
rowNumber
--;
}
}
continue
;
continue
;
}
}
generatedKeys
.
confirmRow
(
newRow
);
generatedKeys
.
confirmRow
(
newRow
);
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
}
}
}
}
}
else
{
}
else
{
table
.
lock
(
session
,
true
,
false
);
table
.
lock
(
session
,
true
,
false
);
if
(
insertFromSelect
)
{
if
(
insertFromSelect
)
{
query
.
query
(
0
,
this
);
query
.
query
(
0
,
this
);
}
else
{
}
else
{
ResultInterface
rows
=
query
.
query
(
0
);
ResultInterface
rows
=
query
.
query
(
0
);
while
(
rows
.
next
())
{
int
updatedRows
=
0
;
generatedKeys
.
nextRow
();
while
(
rows
.
next
())
{
Value
[]
r
=
rows
.
currentRow
();
generatedKeys
.
nextRow
();
try
{
Value
[]
r
=
rows
.
currentRow
();
Row
newRow
=
addRowImpl
(
r
);
try
{
if
(
newRow
!=
null
)
{
Expression
[]
exp
=
new
Expression
[
columns
.
length
];
generatedKeys
.
confirmRow
(
newRow
);
for
(
int
j
=
0
,
len
=
columns
.
length
;
j
<
len
;
j
++)
{
}
exp
[
j
]
=
ValueExpression
.
get
(
r
[
j
]);
}
catch
(
DbException
de
)
{
}
if
(
handleOnDuplicate
(
de
))
{
addRow
(
exp
);
// MySQL returns 2 for updated row
Row
newRow
=
addRowImpl
(
r
);
// TODO: detect no-op change
if
(
newRow
!=
null
)
{
rowNumber
++;
generatedKeys
.
confirmRow
(
newRow
);
}
else
{
}
// INSERT IGNORE case
}
catch
(
DbException
de
)
{
rowNumber
--;
if
(
handleOnDuplicate
(
de
))
{
}
// MySQL returns 2 for updated row
}
// TODO: detect no-op change
}
updatedRows
++;
rows
.
close
();
}
else
{
}
// INSERT IGNORE case
}
rowNumber
--;
table
.
fire
(
session
,
Trigger
.
INSERT
,
false
);
}
return
rowNumber
;
}
}
}
rows
.
close
();
@Override
rowNumber
+=
updatedRows
;
public
void
addRow
(
Value
[]
values
)
{
}
addRowImpl
(
values
);
}
}
table
.
fire
(
session
,
Trigger
.
INSERT
,
false
);
return
rowNumber
;
private
Row
addRowImpl
(
Value
[]
values
)
{
}
Row
newRow
=
table
.
getTemplateRow
();
setCurrentRowNumber
(++
rowNumber
);
@Override
Expression
[]
exp
=
new
Expression
[
columns
.
length
];
public
void
addRow
(
Value
[]
values
)
{
for
(
int
j
=
0
,
len
=
columns
.
length
;
j
<
len
;
j
++)
{
addRowImpl
(
values
);
Column
c
=
columns
[
j
];
}
int
index
=
c
.
getColumnId
();
try
{
private
Row
addRowImpl
(
Value
[]
values
)
{
Value
v
=
c
.
convert
(
values
[
j
],
session
.
getDatabase
().
getMode
());
Row
newRow
=
table
.
getTemplateRow
();
newRow
.
setValue
(
index
,
v
);
setCurrentRowNumber
(++
rowNumber
);
exp
[
j
]
=
ValueExpression
.
get
(
v
);
for
(
int
j
=
0
,
len
=
columns
.
length
;
j
<
len
;
j
++)
{
}
catch
(
DbException
ex
)
{
Column
c
=
columns
[
j
];
throw
setRow
(
ex
,
rowNumber
,
getSQL
(
values
));
int
index
=
c
.
getColumnId
();
}
try
{
}
Value
v
=
c
.
convert
(
values
[
j
],
session
.
getDatabase
().
getMode
());
table
.
validateConvertUpdateSequence
(
session
,
newRow
);
newRow
.
setValue
(
index
,
v
);
boolean
done
=
table
.
fireBeforeRow
(
session
,
null
,
newRow
);
}
catch
(
DbException
ex
)
{
if
(!
done
)
{
throw
setRow
(
ex
,
rowNumber
,
getSQL
(
values
));
addRow
(
exp
);
}
table
.
addRow
(
session
,
newRow
);
}
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
table
.
validateConvertUpdateSequence
(
session
,
newRow
);
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
boolean
done
=
table
.
fireBeforeRow
(
session
,
null
,
newRow
);
return
newRow
;
if
(!
done
)
{
}
table
.
addRow
(
session
,
newRow
);
return
null
;
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
newRow
);
}
table
.
fireAfterRow
(
session
,
null
,
newRow
,
false
);
return
newRow
;
@Override
}
public
int
getRowCount
()
{
return
null
;
return
rowNumber
;
}
}
@Override
@Override
public
int
getRowCount
()
{
public
String
getPlanSQL
()
{
return
rowNumber
;
StatementBuilder
buff
=
new
StatementBuilder
(
"INSERT INTO "
);
}
buff
.
append
(
table
.
getSQL
()).
append
(
'('
);
for
(
Column
c
:
columns
)
{
@Override
buff
.
appendExceptFirst
(
", "
);
public
String
getPlanSQL
()
{
buff
.
append
(
c
.
getSQL
());
StatementBuilder
buff
=
new
StatementBuilder
(
"INSERT INTO "
);
}
buff
.
append
(
table
.
getSQL
()).
append
(
'('
);
buff
.
append
(
")\n"
);
for
(
Column
c
:
columns
)
{
if
(
insertFromSelect
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
"DIRECT "
);
buff
.
append
(
c
.
getSQL
());
}
}
if
(
sortedInsertMode
)
{
buff
.
append
(
")\n"
);
buff
.
append
(
"SORTED "
);
if
(
insertFromSelect
)
{
}
buff
.
append
(
"DIRECT "
);
if
(!
list
.
isEmpty
())
{
}
buff
.
append
(
"VALUES "
);
if
(
sortedInsertMode
)
{
int
row
=
0
;
buff
.
append
(
"SORTED "
);
if
(
list
.
size
()
>
1
)
{
}
buff
.
append
(
'\n'
);
if
(!
list
.
isEmpty
())
{
}
buff
.
append
(
"VALUES "
);
for
(
Expression
[]
expr
:
list
)
{
int
row
=
0
;
if
(
row
++
>
0
)
{
if
(
list
.
size
()
>
1
)
{
buff
.
append
(
",\n"
);
buff
.
append
(
'\n'
);
}
}
buff
.
append
(
'('
);
for
(
Expression
[]
expr
:
list
)
{
buff
.
resetCount
();
if
(
row
++
>
0
)
{
for
(
Expression
e
:
expr
)
{
buff
.
append
(
",\n"
);
buff
.
appendExceptFirst
(
", "
);
}
if
(
e
==
null
)
{
buff
.
append
(
'('
);
buff
.
append
(
"DEFAULT"
);
buff
.
resetCount
();
}
else
{
for
(
Expression
e
:
expr
)
{
buff
.
append
(
e
.
getSQL
());
buff
.
appendExceptFirst
(
", "
);
}
if
(
e
==
null
)
{
}
buff
.
append
(
"DEFAULT"
);
buff
.
append
(
')'
);
}
else
{
}
buff
.
append
(
e
.
getSQL
());
}
else
{
}
buff
.
append
(
query
.
getPlanSQL
());
}
}
buff
.
append
(
')'
);
return
buff
.
toString
();
}
}
}
else
{
buff
.
append
(
query
.
getPlanSQL
());
@Override
}
public
void
prepare
()
{
return
buff
.
toString
();
if
(
columns
==
null
)
{
}
if
(!
list
.
isEmpty
()
&&
list
.
get
(
0
).
length
==
0
)
{
// special case where table is used as a sequence
@Override
columns
=
new
Column
[
0
];
public
void
prepare
()
{
}
else
{
if
(
columns
==
null
)
{
columns
=
table
.
getColumns
();
if
(!
list
.
isEmpty
()
&&
list
.
get
(
0
).
length
==
0
)
{
}
// special case where table is used as a sequence
}
columns
=
new
Column
[
0
];
if
(!
list
.
isEmpty
())
{
}
else
{
for
(
Expression
[]
expr
:
list
)
{
columns
=
table
.
getColumns
();
if
(
expr
.
length
!=
columns
.
length
)
{
}
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
}
if
(!
list
.
isEmpty
())
{
for
(
int
i
=
0
,
len
=
expr
.
length
;
i
<
len
;
i
++)
{
for
(
Expression
[]
expr
:
list
)
{
Expression
e
=
expr
[
i
];
if
(
expr
.
length
!=
columns
.
length
)
{
if
(
e
!=
null
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
if
(
sourceTableFilter
!=
null
){
}
e
.
mapColumns
(
sourceTableFilter
,
0
);
for
(
int
i
=
0
,
len
=
expr
.
length
;
i
<
len
;
i
++)
{
}
Expression
e
=
expr
[
i
];
e
=
e
.
optimize
(
session
);
if
(
e
!=
null
)
{
if
(
e
instanceof
Parameter
)
{
if
(
sourceTableFilter
!=
null
){
Parameter
p
=
(
Parameter
)
e
;
e
.
mapColumns
(
sourceTableFilter
,
0
);
p
.
setColumn
(
columns
[
i
]);
}
}
e
=
e
.
optimize
(
session
);
expr
[
i
]
=
e
;
if
(
e
instanceof
Parameter
)
{
}
Parameter
p
=
(
Parameter
)
e
;
}
p
.
setColumn
(
columns
[
i
]);
}
}
}
else
{
expr
[
i
]
=
e
;
query
.
prepare
();
}
if
(
query
.
getColumnCount
()
!=
columns
.
length
)
{
}
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
}
}
else
{
}
query
.
prepare
();
}
if
(
query
.
getColumnCount
()
!=
columns
.
length
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
@Override
}
public
boolean
isTransactional
()
{
}
return
true
;
}
}
@Override
@Override
public
boolean
isTransactional
()
{
public
ResultInterface
queryMeta
()
{
return
true
;
return
null
;
}
}
@Override
public
void
setSortedInsertMode
(
boolean
sortedInsertMode
)
{
public
ResultInterface
queryMeta
()
{
this
.
sortedInsertMode
=
sortedInsertMode
;
return
null
;
}
}
@Override
public
void
setSortedInsertMode
(
boolean
sortedInsertMode
)
{
public
int
getType
()
{
this
.
sortedInsertMode
=
sortedInsertMode
;
return
CommandInterface
.
INSERT
;
}
}
@Override
public
void
setInsertFromSelect
(
boolean
value
)
{
public
int
getType
()
{
this
.
insertFromSelect
=
value
;
return
CommandInterface
.
INSERT
;
}
}
@Override
public
void
setInsertFromSelect
(
boolean
value
)
{
public
boolean
isCacheable
()
{
this
.
insertFromSelect
=
value
;
return
duplicateKeyAssignmentMap
==
null
||
}
duplicateKeyAssignmentMap
.
isEmpty
();
}
@Override
public
boolean
isCacheable
()
{
/**
return
duplicateKeyAssignmentMap
==
null
||
* @param de duplicate key exception
duplicateKeyAssignmentMap
.
isEmpty
();
* @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
)
{
* @param de duplicate key exception
throw
de
;
* @return {@code true} if row was updated, {@code false} if row was ignored
}
*/
if
(
duplicateKeyAssignmentMap
==
null
||
private
boolean
handleOnDuplicate
(
DbException
de
)
{
duplicateKeyAssignmentMap
.
isEmpty
())
{
if
(
de
.
getErrorCode
()
!=
ErrorCode
.
DUPLICATE_KEY_1
)
{
if
(
ignore
)
{
throw
de
;
return
false
;
}
}
if
(
duplicateKeyAssignmentMap
==
null
||
throw
de
;
duplicateKeyAssignmentMap
.
isEmpty
())
{
}
if
(
ignore
)
{
return
false
;
ArrayList
<
String
>
variableNames
=
new
ArrayList
<>(
}
duplicateKeyAssignmentMap
.
size
());
throw
de
;
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
}
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
String
key
=
table
.
getSchema
().
getName
()
+
"."
+
ArrayList
<
String
>
variableNames
=
new
ArrayList
<>(
table
.
getName
()
+
"."
+
columns
[
i
].
getName
();
duplicateKeyAssignmentMap
.
size
());
variableNames
.
add
(
key
);
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
session
.
setVariable
(
key
,
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
row
[
i
].
getValue
(
session
));
String
key
=
table
.
getSchema
().
getName
()
+
"."
+
}
table
.
getName
()
+
"."
+
columns
[
i
].
getName
();
variableNames
.
add
(
key
);
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
session
.
setVariable
(
key
,
buff
.
append
(
table
.
getSQL
()).
append
(
" SET "
);
row
[
i
].
getValue
(
session
));
for
(
Column
column
:
duplicateKeyAssignmentMap
.
keySet
())
{
}
buff
.
appendExceptFirst
(
", "
);
Expression
ex
=
duplicateKeyAssignmentMap
.
get
(
column
);
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
buff
.
append
(
column
.
getSQL
()).
append
(
"="
).
append
(
ex
.
getSQL
());
buff
.
append
(
table
.
getSQL
()).
append
(
" SET "
);
}
for
(
Column
column
:
duplicateKeyAssignmentMap
.
keySet
())
{
buff
.
append
(
" WHERE "
);
buff
.
appendExceptFirst
(
", "
);
Index
foundIndex
=
(
Index
)
de
.
getSource
();
Expression
ex
=
duplicateKeyAssignmentMap
.
get
(
column
);
if
(
foundIndex
==
null
)
{
buff
.
append
(
column
.
getSQL
()).
append
(
"="
).
append
(
ex
.
getSQL
());
throw
DbException
.
getUnsupportedException
(
}
"Unable to apply ON DUPLICATE KEY UPDATE, no index found!"
);
buff
.
append
(
" WHERE "
);
}
Index
foundIndex
=
(
Index
)
de
.
getSource
();
buff
.
append
(
prepareUpdateCondition
(
foundIndex
).
getSQL
());
if
(
foundIndex
==
null
)
{
String
sql
=
buff
.
toString
();
throw
DbException
.
getUnsupportedException
(
Update
command
=
(
Update
)
session
.
prepare
(
sql
);
"Unable to apply ON DUPLICATE KEY UPDATE, no index found!"
);
command
.
setUpdateToCurrentValuesReturnsZero
(
true
);
}
for
(
Parameter
param
:
command
.
getParameters
())
{
buff
.
append
(
prepareUpdateCondition
(
foundIndex
).
getSQL
());
Parameter
insertParam
=
parameters
.
get
(
param
.
getIndex
());
String
sql
=
buff
.
toString
();
param
.
setValue
(
insertParam
.
getValue
(
session
));
Update
command
=
(
Update
)
session
.
prepare
(
sql
);
}
command
.
setUpdateToCurrentValuesReturnsZero
(
true
);
boolean
result
=
command
.
update
()
>
0
;
for
(
Parameter
param
:
command
.
getParameters
())
{
for
(
String
variableName
:
variableNames
)
{
Parameter
insertParam
=
parameters
.
get
(
param
.
getIndex
());
session
.
setVariable
(
variableName
,
ValueNull
.
INSTANCE
);
param
.
setValue
(
insertParam
.
getValue
(
session
));
}
}
return
result
;
boolean
result
=
command
.
update
()
>
0
;
}
for
(
String
variableName
:
variableNames
)
{
session
.
setVariable
(
variableName
,
ValueNull
.
INSTANCE
);
private
Expression
prepareUpdateCondition
(
Index
foundIndex
)
{
}
// MVPrimaryIndex is playing fast and loose with it's implementation of
return
result
;
// the Index interface.
}
// It returns all of the columns in the table when we call
// getIndexColumns() or getColumns().
private
Expression
prepareUpdateCondition
(
Index
foundIndex
)
{
// Don't have time right now to fix that, so just special-case it.
// MVPrimaryIndex is playing fast and loose with it's implementation of
// PageDataIndex has the same problem.
// the Index interface.
final
Column
[]
indexedColumns
;
// It returns all of the columns in the table when we call
if
(
foundIndex
instanceof
MVPrimaryIndex
)
{
// getIndexColumns() or getColumns().
MVPrimaryIndex
foundMV
=
(
MVPrimaryIndex
)
foundIndex
;
// Don't have time right now to fix that, so just special-case it.
indexedColumns
=
new
Column
[]
{
foundMV
.
getIndexColumns
()[
foundMV
// PageDataIndex has the same problem.
.
getMainIndexColumn
()].
column
};
final
Column
[]
indexedColumns
;
}
else
if
(
foundIndex
instanceof
PageDataIndex
)
{
if
(
foundIndex
instanceof
MVPrimaryIndex
)
{
PageDataIndex
foundPD
=
(
PageDataIndex
)
foundIndex
;
MVPrimaryIndex
foundMV
=
(
MVPrimaryIndex
)
foundIndex
;
int
mainIndexColumn
=
foundPD
.
getMainIndexColumn
();
indexedColumns
=
new
Column
[]
{
foundMV
.
getIndexColumns
()[
foundMV
indexedColumns
=
mainIndexColumn
>=
0
.
getMainIndexColumn
()].
column
};
?
new
Column
[]
{
foundPD
.
getIndexColumns
()[
mainIndexColumn
].
column
}
}
else
if
(
foundIndex
instanceof
PageDataIndex
)
{
:
foundIndex
.
getColumns
();
PageDataIndex
foundPD
=
(
PageDataIndex
)
foundIndex
;
}
else
{
int
mainIndexColumn
=
foundPD
.
getMainIndexColumn
();
indexedColumns
=
foundIndex
.
getColumns
();
indexedColumns
=
mainIndexColumn
>=
0
}
?
new
Column
[]
{
foundPD
.
getIndexColumns
()[
mainIndexColumn
].
column
}
:
foundIndex
.
getColumns
();
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
}
else
{
Expression
condition
=
null
;
indexedColumns
=
foundIndex
.
getColumns
();
for
(
Column
column
:
indexedColumns
)
{
}
ExpressionColumn
expr
=
new
ExpressionColumn
(
session
.
getDatabase
(),
table
.
getSchema
().
getName
(),
table
.
getName
(),
Expression
[]
row
=
list
.
get
(
getCurrentRowNumber
()
-
1
);
column
.
getName
());
Expression
condition
=
null
;
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
for
(
Column
column
:
indexedColumns
)
{
if
(
expr
.
getColumnName
().
equals
(
columns
[
i
].
getName
()))
{
ExpressionColumn
expr
=
new
ExpressionColumn
(
session
.
getDatabase
(),
if
(
condition
==
null
)
{
table
.
getSchema
().
getName
(),
table
.
getName
(),
condition
=
new
Comparison
(
session
,
Comparison
.
EQUAL
,
expr
,
row
[
i
]);
column
.
getName
());
}
else
{
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
condition
=
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
condition
,
if
(
expr
.
getColumnName
().
equals
(
columns
[
i
].
getName
()))
{
new
Comparison
(
session
,
Comparison
.
EQUAL
,
expr
,
row
[
i
]));
if
(
condition
==
null
)
{
}
condition
=
new
Comparison
(
session
,
Comparison
.
EQUAL
,
expr
,
row
[
i
]);
break
;
}
else
{
}
condition
=
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
condition
,
}
new
Comparison
(
session
,
Comparison
.
EQUAL
,
expr
,
row
[
i
]));
}
}
return
condition
;
break
;
}
}
}
public
void
setSourceTableFilter
(
TableFilter
sourceTableFilter
)
{
}
this
.
sourceTableFilter
=
sourceTableFilter
;
return
condition
;
}
}
}
public
void
setSourceTableFilter
(
TableFilter
sourceTableFilter
)
{
this
.
sourceTableFilter
=
sourceTableFilter
;
}
}
h2/src/test/org/h2/test/scripts/dml/insertIgnore.sql
浏览文件 @
55869c8f
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
-- Initial Developer: H2 Group
--
--
SET
MODE
MySQL
;
SET
MODE
MySQL
;
>
ok
>
ok
CREATE
TABLE
TEST
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
CREATE
TABLE
TEST
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
>
ok
>
ok
INSERT
INTO
TEST
VALUES
(
1
,
10
),
(
2
,
20
),
(
3
,
30
),
(
4
,
40
);
INSERT
INTO
TEST
VALUES
(
1
,
10
),
(
2
,
20
),
(
3
,
30
),
(
4
,
40
);
>
update
count
:
4
>
update
count
:
4
INSERT
INTO
TEST
VALUES
(
3
,
31
),
(
5
,
51
);
INSERT
INTO
TEST
VALUES
(
3
,
31
),
(
5
,
51
);
>
exception
DUPLICATE_KEY_1
>
exception
DUPLICATE_KEY_1
SELECT
*
FROM
TEST
ORDER
BY
ID
;
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
ID
VALUE
>
-- -----
>
-- -----
>
1
10
>
1
10
>
2
20
>
2
20
>
3
30
>
3
30
>
4
40
>
4
40
>
rows
(
ordered
):
4
>
rows
(
ordered
):
4
INSERT
IGNORE
INTO
TEST
VALUES
(
3
,
32
),
(
5
,
52
);
INSERT
IGNORE
INTO
TEST
VALUES
(
3
,
32
),
(
5
,
52
);
>
update
count
:
1
>
update
count
:
1
INSERT
IGNORE
INTO
TEST
VALUES
(
4
,
43
);
INSERT
IGNORE
INTO
TEST
VALUES
(
4
,
43
);
>
ok
>
ok
SELECT
*
FROM
TEST
ORDER
BY
ID
;
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
ID
VALUE
>
-- -----
>
-- -----
>
1
10
>
1
10
>
2
20
>
2
20
>
3
30
>
3
30
>
4
40
>
4
40
>
5
52
>
5
52
>
rows
(
ordered
):
5
>
rows
(
ordered
):
5
CREATE
TABLE
TESTREF
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
CREATE
TABLE
TESTREF
(
ID
BIGINT
PRIMARY
KEY
,
VALUE
INT
NOT
NULL
);
>
ok
>
ok
INSERT
INTO
TESTREF
VALUES
(
1
,
11
),
(
2
,
21
),
(
6
,
61
),
(
7
,
71
);
INSERT
INTO
TESTREF
VALUES
(
1
,
11
),
(
2
,
21
),
(
6
,
61
),
(
7
,
71
);
>
update
count
:
4
>
update
count
:
4
INSERT
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
INSERT
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
exception
DUPLICATE_KEY_1
>
exception
DUPLICATE_KEY_1
SELECT
*
FROM
TEST
ORDER
BY
ID
;
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
ID
VALUE
>
-- -----
>
-- -----
>
1
10
>
1
10
>
2
20
>
2
20
>
3
30
>
3
30
>
4
40
>
4
40
>
5
52
>
5
52
>
rows
(
ordered
):
5
>
rows
(
ordered
):
5
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
update
count
:
2
>
update
count
:
2
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
INSERT
IGNORE
INTO
TEST
(
ID
,
VALUE
)
SELECT
ID
,
VALUE
FROM
TESTREF
;
>
ok
>
ok
SELECT
*
FROM
TEST
ORDER
BY
ID
;
SELECT
*
FROM
TEST
ORDER
BY
ID
;
>
ID
VALUE
>
ID
VALUE
>
-- -----
>
-- -----
>
1
10
>
1
10
>
2
20
>
2
20
>
3
30
>
3
30
>
4
40
>
4
40
>
5
52
>
5
52
>
6
61
>
6
61
>
7
71
>
7
71
>
rows
(
ordered
):
7
>
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论