      * ILE RPG example of a REST provider using YAJL to update a customer
      *
      * The URL identifies the customer to work with.
      * The URL identifies the customer to work with.  The HTTP method
      * identifies what to do, so:
      *    GET = read record
      *    PUT = replace cust record
      *
      *    http://whatever/rest/yajlweb
      *
      * It will then look up all invoices for the customer number
      * in the provided date range, and output them all in a
      * list like this one:
      *
      *     [
      *       {
      *         "invoice": "xyz",
      *         "date": "01/30/2012",
      *         "name": "Acme Industries, Inc.",
      *         "amount": 123.45,
      *         "weight": 123.45,
      *       },
      *       { same fields again },
      *       { same fields again },
      *       { etc }
      *     ]
      *
      *
      *  To compile:
      *> CRTSQLRPGI INVJSON SRCFILE(QRPGLESRC) DBGVIEW(*SOURCE) -
      *>            OBJTYPE(*MODULE)
      *> CRTPGM INVJSON BNDSRVPGM(QHTTPSVR/QZHBCGI) ACTGRP(RESTFUL)

     H OPTION(*SRCSTMT: *NODEBUGIO) PGMINFO(*PCML:*MODULE)

     D getenv          PR              *   extproc('getenv')
     D   var                           *   value options(*string)

     D QtmhWrStout     PR                  extproc('QtmhWrStout')
     D   DtaVar                   65535a   options(*varsize) const
     D   DtaVarLen                   10I 0 const
     D   ErrorCode                 8000A   options(*varsize)

     D err             ds                  qualified
     D   bytesProv                   10i 0 inz(0)
     D   bytesAvail                  10i 0 inz(0)

     D row             ds                  qualified
     D   inv                          5a
     D   date                         8s 0
     D   name                        25a
     D   amount                       9p 2
     D   weight                       9p 1

     D CRLF            C                   x'0d25'
     D data            s           5000a   varying
     D uri             s           5000a   varying
     D cust            s              4s 0
     D sdate           s              8s 0
     D edate           s              8s 0
     d custpos         s             10i 0
     d sdatepos        s             10i 0
     d edatepos        s             10i 0
     D dateUSA         s             10a
     d count           s             10i 0

      /free
          exec SQL set option naming=*SYS;

          *inlr = *on;
          uri = %str(getenv('REQUEST_URI'));

          monitor;
             custpos = %scan('/invjson/': uri) + %len('/invjson/');
             sdatepos = %scan('/': uri: custpos) + 1;
             edatepos = %scan('/': uri: sdatepos) + 1;
             cust  = %int(%subst(uri: custpos: (sdatepos-custpos-1)));
             sdate = %int(%subst(uri: sdatepos: (edatepos-sdatepos-1)));
             edate = %int(%subst(uri: edatepos));
          on-error;
             data = 'Status: 500 Invalid URI' + CRLF
                  + 'Content-type: text/json' + CRLF
                  + CRLF
                  + '{ "success": false, +
                       "errmsg": "Invalid URI!" }'
                  + CRLF;
             QtmhWrStout(data: %len(data): err);
             return;
          endmon;

          exec SQL declare C1 cursor for
              select aiOrdn, aiIDat, aiSNme, aiDamt, aiLbs
                from ARSHIST
               where aiCust = :cust
                 and aiIDat between :sdate
                                and :edate;

          exec SQL open C1;
          exec SQL fetch next from C1 into :row;

          if sqlstt<>'00000'
             and %subst(sqlstt:1:2) <> '01'
             and %subst(sqlstt:1:2) <> '02';
             data = 'Status: 500 Query Failed' + CRLF
                  + 'Content-type: text/json' + CRLF
                  + CRLF
                  + '{ "success": false, +
                       "errmsg": "Query failed with SQLSTT=' +
                        SQLSTT + '" }'
                  + CRLF;
             QtmhWrStout(data: %len(data): err);
             return;
          endif;

          data = 'Status: 200 OK' + CRLF
               + 'Content-type: text/json' + CRLF
               + CRLF
               + '{ "success:" true, [';
          QtmhWrStout(data: %len(data): err);

          dow sqlstt='00000' or %subst(sqlstt:1:2)='01';

             count += 1;
             if count = 1;
                data = CRLF;
             else;
                data = ',' + CRLF;
             endif;

             dateUsa = %char( %date(row.date:*iso) : *usa );
             data += '{ +
                        "invoice": "' + row.inv           + '", +
                           "date": "' + dateUsa           + '", +
                           "name": "' + row.name          + '", +
                         "amount": '  + %char(row.amount) + ', +
                         "weight": '  + %char(row.weight) + ' +
                      }';
             QtmhWrStout(data: %len(data): err);

             exec SQL fetch next from C1 into :row;
          enddo;

          exec SQL close C1;

          data = CRLF + ']}' + CRLF;
          QtmhWrStout(data: %len(data): err);
          return;
      /end-free
