From 5755948050da898fb26c801712254e0c2c2e250b Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Fri, 15 Jan 2021 10:50:04 -0600 Subject: [PATCH 1/2] feat: Ability to pass in odbc connection to transport --- lib/transports/odbcTransport.js | 128 +++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 43 deletions(-) diff --git a/lib/transports/odbcTransport.js b/lib/transports/odbcTransport.js index f1d508e..da360e0 100644 --- a/lib/transports/odbcTransport.js +++ b/lib/transports/odbcTransport.js @@ -13,6 +13,62 @@ try { } } +/** + * Function to call xmlservice regardless if an existing connection was passed + * @private + * @param {Object} parameters - Arguments to call xml service procedure + */ +function callXmlService(parameters) { + const { + connection, + sql, + queryParameters, + done, + shouldCloseConnection, + verbose, + } = parameters; + + if (verbose) { + console.log(`SQL to run is ${sql}`); + } + + connection.query(sql, queryParameters, (queryError, results) => { + if (queryError) { + done(queryError, null); + return; + } + let xmlOutput = ''; + + if (!results) { + done('Empty result set was returned', null); + return; + } + + results.forEach((chunk) => { + xmlOutput += chunk.OUT151; + }); + + if (shouldCloseConnection) { + connection.close((closeError) => { + if (closeError) { + done(closeError, null); + return; + } + done(null, xmlOutput); + }); + } else { + done(null, xmlOutput); + } + }); +} + +/** + * @private + * @param {object} config - Configuration options for odbc transport + * @param {string} xmlIn - The xml input to run with xml service + * @param {function} done - User defined callback to invoke when completed + */ + function odbcCall(config, xmlInput, done) { // odbc transport is not available bail out if (odbc === null) { @@ -28,59 +84,45 @@ function odbcCall(config, xmlInput, done) { xslib = 'QXMLSERV', verbose = false, dsn = null, + odbcConnection = null, } = config; const sql = `call ${xslib}.iPLUGR512K(?,?,?)`; - const driver = 'IBM i Access ODBC Driver'; - let connectionString; + const queryParameters = [ipc, ctl, xmlInput]; + const parameters = { + sql, queryParameters, done, verbose, shouldCloseConnection: !odbcConnection, + }; - if (dsn && typeof dsn === 'string') { - connectionString = `DSN=${dsn}`; - } else { - connectionString = `DRIVER=${driver};SYSTEM=${host};`; + if (!odbcConnection) { // get a connection then call xml service + // generate connection string + let connectionString; - if (username && typeof username === 'string') { - connectionString += `UID=${username};`; - } - if (password && typeof password === 'string') { - connectionString += `PWD=${password};`; - } - } - - if (verbose) { - console.log(`SQL to run is ${sql}`); - } + if (dsn && typeof dsn === 'string') { + connectionString = `DSN=${dsn}`; + } else { + connectionString = `DRIVER=IBM i Access ODBC Driver;SYSTEM=${host};`; - odbc.connect(connectionString, (connectError, connection) => { - if (connectError) { - done(connectError, null); - return; + if (username && typeof username === 'string') { + connectionString += `UID=${username};`; + } + if (password && typeof password === 'string') { + connectionString += `PWD=${password};`; + } } - connection.query(sql, [ipc, ctl, xmlInput], (queryError, results) => { - if (queryError) { - done(queryError, null); + + // connect and call xmlservice + odbc.connect(connectionString, (connectError, connection) => { + if (connectError) { + done(connectError, null); return; } - connection.close((closeError) => { - if (closeError) { - done(closeError, null); - return; - } - - if (!results) { - done('Empty result set was returned', null); - return; - } - - let xmlOutput = ''; - - results.forEach((chunk) => { - xmlOutput += chunk.OUT151; - }); - done(null, xmlOutput); - }); + parameters.connection = connection; + callXmlService(parameters); }); - }); + } else { // passed in connection should already be connected + parameters.connection = odbcConnection; + callXmlService(parameters); + } } exports.odbcCall = odbcCall; From 5e3a77fcac9a400e3fd5e5feb52544d8b3c81236 Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Fri, 15 Jan 2021 10:56:29 -0600 Subject: [PATCH 2/2] doc: Add odbcConnection prop to odbc transport options --- lib/Connection.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Connection.js b/lib/Connection.js index dc9ea05..ca076c7 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -23,6 +23,7 @@ const availableTransports = { * @property {string} [ipc=*NA] - The key name/security route to XMLSERVICE job. Default is ``*NA``. * @property {string} [ctl=*here] - The control options for XMLSERVICE jobs. Default is ``*here``. * @property {string} [xslib=QXMLSERV] - The XMLSERVICE library. Default is ``QXMLSERV``. + * @property {object} [odbcConnection] - An existing odbc connection already connected to the db. */ /**