Triggers
This section describes the DDL commands pertaining to triggers.
Create trigger
Define a new trigger.
{create | alter} type type-name "{"
create trigger name
after
{insert | update | delete} [, ...]
for {each | all}
[ when (condition) ]
do expr
"}"
Description
The command create trigger
nested under create type
or alter type
defines a new trigger for a given object type.
The trigger name must be distinct from that of any existing trigger on the same type.
Parameters
The options of this command are identical to the SDL trigger declaration.
Example
Declare a trigger that inserts a Log
object for each new User
object:
alter type User {
create trigger log_insert after insert for each do (
insert Log {
action := 'insert',
target_name := __new__.name
}
);
};
Declare a trigger that inserts a Log
object conditionally when an update
query makes a change to a User
object:
alter type User {
create trigger log_update after update for each
when (<json>__old__ {**} != <json>__new__ {**})
do (
insert Log {
action := 'update',
target_name := __new__.name,
change := __old__.name ++ '->' ++ __new__.name
}
);
}
Drop trigger
Remove a trigger.
alter type type-name "{"
drop trigger name;
"}"
Description
The command drop trigger
inside an alter type
block removes the
definition of an existing trigger on the specified type.
Parameters
- type-name
-
The name (optionally module-qualified) of the type being triggered on.
- name
-
The name of the trigger.
Example
Remove the log_insert
trigger on the User
type:
alter type User {
drop trigger log_insert;
};