Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
64fcf884
提交
64fcf884
authored
10月 22, 2013
作者:
noelgrandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 73: MySQL compatibility: support REPLACE, patch by Cemo Koc.
上级
970c2d36
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
427 行增加
和
1 行删除
+427
-1
changelog.html
h2/src/docsrc/html/changelog.html
+1
-1
CommandInterface.java
h2/src/main/org/h2/command/CommandInterface.java
+5
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+42
-0
Replace.java
h2/src/main/org/h2/command/dml/Replace.java
+320
-0
TestReplace.java
h2/src/test/org/h2/test/db/TestReplace.java
+59
-0
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
64fcf884
...
...
@@ -18,7 +18,7 @@ Change Log
<h1>
Change Log
</h1>
<h2>
Next Version (unreleased)
</h2>
<ul><li>
-
<ul><li>
Issue 73: MySQL compatibility: support REPLACE, patch by Cemo Koc.
</li></ul>
<h2>
Version 1.3.174 (2013-10-19)
</h2>
...
...
h2/src/main/org/h2/command/CommandInterface.java
浏览文件 @
64fcf884
...
...
@@ -335,6 +335,11 @@ public interface CommandInterface {
*/
int
MERGE
=
62
;
/**
* The type of a REPLACE statement.
*/
int
REPLACE
=
63
;
/**
* The type of a no operation statement.
*/
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
64fcf884
...
...
@@ -70,6 +70,7 @@ import org.h2.command.dml.Insert;
import
org.h2.command.dml.Merge
;
import
org.h2.command.dml.NoOperation
;
import
org.h2.command.dml.Query
;
import
org.h2.command.dml.Replace
;
import
org.h2.command.dml.RunScriptCommand
;
import
org.h2.command.dml.ScriptCommand
;
import
org.h2.command.dml.Select
;
...
...
@@ -407,6 +408,8 @@ public class Parser {
c
=
parseRunScript
();
}
else
if
(
readIf
(
"RELEASE"
))
{
c
=
parseReleaseSavepoint
();
}
else
if
(
readIf
(
"REPLACE"
))
{
c
=
parseReplace
();
}
break
;
case
's'
:
...
...
@@ -1044,6 +1047,45 @@ public class Parser {
return
command
;
}
/**
* MySQL compatibility. REPLACE is similar to MERGE.
*/
private
Replace
parseReplace
()
{
Replace
command
=
new
Replace
(
session
);
currentPrepared
=
command
;
read
(
"INTO"
);
Table
table
=
readTableOrView
();
command
.
setTable
(
table
);
if
(
readIf
(
"("
))
{
if
(
isSelect
())
{
command
.
setQuery
(
parseSelect
());
read
(
")"
);
return
command
;
}
Column
[]
columns
=
parseColumnList
(
table
);
command
.
setColumns
(
columns
);
}
if
(
readIf
(
"VALUES"
))
{
do
{
ArrayList
<
Expression
>
values
=
New
.
arrayList
();
read
(
"("
);
if
(!
readIf
(
")"
))
{
do
{
if
(
readIf
(
"DEFAULT"
))
{
values
.
add
(
null
);
}
else
{
values
.
add
(
readExpression
());
}
}
while
(
readIfMore
());
}
command
.
addRow
(
values
.
toArray
(
new
Expression
[
values
.
size
()]));
}
while
(
readIf
(
","
));
}
else
{
command
.
setQuery
(
parseSelect
());
}
return
command
;
}
private
TableFilter
readTableFilter
(
boolean
fromOuter
)
{
Table
table
;
String
alias
=
null
;
...
...
h2/src/main/org/h2/command/dml/Replace.java
0 → 100644
浏览文件 @
64fcf884
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
command
.
dml
;
import
org.h2.api.Trigger
;
import
org.h2.command.Command
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.Prepared
;
import
org.h2.constant.ErrorCode
;
import
org.h2.engine.Right
;
import
org.h2.engine.Session
;
import
org.h2.engine.UndoLogRecord
;
import
org.h2.expression.Expression
;
import
org.h2.expression.Parameter
;
import
org.h2.index.Index
;
import
org.h2.message.DbException
;
import
org.h2.result.ResultInterface
;
import
org.h2.result.Row
;
import
org.h2.table.Column
;
import
org.h2.table.Table
;
import
org.h2.util.New
;
import
org.h2.util.StatementBuilder
;
import
org.h2.value.Value
;
import
java.util.ArrayList
;
/**
* This class represents the MySQL-compatibility REPLACE statement
*/
public
class
Replace
extends
Prepared
{
private
Table
table
;
private
Column
[]
columns
;
private
Column
[]
keys
;
private
final
ArrayList
<
Expression
[]>
list
=
New
.
arrayList
();
private
Query
query
;
private
Prepared
update
;
public
Replace
(
Session
session
)
{
super
(
session
);
}
@Override
public
void
setCommand
(
Command
command
)
{
super
.
setCommand
(
command
);
if
(
query
!=
null
)
{
query
.
setCommand
(
command
);
}
}
public
void
setTable
(
Table
table
)
{
this
.
table
=
table
;
}
public
void
setColumns
(
Column
[]
columns
)
{
this
.
columns
=
columns
;
}
public
void
setKeys
(
Column
[]
keys
)
{
this
.
keys
=
keys
;
}
public
void
setQuery
(
Query
query
)
{
this
.
query
=
query
;
}
/**
* Add a row to this replace statement.
*
* @param expr the list of values
*/
public
void
addRow
(
Expression
[]
expr
)
{
list
.
add
(
expr
);
}
@Override
public
int
update
()
{
int
count
;
session
.
getUser
().
checkRight
(
table
,
Right
.
INSERT
);
session
.
getUser
().
checkRight
(
table
,
Right
.
UPDATE
);
setCurrentRowNumber
(
0
);
if
(
list
.
size
()
>
0
)
{
count
=
0
;
for
(
int
x
=
0
,
size
=
list
.
size
();
x
<
size
;
x
++)
{
setCurrentRowNumber
(
x
+
1
);
Expression
[]
expr
=
list
.
get
(
x
);
Row
newRow
=
table
.
getTemplateRow
();
for
(
int
i
=
0
,
len
=
columns
.
length
;
i
<
len
;
i
++)
{
Column
c
=
columns
[
i
];
int
index
=
c
.
getColumnId
();
Expression
e
=
expr
[
i
];
if
(
e
!=
null
)
{
// e can be null (DEFAULT)
try
{
Value
v
=
c
.
convert
(
e
.
getValue
(
session
));
newRow
.
setValue
(
index
,
v
);
}
catch
(
DbException
ex
)
{
throw
setRow
(
ex
,
count
,
getSQL
(
expr
));
}
}
}
replace
(
newRow
);
count
++;
}
}
else
{
ResultInterface
rows
=
query
.
query
(
0
);
count
=
0
;
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
true
);
table
.
lock
(
session
,
true
,
false
);
while
(
rows
.
next
())
{
count
++;
Value
[]
r
=
rows
.
currentRow
();
Row
newRow
=
table
.
getTemplateRow
();
setCurrentRowNumber
(
count
);
for
(
int
j
=
0
;
j
<
columns
.
length
;
j
++)
{
Column
c
=
columns
[
j
];
int
index
=
c
.
getColumnId
();
try
{
Value
v
=
c
.
convert
(
r
[
j
]);
newRow
.
setValue
(
index
,
v
);
}
catch
(
DbException
ex
)
{
throw
setRow
(
ex
,
count
,
getSQL
(
r
));
}
}
replace
(
newRow
);
}
rows
.
close
();
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
false
);
}
return
count
;
}
private
void
replace
(
Row
row
)
{
int
count
=
update
(
row
);
if
(
count
==
0
)
{
try
{
table
.
validateConvertUpdateSequence
(
session
,
row
);
boolean
done
=
table
.
fireBeforeRow
(
session
,
null
,
row
);
if
(!
done
)
{
table
.
lock
(
session
,
true
,
false
);
table
.
addRow
(
session
,
row
);
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
row
);
table
.
fireAfterRow
(
session
,
null
,
row
,
false
);
}
}
catch
(
DbException
e
)
{
if
(
e
.
getErrorCode
()
==
ErrorCode
.
DUPLICATE_KEY_1
)
{
// possibly a concurrent replace or insert
Index
index
=
(
Index
)
e
.
getSource
();
if
(
index
!=
null
)
{
// verify the index columns match the key
Column
[]
indexColumns
=
index
.
getColumns
();
boolean
indexMatchesKeys
=
false
;
if
(
indexColumns
.
length
<=
keys
.
length
)
{
for
(
int
i
=
0
;
i
<
indexColumns
.
length
;
i
++)
{
if
(
indexColumns
[
i
]
!=
keys
[
i
])
{
indexMatchesKeys
=
false
;
break
;
}
}
}
if
(
indexMatchesKeys
)
{
throw
DbException
.
get
(
ErrorCode
.
CONCURRENT_UPDATE_1
,
table
.
getName
());
}
}
}
throw
e
;
}
}
else
if
(
count
!=
1
)
{
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_KEY_1
,
table
.
getSQL
());
}
}
private
int
update
(
Row
row
)
{
// if there is no valid primary key, the statement degenerates to an INSERT
if
(
update
==
null
){
return
0
;
}
ArrayList
<
Parameter
>
k
=
update
.
getParameters
();
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
Column
col
=
columns
[
i
];
Value
v
=
row
.
getValue
(
col
.
getColumnId
());
Parameter
p
=
k
.
get
(
i
);
p
.
setValue
(
v
);
}
for
(
int
i
=
0
;
i
<
keys
.
length
;
i
++)
{
Column
col
=
keys
[
i
];
Value
v
=
row
.
getValue
(
col
.
getColumnId
());
if
(
v
==
null
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_CONTAINS_NULL_VALUES_1
,
col
.
getSQL
());
}
Parameter
p
=
k
.
get
(
columns
.
length
+
i
);
p
.
setValue
(
v
);
}
return
update
.
update
();
}
@Override
public
String
getPlanSQL
()
{
StatementBuilder
buff
=
new
StatementBuilder
(
"REPLACE INTO "
);
buff
.
append
(
table
.
getSQL
()).
append
(
'('
);
for
(
Column
c
:
columns
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
c
.
getSQL
());
}
buff
.
append
(
')'
);
buff
.
append
(
'\n'
);
if
(
list
.
size
()
>
0
)
{
buff
.
append
(
"VALUES "
);
int
row
=
0
;
for
(
Expression
[]
expr
:
list
)
{
if
(
row
++
>
0
)
{
buff
.
append
(
", "
);
}
buff
.
append
(
'('
);
buff
.
resetCount
();
for
(
Expression
e
:
expr
)
{
buff
.
appendExceptFirst
(
", "
);
if
(
e
==
null
)
{
buff
.
append
(
"DEFAULT"
);
}
else
{
buff
.
append
(
e
.
getSQL
());
}
}
buff
.
append
(
')'
);
}
}
else
{
buff
.
append
(
query
.
getPlanSQL
());
}
return
buff
.
toString
();
}
@Override
public
void
prepare
()
{
if
(
columns
==
null
)
{
if
(
list
.
size
()
>
0
&&
list
.
get
(
0
).
length
==
0
)
{
// special case where table is used as a sequence
columns
=
new
Column
[
0
];
}
else
{
columns
=
table
.
getColumns
();
}
}
if
(
list
.
size
()
>
0
)
{
for
(
Expression
[]
expr
:
list
)
{
if
(
expr
.
length
!=
columns
.
length
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
for
(
int
i
=
0
;
i
<
expr
.
length
;
i
++)
{
Expression
e
=
expr
[
i
];
if
(
e
!=
null
)
{
expr
[
i
]
=
e
.
optimize
(
session
);
}
}
}
}
else
{
query
.
prepare
();
if
(
query
.
getColumnCount
()
!=
columns
.
length
)
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
}
if
(
keys
==
null
)
{
Index
idx
=
table
.
getPrimaryKey
();
if
(
idx
==
null
)
{
throw
DbException
.
get
(
ErrorCode
.
CONSTRAINT_NOT_FOUND_1
,
"PRIMARY KEY"
);
}
keys
=
idx
.
getColumns
();
}
// if there is no valid primary key, the statement degenerates to an INSERT
for
(
Column
key
:
keys
)
{
boolean
found
=
false
;
for
(
Column
column
:
columns
)
{
if
(
column
.
getColumnId
()
==
key
.
getColumnId
())
{
found
=
true
;
break
;
}
}
if
(!
found
)
{
return
;
}
}
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
buff
.
append
(
table
.
getSQL
()).
append
(
" SET "
);
for
(
Column
c
:
columns
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
c
.
getSQL
()).
append
(
"=?"
);
}
buff
.
append
(
" WHERE "
);
buff
.
resetCount
();
for
(
Column
c
:
keys
)
{
buff
.
appendExceptFirst
(
" AND "
);
buff
.
append
(
c
.
getSQL
()).
append
(
"=?"
);
}
String
sql
=
buff
.
toString
();
update
=
session
.
prepare
(
sql
);
}
@Override
public
boolean
isTransactional
()
{
return
true
;
}
@Override
public
ResultInterface
queryMeta
()
{
return
null
;
}
@Override
public
int
getType
()
{
return
CommandInterface
.
REPLACE
;
}
@Override
public
boolean
isCacheable
()
{
return
true
;
}
}
h2/src/test/org/h2/test/db/TestReplace.java
0 → 100644
浏览文件 @
64fcf884
package
org
.
h2
.
test
.
db
;
import
org.h2.test.TestBase
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
/**
* Test the MySQL-compatibility REPLACE command.
*
* @author Cemo
*/
public
class
TestReplace
extends
TestBase
{
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
@Override
public
void
test
()
throws
SQLException
{
deleteDb
(
"replace"
);
Connection
conn
=
getConnection
(
"replace"
);
testReplace
(
conn
);
conn
.
close
();
deleteDb
(
"replace"
);
}
private
void
testReplace
(
Connection
conn
)
throws
SQLException
{
Statement
stat
=
conn
.
createStatement
();
ResultSet
rs
;
stat
.
execute
(
"CREATE TABLE TABLE_WORD ("
+
" WORD_ID int(11) NOT NULL AUTO_INCREMENT,"
+
" WORD varchar(128) NOT NULL,"
+
" PRIMARY KEY (WORD_ID)"
+
");"
);
stat
.
execute
(
"REPLACE INTO TABLE_WORD ( WORD ) VALUES ('aaaaaaaaaa')"
);
stat
.
execute
(
"REPLACE INTO TABLE_WORD ( WORD ) VALUES ('bbbbbbbbbb')"
);
stat
.
execute
(
"REPLACE INTO TABLE_WORD ( WORD_ID, WORD ) VALUES (3, 'cccccccccc')"
);
rs
=
stat
.
executeQuery
(
"SELECT WORD FROM TABLE_WORD where WORD_ID = 1"
);
rs
.
next
();
assertEquals
(
"aaaaaaaaaa"
,
rs
.
getNString
(
1
));
stat
.
execute
(
"REPLACE INTO TABLE_WORD ( WORD_ID, WORD ) VALUES (1, 'REPLACED')"
);
rs
=
stat
.
executeQuery
(
"SELECT WORD FROM TABLE_WORD where WORD_ID = 1"
);
rs
.
next
();
assertEquals
(
"REPLACED"
,
rs
.
getNString
(
1
));
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论