Triggers open up a world of possibilities in terms of what an application can do "under the covers."
Recently, over on Midrange.com, an absolutely wonderful resource run by David Gibbs for users and developers of the IBM i system, a developer reported having a problem related to using triggers. Basically, what he wanted to do was detect an update to a given file and then, unbeknownst to the application program performing the update, change the value of one or more fields within the updated record. This article is very loosely based on the response I provided to his problem. The "very loosely based" is essentially the difference between my posted 10-minute quick response and a 30-minute "let's dig a little deeper" answer.
Triggers, if you are not familiar with them, represent an exit point within the system where you can have an application program called whenever certain changes are made to a record within a file. The changes that can cause the program to be called can be inserting a new record, updating an existing record, deleting a record, or even just reading a record. Today, we'll look at what's required to write a trigger program to handle insert, update, and delete activity over a given file.
The file we will use for demonstration purposes is MYFILE. The DDS definition we'll use is this:
R MYRECORD
MYKEY 10A
MYCHARFLD 10A
MYPKDFLD 5P 0
MYZNDFLD 9S 0
MYINTFLD 9B 0
K MYKEY
MYFILE is just a run-of-the-mill keyed physical file with a variety of data types (character, packed decimal, binary, etc.) in use for various fields. The file could be defined using DDS as shown or created using other mechanisms such as an SQL Create Table statement. Assuming the previous DDS is stored in member MYFILE of source file QDDSSRC, you can create the file using the Create Physical File (CRTPF) command as shown below.
CRTPF FILE(MYFILE) SRCFILE(QDDSSRC)
Skipping over for the moment how we tell the system to call our trigger program when a given change to a record occurs, the system will pass the program two parameters. The first parameter contains information about the current operation (for instance, the name of the file being maintained, the type of operation— update/write/delete/read, offsets within the parameter of where to find a copy of the original and/or new record being operated on, the original and/or new record, etc.) and the second parameter the length of the first parameter. These two parameters are introduced in the Knowledge Center under the topic How trigger programs work with specific layout definitions under Trigger buffer sections and Trigger buffer field descriptions. With these links as resources, here is our sample trigger program MYFILETRG.
h dftactgrp(*no)
dMyFileTrg pr
d TrgBfr likeds(QDBTB)
d LenTrgBfr 10i 0 const
dMyFileTrg pi
d TrgBfr likeds(QDBTB)
d LenTrgBfr 10i 0 const
d BeforePtr s *
d Before e ds qualified
d based(BeforePtr)
d extname(MYFILE)
d AfterPtr s *
d After e ds qualified
d based(AfterPtr)
d extname(MYFILE)
d Insert c '1'
d Delete c '2'
d Update c '3'
d CalledAfter c '1'
d CalledBefore c '2'
/copy qsysinc/qrpglesrc,trgbuf
/free
select;
when TrgBfr.QDBFilN02 <> 'MYFILE';
dsply 'MyFileTrg called for wrong file';
when ((TrgBfr.QDBTT = CalledBefore) and
(TrgBfr.QDBTE = Insert));
AfterPtr = %addr(TrgBfr) + TrgBfr.QDBNRO;
dsply ('Key ' + %trimr(After.MyKey) +
' added. MyIntFld: ' +
%char(After.MyIntFld));
when ((TrgBfr.QDBTT = CalledBefore) and
(TrgBfr.QDBTE = Update));
BeforePtr = %addr(TrgBfr) + TrgBfr.QDBORO;
AfterPtr = %addr(TrgBfr) + TrgBfr.QDBNRO;
dsply ('Value before update: ' + %char(Before.MyPkdFld));
dsply ('Value after update: ' + %char(After.MyPkdFld));
After.MyPkdFld += 1;
dsply ('Value after trigger: ' + %char(After.MyPkdFld));
when ((TrgBfr.QDBTT = CalledBefore) and
(TrgBfr.QDBTE = Delete));
BeforePtr = %addr(TrgBfr) + TrgBfr.QDBORO;
dsply ('Key ' + %trimr(Before.MyKey) +
' deleted. MyZndFld: ' +
%char(Before.MyZndFld));
other;
dsply ('MyFileTrg failure. Time: ' +
TrgBfr.QDBTT +
' Type: ' +
TrgBfr.QDBTE);
endsl;
*inlr = *on;
return;
/end-free
The program defines the two parameters passed by the system when calling a trigger program as TrgBfr and LenTrgBfr. TrgBfr is defined as being LIKEDS(QDBTB) with QDBTB being an IBM-provided data structure definition (found in QSYSINC/QRPGLESRC, member TRGBUF) for the fixed location fields of the trigger buffer.
As this trigger program will be working with one specific file, MYFILE, it also defines two based data structures. These data structures, Before and After, correspond to the original and new record images, respectively. The data structures are defined using EXTNAME(MYFILE) so that RPG will generate the subfields of Before and After using the field descriptions of MYFILE. The data structures are based—using pointer variables BeforePtr and AfterPtr, respectively—so that the trigger program can directly access the before and after record images without having to move/substring the record values from where they are located in the TrgBfr parameter to non-based variables within the program.
For convenience, the program also defines five named constants. These constants provide for more meaningful reading/documentation of the program with, for instance, the constant 'Insert' corresponding to the value '1' of the Trigger Event (QDBTB subfield QDBTE, which documents that a '1' represents an Insert operation to the file).
The processing within the trigger program is quite straightforward. The program enters a SELECT group where it first checks if the file being passed is indeed MYFILE. If it isn't, an error message is DSPLYed.
If the trigger program has been called before an insert operation then AfterPtr is set to the address of the new record image by taking the address of the TrgBfr parameter and adding the offset to the copy of the record representing the new record (TrgBfr.QDBNRO). The value of the fields MyKey and MyIntFld are then DSPLYed.
If the trigger program has been called before an update operation, then BeforePtr is set to the address of the original record image by taking the address of the TrgBfr parameter and adding the offset to the copy of the record representing the original record (TrgBfr.QDBORO) and AfterPtr is set to the address of the new record image. The original value of MyPkdFld and the updated value of MyPkdFld are then DSPLYed. Recalling that the original request on Midrange was to update a field within the trigger program, the program then also adds 1 to the updated MyPkdFld value and DSPLYs this new value.
If the trigger program has been called before a delete operation, then BeforePtr is set to the address of the original record image, and the MyKey and MyIntFld values of what are being deleted are DSPLYed.
If the trigger program has been called for some OTHER reason than those handled by previous WHEN checks, then the program DSPLYs a failure message with the message, including the unexpected time and event values.
The program, having served its purpose, then ends.
Assuming the trigger program source is in member MYFILETRG of source file QRPGLESRC, you can create the program using this command:
CRTBNDRPG PGM(MYFILETRG)
Having created the trigger program, we'll now register MYFILETRG with the MYFILE file previously created. This registration is done using the Add Physical File Trigger (ADDPFTRG) command.
The two following commands will cause our trigger program to be called before any insert/write and delete operations on MYFILE, respectively.
ADDPFTRG FILE(MYFILE) TRGTIME(*BEFORE) TRGEVENT(*INSERT) PGM(MYFILETRG)
ADDPFTRG FILE(MYFILE) TRGTIME(*BEFORE) TRGEVENT(*DELETE) PGM(MYFILETRG)
This command will have our trigger program called after any insert/write activity on MYFILE:
ADDPFTRG FILE(MYFILE) TRGTIME(*AFTER) TRGEVENT(*INSERT) PGM(MYFILETRG)
This command will have our trigger program called before any update operations on MYFILE. In addition, the trigger program will be capable of changing the values of the MYFILE record prior to the system actually writing the updated record (the original request on Midrange). This ability to update record values is due to our specifying the ALWREPCHG(*YES) parameter value.
ADDPFTRG FILE(MYFILE) TRGTIME(*BEFORE) TRGEVENT(*UPDATE) PGM(MYFILETRG) ALWREPCHG(*YES)
Now let's do some maintenance of file MYFILE.
For brevity, I'm going to use SQL to insert, update, and then delete a record from MYFILE. You could also use RPG file I/O, a utility such as DFU, or whatever means you normally use to maintain your databases.
Using STRSQL, I run the following SQL statements:
Insert into MyFile Values('A', 'TextA', 1, 2, 3)
Update MyFile Set MyPkdFld = 13 where MyKey = 'A'
Delete from MyFile
When running the previous statements, several messages will be generated by the trigger program MYFILETRG. These messages will be:
DSPLY Key A added. MyIntFld: 3
DSPLY MyFileTrg failure. Time: 1 Type: 1
DSPLY Value before update: 1
DSPLY Value after update: 13
DSPLY Value after trigger: 14
DSPLY Key A deleted. MyZndFld: 2
The first and second messages are generated when the SQL INSERT statement is run. The first message is due to the WHEN operation with a trigger time (TrgBfr.QDBTT) of before and a trigger event (TrgBfr.QDBTE) of Insert. The second message is due to OTHER operation as none of the WHEN operations handle a trigger time of after and a trigger event of Insert.
The third, fourth, and fifth messages are generated when the SQL UPDATE statement is run. These DSPLYs are due to the WHEN operation with a trigger time of before and a trigger event of Update. The messages reflect the original value of MyPkdFld, the user application program updated value of MyPkdFld, and the trigger program updated value of MyPkdFld, respectively. If you happen to display MYFILE after the SQL UPDATE statement runs, you'll find that MyPkdFld is indeed set to 14 in the database.
The last message reflects when the SQL DELETE statement was run. The message is due to the WHEN operation with a trigger time of before and a trigger event of Delete.
Triggers are that simple to use, and they open up a world of possibilities in terms of what an application can do "under the covers." The approach taken with trigger program MyFileTrg is one way to implement a trigger. In future articles, we can look at other (to my way of thinking, more flexible) methods.
As usual, if you have any API questions, send them to me at
LATEST COMMENTS
MC Press Online