-
Notifications
You must be signed in to change notification settings - Fork 38
feat: The ability to use an existing odbc connection #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
}); | ||
}); | ||
} | ||
Comment on lines
+55
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be great to get this merged in. I would like to request some changes before merging this one in though. Instead of nesting the processResults and query functions maybe this should be split into one method because whether or not an existing connection is passed we would need to query xml service and process the results. The only difference would be if we should close the connection or not. If an existing connection was not passed close it otherwise don't call close. Checkout this branch with how I think this could be done Let me know If you have time to add the requested changes so we can fix this up otherwise I can pull these changes from this branch into a new PR and work on getting it merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abmusse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could make this a configurable transport option. That way the user could allow closing the connection object if desired. Would this be desirable for your application? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm flexible either way as long as a programmer is fully aware that it will close the connection on their behalf unless the parameter is set appropriately. In my case, I use the same connection to query results of a program call in the QTEMP library, and if it's closed I'll lose the reference to the job. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As written now, the consuming application would not need to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it! Thanks! |
||
|
||
if (!odbcConnection) { | ||
odbc.connect(connectionString, (connectError, connection) => { | ||
if (connectError) { | ||
done(connectError, null); | ||
return; | ||
} | ||
query(connection); | ||
}); | ||
} else { | ||
query(odbcConnection); | ||
} | ||
} | ||
|
||
exports.odbcCall = odbcCall; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 52 documents the idb transport options therefore we should not add odbcConnection property here.