Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
658332f0
提交
658332f0
authored
1月 23, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Triggers: INSTEAD OF triggers are now supported.
上级
ed7740d3
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
13 行删除
+72
-13
TriggerObject.java
h2/src/main/org/h2/schema/TriggerObject.java
+15
-6
Table.java
h2/src/main/org/h2/table/Table.java
+13
-7
TestTriggersConstraints.java
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
+44
-0
没有找到文件。
h2/src/main/org/h2/schema/TriggerObject.java
浏览文件 @
658332f0
...
...
@@ -35,6 +35,7 @@ public class TriggerObject extends SchemaObjectBase {
*/
public
static
final
int
DEFAULT_QUEUE_SIZE
=
1024
;
private
boolean
insteadOf
;
private
boolean
before
;
private
int
typeMask
;
private
boolean
rowBased
;
...
...
@@ -56,6 +57,10 @@ public class TriggerObject extends SchemaObjectBase {
this
.
before
=
before
;
}
public
void
setInsteadOf
(
boolean
insteadOf
)
{
this
.
insteadOf
=
insteadOf
;
}
private
synchronized
void
load
(
Session
session
)
throws
SQLException
{
if
(
triggerCallback
!=
null
)
{
return
;
...
...
@@ -148,14 +153,15 @@ public class TriggerObject extends SchemaObjectBase {
* @param newRow the new row
* @param beforeAction true if this method is called before the operation is
* applied
* @param rollback when the operation occurred within a rollback
* @param rollback when the operation occurred within a rollback
* @return true if no further action is required (for 'instead of' triggers)
*/
public
void
fireRow
(
Session
session
,
Row
oldRow
,
Row
newRow
,
boolean
beforeAction
,
boolean
rollback
)
throws
SQLException
{
public
boolean
fireRow
(
Session
session
,
Row
oldRow
,
Row
newRow
,
boolean
beforeAction
,
boolean
rollback
)
throws
SQLException
{
if
(!
rowBased
||
before
!=
beforeAction
)
{
return
;
return
false
;
}
if
(
rollback
&&
!
onRollback
)
{
return
;
return
false
;
}
load
(
session
);
Object
[]
oldList
;
...
...
@@ -177,7 +183,7 @@ public class TriggerObject extends SchemaObjectBase {
}
}
if
(!
fire
)
{
return
;
return
false
;
}
oldList
=
convertToObjectList
(
oldRow
);
newList
=
convertToObjectList
(
newRow
);
...
...
@@ -217,6 +223,7 @@ public class TriggerObject extends SchemaObjectBase {
session
.
setCommitOrRollbackDisabled
(
oldDisabled
);
session
.
setAutoCommit
(
old
);
}
return
insteadOf
;
}
/**
...
...
@@ -259,7 +266,9 @@ public class TriggerObject extends SchemaObjectBase {
public
String
getCreateSQLForCopy
(
Table
targetTable
,
String
quotedName
)
{
StringBuilder
buff
=
new
StringBuilder
(
"CREATE FORCE TRIGGER "
);
buff
.
append
(
quotedName
);
if
(
before
)
{
if
(
insteadOf
)
{
buff
.
append
(
" INSTEAD OF "
);
}
else
if
(
before
)
{
buff
.
append
(
" BEFORE "
);
}
else
{
buff
.
append
(
" AFTER "
);
...
...
h2/src/main/org/h2/table/Table.java
浏览文件 @
658332f0
...
...
@@ -745,13 +745,15 @@ public abstract class Table extends SchemaObjectBase {
/**
* Fire all triggers that need to be called before a row is updated.
*
* @param session the session
* @param oldRow the old data or null for an insert
* @param newRow the new data or null for a delete
* @param session the session
* @param oldRow the old data or null for an insert
* @param newRow the new data or null for a delete
* @return true if no further action is required (for 'instead of' triggers)
*/
public
void
fireBeforeRow
(
Session
session
,
Row
oldRow
,
Row
newRow
)
throws
SQLException
{
fireRow
(
session
,
oldRow
,
newRow
,
true
,
false
);
public
boolean
fireBeforeRow
(
Session
session
,
Row
oldRow
,
Row
newRow
)
throws
SQLException
{
boolean
done
=
fireRow
(
session
,
oldRow
,
newRow
,
true
,
false
);
fireConstraints
(
session
,
oldRow
,
newRow
,
true
);
return
done
;
}
private
void
fireConstraints
(
Session
session
,
Row
oldRow
,
Row
newRow
,
boolean
before
)
throws
SQLException
{
...
...
@@ -779,12 +781,16 @@ public abstract class Table extends SchemaObjectBase {
}
}
private
void
fireRow
(
Session
session
,
Row
oldRow
,
Row
newRow
,
boolean
beforeAction
,
boolean
rollback
)
throws
SQLException
{
private
boolean
fireRow
(
Session
session
,
Row
oldRow
,
Row
newRow
,
boolean
beforeAction
,
boolean
rollback
)
throws
SQLException
{
if
(
triggers
!=
null
)
{
for
(
TriggerObject
trigger
:
triggers
)
{
trigger
.
fireRow
(
session
,
oldRow
,
newRow
,
beforeAction
,
rollback
);
boolean
done
=
trigger
.
fireRow
(
session
,
oldRow
,
newRow
,
beforeAction
,
rollback
);
if
(
done
)
{
return
true
;
}
}
}
return
false
;
}
public
boolean
isGlobalTemporary
()
{
...
...
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
浏览文件 @
658332f0
...
...
@@ -37,6 +37,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
public
void
test
()
throws
SQLException
{
deleteDb
(
"trigger"
);
testViewTrigger
();
testTriggerBeforeSelect
();
testTriggerAlterTable
();
testTriggers
();
...
...
@@ -44,6 +45,49 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
deleteDb
(
"trigger"
);
}
private
void
testViewTrigger
()
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)"
);
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 \""
+
TestView
.
class
.
getName
()
+
"\""
);
if
(!
config
.
memory
)
{
conn
.
close
();
conn
=
getConnection
(
"trigger"
);
stat
=
conn
.
createStatement
();
}
stat
.
execute
(
"insert into test_view values(1)"
);
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 implementation.
*/
public
static
class
TestView
implements
Trigger
{
PreparedStatement
prepInsert
;
public
void
init
(
Connection
conn
,
String
schemaName
,
String
triggerName
,
String
tableName
,
boolean
before
,
int
type
)
throws
SQLException
{
prepInsert
=
conn
.
prepareStatement
(
"insert into test values(?)"
);
}
public
void
fire
(
Connection
conn
,
Object
[]
oldRow
,
Object
[]
newRow
)
throws
SQLException
{
prepInsert
.
setInt
(
1
,
(
Integer
)
newRow
[
0
]);
prepInsert
.
execute
();
}
}
private
void
testTriggerBeforeSelect
()
throws
SQLException
{
Connection
conn
;
Statement
stat
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论