Previous Topic: IBM i Database Trigger SupportNext Topic: Typical Trigger Implementation


Implementing Triggers

A trigger is attached to a file using the IBM Add Physical File Trigger (ADDPFTRG) command. This command specifies the database change operation that must occur for the trigger to fire. It also specifies the time the trigger should fire relative to the database change operation (before or after the database change has occurred). A trigger program defines the set of actions to perform when the trigger is fired. Trigger programs are named in the ADDPFTRG command.

When an application program makes a change to the data in a database file, i OS Data Management (DM) checks for the existence of a trigger for the file. If a trigger exists, DM then calls the specified trigger program. The application program never explicitly calls the trigger program.

IBM defines the parameters that must be passed by DM to all trigger programs as follows:

The trigger buffer parameter consists of a fixed-length portion and a variable-length portion. The fixed-length portion contains various fields that describe the trigger. The fixed-length portion contains a series of offset/length pairs that define where the old record format (ORF) and the new record format (NRF) are stored within the variable-length portion.

The variable-length portion contains the ORF and NRF values themselves as well as null-byte maps for each record format. The trigger buffer length parameter defines the overall length of the trigger buffer parameter. With these parameters, the trigger program has access both to information about the trigger itself and also full details of the data being updated, deleted, inserted, or read.

In addition to performing additional processing to that defined in the application program that is changing the database, a trigger can actually cancel the database change and signal to the application program that the change was unsuccessful. Under some circumstances, the trigger can also change the data that is being written to the database, overriding the data used in the update statement in the application program.