Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
076843af
提交
076843af
authored
2月 18, 2011
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New sample application that shows how to use "instead of" triggers to support updatable views.
上级
5347d452
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
94 行增加
和
1 行删除
+94
-1
help.csv
h2/src/docsrc/help/help.csv
+3
-0
changelog.html
h2/src/docsrc/html/changelog.html
+2
-1
UpdatableView.java
h2/src/test/org/h2/samples/UpdatableView.java
+87
-0
TestSampleApps.java
h2/src/test/org/h2/test/unit/TestSampleApps.java
+2
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
076843af
...
...
@@ -605,6 +605,7 @@ Exceptions that occur within such triggers are ignored.
INSTEAD OF triggers are implicitly row based and behave like BEFORE triggers.
Only the first such trigger is called. Such triggers on views are supported.
They can be used to make views updatable.
The MERGE statement will call both INSERT and UPDATE triggers.
Not supported are SELECT triggers with the option FOR EACH ROW,
...
...
@@ -645,6 +646,8 @@ dependent views will not need to be recreated. If dependent views will become
invalid as a result of the change an error will be generated, but this error
can be ignored if the FORCE clause is also used.
Views are not updatable except when using 'instead of' triggers.
Admin rights are required to execute this command.
This command commits an open transaction.
","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
076843af
...
...
@@ -18,7 +18,8 @@ Change Log
<h1>
Change Log
</h1>
<h2>
Next Version (unreleased)
</h2>
<ul><li>
H2 Console: opening a MS SQL Server database is now faster.
<ul><li>
New sample application that shows how to use "instead of" triggers to support updatable views.
</li><li>
H2 Console: opening a MS SQL Server database is now faster.
Also, listing the database meta data sometimes resulted in an exception.
</li><li>
Linked tables / MS SQL Server: if the tables were created in mixed case in MS SQL Server,
then the identifiers in the linked tables were case sensitive (in H2).
...
...
h2/src/test/org/h2/samples/UpdatableView.java
0 → 100644
浏览文件 @
076843af
/*
* 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
.
samples
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
org.h2.tools.TriggerAdapter
;
/**
* This sample application shows how to use triggers to create updatable views.
*/
public
class
UpdatableView
extends
TriggerAdapter
{
private
PreparedStatement
prepDelete
,
prepInsert
;
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public
static
void
main
(
String
...
args
)
throws
Exception
{
Class
.
forName
(
"org.h2.Driver"
);
Connection
conn
=
DriverManager
.
getConnection
(
"jdbc:h2:mem:"
);
Statement
stat
;
stat
=
conn
.
createStatement
();
// create the table and the view
stat
.
execute
(
"create table test(id int primary key, name varchar)"
);
stat
.
execute
(
"create view test_view as select * from test"
);
// create the trigger that is called whenever
// the data in the view is modified
stat
.
execute
(
"create trigger t_test_view instead of "
+
"insert, update, delete on test_view for each row "
+
"call \""
+
UpdatableView
.
class
.
getName
()
+
"\""
);
// test a few operations
stat
.
execute
(
"insert into test_view values(1, 'Hello'), (2, 'World')"
);
stat
.
execute
(
"update test_view set name = 'Hallo' where id = 1"
);
stat
.
execute
(
"delete from test_view where id = 2"
);
// print the contents of the table and the view
System
.
out
.
println
(
"table test:"
);
ResultSet
rs
;
rs
=
stat
.
executeQuery
(
"select * from test"
);
while
(
rs
.
next
())
{
System
.
out
.
println
(
rs
.
getInt
(
1
)
+
" "
+
rs
.
getString
(
2
));
}
System
.
out
.
println
();
System
.
out
.
println
(
"test_view:"
);
rs
=
stat
.
executeQuery
(
"select * from test_view"
);
while
(
rs
.
next
())
{
System
.
out
.
println
(
rs
.
getInt
(
1
)
+
" "
+
rs
.
getString
(
2
));
}
conn
.
close
();
}
public
void
init
(
Connection
conn
,
String
schemaName
,
String
triggerName
,
String
tableName
,
boolean
before
,
int
type
)
throws
SQLException
{
prepDelete
=
conn
.
prepareStatement
(
"delete from test where id = ?"
);
prepInsert
=
conn
.
prepareStatement
(
"insert into test values(?, ?)"
);
super
.
init
(
conn
,
schemaName
,
triggerName
,
tableName
,
before
,
type
);
}
public
void
fire
(
Connection
conn
,
ResultSet
oldRow
,
ResultSet
newRow
)
throws
SQLException
{
if
(
oldRow
!=
null
&&
oldRow
.
next
())
{
prepDelete
.
setInt
(
1
,
oldRow
.
getInt
(
1
));
prepDelete
.
execute
();
}
if
(
newRow
!=
null
&&
newRow
.
next
())
{
prepInsert
.
setInt
(
1
,
newRow
.
getInt
(
1
));
prepInsert
.
setString
(
2
,
newRow
.
getString
(
2
));
prepInsert
.
execute
();
}
}
}
h2/src/test/org/h2/test/unit/TestSampleApps.java
浏览文件 @
076843af
...
...
@@ -76,6 +76,8 @@ public class TestSampleApps extends TestBase {
// process)
testApp
(
"The sum is 20.00"
,
org
.
h2
.
samples
.
TriggerSample
.
class
);
testApp
(
"Hello: 1\nWorld: 2"
,
org
.
h2
.
samples
.
TriggerPassData
.
class
);
testApp
(
"table test:\n1 Hallo\n\ntest_view:\n1 Hallo"
,
org
.
h2
.
samples
.
UpdatableView
.
class
);
testApp
(
"adding test data...\n"
+
"defrag to reduce random access...\n"
+
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论