|
| 1 | +// Copyright (c) International Business Machines Corp. 2019 |
| 2 | +// All Rights Reserved |
| 3 | + |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 5 | +// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 6 | +// including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 7 | +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 11 | +// substantial portions of the Software. |
| 12 | + |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 14 | +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 15 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 16 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +function odbcCall(config, xmlInput, done) { |
| 20 | + // eslint-disable-next-line global-require |
| 21 | + const { Connection } = require('odbc'); |
| 22 | + |
| 23 | + const { |
| 24 | + host = 'localhost', |
| 25 | + username = null, |
| 26 | + password = null, |
| 27 | + ipc = '*NA', |
| 28 | + ctl = '*here', |
| 29 | + xslib = 'QXMLSERV', |
| 30 | + verbose = false, |
| 31 | + dsn = null, |
| 32 | + } = config; |
| 33 | + |
| 34 | + const sql = `call ${xslib}.iPLUGR512K(?,?,?)`; |
| 35 | + const driver = 'IBM i Access ODBC Driver'; |
| 36 | + let connectionString; |
| 37 | + |
| 38 | + if (dsn && typeof dsn === 'string') { |
| 39 | + connectionString = `DSN=${dsn}`; |
| 40 | + } else { |
| 41 | + connectionString = `DRIVER=${driver};SYSTEM=${host};`; |
| 42 | + |
| 43 | + if (username && typeof username === 'string') { |
| 44 | + connectionString += `UID=${username};`; |
| 45 | + } |
| 46 | + if (password && typeof password === 'string') { |
| 47 | + connectionString += `PWD=${password};`; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (verbose) { |
| 52 | + console.log(`SQL to run is ${sql}`); |
| 53 | + } |
| 54 | + |
| 55 | + let connection; |
| 56 | + |
| 57 | + // potentially throws an error with invalid SYSTEM, UID, PWD |
| 58 | + try { |
| 59 | + connection = new Connection(connectionString); |
| 60 | + } catch (error) { |
| 61 | + done(error, null); |
| 62 | + } |
| 63 | + |
| 64 | + connection.query(sql, [ipc, ctl, xmlInput], (queryError, results) => { |
| 65 | + if (queryError) { |
| 66 | + done(queryError, null); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + connection.close((closeError) => { |
| 71 | + if (closeError) { |
| 72 | + done(closeError, null); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (!results) { |
| 77 | + done('Empty result set was returned', null); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + let xmlOutput = ''; |
| 82 | + |
| 83 | + results.forEach((chunk) => { |
| 84 | + xmlOutput += chunk.OUT151; |
| 85 | + }); |
| 86 | + |
| 87 | + done(null, xmlOutput); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +exports.odbcCall = odbcCall; |
0 commit comments