File Triggers
A file trigger is a cataloged subroutine that is called automatically whenever an item is written to or deleted from a particular file. A trigger can be set to run before or after an item is written and before or after an item is deleted.
- Triggers that run before file operations are mainly used to validate the attempted change to the database against user-defined constraints, or "business rules", and allow the change only if the constraint is satisfied.
- Triggers that run after file operations are normally used to create audit trails and other transaction logs.
- All types of trigger can be used to create relationships between files, to ensure that whenever one file is updated, another related file is also updated.
Note that file triggers do not run during restore operations.
The section File Triggers in the DataBasic Reference describes how write a trigger.
How to Associate a Trigger with a File
Once a trigger has been written, it will need to be associated with the appropriate Reality file. To do this, use the CREATE-TRIGGER command; you must specify the name of the file, the name of the trigger subroutine and the type of trigger required: PRE-WRITE, POST-WRITE, PRE-DELETE or POST-DELETE, PRE-READ or POST-READ, PRE-CLEAR-FILE or POST-CLEAR-FILE, PRE-DELETE-FILE or POST-DELETE-FILE. Note that the trigger subroutine must be cataloged in the master dictionary of the account containing the file. A trigger can be associated with a file data section, a file dictionary, or an account's master dictionary.
Caution
When associating a trigger with a file, you need to be aware of the effects of any other triggers that might run as a consequence.
The DELETE-TRIGGER command allows you to remove trigger associations. In this case you need only specify the file name and the trigger type. Note that DELETE-TRIGGER file-specifier * will remove all triggers from a file.
LIST-TRIGGERS lists the triggers associated with a file; LIST-ALL-TRIGGERS lists all the triggers associated with a Reality account.
Note
It is strongly recommended that triggers should not be associated with system files.
Commands that might run Triggers
When running any command that updates a file, you should be aware of the effects of any file triggers that might be run as a consequence. For example, the following commands can cause triggers to be executed (the list is not exhaustive).
TCL Commands:
BASIC(writes a $ item)
COPY (trigger on destination file)
DELETE
EDELETE
ED{IT} (see EDITOR Commands below)
SE{D{IT}} (see Screen Editor Commands below)
SQL (update commands)
Proc Commands:
F-DELETE
F-WRITE
EDITOR Commands
FD
FI
FS
Screen Editor Commands
F
You should also be aware that, if you place triggers on DICT sections or an account's MD, many more commands might cause these triggers to run. For example:
CATALOG
CREATE-FILE
CREATE-INDEX
DELETE-FILE
DELETE-INDEX
Note that the DataBasic write and delete statements can also cause triggers to run. Refer to the DataBasic Reference for details.