Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
734f8950
提交
734f8950
authored
9月 29, 2016
作者:
Noel Grandin
提交者:
GitHub
9月 29, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #363 from dyorgio/master
Added support to define last IDENTIFIER on a Trigger.
上级
d605b46f
978f142f
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
100 行增加
和
2 行删除
+100
-2
Session.java
h2/src/main/org/h2/engine/Session.java
+9
-0
TriggerObject.java
h2/src/main/org/h2/schema/TriggerObject.java
+12
-2
TestTriggersConstraints.java
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
+79
-0
没有找到文件。
h2/src/main/org/h2/engine/Session.java
浏览文件 @
734f8950
...
@@ -78,6 +78,7 @@ public class Session extends SessionWithState {
...
@@ -78,6 +78,7 @@ public class Session extends SessionWithState {
private
int
lockTimeout
;
private
int
lockTimeout
;
private
Value
lastIdentity
=
ValueLong
.
get
(
0
);
private
Value
lastIdentity
=
ValueLong
.
get
(
0
);
private
Value
lastScopeIdentity
=
ValueLong
.
get
(
0
);
private
Value
lastScopeIdentity
=
ValueLong
.
get
(
0
);
private
Value
lastTriggerIdentity
=
null
;
private
int
firstUncommittedLog
=
Session
.
LOG_WRITTEN
;
private
int
firstUncommittedLog
=
Session
.
LOG_WRITTEN
;
private
int
firstUncommittedPos
=
Session
.
LOG_WRITTEN
;
private
int
firstUncommittedPos
=
Session
.
LOG_WRITTEN
;
private
HashMap
<
String
,
Savepoint
>
savepoints
;
private
HashMap
<
String
,
Savepoint
>
savepoints
;
...
@@ -1000,6 +1001,14 @@ public class Session extends SessionWithState {
...
@@ -1000,6 +1001,14 @@ public class Session extends SessionWithState {
return
lastScopeIdentity
;
return
lastScopeIdentity
;
}
}
public
void
setLastTriggerIdentity
(
Value
last
)
{
this
.
lastTriggerIdentity
=
last
;
}
public
Value
getLastTriggerIdentity
()
{
return
lastTriggerIdentity
;
}
/**
/**
* Called when a log entry for this session is added. The session keeps
* Called when a log entry for this session is added. The session keeps
* track of the first entry in the transaction log that is not yet
* track of the first entry in the transaction log that is not yet
...
...
h2/src/main/org/h2/schema/TriggerObject.java
浏览文件 @
734f8950
...
@@ -167,7 +167,12 @@ public class TriggerObject extends SchemaObjectBase {
...
@@ -167,7 +167,12 @@ public class TriggerObject extends SchemaObjectBase {
throw
DbException
.
get
(
ErrorCode
.
ERROR_EXECUTING_TRIGGER_3
,
e
,
getName
(),
throw
DbException
.
get
(
ErrorCode
.
ERROR_EXECUTING_TRIGGER_3
,
e
,
getName
(),
triggerClassName
!=
null
?
triggerClassName
:
"..source.."
,
e
.
toString
());
triggerClassName
!=
null
?
triggerClassName
:
"..source.."
,
e
.
toString
());
}
finally
{
}
finally
{
if
(
session
.
getLastTriggerIdentity
()
!=
null
)
{
session
.
setLastScopeIdentity
(
session
.
getLastTriggerIdentity
());
session
.
setLastTriggerIdentity
(
null
);
}
else
{
session
.
setLastScopeIdentity
(
identity
);
session
.
setLastScopeIdentity
(
identity
);
}
if
(
type
!=
Trigger
.
SELECT
)
{
if
(
type
!=
Trigger
.
SELECT
)
{
session
.
setCommitOrRollbackDisabled
(
old
);
session
.
setCommitOrRollbackDisabled
(
old
);
}
}
...
@@ -262,7 +267,12 @@ public class TriggerObject extends SchemaObjectBase {
...
@@ -262,7 +267,12 @@ public class TriggerObject extends SchemaObjectBase {
throw
DbException
.
convert
(
e
);
throw
DbException
.
convert
(
e
);
}
}
}
finally
{
}
finally
{
if
(
session
.
getLastTriggerIdentity
()
!=
null
)
{
session
.
setLastScopeIdentity
(
session
.
getLastTriggerIdentity
());
session
.
setLastTriggerIdentity
(
null
);
}
else
{
session
.
setLastScopeIdentity
(
identity
);
session
.
setLastScopeIdentity
(
identity
);
}
session
.
setCommitOrRollbackDisabled
(
oldDisabled
);
session
.
setCommitOrRollbackDisabled
(
oldDisabled
);
session
.
setAutoCommit
(
old
);
session
.
setAutoCommit
(
old
);
}
}
...
...
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
浏览文件 @
734f8950
...
@@ -15,9 +15,12 @@ import java.util.HashSet;
...
@@ -15,9 +15,12 @@ import java.util.HashSet;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.Trigger
;
import
org.h2.api.Trigger
;
import
org.h2.engine.Session
;
import
org.h2.jdbc.JdbcConnection
;
import
org.h2.test.TestBase
;
import
org.h2.test.TestBase
;
import
org.h2.tools.TriggerAdapter
;
import
org.h2.tools.TriggerAdapter
;
import
org.h2.util.Task
;
import
org.h2.util.Task
;
import
org.h2.value.ValueLong
;
/**
/**
* Tests for trigger and constraints.
* Tests for trigger and constraints.
...
@@ -44,6 +47,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
...
@@ -44,6 +47,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
testTriggerAdapter
();
testTriggerAdapter
();
testTriggerSelectEachRow
();
testTriggerSelectEachRow
();
testViewTrigger
();
testViewTrigger
();
testViewTriggerGeneratedKeys
();
testTriggerBeforeSelect
();
testTriggerBeforeSelect
();
testTriggerAlterTable
();
testTriggerAlterTable
();
testTriggerAsSource
();
testTriggerAsSource
();
...
@@ -203,6 +207,44 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
...
@@ -203,6 +207,44 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
conn
.
close
();
conn
.
close
();
}
}
private
void
testViewTriggerGeneratedKeys
()
throws
SQLException
{
Connection
conn
;
Statement
stat
;
conn
=
getConnection
(
"trigger"
);
stat
=
conn
.
createStatement
();
stat
.
execute
(
"drop table if exists test"
);
stat
.
execute
(
"create table test(id int identity)"
);
stat
.
execute
(
"create view test_view as select * from test"
);
stat
.
execute
(
"create trigger test_view_insert "
+
"instead of insert on test_view for each row call \""
+
TestViewGeneratedKeys
.
class
.
getName
()
+
"\""
);
if
(!
config
.
memory
)
{
conn
.
close
();
conn
=
getConnection
(
"trigger"
);
stat
=
conn
.
createStatement
();
}
PreparedStatement
pstat
;
pstat
=
conn
.
prepareStatement
(
"insert into test_view values()"
,
Statement
.
RETURN_GENERATED_KEYS
);
int
count
=
pstat
.
executeUpdate
();
assertEquals
(
1
,
count
);
ResultSet
gkRs
;
gkRs
=
pstat
.
getGeneratedKeys
();
assertTrue
(
gkRs
.
next
());
assertEquals
(
1
,
gkRs
.
getInt
(
1
));
assertFalse
(
gkRs
.
next
());
ResultSet
rs
;
rs
=
stat
.
executeQuery
(
"select * from test"
);
assertTrue
(
rs
.
next
());
assertFalse
(
rs
.
next
());
stat
.
execute
(
"drop view test_view"
);
stat
.
execute
(
"drop table test"
);
conn
.
close
();
}
/**
/**
* A test trigger adapter implementation.
* A test trigger adapter implementation.
*/
*/
...
@@ -287,6 +329,43 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
...
@@ -287,6 +329,43 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
}
}
public
static
class
TestViewGeneratedKeys
implements
Trigger
{
PreparedStatement
prepInsert
;
@Override
public
void
init
(
Connection
conn
,
String
schemaName
,
String
triggerName
,
String
tableName
,
boolean
before
,
int
type
)
throws
SQLException
{
prepInsert
=
conn
.
prepareStatement
(
"insert into test values()"
,
Statement
.
RETURN_GENERATED_KEYS
);
}
@Override
public
void
fire
(
Connection
conn
,
Object
[]
oldRow
,
Object
[]
newRow
)
throws
SQLException
{
if
(
newRow
!=
null
)
{
prepInsert
.
execute
();
ResultSet
rs
=
prepInsert
.
getGeneratedKeys
();
if
(
rs
.
next
())
{
JdbcConnection
jconn
=
(
JdbcConnection
)
conn
;
Session
session
=
(
Session
)
jconn
.
getSession
();
session
.
setLastTriggerIdentity
(
ValueLong
.
get
(
rs
.
getLong
(
1
)));
}
}
}
@Override
public
void
close
()
{
// ignore
}
@Override
public
void
remove
()
{
// ignore
}
}
private
void
testTriggerBeforeSelect
()
throws
SQLException
{
private
void
testTriggerBeforeSelect
()
throws
SQLException
{
Connection
conn
;
Connection
conn
;
Statement
stat
;
Statement
stat
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论