Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
c1692aa6
提交
c1692aa6
authored
2月 21, 2012
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 362: support LIMIT in UPDATE statements.
上级
177e081e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
145 行增加
和
1 行删除
+145
-1
help.csv
h2/src/docsrc/help/help.csv
+1
-1
Parser.java
h2/src/main/org/h2/command/Parser.java
+4
-0
Update.java
h2/src/main/org/h2/command/dml/Update.java
+19
-0
TestLimitUpdates.java
h2/src/test/org/h2/test/jdbc/TestLimitUpdates.java
+121
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
c1692aa6
...
@@ -59,7 +59,7 @@ INSERT INTO TEST VALUES(1, 'Hello')
...
@@ -59,7 +59,7 @@ INSERT INTO TEST VALUES(1, 'Hello')
"Commands (DML)","UPDATE","
"Commands (DML)","UPDATE","
UPDATE tableName [ [ AS ] newTableAlias ]
UPDATE tableName [ [ AS ] newTableAlias ]
SET { columnName = { DEFAULT | expression } } [,...]
SET { columnName = { DEFAULT | expression } } [,...]
[ WHERE expression ]
[ WHERE expression ]
[ LIMIT expression ]
","
","
Updates data in a table.
Updates data in a table.
","
","
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
c1692aa6
...
@@ -701,6 +701,10 @@ public class Parser {
...
@@ -701,6 +701,10 @@ public class Parser {
Expression
condition
=
readExpression
();
Expression
condition
=
readExpression
();
command
.
setCondition
(
condition
);
command
.
setCondition
(
condition
);
}
}
if
(
readIf
(
"LIMIT"
))
{
Expression
limit
=
readTerm
().
optimize
(
session
);
command
.
setLimit
(
limit
);
}
setSQL
(
command
,
"UPDATE"
,
start
);
setSQL
(
command
,
"UPDATE"
,
start
);
return
command
;
return
command
;
}
}
...
...
h2/src/main/org/h2/command/dml/Update.java
浏览文件 @
c1692aa6
...
@@ -29,6 +29,7 @@ import org.h2.util.New;
...
@@ -29,6 +29,7 @@ import org.h2.util.New;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
import
org.h2.util.StringUtils
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.ValueNull
;
/**
/**
* This class represents the statement
* This class represents the statement
...
@@ -38,6 +39,10 @@ public class Update extends Prepared {
...
@@ -38,6 +39,10 @@ public class Update extends Prepared {
private
Expression
condition
;
private
Expression
condition
;
private
TableFilter
tableFilter
;
private
TableFilter
tableFilter
;
/** The limit expression as specified in the LIMIT clause. */
private
Expression
limitExpr
;
private
ArrayList
<
Column
>
columns
=
New
.
arrayList
();
private
ArrayList
<
Column
>
columns
=
New
.
arrayList
();
private
HashMap
<
Column
,
Expression
>
expressionMap
=
New
.
hashMap
();
private
HashMap
<
Column
,
Expression
>
expressionMap
=
New
.
hashMap
();
...
@@ -86,8 +91,18 @@ public class Update extends Prepared {
...
@@ -86,8 +91,18 @@ public class Update extends Prepared {
setCurrentRowNumber
(
0
);
setCurrentRowNumber
(
0
);
int
count
=
0
;
int
count
=
0
;
Column
[]
columns
=
table
.
getColumns
();
Column
[]
columns
=
table
.
getColumns
();
int
limitRows
=
-
1
;
if
(
limitExpr
!=
null
)
{
Value
v
=
limitExpr
.
getValue
(
session
);
if
(
v
!=
ValueNull
.
INSTANCE
)
{
limitRows
=
v
.
getInt
();
}
}
while
(
tableFilter
.
next
())
{
while
(
tableFilter
.
next
())
{
setCurrentRowNumber
(
count
+
1
);
setCurrentRowNumber
(
count
+
1
);
if
(
limitRows
>=
0
&&
count
>=
limitRows
)
{
break
;
}
if
(
condition
==
null
||
Boolean
.
TRUE
.
equals
(
condition
.
getBooleanValue
(
session
)))
{
if
(
condition
==
null
||
Boolean
.
TRUE
.
equals
(
condition
.
getBooleanValue
(
session
)))
{
Row
oldRow
=
tableFilter
.
get
();
Row
oldRow
=
tableFilter
.
get
();
Row
newRow
=
table
.
getTemplateRow
();
Row
newRow
=
table
.
getTemplateRow
();
...
@@ -185,6 +200,10 @@ public class Update extends Prepared {
...
@@ -185,6 +200,10 @@ public class Update extends Prepared {
return
CommandInterface
.
UPDATE
;
return
CommandInterface
.
UPDATE
;
}
}
public
void
setLimit
(
Expression
limit
)
{
this
.
limitExpr
=
limit
;
}
public
boolean
isCacheable
()
{
public
boolean
isCacheable
()
{
return
true
;
return
true
;
}
}
...
...
h2/src/test/org/h2/test/jdbc/TestLimitUpdates.java
0 → 100644
浏览文件 @
c1692aa6
/*
* Copyright 2004-2011 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
.
test
.
jdbc
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
org.h2.test.TestBase
;
/**
* Test for limit updates.
*/
public
class
TestLimitUpdates
extends
TestBase
{
private
static
final
String
DATABASE_NAME
=
"limitUpdates"
;
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
public
void
test
()
throws
SQLException
{
testLimitUpdates
();
deleteDb
(
DATABASE_NAME
);
}
private
void
testLimitUpdates
()
throws
SQLException
{
deleteDb
(
DATABASE_NAME
);
Connection
conn
=
null
;
PreparedStatement
prep
=
null
;
try
{
conn
=
getConnection
(
DATABASE_NAME
);
prep
=
conn
.
prepareStatement
(
"CREATE TABLE TEST(KEY_ID INT PRIMARY KEY, VALUE_ID INT)"
);
prep
.
executeUpdate
();
prep
.
close
();
prep
=
conn
.
prepareStatement
(
"INSERT INTO TEST VALUES(?, ?)"
);
int
numRows
=
10
;
for
(
int
i
=
0
;
i
<
numRows
;
++
i
)
{
prep
.
setInt
(
1
,
i
);
prep
.
setInt
(
2
,
0
);
prep
.
execute
();
}
assertEquals
(
numRows
,
countWhere
(
conn
,
0
));
// update all elements than available
prep
.
close
();
prep
=
conn
.
prepareStatement
(
"UPDATE TEST SET VALUE_ID = ?"
);
prep
.
setInt
(
1
,
1
);
prep
.
execute
();
assertEquals
(
numRows
,
countWhere
(
conn
,
1
));
// update less elements than available
updateLimit
(
conn
,
2
,
numRows
/
2
);
assertEquals
(
numRows
/
2
,
countWhere
(
conn
,
2
));
// update more elements than available
updateLimit
(
conn
,
3
,
numRows
*
2
);
assertEquals
(
numRows
,
countWhere
(
conn
,
3
));
// update no elements
updateLimit
(
conn
,
4
,
0
);
assertEquals
(
0
,
countWhere
(
conn
,
4
));
}
finally
{
if
(
prep
!=
null
)
{
prep
.
close
();
}
if
(
conn
!=
null
)
{
conn
.
close
();
}
}
}
private
static
int
countWhere
(
final
Connection
conn
,
final
int
where
)
throws
SQLException
{
PreparedStatement
prep
=
null
;
ResultSet
rs
=
null
;
try
{
prep
=
conn
.
prepareStatement
(
"SELECT COUNT(*) FROM TEST WHERE VALUE_ID = ?"
);
prep
.
setInt
(
1
,
where
);
rs
=
prep
.
executeQuery
();
rs
.
next
();
return
rs
.
getInt
(
1
);
}
finally
{
if
(
rs
!=
null
)
{
rs
.
close
();
}
if
(
prep
!=
null
)
{
prep
.
close
();
}
}
}
private
static
void
updateLimit
(
final
Connection
conn
,
final
int
value
,
final
int
limit
)
throws
SQLException
{
PreparedStatement
prep
=
null
;
try
{
prep
=
conn
.
prepareStatement
(
"UPDATE TEST SET VALUE_ID = ? LIMIT ?"
);
prep
.
setInt
(
1
,
value
);
prep
.
setInt
(
2
,
limit
);
prep
.
execute
();
}
finally
{
if
(
prep
!=
null
)
{
prep
.
close
();
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论