midrange.com code scratchpad
Name:
Example DB Event Handler
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
05/20/2015 03:13:25 pm
IP:
Logged
Description:
Example of data validation handler, evoked before database insert and update events. Ensures that required fields are filled in and contain valid data; otherwise descriptive error messages are returned to the client.
Code:
  1.      h nomain copyright('Relational Data Corporation 2015')
  2.  
  3.       /copy *libl/qdbssrc,rdtrghlr#1
  4.       /copy *libl/qdbssrc,rdtrginf#1
  5.       /copy *libl/qrpglesrc,rdmsgapi#1
  6.  
  7.       //-----------------------------------------------------------------
  8.       // module level data
  9.       //-----------------------------------------------------------------
  10.  
  11.      d error           s              1a
  12.      d trgptr          s               *   import
  13.  
  14.      d rec           e ds                  qualified extname(dtrg100p)
  15.  
  16.      d checkObj        pr                  extpgm('DTRG100C')
  17.      d  library                      10a   const
  18.      d  file                         10a   const
  19.      d  error                         1a
  20.  
  21.       //-----------------------------------------------------------------
  22.       // initialization
  23.       //-----------------------------------------------------------------
  24.  
  25.      p init            b                   export
  26.  
  27.       /free
  28.  
  29.        msgFileOpen();
  30.  
  31.       /end-free
  32.  
  33.      p init            e
  34.  
  35.       //-----------------------------------------------------------------
  36.       // process event
  37.       //-----------------------------------------------------------------
  38.  
  39.      p process         b                   export
  40.  
  41.       /free
  42.  
  43.        rec = trg.after;
  44.  
  45.        //-----------------------------------------------------------------
  46.        // is library filled in?
  47.        //-----------------------------------------------------------------
  48.  
  49.        if rec.library = *blanks;
  50.         trgMsgAdd(type_error
  51.          :msgGetText('WLMAINT':11:'Library.')
  52.          :'LIBRARY');
  53.         trg.escape = *on;
  54.         return;
  55.        endif;
  56.  
  57.        //-----------------------------------------------------------------
  58.        // is file name filled in?
  59.        //-----------------------------------------------------------------
  60.  
  61.        if rec.file = *blanks;
  62.         trgMsgAdd(type_error
  63.          :msgGetText('WLMAINT':11:'File Name.')
  64.          :'FILE');
  65.         trg.escape = *on;
  66.         return;
  67.        endif;
  68.  
  69.        //-----------------------------------------------------------------
  70.        // is handler name filled in?
  71.        //-----------------------------------------------------------------
  72.  
  73.        if rec.handler = *blanks;
  74.         trgMsgAdd(type_error
  75.          :msgGetText('WLMAINT':11:'Handler.')
  76.          :'HANDLER');
  77.         trg.escape = *on;
  78.         return;
  79.        endif;
  80.  
  81.        //-----------------------------------------------------------------
  82.        // do library and file exist?
  83.        //-----------------------------------------------------------------
  84.  
  85.        clear error;
  86.  
  87.        checkObj(rec.library:rec.file:error);
  88.  
  89.        select;
  90.         when error = '1';
  91.          exsr lib_not_found;
  92.         when error = '2';
  93.          exsr file_not_found;
  94.        endsl;
  95.  
  96.        return;
  97.  
  98.        //-----------------------------------------------------------------
  99.        // error - library not found
  100.        //-----------------------------------------------------------------
  101.  
  102.        begsr lib_not_found;
  103.  
  104.         trgMsgAdd(type_error
  105.          :msgGetText('VALIDATION':1:%trimr(rec.library))
  106.          :'LIBRARY');
  107.  
  108.         trg.escape = *on;
  109.  
  110.         return;
  111.  
  112.        endsr;
  113.  
  114.        //-----------------------------------------------------------------
  115.        // error - file not found in library
  116.        //-----------------------------------------------------------------
  117.  
  118.        begsr file_not_found;
  119.  
  120.         trgMsgAdd(type_error
  121.          :msgGetText('VALIDATION':2:%trimr(rec.file)+'|'+%trimr(rec.library))
  122.          :'FILE');
  123.  
  124.         trg.escape = *on;
  125.  
  126.         return;
  127.  
  128.        endsr;
  129.  
  130.       /end-free
  131.  
  132.      p process         e
  133.  
  134.       //-----------------------------------------------------------------
  135.       // clean up
  136.       //-----------------------------------------------------------------
  137.  
  138.      p term            b                   export
  139.  
  140.       /free
  141.  
  142.        msgFileClose();
  143.  
  144.        *inlr = *on;
  145.  
  146.       /end-free
  147.  
  148.      p term            e
  149.  
© 2004-2019 by midrange.com generated in 0.006s valid xhtml & css