× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Re: FW: Triggers
  • From: "Scott Klement" <infosys@xxxxxxxxxxxx>
  • Date: 03 Aug 1999 18:08:19 -0500

Hello...


Babu Suresh <SureshB@alfuttaim.co.ae> wrote:
>
> >
> > Dear all,
> >
> >  Thanks for the response and solutions provided to me for my earli
> > query about OPNQRYF.
> >
> >  Now I need the help in using triggers.
> >
> >  I know that triggers are the programs which can be made to get
> > executed when a record is inserted or updated or deleted from a
>  file. My
> > requirement is to find a value of a partcular field in that record
>  which
> > has just been Written/Updated. How to achieve this with the help o
> > trigger. If there is any other way please suggest.
> >
> >
> > Thank you.
> > P.Sureshbabu
> > DSQ Software Ltd.
> > Chennai.
> > India.

The way that I like to do triggers is to create an externally
defined data structure, using the layout of the file that your
trigger is on.

If that external DS is based on a pointer, you can then set it to
point to the area of memory that the record buffer is in, based
on the offset in the trigger structure...

like so:

D AFTER           C                   CONST('1')
D BEFORE          C                   CONST('2')
D INSERT          C                   CONST('1')
D DELETE          C                   CONST('2')
D UPDATE          C                   CONST('3')

D dsBefore      e ds                  EXTNAME(MYFILE)
D                                     BASED(p_Before)
D                                     PREFIX(dsB_)
D p_Before        S               *

D dsAfter       e ds                  EXTNAME(MYFILE)
D                                     BASED(p_After)
D                                     PREFIX(dsA_)
D p_After         S               *

D dsTrg           ds
D   dsTrgBuff             1   3072A   DIM(3072)
D   dsTrgFile             1     10A
D   dsTrgLib             11     20A
D   dsTrgMbr             21     30A
D   dsTrgEvent           31     31A
D   dsTrgTime            32     32A
D   dsTrgCmtLk           33     33A
D   dsTrgFill1           34     36A
D   dsTrgCSID            37     40I 0
D   dsTrgFill2           41     48A
D   dsTrgBOff            49     52I 0
D   dsTrgBLen            53     56I 0
D   dsTrgBNOff           57     60I 0
D   dsTrgBNLen           61     64I 0
D   dsTrgAOff            65     68I 0
D   dsTrgALen            69     72I 0
D   dsTrgANOff           73     76I 0
D   dsTrgANLen           77     80I 0
D   dsTrgFill3           81     96A

D peLength        S             10I 0

C     *ENTRY        PLIST
C                   PARM                    dsTrg
c                   parm                    peLength

c                   Select
c                   when      dsTrgEvent = UPDATE
c                   eval      p_Before = %addr(dsTrgBuff(dsTrgBOff+1))
c                   eval      p_After = %addr(dsTrgBuff(dsTrgAOff+1))
c                   exsr      RecUpdated

c                   when      dsTrgEvent = INSERT
c                   eval      p_After = %addr(dsTrgBuff(dsTrgAOff+1))
c                   exsr      RecWritten

c                   when      dsTrgEvent = DELETE
c                   eval      p_Before = %addr(dsTrgBuff(dsTrgBOff+1))
c                   exsr      RecDeleted
c                   Endsl

C                   return

CSR   RecUpdated    begsr
C*
C* ... code goes here to deal with an updated record ...
C*
C*  You can use dsB_(fldname) to see what the data was
C*    BEFORE the update, and dsA_(fldname) to see what
C*    the data will be AFTER the update.
C*
C*  example:   if your external name is CUST, then:
C*       dsB_CUST is the field before the update
C*       dsA_CUST is the field after the update.
C*
CSR                 endsr

CSR   RecWritten    begsr
C*
C*  ...  code goes here to handle when a new record is added ...
C*
C*   Because there is no prior data for this record in the
C*   file, you can only use the "AFTER" fields from the
C*   data structure.
C*
C*    example:   if the external name is CUST, then:
C*        dsA_CUST is what the field will be after the write.
C*
CSR                 endsr

CSR   RecDeleted    begsr
C*
C*  ...Code goes here for deleted records...
C*
C*  Use dsB_ to check what the rec is before its deleted...
C*
CSR                 endsr

>
> Dear all,
>
>  One more addition to my below query:
>  If that particular record is written/updated by an RPG or CLP, Is i
> possible to know what is the name of the program?
>
>  Expecting help.
>
> Thanks
> P.Sureshbabu
> DSQ Software ltd.
> Madras,
> India.
>

You should be able to determine the name of the RPG that updated
the file -- but CL programs cant update/add records, so that's
not a huge issue :)

Retrieving the program name is a bit more difficult.   The only
way I know how to do this is to send a message back to the previous
program in your call stack, then receive that same message (from
the previous program's queue) and determine what program it was
sent to.

I'd strongly encourage you to NOT do this in a trigger.  It will
slow down your file updates.  If you REALLY want to do it, however,
let me know and I'll send an example of doing this to you.

Hope that helps...

Scott Klement
Information Systems Manager
Klement's Sausage Co, Inc.

+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---END



As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.