Skip to content

Commit 8c24c9a

Browse files
committed
added support for using an existing node-odbc connection
Signed-off-by: Eric Henson <ehenson238@gmail.com>
1 parent 0fedfea commit 8c24c9a

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

lib/transports/odbcTransport.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function odbcCall(config, xmlInput, done) {
2020
const odbc = require('odbc');
2121

2222
const {
23+
odbcConnection = null,
2324
host = 'localhost',
2425
username = null,
2526
password = null,
@@ -51,36 +52,52 @@ function odbcCall(config, xmlInput, done) {
5152
console.log(`SQL to run is ${sql}`);
5253
}
5354

54-
odbc.connect(connectionString, (connectError, connection) => {
55-
if (connectError) {
56-
done(connectError, null);
55+
function processXml(results) {
56+
if (!results) {
57+
done('Empty result set was returned', null);
5758
return;
5859
}
60+
61+
let xmlOutput = '';
62+
63+
results.forEach((chunk) => {
64+
xmlOutput += chunk.OUT151;
65+
});
66+
done(null, xmlOutput);
67+
}
68+
69+
function query(connection) {
5970
connection.query(sql, [ipc, ctl, xmlInput], (queryError, results) => {
6071
if (queryError) {
6172
done(queryError, null);
6273
return;
6374
}
64-
connection.close((closeError) => {
65-
if (closeError) {
66-
done(closeError, null);
67-
return;
68-
}
69-
70-
if (!results) {
71-
done('Empty result set was returned', null);
72-
return;
73-
}
75+
if (!odbcConnection) {
76+
connection.close((closeError) => {
77+
if (closeError) {
78+
done(closeError, null);
79+
return;
80+
}
7481

75-
let xmlOutput = '';
76-
77-
results.forEach((chunk) => {
78-
xmlOutput += chunk.OUT151;
82+
processXml(results);
7983
});
80-
done(null, xmlOutput);
81-
});
84+
} else {
85+
processXml(results);
86+
}
8287
});
83-
});
88+
}
89+
90+
if (!odbcConnection) {
91+
odbc.connect(connectionString, (connectError, connection) => {
92+
if (connectError) {
93+
done(connectError, null);
94+
return;
95+
}
96+
query(connection);
97+
});
98+
} else {
99+
query(odbcConnection);
100+
}
84101
}
85102

86103
exports.odbcCall = odbcCall;

0 commit comments

Comments
 (0)