Submitted by Anonymous (not verified) on Tue, 11/04/2014 - 00:00
Forums

Hi,

Does db_execute support parameters in JSON format and callback functions?

Niels Liisberg

Tue, 11/04/2014 - 00:00

Hi,

Now the db_execute also support parameters in JSON format and callback functions to manipulate the meta object sent to the client:

5250

 

<%
/include qasphdr,SqlVar
/include qasphdr,JsonParser

// This is your prototype to your meta callback
d sqlMetaCallback...
d                 pr            10i 0
d metaObj                         *   value


var err       bool
var sqlStmt   varchar(4096)
var sqlParms  varchar(4096)

// Example:
// systest.icebreak.org/dbexecute.aspx
// Extracting at tree store from one SQL table
// -------------------------------------------
/free
  *inlr = *ON;

  // - Not needed, this will be set automatically:
  // setContentType('application/json; charset=UTF-8');


 // Note paramters will be esaped to enshure no sql injections
 // This is the safe way to do it:
 sqlparms = ('{                                                    +
         manuid : "SONY",                                              +
         orderbycol: 1                                                 +
 }');


  sqlStmt = ('{ +
                sql: "SELECT * from product where manuid = $manuid        +
                   order by $orderbycol",                              +
          limit : 10,                                                   +
         start : 1,                                                    +
         xxcallBack: "anyJsonPFunctionName",                           +
         metaData: true,                                               +
                doMetaHeaderCallback : true,                                  +
          doMetaRowCallback : false                                     +
 }');


  err = db_Execute(sqlStmt : sqlParms: %paddr(sqlMetaCallback));
  if err;
         %><% = getLastError('*TEXT') %><%
 endif;

/end-free
/* -------------------------------------------------------------------------------------------
   The "metaCallback" can be called for each row and/or for meta only dependet on the options:

         doMetaHeaderCallback : true -> do one call got the header
          doMetaRowCallback : true -> do one call for each row

   This sample demonstrates how to add an extra "title" header attibute to the meta data
   which goes to the client directly after

--Name------------b-------------------Keywords-----------------------------Comments------------ 
*/
p sqlMetaCallback...
p                 B
d                 PI            10i 0
d metaObj                         *   value

 // Local variables:
d SqlHdr          ds                  likeds(I_sqlhdr)
d iterCols        ds                  likeds(json_iterator)

/free

     // For metadata we have the "fields" array...
  iterCols = json_setIterator(metaObj : 'fields');
  dow json_ForEach(iterCols);
         sql_GetColHeader ( SqlHdr : iterCols.count);
          json_setvalue(iterCols.this : 'title':
                  sqlHdr.sqlColHdr1 + ' ' +
                 sqlHdr.sqlColHdr2 + ' ' +
                 sqlHdr.sqlColHdr3
         );
  enddo;

  // Just for fun: Show the sql on the meta element:
  json_setValue(metaObj: 'sql' : sqlStmt);

  // Return I_CONTINUE as long a you want to iterate. If you want to break the loop 
        then return I_BREAK
 return I_CONTINUE;

/end-free
p                  e

Result in: ( Notice the “title” and the “sql” in the "metaData" 

Best regards,

Niels Liisberg