The Display Trigger Description (DSPTRGD) Command

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

The new DB2/400 trigger function is a very useful addition to an already great database. Properly implemented, triggers can increase the intelligence of your database by causing it to automatically respond to database events such as adding, deleting, or updating records. For example, a trigger could be implemented to automatically fax an order confirmation whenever a record is added to the order file.

You define triggers for physical files; the definition of what causes the trigger to fire and the program that is called when the trigger fires is contained in the file description. The Display File Description (DSPFD) command will display or print this information, but the format is cluttered. If you want to view trigger information for multiple files, the information will be spread across numerous pages.

The Display Trigger Description (DSPTRGD) command provides a solution to this problem. DSPTRGD displays trigger descriptions in a subfile and uses one single line record to display each trigger description, as illustrated in 1. As you can see, the display includes the library and file the trigger is assigned to, trigger time, trigger event, update condition, and trigger program and library. (For more information, see "DB2/400 Trigger Implementation," MC, October 1995.)

The Display Trigger Description (DSPTRGD) command provides a solution to this problem. DSPTRGD displays trigger descriptions in a subfile and uses one single line record to display each trigger description, as illustrated in Figure 1. As you can see, the display includes the library and file the trigger is assigned to, trigger time, trigger event, update condition, and trigger program and library. (For more information, see "DB2/400 Trigger Implementation," MC, October 1995.)

You can run DSPTRGD for a specific file, a generic (GENERIC*) file name, or all (*ALL) files. You can qualify the file with a specific library or with these special library values: *ALL, *ALLUSR, *USRLIBL, *CURLIB, or *LIBL.

2 contains the source for the DSPTRGD command. 3 contains display file TRG002DF, 4 contains the command processing program (CPP) TRG002CL, and 5 contains program TRG003CL, which builds the subfile information.

Figure 2 contains the source for the DSPTRGD command. Figure 3 contains display file TRG002DF, Figure 4 contains the command processing program (CPP) TRG002CL, and Figure 5 contains program TRG003CL, which builds the subfile information.

How It Works

The DSPTRGD application uses a subfile technique described in "CL Subfiles," TechTalk, MC, August 1995. The unique thing about this technique is the fact that it uses a CL program to interact with a subfile. It's an ideal solution for simple subfile applications such as the DSPTRGD command.

This technique works by changing the display characteristics of a message subfile. Normally, message subfiles use a subfile page (SFLPAG) value of 1, a subfile size (SFLSIZ) value of 2, and a subfile message record (SFLMSGRCD) value of 24. Whenever you send a message to the message subfile, a single line message is displayed on line 24 of the display. If more than one message is sent to the message subfile, a '+' character displays at the end of the message indicating that you can place the cursor on the message and use the roll keys to display additional messages.

For this technique, you expand the number of records to be displayed at the same time (SFLPAG) to a value that is greater than 1; change the number of records in the subfile (SFLSIZ) to a value 1 greater than the SFLPAG value; and change the line number value of the subfile message record (SFLMSGRCD) to a value less than 24. The subfile for the DSPTRGD command uses a SFLPAG value of 18, a SFLSIZ value of 19, and the line number value of the SFLMSGRCD is 4 (see 3). This allows the message subfile to behave similarly to a normal subfile.

For this technique, you expand the number of records to be displayed at the same time (SFLPAG) to a value that is greater than 1; change the number of records in the subfile (SFLSIZ) to a value 1 greater than the SFLPAG value; and change the line number value of the subfile message record (SFLMSGRCD) to a value less than 24. The subfile for the DSPTRGD command uses a SFLPAG value of 18, a SFLSIZ value of 19, and the line number value of the SFLMSGRCD is 4 (see Figure 3). This allows the message subfile to behave similarly to a normal subfile.

The CPP for the DSPTRGD command, TRG002CL (see 4), starts by calling program TRG003CL (see 5), which executes the Display File Description (DSPFD) command and creates an outfile containing the trigger descriptions for the files you've specified. TRG003CL reads the outfile, concatenates the information from a record, and sends the data to the program message queue of the caller (TRG002CL) through the Send Program Message (SNDPGMMSG) command.

The CPP for the DSPTRGD command, TRG002CL (see Figure 4), starts by calling program TRG003CL (see Figure 5), which executes the Display File Description (DSPFD) command and creates an outfile containing the trigger descriptions for the files you've specified. TRG003CL reads the outfile, concatenates the information from a record, and sends the data to the program message queue of the caller (TRG002CL) through the Send Program Message (SNDPGMMSG) command.

Since all messages sent to a program message queue are automatically loaded into the message subfile, you don't need to explicitly load the subfile. After TRG003CL finishes reading all the records in the outfile, TRG002CL displays the subfile through a series of SNDF and SNDRCVF commands, very similar to RPG WRITE and EXTFMT operations.

Check It Out

If you're using triggers and you want a way to view multiple trigger descriptions in an efficient format, check out the DSPTRGD utility. Even if you're not using triggers yet, you should look at the code for this utility to learn more about the CL subfile technique. Although this command could have been written using RPG, I wanted to write it in CL to show you how easy it is to use subfiles in CL programs-something most of us thought was impossible. For simple applications such as the DSPTRGD command, CL and a message subfile may be all you need. I'm sure you can find many other ways to employ this useful technique.

Richard Shaler is a senior technical editor for Midrange Computing.

The Display Trigger Description (DSPTRGD) Command

Figure 1: Output from the DSPTRGD Command



The Display Trigger Description (DSPTRGD) Command

Figure 2: DSPTRGD Command

 /*==================================================================*/ /* To compile: */ /* */ /* CRTCMD CMD(XXX/DSPTRGD) (XXX/TRG002CL) + */ /* SRCFILE(XXX/QCMDSRC) */ /* */ /*==================================================================*/ DSPTRGD: CMD PROMPT('Display Trigger Description') PARM KWD(FILE) TYPE(FILE) MIN(1) PROMPT('File') FILE: QUAL TYPE(*GENERIC) LEN(10) SPCVAL((*ALL)) QUAL TYPE(*NAME) LEN(10) DFT(*CURLIB) + SPCVAL((*LIBL) (*CURLIB) (*USRLIBL) + (*ALLUSR) (*ALL)) PROMPT('Library') 
The Display Trigger Description (DSPTRGD) Command

Figure 3: Display File TRG002DF

 *=============================================================== * To compile: * * CRTDSPF FILE(XXX/TRG002DF) SRCFILE(XXX/QDDSSRC) * *=============================================================== *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 A DSPSIZ(24 80 *DS3) A CA03(03) A CA12(03) A PRINT A R TRGDSP OVERLAY A 1 27'Display Trigger Description' A DSPATR(HI) A 3 2'Library' A 3 13'File' A 3 27'Time' A 3 37'Event' A 3 47'Cond' A 3 57'Library' A 3 68'Program' A R MSGSFL SFL A SFLMSGRCD(04) A MSGKEY SFLMSGKEY A PGMQ SFLPGMQ A R MSGCTL SFLCTL(MSGSFL) A SFLDSP A SFLDSPCTL A N03 SFLEND(*MORE) A SFLINZ A SFLSIZ(0019) A SFLPAG(0018) A PGMQ SFLPGMQ A R FKEYS OVERLAY A 23 2'F3=Exit' COLOR(BLU) *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
The Display Trigger Description (DSPTRGD) Command

Figure 4: CPP CL Program TRG002CL

 /*==================================================================*/ /* To compile: */ /* */ /* CRTCLPGM PGM(XXX/TRG002CL) SRCFILE(XXX/QCLSRC) */ /* */ /*==================================================================*/ PGM PARM(&FILE) DCL VAR(&FILE) TYPE(*CHAR) LEN(20) DCL VAR(&MSG) TYPE(*CHAR) LEN(80) DCL VAR(&MSGID) TYPE(*CHAR) LEN(7) DCL VAR(&MSGDTA) TYPE(*CHAR) LEN(80) DCLF FILE(TRG002DF) MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERROR)) CHGVAR VAR(&PGMQ) VALUE(TRG002CL) CALL PGM(TRG003CL) PARM(&FILE) SNDF RCDFMT(MSGCTL) SNDF RCDFMT(FKEYS) SNDRCVF RCDFMT(TRGDSP) GOTO CMDLBL(ENDPGM) ERROR: RCVMSG MSGTYPE(*EXCP) MSGDTA(&MSGDTA) MSGID(&MSGID) SNDPGMMSG MSGID(&MSGID) MSGF(QCPFMSG) MSGDTA(&MSGDTA) + MSGTYPE(*ESCAPE) ENDPGM: ENDPGM 
The Display Trigger Description (DSPTRGD) Command

Figure 5: CL Program TRG003CL

 /*==================================================================*/ /* To compile: */ /* */ /* CRTCLPGM PGM(XXX/TRG003CL) SRCFILE(XXX/QCLSRC) */ /* */ /*==================================================================*/ PGM PARM(&FILE) DCL VAR(&FILE) TYPE(*CHAR) LEN(20) DCL VAR(&TTM) TYPE(*CHAR) LEN(6) DCL VAR(&TEV) TYPE(*CHAR) LEN(6) DCL VAR(&TCN) TYPE(*CHAR) LEN(6) DCL VAR(&RCDSFND) TYPE(*LGL) DCL VAR(&MSG) TYPE(*CHAR) LEN(80) DCL VAR(&MSGID) TYPE(*CHAR) LEN(7) DCL VAR(&MSGDTA) TYPE(*CHAR) LEN(80) DCLF FILE(QAFDTRG) MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERROR)) OVRDBF FILE(QAFDTRG) TOFILE(QTEMP/DSPFD) DSPFD FILE(%SST(&FILE 11 10)/%SST(&FILE 1 10)) + TYPE(*TRG) OUTPUT(*OUTFILE) FILEATR(*PF) + OUTFILE(QTEMP/DSPFD) OUTMBR(*FIRST *REPLACE) LOOP: RCVF MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(DONE)) CHGVAR VAR(&TTM) VALUE(' ') CHGVAR VAR(&TEV) VALUE(' ') CHGVAR VAR(&TCN) VALUE(' ') IF COND(&TRTRTM = 'B') THEN(CHGVAR VAR(&TTM) + VALUE('Before')) IF COND(&TRTRTM = 'A') THEN(CHGVAR VAR(&TTM) + VALUE('After ')) IF COND(&TRTREV = 'D') THEN(CHGVAR VAR(&TEV) + VALUE('Delete')) IF COND(&TRTREV = 'I') THEN(CHGVAR VAR(&TEV) + VALUE('Insert')) IF COND(&TRTREV = 'U') THEN(CHGVAR VAR(&TEV) + VALUE('Update')) IF COND(&TRTRCN = 'A') THEN(CHGVAR VAR(&TCN) + VALUE('Always')) IF COND(&TRTRCN = 'C') THEN(CHGVAR VAR(&TCN) + VALUE('Change')) CHGVAR VAR(&RCDSFND) VALUE('1') CHGVAR VAR(&MSG) VALUE(&TRLIB *CAT ' ' *CAT + &TRFILE *CAT ' ' *CAT &TTM *CAT + ' ' *CAT &TEV *CAT ' ' + *CAT &TCN *CAT ' ' *CAT + &TRTRLB *CAT ' ' *CAT &TRTRIG) SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) MSGDTA(&MSG) GOTO CMDLBL(LOOP) DONE: IF COND(*NOT &RCDSFND) THEN(DO) CHGVAR VAR(&MSG) VALUE('No files found with + triggers.') SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) MSGDTA(&MSG) ENDDO GOTO CMDLBL(ENDPGM) ERROR: RCVMSG MSGTYPE(*EXCP) MSGDTA(&MSGDTA) MSGID(&MSGID) SNDPGMMSG MSGID(&MSGID) MSGF(QCPFMSG) MSGDTA(&MSGDTA) + MSGTYPE(*ESCAPE) ENDPGM: ENDPGM 
BLOG COMMENTS POWERED BY DISQUS