Giter Club home page Giter Club logo

Comments (7)

abmusse avatar abmusse commented on July 17, 2024

I think we are done here, Marking it as resolved.

Thanks!

from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

@brianmjerome Good Catch, should be:

#!js
...

else if (typeof value === 'number') { //Number
      let indicator;
      Number.isInteger(value) ? indicator = idbp.SQL_BIND_INT : indicator = idbp.SQL_BIND_NUMERIC;
      boundParams.push( [value, io, indicator] );
}

...

OR:

else if (typeof value === 'number') { //Number
      boundParams.push( [value, io, idbp[`SQL_BIND_${Number.isInteger(value) ? 'INT' : 'NUMERIC'}`] ] );
}


from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

Original comment by Brian Jerome (Bitbucket: bjerome, GitHub: brianmjerome).


@abmusse Looks good except I see the indicator isn't really being used. Can that evaluation be done directly on the following line in the push?

from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

OK I have touched things up to support allowing more flexibility for prepareExecute(sql, params, options);

Added an options object as a parameter to allow for overriding the default io indicator for all params passed.
You can now change the default io indicator globally (INPUTOUTPUT) by setting prepareExecute(sql, params, {io: 'in | out'})

or you can do so at an individual parameter level by passing an object within the parameter list.

the object format: {value: "string | Number | boolean | null" , io: "in | out | both" , asClob: "true | false"}

For example:

#!js
const {DBPool} = require('idb-pconnector');
const pool = new DBPool({url: '*LOCAL'}, {debug: true});

(async () => {
  let sql = 'call QXMLSERV.iPLUG512K(?,?,?,?)',
    xmlIn = '<xmlservice><sh>system "wrksbs"<\/sh><\/xmlservice>',
    xmlOut = {asClob: true, value: '', io: 'out'},
    params = ['*NA','*here', xmlIn, xmlOut],
    results = await pool.prepareExecute(sql, params, {io: 'in'});

  console.log(results);
})();

formatParams is now called from a higher level function setupParams(params, options)

#!js

  setupParams(params, options) {
    let boundParams = [],
      me = this,
      {io} = options,
      parameter;

    io = io || 'both';

    for ( parameter of params  ){
      if (typeof parameter === 'object'){
        let {value, asClob = false} = parameter;

        if (typeof value === 'undefined' ){
          throw Error('The parameter object must define a value property');
        }
        // assigning the value of io from the parameter object to the io variable declared before.
        // if io from parameter object is undefined default back to value of io from before.
        ({io = io} = parameter);
        //allows customization of io for an individual parameter.
        me.formatParams(boundParams, value, {io, asClob});
      } else {
        // when just passing values but would like to overide default io (INPUT_OUTPUT)
        me.formatParams(boundParams, parameter, {io});
      }
    }
    me.log(`Size of BoundParams: ${boundParams.length} \n ${JSON.stringify(boundParams)}`);
    return boundParams;
  }

  formatParams(boundParams, value, options){
    let {io, asClob} = options;

    io = idbp[`SQL_PARAM_${io === 'both' ? 'INPUT_OUTPUT': (io.toUpperCase() + 'PUT')}`];

    if (typeof value === 'string'){ //String
      if (asClob){ //as clob
        boundParams.push([value, io, idbp.BIND_CLOB]);
      } else { // as string
        boundParams.push([value, io, idbp.BIND_STRING]);
      }
    } else if (typeof value === 'number') { //Number
      let indicator;
      Number.isInteger(value) ? indicator = idbp.SQL_BIND_INT : indicator = idbp.SQL_BIND_NUMERIC;
      boundParams.push( [value, io, idbp.BIND_INT] );
    } else if (value === null) { //Null
      boundParams.push([value, io, idbp.SQL_BIND_NULL_DATA]);
    } else if (Buffer.isBuffer(value)){ //Binary/blob
      boundParams.push([value, io, idbp.SQL_BIND_BINARY]);
    } else if (typeof value === 'boolean'){ //Boolean
      boundParams.push([value, io, idbp.SQL_BIND_BOOLEAN]);
    } else {
      me.log(`Parameter that caused error was ${JSON.stringify(value)}`);
      throw TypeError('Parameters to bind should be String, Number, null, boolean, or Buffer');
    }
  }

from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

Original comment by Brian Jerome (Bitbucket: bjerome, GitHub: brianmjerome).


Take it one step further and let the object also specify the io :-) Could default io to both if typeof io === undefined.

#!javascript
{ value: 'some string', io: 'in', asClob: true }

...
let io = parameter.io || 'both';
boundParams.push([parameter, idbp[`SQL_PARAM_${io === 'both' ? 'INPUT_OUTPUT' : (io.toUpperCase() + 'PUT')}`], bindType);

from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

The main use case for me would be when calling XMLSERVICE stored procedure which requires the xmlin and xmlout parameters to be bound as a CLOB.

I guess we can allow the array of a parameters to contain an object with the format: { asClob: true, value: 'some string'}.

BTW I noticed an issue with the null condition,

#!js

else if (typeof parameter === null) { //Null
        boundParams.push([parameter, SQL_PARAM_INPUT_OUTPUT, idbp.SQL_BIND_NULL_DATA]);
      }

when you run typeof(null) 'object' is returned, instead this line should be:

#!js

else if (parameter === null) { //Null
        boundParams.push([parameter, SQL_PARAM_INPUT_OUTPUT, idbp.SQL_BIND_NULL_DATA]);
      }

from nodejs-idb-pconnector.

abmusse avatar abmusse commented on July 17, 2024

Original comment by Brian Jerome (Bitbucket: bjerome, GitHub: brianmjerome).


Should we allow the user to pass in specific types? I'm not sure when it'd need to be a CLOB if not the other types.

from nodejs-idb-pconnector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.