Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
55e2d0c4
提交
55e2d0c4
authored
3月 07, 2011
作者:
noelgrandin@gmail.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improved MS SQL Server and MySQL compatibility: support DELETE with TOP or LIMIT.
上级
fe9b5e70
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
88 行增加
和
5 行删除
+88
-5
help.csv
h2/src/docsrc/help/help.csv
+2
-1
changelog.html
h2/src/docsrc/html/changelog.html
+2
-1
roadmap.html
h2/src/docsrc/html/roadmap.html
+0
-1
Parser.java
h2/src/main/org/h2/command/Parser.java
+8
-0
Delete.java
h2/src/main/org/h2/command/dml/Delete.java
+33
-2
TestCases.java
h2/src/test/org/h2/test/db/TestCases.java
+43
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
55e2d0c4
...
@@ -66,9 +66,10 @@ UPDATE PERSON P SET NAME=(SELECT A.NAME FROM ADDRESS A WHERE A.ID=P.ID);
...
@@ -66,9 +66,10 @@ UPDATE PERSON P SET NAME=(SELECT A.NAME FROM ADDRESS A WHERE A.ID=P.ID);
"
"
"Commands (DML)","DELETE","
"Commands (DML)","DELETE","
DELETE
FROM tableName [ WHERE expression
]
DELETE
[ TOP term ] FROM tableName [ WHERE expression ] [ LIMIT term
]
","
","
Deletes rows form a table.
Deletes rows form a table.
If TOP or LIMIT is specified, at most the specified number of rows are deleted.
","
","
DELETE FROM TEST WHERE ID=2
DELETE FROM TEST WHERE ID=2
"
"
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
55e2d0c4
...
@@ -19,7 +19,8 @@ Change Log
...
@@ -19,7 +19,8 @@ Change Log
<h2>
Next Version (unreleased)
</h2>
<h2>
Next Version (unreleased)
</h2>
<ul>
<ul>
<li>
Support for COSH, SINH, and TANH functions.
<li>
Improved MS SQL Server and MySQL compatibility: support DELETE with TOP or LIMIT.
</li><li>
Support for COSH, SINH, and TANH functions.
</li><li>
Support for the % operator (modulo) thanks to Noel Grandin.
</li><li>
Support for the % operator (modulo) thanks to Noel Grandin.
</li><li>
Issue 288: Some right outer join queries failed to produce the correct result or
</li><li>
Issue 288: Some right outer join queries failed to produce the correct result or
threw exceptions such as "column x must be in the group by list".
threw exceptions such as "column x must be in the group by list".
...
...
h2/src/docsrc/html/roadmap.html
浏览文件 @
55e2d0c4
...
@@ -302,7 +302,6 @@ See also <a href="build.html#providing_patches">Providing Patches</a>.
...
@@ -302,7 +302,6 @@ See also <a href="build.html#providing_patches">Providing Patches</a>.
</li><li>
Sequences: CURRVAL should be session specific. Compatibility with PostgreSQL.
</li><li>
Sequences: CURRVAL should be session specific. Compatibility with PostgreSQL.
</li><li>
Auto-server: add option to define the IP address range or list.
</li><li>
Auto-server: add option to define the IP address range or list.
</li><li>
Index creation using deterministic functions.
</li><li>
Index creation using deterministic functions.
</li><li>
Support DELETE with TOP or LIMIT. See also: http://dev.mysql.com/doc/refman/5.1/de/delete.html
</li><li>
ANALYZE: for unique indexes that allow null, count the number of null.
</li><li>
ANALYZE: for unique indexes that allow null, count the number of null.
</li><li>
AUTO_SERVER: support changing IP addresses (disable a network while the database is open).
</li><li>
AUTO_SERVER: support changing IP addresses (disable a network while the database is open).
</li><li>
Avoid using java.util.Calendar internally because it's slow, complicated, and buggy.
</li><li>
Avoid using java.util.Calendar internally because it's slow, complicated, and buggy.
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
55e2d0c4
...
@@ -710,6 +710,10 @@ public class Parser {
...
@@ -710,6 +710,10 @@ public class Parser {
private
Delete
parseDelete
()
{
private
Delete
parseDelete
()
{
Delete
command
=
new
Delete
(
session
);
Delete
command
=
new
Delete
(
session
);
Expression
limit
=
null
;
if
(
readIf
(
"TOP"
))
{
limit
=
readTerm
().
optimize
(
session
);
}
currentPrepared
=
command
;
currentPrepared
=
command
;
int
start
=
lastParseIndex
;
int
start
=
lastParseIndex
;
readIf
(
"FROM"
);
readIf
(
"FROM"
);
...
@@ -719,6 +723,10 @@ public class Parser {
...
@@ -719,6 +723,10 @@ public class Parser {
Expression
condition
=
readExpression
();
Expression
condition
=
readExpression
();
command
.
setCondition
(
condition
);
command
.
setCondition
(
condition
);
}
}
if
(
readIf
(
"LIMIT"
)
&&
limit
==
null
)
{
limit
=
readTerm
().
optimize
(
session
);
}
command
.
setLimit
(
limit
);
setSQL
(
command
,
"DELETE"
,
start
);
setSQL
(
command
,
"DELETE"
,
start
);
return
command
;
return
command
;
}
}
...
...
h2/src/main/org/h2/command/dml/Delete.java
浏览文件 @
55e2d0c4
...
@@ -30,6 +30,11 @@ public class Delete extends Prepared {
...
@@ -30,6 +30,11 @@ public class Delete extends Prepared {
private
Expression
condition
;
private
Expression
condition
;
private
TableFilter
tableFilter
;
private
TableFilter
tableFilter
;
/**
* The limit expression as specified in the LIMIT or TOP clause.
*/
private
Expression
limitExpr
;
public
Delete
(
Session
session
)
{
public
Delete
(
Session
session
)
{
super
(
session
);
super
(
session
);
}
}
...
@@ -50,6 +55,10 @@ public class Delete extends Prepared {
...
@@ -50,6 +55,10 @@ public class Delete extends Prepared {
table
.
fire
(
session
,
Trigger
.
DELETE
,
true
);
table
.
fire
(
session
,
Trigger
.
DELETE
,
true
);
table
.
lock
(
session
,
true
,
false
);
table
.
lock
(
session
,
true
,
false
);
RowList
rows
=
new
RowList
(
session
);
RowList
rows
=
new
RowList
(
session
);
int
limitRows
=
-
1
;
if
(
limitExpr
!=
null
)
{
limitRows
=
limitExpr
.
getValue
(
session
).
getInt
();
}
try
{
try
{
setCurrentRowNumber
(
0
);
setCurrentRowNumber
(
0
);
while
(
tableFilter
.
next
())
{
while
(
tableFilter
.
next
())
{
...
@@ -66,22 +75,36 @@ public class Delete extends Prepared {
...
@@ -66,22 +75,36 @@ public class Delete extends Prepared {
}
}
}
}
int
rowScanCount
=
0
;
int
rowScanCount
=
0
;
int
rowsDeleted
=
0
;
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
if
((++
rowScanCount
&
127
)
==
0
)
{
if
((++
rowScanCount
&
127
)
==
0
)
{
checkCanceled
();
checkCanceled
();
}
}
Row
row
=
rows
.
next
();
Row
row
=
rows
.
next
();
table
.
removeRow
(
session
,
row
);
table
.
removeRow
(
session
,
row
);
rowsDeleted
++;
session
.
log
(
table
,
UndoLogRecord
.
DELETE
,
row
);
session
.
log
(
table
,
UndoLogRecord
.
DELETE
,
row
);
if
(
limitRows
!=
-
1
)
{
if
(
rowsDeleted
==
limitRows
)
{
break
;
}
}
}
}
if
(
table
.
fireRow
())
{
if
(
table
.
fireRow
())
{
int
count
=
0
;
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
for
(
rows
.
reset
();
rows
.
hasNext
();)
{
Row
row
=
rows
.
next
();
Row
row
=
rows
.
next
();
table
.
fireAfterRow
(
session
,
row
,
null
,
false
);
table
.
fireAfterRow
(
session
,
row
,
null
,
false
);
count
++;
if
(
limitRows
!=
-
1
)
{
if
(
count
==
limitRows
)
{
break
;
}
}
}
}
}
}
table
.
fire
(
session
,
Trigger
.
DELETE
,
false
);
table
.
fire
(
session
,
Trigger
.
DELETE
,
false
);
return
rows
.
size
()
;
return
rows
Deleted
;
}
finally
{
}
finally
{
rows
.
close
();
rows
.
close
();
}
}
...
@@ -89,10 +112,14 @@ public class Delete extends Prepared {
...
@@ -89,10 +112,14 @@ public class Delete extends Prepared {
public
String
getPlanSQL
()
{
public
String
getPlanSQL
()
{
StringBuilder
buff
=
new
StringBuilder
();
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
"DELETE FROM "
).
append
(
tableFilter
.
getPlanSQL
(
false
));
buff
.
append
(
"DELETE "
);
buff
.
append
(
"FROM "
).
append
(
tableFilter
.
getPlanSQL
(
false
));
if
(
condition
!=
null
)
{
if
(
condition
!=
null
)
{
buff
.
append
(
"\nWHERE "
).
append
(
StringUtils
.
unEnclose
(
condition
.
getSQL
()));
buff
.
append
(
"\nWHERE "
).
append
(
StringUtils
.
unEnclose
(
condition
.
getSQL
()));
}
}
if
(
limitExpr
!=
null
)
{
buff
.
append
(
"\nLIMIT ("
).
append
(
StringUtils
.
unEnclose
(
limitExpr
.
getSQL
())).
append
(
")"
);
}
return
buff
.
toString
();
return
buff
.
toString
();
}
}
...
@@ -119,4 +146,8 @@ public class Delete extends Prepared {
...
@@ -119,4 +146,8 @@ public class Delete extends Prepared {
return
CommandInterface
.
DELETE
;
return
CommandInterface
.
DELETE
;
}
}
public
void
setLimit
(
Expression
limit
)
{
this
.
limitExpr
=
limit
;
}
}
}
h2/src/test/org/h2/test/db/TestCases.java
浏览文件 @
55e2d0c4
...
@@ -36,6 +36,7 @@ public class TestCases extends TestBase {
...
@@ -36,6 +36,7 @@ public class TestCases extends TestBase {
}
}
public
void
test
()
throws
Exception
{
public
void
test
()
throws
Exception
{
testDeleteTop
();
testUnicode
();
testUnicode
();
testOuterJoin
();
testOuterJoin
();
testCommentOnColumnWithSchemaEqualDatabase
();
testCommentOnColumnWithSchemaEqualDatabase
();
...
@@ -1175,4 +1176,46 @@ public class TestCases extends TestBase {
...
@@ -1175,4 +1176,46 @@ public class TestCases extends TestBase {
}
}
}
}
private
void
testDeleteTop
()
throws
SQLException
{
deleteDb
(
"cases"
);
Connection
conn
=
getConnection
(
"cases"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"CREATE TABLE TEST(id int) AS SELECT x FROM system_range(1, 100)"
);
stat
.
execute
(
"DELETE TOP 10 FROM TEST"
);
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT COUNT(*) FROM TEST"
);
assertTrue
(
rs
.
next
());
assertEquals
(
90
,
rs
.
getInt
(
1
));
stat
.
execute
(
"DELETE FROM TEST LIMIT ((SELECT COUNT(*) FROM TEST) / 10)"
);
rs
=
stat
.
executeQuery
(
"SELECT COUNT(*) FROM TEST"
);
assertTrue
(
rs
.
next
());
assertEquals
(
81
,
rs
.
getInt
(
1
));
rs
=
stat
.
executeQuery
(
"EXPLAIN DELETE FROM TEST LIMIT ((SELECT COUNT(*) FROM TEST) / 10)"
);
rs
.
next
();
assertEquals
(
"DELETE FROM PUBLIC.TEST\n"
+
" /* PUBLIC.TEST.tableScan */\n"
+
"LIMIT ((SELECT\n"
+
" COUNT(*)\n"
+
"FROM PUBLIC.TEST\n"
+
" /* PUBLIC.TEST.tableScan */\n"
+
"/* direct lookup */) / 10)"
,
rs
.
getString
(
1
));
PreparedStatement
prep
;
prep
=
conn
.
prepareStatement
(
"SELECT * FROM TEST LIMIT ?"
);
prep
.
setInt
(
1
,
10
);
prep
.
execute
();
prep
=
conn
.
prepareStatement
(
"DELETE FROM TEST LIMIT ?"
);
prep
.
setInt
(
1
,
10
);
prep
.
execute
();
rs
=
stat
.
executeQuery
(
"SELECT COUNT(*) FROM TEST"
);
assertTrue
(
rs
.
next
());
assertEquals
(
71
,
rs
.
getInt
(
1
));
conn
.
close
();
}
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论