diff --git a/lib/Connection.js b/lib/Connection.js index 490d3292..54d836e8 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -37,6 +37,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. */ /** @@ -48,6 +49,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 optional odbc connection instead of a creating a new one. */ /** diff --git a/lib/transports/odbcTransport.js b/lib/transports/odbcTransport.js index 46684a9d..f48abb6b 100644 --- a/lib/transports/odbcTransport.js +++ b/lib/transports/odbcTransport.js @@ -20,6 +20,7 @@ function odbcCall(config, xmlInput, done) { const odbc = require('odbc'); const { + odbcConnection = null, host = 'localhost', username = null, password = null, @@ -51,36 +52,52 @@ function odbcCall(config, xmlInput, done) { console.log(`SQL to run is ${sql}`); } - odbc.connect(connectionString, (connectError, connection) => { - if (connectError) { - done(connectError, null); + function processResults(results) { + if (!results) { + done('Empty result set was returned', null); return; } + + let xmlOutput = ''; + + results.forEach((chunk) => { + xmlOutput += chunk.OUT151; + }); + done(null, xmlOutput); + } + + function query(connection) { connection.query(sql, [ipc, ctl, xmlInput], (queryError, results) => { if (queryError) { done(queryError, null); return; } - connection.close((closeError) => { - if (closeError) { - done(closeError, null); - return; - } - - if (!results) { - done('Empty result set was returned', null); - return; - } + if (!odbcConnection) { + connection.close((closeError) => { + if (closeError) { + done(closeError, null); + return; + } - let xmlOutput = ''; - - results.forEach((chunk) => { - xmlOutput += chunk.OUT151; + processResults(results); }); - done(null, xmlOutput); - }); + } else { + processResults(results); + } }); - }); + } + + if (!odbcConnection) { + odbc.connect(connectionString, (connectError, connection) => { + if (connectError) { + done(connectError, null); + return; + } + query(connection); + }); + } else { + query(odbcConnection); + } } exports.odbcCall = odbcCall;