midrange.com code scratchpad
Name:
List software products UDTF
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
06/02/2017 08:40:37 pm
IP:
Logged
Description:
UDTF wrapping the QSZSLTPR API
Code:
  1. **free
  2. // Select Product QSZSLTPR API
  3.  
  4. // matching DDL:
  5. // create function buck.lstprd()
  6. //   returns table
  7. //   ( product_id char(7),
  8. //     product_option char(5),
  9. //     release_level char(6),
  10. //     installed char(1),
  11. //     supported char(1),
  12. //     registration_type char(2),
  13. //     registration_value char(14),
  14. //     description char(132)
  15. //   )
  16. // language rpgle
  17. // parameter style db2sql
  18. // not deterministic
  19. // reads sql data
  20. // returns null on null input
  21. // external action
  22. // not fenced
  23. // program type main
  24. // no final call
  25. // disallow parallel
  26. // no scratchpad
  27. // external name BUCK.LSTPRD
  28. // cardinality 100;
  29. //
  30. // sample usage:
  31. // select * from table(buck.lstprd()) as prod;
  32. //
  33. // documentation at
  34. // https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/apis/qszsltpr.htm
  35.  
  36. /copy qrpglesrc,stdhspec
  37.  
  38. /copy qsysinc/qrpglesrc,QSZSLTPR
  39. /copy qsysinc/qrpglesrc,QUSEC
  40.  
  41.  
  42. dcl-pr LSTPRD;
  43.   // input parms (none)
  44.  
  45.   // output parms
  46.   prdID char(7);
  47.   prdOpt char(5);
  48.   rlsLvl char(6);
  49.   installed char(1);
  50.   supported char(1);
  51.   registration_type char(2);
  52.   registration_value char(14);
  53.   description char(132);
  54.  
  55.   // input parms null map (none)
  56.  
  57.   // output parms null map
  58.   prdID_null like(IS_NULL);
  59.   prdOpt_null like(IS_NULL);
  60.   rlsLvl_null like(IS_NULL);
  61.   installed_null like(IS_NULL);
  62.   supported_null like(IS_NULL);
  63.   registration_type_null like(IS_NULL);
  64.   registration_value_null like(IS_NULL);
  65.   description_null like(IS_NULL);
  66.  
  67.   // SQL feedback
  68.   sqlstate_out char(5);
  69.   functname varchar(517) const options(*varsize);
  70.   specname varchar(128) const options(*varsize);
  71.   errormsg varchar(70) options(*varsize);
  72.   calltype int(10);
  73. end-pr;
  74.  
  75. dcl-pi LSTPRD;
  76.   // input parms (none)
  77.  
  78.   // output parms
  79.   prdID char(7);
  80.   prdOpt char(5);
  81.   rlsLvl char(6);
  82.   installed char(1);
  83.   supported char(1);
  84.   registration_type char(2);
  85.   registration_value char(14);
  86.   description char(132);
  87.  
  88.   // input parms null map (none)
  89.  
  90.   // output parms null map
  91.   prdID_null like(IS_NULL);
  92.   prdOpt_null like(IS_NULL);
  93.   rlsLvl_null like(IS_NULL);
  94.   installed_null like(IS_NULL);
  95.   supported_null like(IS_NULL);
  96.   registration_type_null like(IS_NULL);
  97.   registration_value_null like(IS_NULL);
  98.   description_null like(IS_NULL);
  99.  
  100.   // SQL feedback
  101.   sqlstate_out char(5);
  102.   functname varchar(517) const options(*varsize);
  103.   specname varchar(128) const options(*varsize);
  104.   errormsg varchar(70) options(*varsize);
  105.   calltype int(10);
  106. end-pi;
  107.  
  108.  
  109. // work fields
  110. dcl-c SQL_NORMAL const('00000');
  111. dcl-c SQL_NODATA const('02000');
  112. dcl-s IS_NULL int(5) inz(-1);
  113. dcl-s NOT_NULL int(5) inz(0);
  114. dcl-s r int(10);
  115.  
  116.  
  117. dcl-pr sltPrd extpgm('QSZSLTPR');
  118.  prdList like(prdList);
  119.  inpInfo like(inpInfo) const;
  120.  format char(8) const;
  121.  inpList like(inpList) const;
  122.  outInfo like(outInfo);
  123.  errcde like(qusec);
  124. end-pr;
  125.  
  126. dcl-s prdList like(QSZS0200) dim(100);
  127.  
  128. dcl-ds inpInfo qualified;
  129.  nbrrcds int(10);
  130.  nbrprods char(10);
  131.  initial_view char(1);
  132.  allow_exit char(1);
  133.  prdoptions char(10);
  134.  prod char(10);
  135.  rcdsinlist int(10);
  136. end-ds;
  137.  
  138. dcl-s inpList char(18) inz(*blanks);
  139.  
  140. dcl-ds outInfo qualified;
  141.  recsize int(10);
  142.  recsavail int(10);
  143.  action int(10);
  144. end-ds;
  145.  
  146.  
  147. // External UDTFs like this get called many times for a given SQL statement
  148. // once for open (-1)
  149. // many times each for fetch (0)
  150. // once for close (1)
  151. // WE tell the database when we're done by setting the outbound SQLstate
  152.  
  153. sqlstate_out = SQL_NORMAL;
  154.  
  155. select;
  156.   when calltype = -1;
  157.     exsr UDTF_open;
  158.   when calltype = 0;
  159.     exsr UDTF_fetch;
  160.   when calltype = 1;
  161.     exsr UDTF_close;
  162.     *inlr = *on;
  163. endsl;
  164.  
  165. // No LR until we're really done done.
  166. return;
  167.  
  168. //------------------------------------------------------------------
  169. begsr UDTF_open;
  170.  
  171.   // number of rows retrieved so far
  172.   r = 0;
  173.  
  174.   inpInfo.allow_exit = '1';
  175.   inpInfo.initial_view = '1';
  176.   inpInfo.nbrprods = '*ALL';
  177.   inpInfo.nbrrcds = %elem(prdList);
  178.   inpInfo.prod = '*INSTLD';
  179.   inpInfo.prdoptions = '*ALL';
  180.   inpInfo.rcdsinlist = 0;
  181.  
  182.   QUSBPRV = 0;
  183.   QUSBAVL = 0;
  184.  
  185.   // get the list
  186.   sltPrd(prdList(1): inpInfo: 'PRDS0200': inpList: outInfo: QUSEC);
  187.  
  188.   // if abnormal, forward SQLSTATE to the database manager and quit
  189.   if outInfo.action <> 0;
  190.     sqlstate_out = SQL_NODATA;
  191.     dump(a) 'open()';
  192.     *inlr = *on;
  193.   endif;
  194.  
  195. endsr;
  196.  
  197. //------------------------------------------------------------------
  198. begsr UDTF_fetch;
  199.  
  200.   // get the next record in the array
  201.   r += 1;
  202.   if r >  outInfo.recsavail;
  203.     // return to database manager with NODATA set
  204.     // the next call to the UDTF will be to CLOSE
  205.         sqlstate_out = SQL_NODATA;
  206.     return;
  207.   endif;
  208.  
  209.   QSZS0200 = prdList(r);
  210.   prdID = QSZPI03;
  211.   prdOpt = QSZPO05;
  212.   rlsLvl = QSZRL06;
  213.   installed = QSZIF00;
  214.   supported = QSZSF00;
  215.   registration_type = QSZRT00;
  216.   registration_value = QSZRV00;
  217.   description = QSZDT00;
  218.  
  219. endsr;
  220.  
  221.  
  222. //------------------------------------------------------------------
  223. begsr UDTF_close;
  224.   // called by DB2 after we've told it we are at NODATA
  225.   *inlr = *on;
  226. endsr; 
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css