DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  DB Query to execute in Gateway Script

    Posted 03/07/20 12:32 PM
    Hi Team,
    @Hermann Stamm-Wilbrandt Please help on the below.

    Could you please guide me to how to execute DB query in Gateway script.

    I tried to use transform.xslt function to run DB query, the query is being executed but the response I am getting as node set, but how can i etract only one col value in Gateway script.

    How to use response of transform.xslt in gateway script. below is the response, i need to use only pciCardID value in Gateway script, how to fetch the value alone in Gatewayscript.

    Response of Transform.xslt:
    sql result="success">
        <row>
            <column>
                <name>cardNumber</name>
                <value>123456789</value>
            </column>
            <column>
                <name>pciCardID</name>
                <value>ABCDEFGHIJK</value>
            </column>
            <column>
                <name>createAt</name>
                <value>2020-03-05 20:05:31.000000000</value>
            </column>
        </row>
    </sql>

    My Gateway script:
    var sm = require('service-metadata');
    var transform = require('transform');
    var ctx = session.name('Ctx') || session.createContext('Ctx');

    session.input.readAsBuffers (function (error, bufs) {
    if (error) { session.reject(error); return; }
    else
    {
    var regex = /000[2-8][1-9][0-9]{14}/gm;
    var str = bufs.toString();
    var fistMatch = str.search(regex);
    var matchRes = str.match(regex);
    var splitRes = str.split(regex);

    var i = 0;
    var result = "";
    if (fistMatch != '0') {
    for (i = 0; i < matchRes.length; i++) {
    var CRD_NBR = matchRes[i].substr(3,16);
    ctx.setVar('cardnumber', CRD_NBR);
    var options = {
    "location": "local:////SFTP_DBqueryPCIID.xsl"

    };
    transform.xslt(options, function(error, nodelist) {
    // session.output.write(XML.stringify({omitXmlDeclaration: true}, nodelist));
    var dpbase = XML.stringify({omitXmlDeclaration: true}, nodelist);
    var dbstring = dpbase.split('<name>pciCardID</name><value>')[1];
    var cardPICID = dbstring.substring(0, 16);

    });

    var cardPICID = ctx.getVar('var://context/ctx/pciCardID');
    result += splitRes[i] + cardPICID;

    }
    }
    else {
    for (i = 0; i < matchRes.length; i++) {
    result += splitRes[i] + matchRes[i].substr(0,6) + "******" + matchRes[i].substr(13,19));
    }
    }
    result += splitRes[splitRes.length - 1];
    //session.output.write(result);
    }

    });







    ------------------------------
    krishna
    ------------------------------


  • 2.  RE: DB Query to execute in Gateway Script

    Posted 03/07/20 10:09 PM
    Hi,
    while it is technically possible to do information extraction from XML via string operations on XML.stringify(nodelist), it is a bad idea.
    I would use the other function in transform module, transform.xpath() to extract the information:
    https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.7.0/com.ibm.dp.doc/transform_js.html

    In addition, seeing GatewayScript using tansform.xslt/( nd transform.xpath(), I would query deciion to use GatewayScript action.
    My rule of thumb on DataPower processing:
    For processing JSON or binary data, use GatewayScript., for processing XML data, use XSLT.

    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    ------------------------------



  • 3.  RE: DB Query to execute in Gateway Script

    Posted 03/08/20 03:40 AM
    Hi Hermann,

    Yes I am using Gateway script, is there any way we can run DB query via Gateway script.

    I need to user only the carnumber value from the result of DB query instead of complete nodeset, could you please help.

    ------------------------------
    Krishna Naik
    ------------------------------



  • 4.  RE: DB Query to execute in Gateway Script

    Posted 03/08/20 03:04 PM
    Edited by Krishna Naik 03/09/20 01:02 AM
    @Hermann Stamm-Wilbrandt

    Hi Hermann, Yes you are correct.

    We have request as Non xml example as below:
    0000000000000000000000000000000000000000I2GT IAS EXTRACT2018112920181128201811302.2
    721000539951851170082400401M20200209D0496C0000000000000007E0000000020181129
    721000549951851170082400401M20200209D0496C0000000000000007E0000000020181129

    Requirement is: Datapower has to read the input data, the card number present at each line, those card number need to be replaced with corresponding PCIID values from Database.

    Development: I have used Gateway script to read the incoming request, and base don regular expression I am extracting the card number, now I need to run DB query to get the corresponding values for that cardnumber and replace in file with that value.

    I am facing challenge now to read the result of the DB query which I have executed via xslt.tranform in Gateway script, I am able to execute the SQL stmt in XSLT and storing the value in context variable and then I am trying to read that value in Gateway script but i could see in the logs it says undefined (mpgw (Test_PCI): cardPICID:undefined) , could you please help me how to read the result in Gateway script

    My Gaeway Script is as below:

    var sm = require('service-metadata');
    var transform = require('transform');
    var ctx = session.name('Ctx') || session.createContext('Ctx');

    function uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
    });
    }

    session.input.readAsBuffers (function (error, bufs) {
    if (error) { session.reject(error); return; }
    else
    {
    var regex = /000[5-6][1-3][0-9]{14}/gm;
    var str = bufs.toString();
    var fistMatch = str.search(regex);
    var matchRes = str.match(regex);
    var splitRes = str.split(regex);

    var i = 0;
    var result = "";
    if (fistMatch != '0') {
    for (i = 0; i < matchRes.length; i++) {
    //DB Query to execute to get the cardPCIID, and to if there is no card pcid in the table pass the CARD value as PCID
    var CRD_NBR = matchRes[i].substr(3,16);
    ctx.setVariable('cardnumber', CRD_NBR);
    var options = {
    "location": "local:///KKM_DBqueryPCIID.xsl"

    };
    transform.xslt(options, function(error, nodelist) {
    if (error) {
    ctx.setVar('error', error);
    }
    else{
    return(nodelist);

    }
    });

    var cardID = ctx.getVariable('requestPCIID');
    //var cardPICID = ctx.getVar('var://context/ctx/requestPCIID');

    result += splitRes[i] + cardPICID;

    }
    }
    else {
    for (i = 0; i < matchRes.length; i++) {
    result += splitRes[i] + matchRes[i].substr(0,6) + "******" + matchRes[i].substr(13,19) + uuidv4(matchRes[i]);
    }
    }
    result += splitRes[splitRes.length - 1];
    //session.output.write(result);
    }

    });

    SQL - KKM_DBqueryPCIID.xsl:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:dpconfig="http://www.datapower.com/param/config"
    extension-element-prefixes="dp"
    exclude-result-prefixes="dp dpconfig">
    <xsl:template match="/">

    <xsl:variable name="CRDNBR" select="dp:variable('var://context/Ctx/cardnumber')" />


    <!--<xsl:variable name="myQuery" select="'SELECT * from PCI.PCICardMapping WHERE cardNumber = '5399518511700824'"/>-->
    <xsl:variable name="myQuery">
    SELECT "pciCardID" FROM "PCI"."PCICardMapping" WHERE "cardNumber" = <xsl:text>'</xsl:text><xsl:value-of select="$CRDNBR"/><xsl:text>'</xsl:text>
    </xsl:variable>
    <xsl:variable name="result">
    <xsl:copy-of select="dp:sql-execute('pcidpdatasource',$myQuery)"/>
    </xsl:variable>
    <xsl:variable name="cardPCIID">
    <xsl:value-of select="$result/sql/row/column[name='pciCardID']/value"/>
    </xsl:variable>
    <dp:set-variable name="'var://context/ctx/requestPCIID'" value="string($cardPCIID)"/>
    <xsl:value-of select="$cardPCIID"/>
    </xsl:template>
    </xsl:stylesheet>​

    ------------------------------
    Krishna Naik
    ------------------------------



  • 5.  RE: DB Query to execute in Gateway Script

    Posted 03/09/20 04:42 AM
    As I said, in GatewayScript you have to use transform.xslt() for SQL query, and should use transform.xpath() to extract data from XML.
    And it is better to do that with a XSLT transform action instead of Gatewayscript action.

    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    ------------------------------