Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b02f651e
提交
b02f651e
authored
1月 27, 2015
作者:
noelgrandin@gmail.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow declaring triggers as source code (like functions). Patch by Sylvain CUAZ.
上级
53c4629a
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
104 行增加
和
14 行删除
+104
-14
help.csv
h2/src/docsrc/help/help.csv
+6
-2
changelog.html
h2/src/docsrc/html/changelog.html
+1
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+6
-2
CreateTrigger.java
h2/src/main/org/h2/command/ddl/CreateTrigger.java
+9
-1
TriggerObject.java
h2/src/main/org/h2/schema/TriggerObject.java
+57
-5
TestTriggersConstraints.java
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
+25
-4
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
b02f651e
...
...
@@ -639,7 +639,7 @@ CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))
"Commands (DDL)","CREATE TRIGGER","
CREATE TRIGGER [ IF NOT EXISTS ] newTriggerName { BEFORE | AFTER | INSTEAD OF }
{ INSERT | UPDATE | DELETE | SELECT | ROLLBACK } [,...] ON tableName [ FOR EACH ROW ]
[ QUEUE int ] [ NOWAIT ]
CALL triggeredClassName
[ QUEUE int ] [ NOWAIT ]
{ CALL triggeredClassName | AS sourceCodeString }
","
Creates a new trigger.
The trigger class must be public and implement ""org.h2.api.Trigger"".
...
...
@@ -647,6 +647,9 @@ Inner classes are not supported.
The class must be available in the classpath of the database engine
(when using the server mode, it must be in the classpath of the server).
The sourceCodeString must define a single method with no parameters that returns ""org.h2.api.Trigger"".
See CREATE ALIAS for requirements regarding the compilation.
BEFORE triggers are called after data conversion is made, default values are set,
null and length constraint checks have been made;
but before other constraints have been checked.
...
...
@@ -682,7 +685,8 @@ The schema name does not need to be specified when creating the trigger.
This command commits an open transaction in this connection.
","
CREATE TRIGGER TRIG_INS BEFORE INSERT ON TEST FOR EACH ROW CALL ""MyTrigger""
CREATE TRIGGER TRIG_INS BEFORE INSERT ON TEST FOR EACH ROW CALL ""MyTrigger"";
CREATE TRIGGER TRIG_SRC BEFORE INSERT ON TEST AS $$org.h2.api.Trigger create(){ return new MyTrigger(""constructorParam""); } $$;
"
"Commands (DDL)","CREATE USER","
CREATE USER [ IF NOT EXISTS ] newUserName
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
b02f651e
...
...
@@ -50,6 +50,7 @@ Change Log
the logic that cleared the flag could never be reached, resulting in performance degradation.
Reported by Alexander Nesterov.
</li><li>
Issue 552: Implement BIT_AND and BIT_OR aggregate functions.
</li><li>
Allow declaring triggers as source code (like functions). Patch by Sylvain CUAZ.
</li></ul>
<h2>
Version 1.4.184 Beta (2014-12-19)
</h2>
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
b02f651e
...
...
@@ -4544,8 +4544,12 @@ public class Parser {
command
.
setQueueSize
(
readPositiveInt
());
}
command
.
setNoWait
(
readIf
(
"NOWAIT"
));
read
(
"CALL"
);
command
.
setTriggerClassName
(
readUniqueIdentifier
());
if
(
readIf
(
"AS"
))
{
command
.
setTriggerSource
(
readString
());
}
else
{
read
(
"CALL"
);
command
.
setTriggerClassName
(
readUniqueIdentifier
());
}
return
command
;
}
...
...
h2/src/main/org/h2/command/ddl/CreateTrigger.java
浏览文件 @
b02f651e
...
...
@@ -32,6 +32,7 @@ public class CreateTrigger extends SchemaCommand {
private
boolean
noWait
;
private
String
tableName
;
private
String
triggerClassName
;
private
String
triggerSource
;
private
boolean
force
;
private
boolean
onRollback
;
...
...
@@ -51,6 +52,10 @@ public class CreateTrigger extends SchemaCommand {
this
.
triggerClassName
=
triggerClassName
;
}
public
void
setTriggerSource
(
String
triggerSource
)
{
this
.
triggerSource
=
triggerSource
;
}
public
void
setTypeMask
(
int
typeMask
)
{
this
.
typeMask
=
typeMask
;
}
...
...
@@ -106,7 +111,10 @@ public class CreateTrigger extends SchemaCommand {
trigger
.
setRowBased
(
rowBased
);
trigger
.
setTypeMask
(
typeMask
);
trigger
.
setOnRollback
(
onRollback
);
trigger
.
setTriggerClassName
(
triggerClassName
,
force
);
if
(
this
.
triggerClassName
!=
null
)
trigger
.
setTriggerClassName
(
triggerClassName
,
force
);
else
trigger
.
setTriggerSource
(
triggerSource
,
force
);
db
.
addSchemaObject
(
session
,
trigger
);
table
.
addTrigger
(
trigger
);
return
0
;
...
...
h2/src/main/org/h2/schema/TriggerObject.java
浏览文件 @
b02f651e
...
...
@@ -5,12 +5,14 @@
*/
package
org
.
h2
.
schema
;
import
java.lang.reflect.Method
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.Trigger
;
import
org.h2.command.Parser
;
import
org.h2.engine.Constants
;
import
org.h2.engine.DbObject
;
import
org.h2.engine.Session
;
import
org.h2.message.DbException
;
...
...
@@ -18,7 +20,9 @@ import org.h2.message.Trace;
import
org.h2.result.Row
;
import
org.h2.table.Table
;
import
org.h2.util.JdbcUtils
;
import
org.h2.util.SourceCompiler
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
...
...
@@ -43,6 +47,7 @@ public class TriggerObject extends SchemaObjectBase {
private
boolean
noWait
;
private
Table
table
;
private
String
triggerClassName
;
private
String
triggerSource
;
private
Trigger
triggerCallback
;
public
TriggerObject
(
Schema
schema
,
int
id
,
String
name
,
Table
table
)
{
...
...
@@ -66,7 +71,12 @@ public class TriggerObject extends SchemaObjectBase {
try
{
Session
sysSession
=
database
.
getSystemSession
();
Connection
c2
=
sysSession
.
createConnection
(
false
);
Object
obj
=
JdbcUtils
.
loadUserClass
(
triggerClassName
).
newInstance
();
final
Object
obj
;
if
(
triggerClassName
!=
null
)
{
obj
=
JdbcUtils
.
loadUserClass
(
triggerClassName
).
newInstance
();
}
else
{
obj
=
loadFromSource
();
}
triggerCallback
=
(
Trigger
)
obj
;
triggerCallback
.
init
(
c2
,
getSchema
().
getName
(),
getName
(),
table
.
getName
(),
before
,
typeMask
);
...
...
@@ -74,7 +84,25 @@ public class TriggerObject extends SchemaObjectBase {
// try again later
triggerCallback
=
null
;
throw
DbException
.
get
(
ErrorCode
.
ERROR_CREATING_TRIGGER_OBJECT_3
,
e
,
getName
(),
triggerClassName
,
e
.
toString
());
triggerClassName
!=
null
?
triggerClassName
:
"..source.."
,
e
.
toString
());
}
}
private
Trigger
loadFromSource
()
{
final
SourceCompiler
compiler
=
database
.
getCompiler
();
synchronized
(
compiler
)
{
final
String
fullClassName
=
Constants
.
USER_PACKAGE
+
".trigger."
+
getName
();
compiler
.
setSource
(
fullClassName
,
triggerSource
);
try
{
final
Method
m
=
compiler
.
getMethod
(
fullClassName
);
if
(
m
.
getParameterTypes
().
length
>
0
)
throw
new
IllegalStateException
(
"No parameters are allowed for a trigger"
);
return
(
Trigger
)
m
.
invoke
(
null
);
}
catch
(
DbException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
DbException
.
get
(
ErrorCode
.
SYNTAX_ERROR_1
,
e
,
triggerSource
);
}
}
}
...
...
@@ -86,7 +114,23 @@ public class TriggerObject extends SchemaObjectBase {
* should be ignored
*/
public
void
setTriggerClassName
(
String
triggerClassName
,
boolean
force
)
{
this
.
setTriggerAction
(
triggerClassName
,
null
,
force
);
}
/**
* Set the trigger source code and compile it if possible.
*
* @param source the source code of a method returning a {@link Trigger}
* @param force whether exceptions (due to syntax error)
* should be ignored
*/
public
void
setTriggerSource
(
String
source
,
boolean
force
)
{
this
.
setTriggerAction
(
null
,
source
,
force
);
}
private
void
setTriggerAction
(
String
triggerClassName
,
String
source
,
boolean
force
)
{
this
.
triggerClassName
=
triggerClassName
;
this
.
triggerSource
=
source
;
try
{
load
();
}
catch
(
DbException
e
)
{
...
...
@@ -118,9 +162,9 @@ public class TriggerObject extends SchemaObjectBase {
Value
identity
=
session
.
getLastScopeIdentity
();
try
{
triggerCallback
.
fire
(
c2
,
null
,
null
);
}
catch
(
Throwable
e
)
{
}
catch
(
Throwable
e
)
{
throw
DbException
.
get
(
ErrorCode
.
ERROR_EXECUTING_TRIGGER_3
,
e
,
getName
(),
triggerClassName
,
e
.
toString
());
triggerClassName
!=
null
?
triggerClassName
:
"..source.."
,
e
.
toString
());
}
finally
{
session
.
setLastScopeIdentity
(
identity
);
if
(
type
!=
Trigger
.
SELECT
)
{
...
...
@@ -283,7 +327,10 @@ public class TriggerObject extends SchemaObjectBase {
}
else
{
buff
.
append
(
" QUEUE "
).
append
(
queueSize
);
}
buff
.
append
(
" CALL "
).
append
(
Parser
.
quoteIdentifier
(
triggerClassName
));
if
(
triggerClassName
!=
null
)
buff
.
append
(
" CALL "
).
append
(
Parser
.
quoteIdentifier
(
triggerClassName
));
else
buff
.
append
(
" AS "
).
append
(
StringUtils
.
quoteStringSQL
(
triggerSource
));
return
buff
.
toString
();
}
...
...
@@ -335,6 +382,7 @@ public class TriggerObject extends SchemaObjectBase {
}
table
=
null
;
triggerClassName
=
null
;
triggerSource
=
null
;
triggerCallback
=
null
;
invalidate
();
}
...
...
@@ -371,6 +419,10 @@ public class TriggerObject extends SchemaObjectBase {
return
triggerClassName
;
}
public
String
getTriggerSource
()
{
return
triggerSource
;
}
/**
* Close the trigger.
*/
...
...
h2/src/test/org/h2/test/db/TestTriggersConstraints.java
浏览文件 @
b02f651e
...
...
@@ -46,6 +46,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
testViewTrigger
();
testTriggerBeforeSelect
();
testTriggerAlterTable
();
testTriggerAsSource
();
testTriggers
();
testConstraints
();
testCheckConstraintErrorMessage
();
...
...
@@ -386,19 +387,39 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
}
private
void
testTriggerAlterTable
()
throws
SQLException
{
deleteDb
(
"trigger"
);
testTrigger
(
false
);
}
private
void
testTriggerAsSource
()
throws
SQLException
{
deleteDb
(
"trigger"
);
testTrigger
(
true
);
}
private
void
testTrigger
(
final
boolean
asSource
)
throws
SQLException
{
final
String
callSeq
=
"call seq.nextval"
;
Connection
conn
=
getConnection
(
"trigger"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"DROP TABLE IF EXISTS TEST"
);
stat
.
execute
(
"create sequence seq"
);
stat
.
execute
(
"create table test(id int primary key)"
);
assertSingleValue
(
stat
,
"call seq.nextval"
,
1
);
assertSingleValue
(
stat
,
callSeq
,
1
);
conn
.
setAutoCommit
(
false
);
stat
.
execute
(
"create trigger test_upd before insert on test call \""
+
if
(
asSource
)
{
String
triggerClassName
=
this
.
getClass
().
getName
()
+
"."
+
TestTriggerAlterTable
.
class
.
getSimpleName
();
stat
.
execute
(
"create trigger test_upd before insert on test "
+
"as $$org.h2.api.Trigger create() "
+
"{ return new "
+
triggerClassName
+
"(\"seq\"); } $$"
);
}
else
{
stat
.
execute
(
"create trigger test_upd before insert on test call \""
+
TestTriggerAlterTable
.
class
.
getName
()
+
"\""
);
}
stat
.
execute
(
"insert into test values(1)"
);
assertSingleValue
(
stat
,
"call seq.nextval"
,
3
);
assertSingleValue
(
stat
,
callSeq
,
3
);
stat
.
execute
(
"alter table test add column name varchar"
);
assertSingleValue
(
stat
,
"call seq.nextval"
,
4
);
assertSingleValue
(
stat
,
callSeq
,
4
);
stat
.
execute
(
"drop sequence seq"
);
stat
.
execute
(
"drop table test"
);
conn
.
close
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论