Skip to content

Commit

Permalink
[odbcTransport.js] Add odbc transport
Browse files Browse the repository at this point in the history
  • Loading branch information
abmusse committed Jun 6, 2019
1 parent f7b8fd8 commit 7956a15
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions lib/transports/odbcTransport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function odbcCall(config, xmlInput, done) {
// eslint-disable-next-line global-require
const { Connection } = require('odbc');

const {
host = 'localhost',
username = null,
password = null,
ipc = '*NA',
ctl = '*here',
xslib = 'QXMLSERV',
verbose = false,
dsn = null,
} = config;

const sql = `call ${xslib}.iPLUGR512K(?,?,?)`;
const driver = 'IBM i Access ODBC Driver';
let connectionString;

if (dsn && typeof dsn === 'string') {
connectionString = `DSN=${dsn}`;
} else {
connectionString = `DRIVER=${driver};SYSTEM=${host};`;

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}`);
}

let connection;

// potentially throws an error with invalid SYSTEM, UID, PWD
try {
connection = new Connection(connectionString);
} catch (error) {
done(error, null);
}

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;
}

let xmlOutput = '';

results.forEach((chunk) => {
xmlOutput += chunk.OUT151;
});

done(null, xmlOutput);
});
});
}

exports.odbcCall = odbcCall;

0 comments on commit 7956a15

Please sign in to comment.