Comments (7)
I think we are done here, Marking it as resolved.
Thanks!
from nodejs-idb-pconnector.
@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.
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.
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.
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.
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.
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)
- setLibraryList and sql user defined functions HOT 5
- dbpool failed to detach HOT 19
- Run linter during PR HOT 1
- Fix eslint errors
- Max number of simultaneous connections HOT 8
- Drop standard-version as its now deprecated HOT 2
- Calling Store Proc passing well IN parms , not getting OUT parm HOT 2
- DBPool example is incomplete.. HOT 1
- Please make more clear how to call a Stored Procedure HOT 7
- No clear explanation on how to call stored procedure. Not receiving output.
- More examples HOT 5
- bindParam() is deprecated and you should use bindParameters() instead HOT 8
- Fix copyright headers on source files
- Add package-lock.json HOT 1
- DBPool 'new connection' event emitter issues HOT 1
- build: Add standard-version HOT 1
- Migrate docs to readthedocs HOT 1
- Add exempt-pr-label
- DBPool.prepareExecute should provide a way to enable numeric type conversion HOT 7
- fetchAll keys lower case HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nodejs-idb-pconnector.