Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b8e6dc27
提交
b8e6dc27
authored
7月 15, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix update count for REPLACE and convert test into SQL script
上级
3b3c53f9
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
47 行增加
和
83 行删除
+47
-83
Replace.java
h2/src/main/org/h2/command/dml/Replace.java
+14
-10
TestAll.java
h2/src/test/org/h2/test/TestAll.java
+0
-2
TestReplace.java
h2/src/test/org/h2/test/db/TestReplace.java
+0
-70
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
replace.sql
h2/src/test/org/h2/test/scripts/dml/replace.sql
+32
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Replace.java
浏览文件 @
b8e6dc27
...
@@ -79,13 +79,12 @@ public class Replace extends Prepared {
...
@@ -79,13 +79,12 @@ public class Replace extends Prepared {
@Override
@Override
public
int
update
()
{
public
int
update
()
{
int
count
;
int
count
=
0
;
session
.
getUser
().
checkRight
(
table
,
Right
.
INSERT
);
session
.
getUser
().
checkRight
(
table
,
Right
.
INSERT
);
session
.
getUser
().
checkRight
(
table
,
Right
.
UPDATE
);
session
.
getUser
().
checkRight
(
table
,
Right
.
UPDATE
);
setCurrentRowNumber
(
0
);
setCurrentRowNumber
(
0
);
Mode
mode
=
session
.
getDatabase
().
getMode
();
Mode
mode
=
session
.
getDatabase
().
getMode
();
if
(!
list
.
isEmpty
())
{
if
(!
list
.
isEmpty
())
{
count
=
0
;
for
(
int
x
=
0
,
size
=
list
.
size
();
x
<
size
;
x
++)
{
for
(
int
x
=
0
,
size
=
list
.
size
();
x
<
size
;
x
++)
{
setCurrentRowNumber
(
x
+
1
);
setCurrentRowNumber
(
x
+
1
);
Expression
[]
expr
=
list
.
get
(
x
);
Expression
[]
expr
=
list
.
get
(
x
);
...
@@ -104,16 +103,13 @@ public class Replace extends Prepared {
...
@@ -104,16 +103,13 @@ public class Replace extends Prepared {
}
}
}
}
}
}
replace
(
newRow
);
count
+=
replace
(
newRow
);
count
++;
}
}
}
else
{
}
else
{
ResultInterface
rows
=
query
.
query
(
0
);
ResultInterface
rows
=
query
.
query
(
0
);
count
=
0
;
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
true
);
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
true
);
table
.
lock
(
session
,
true
,
false
);
table
.
lock
(
session
,
true
,
false
);
while
(
rows
.
next
())
{
while
(
rows
.
next
())
{
count
++;
Value
[]
r
=
rows
.
currentRow
();
Value
[]
r
=
rows
.
currentRow
();
Row
newRow
=
table
.
getTemplateRow
();
Row
newRow
=
table
.
getTemplateRow
();
setCurrentRowNumber
(
count
);
setCurrentRowNumber
(
count
);
...
@@ -127,7 +123,7 @@ public class Replace extends Prepared {
...
@@ -127,7 +123,7 @@ public class Replace extends Prepared {
throw
setRow
(
ex
,
count
,
getSQL
(
r
));
throw
setRow
(
ex
,
count
,
getSQL
(
r
));
}
}
}
}
replace
(
newRow
);
count
+=
replace
(
newRow
);
}
}
rows
.
close
();
rows
.
close
();
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
false
);
table
.
fire
(
session
,
Trigger
.
UPDATE
|
Trigger
.
INSERT
,
false
);
...
@@ -135,7 +131,13 @@ public class Replace extends Prepared {
...
@@ -135,7 +131,13 @@ public class Replace extends Prepared {
return
count
;
return
count
;
}
}
private
void
replace
(
Row
row
)
{
/**
* Updates an existing row or inserts a new one.
*
* @param row row to replace
* @return 1 if row was inserted, 2 if row was updated
*/
private
int
replace
(
Row
row
)
{
int
count
=
update
(
row
);
int
count
=
update
(
row
);
if
(
count
==
0
)
{
if
(
count
==
0
)
{
try
{
try
{
...
@@ -147,6 +149,7 @@ public class Replace extends Prepared {
...
@@ -147,6 +149,7 @@ public class Replace extends Prepared {
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
row
);
session
.
log
(
table
,
UndoLogRecord
.
INSERT
,
row
);
table
.
fireAfterRow
(
session
,
null
,
row
,
false
);
table
.
fireAfterRow
(
session
,
null
,
row
,
false
);
}
}
return
1
;
}
catch
(
DbException
e
)
{
}
catch
(
DbException
e
)
{
if
(
e
.
getErrorCode
()
==
ErrorCode
.
DUPLICATE_KEY_1
)
{
if
(
e
.
getErrorCode
()
==
ErrorCode
.
DUPLICATE_KEY_1
)
{
// possibly a concurrent replace or insert
// possibly a concurrent replace or insert
...
@@ -170,9 +173,10 @@ public class Replace extends Prepared {
...
@@ -170,9 +173,10 @@ public class Replace extends Prepared {
}
}
throw
e
;
throw
e
;
}
}
}
else
if
(
count
!
=
1
)
{
}
else
if
(
count
=
=
1
)
{
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_KEY_1
,
table
.
getSQL
())
;
return
2
;
}
}
throw
DbException
.
get
(
ErrorCode
.
DUPLICATE_KEY_1
,
table
.
getSQL
());
}
}
private
int
update
(
Row
row
)
{
private
int
update
(
Row
row
)
{
...
...
h2/src/test/org/h2/test/TestAll.java
浏览文件 @
b8e6dc27
...
@@ -62,7 +62,6 @@ import org.h2.test.db.TestPowerOff;
...
@@ -62,7 +62,6 @@ import org.h2.test.db.TestPowerOff;
import
org.h2.test.db.TestQueryCache
;
import
org.h2.test.db.TestQueryCache
;
import
org.h2.test.db.TestReadOnly
;
import
org.h2.test.db.TestReadOnly
;
import
org.h2.test.db.TestRecursiveQueries
;
import
org.h2.test.db.TestRecursiveQueries
;
import
org.h2.test.db.TestReplace
;
import
org.h2.test.db.TestRights
;
import
org.h2.test.db.TestRights
;
import
org.h2.test.db.TestRowFactory
;
import
org.h2.test.db.TestRowFactory
;
import
org.h2.test.db.TestRunscript
;
import
org.h2.test.db.TestRunscript
;
...
@@ -793,7 +792,6 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
...
@@ -793,7 +792,6 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest
(
new
TestView
());
addTest
(
new
TestView
());
addTest
(
new
TestViewAlterTable
());
addTest
(
new
TestViewAlterTable
());
addTest
(
new
TestViewDropView
());
addTest
(
new
TestViewDropView
());
addTest
(
new
TestReplace
());
addTest
(
new
TestSynonymForTable
());
addTest
(
new
TestSynonymForTable
());
addTest
(
new
TestColumnNamer
());
addTest
(
new
TestColumnNamer
());
...
...
h2/src/test/org/h2/test/db/TestReplace.java
deleted
100644 → 0
浏览文件 @
3b3c53f9
/*
* 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: Cemo
*/
package
org
.
h2
.
test
.
db
;
import
org.h2.test.TestBase
;
import
org.h2.test.TestDb
;
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
TestDb
{
/**
* 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
));
}
}
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
b8e6dc27
...
@@ -128,7 +128,7 @@ public class TestScript extends TestDb {
...
@@ -128,7 +128,7 @@ public class TestScript extends TestDb {
"dropDomain"
,
"dropIndex"
,
"dropSchema"
,
"truncateTable"
})
{
"dropDomain"
,
"dropIndex"
,
"dropSchema"
,
"truncateTable"
})
{
testScript
(
"ddl/"
+
s
+
".sql"
);
testScript
(
"ddl/"
+
s
+
".sql"
);
}
}
for
(
String
s
:
new
String
[]
{
"error_reporting"
,
"insertIgnore"
,
for
(
String
s
:
new
String
[]
{
"error_reporting"
,
"insertIgnore"
,
"replace"
,
"mergeUsing"
,
"script"
,
"with"
})
{
"mergeUsing"
,
"script"
,
"with"
})
{
testScript
(
"dml/"
+
s
+
".sql"
);
testScript
(
"dml/"
+
s
+
".sql"
);
}
}
...
...
h2/src/test/org/h2/test/scripts/dml/replace.sql
0 → 100644
浏览文件 @
b8e6dc27
-- 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
--
CREATE
TABLE
TABLE_WORD
(
WORD_ID
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
WORD
varchar
(
128
)
NOT
NULL
,
PRIMARY
KEY
(
WORD_ID
)
);
>
ok
REPLACE
INTO
TABLE_WORD
(
WORD
)
VALUES
(
'aaaaaaaaaa'
);
>
update
count
:
1
REPLACE
INTO
TABLE_WORD
(
WORD
)
VALUES
(
'bbbbbbbbbb'
);
>
update
count
:
1
REPLACE
INTO
TABLE_WORD
(
WORD_ID
,
WORD
)
VALUES
(
3
,
'cccccccccc'
);
>
update
count
:
1
SELECT
WORD
FROM
TABLE_WORD
where
WORD_ID
=
1
;
>>
aaaaaaaaaa
REPLACE
INTO
TABLE_WORD
(
WORD_ID
,
WORD
)
VALUES
(
1
,
'REPLACED'
);
>
update
count
:
2
SELECT
WORD
FROM
TABLE_WORD
where
WORD_ID
=
1
;
>>
REPLACED
DROP
TABLE
TABLE_WORD
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论