Skip to content
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

add bigint detection and sets mssql req.input when appropriate #1445

Merged
merged 5 commits into from
May 29, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/dialects/mssql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ assign(Client_MSSQL.prototype, {
// Grab a connection, run the query via the MSSQL streaming interface,
// and pass that through to the stream we've sent back to the client.
_stream(connection, obj, stream, options) {
const client = this;
options = options || {}
if (!obj || typeof obj === 'string') obj = {sql: obj}
// convert ? params into positional bindings (@p1)
Expand All @@ -104,7 +105,7 @@ assign(Client_MSSQL.prototype, {
req.stream = true;
if (obj.bindings) {
for (let i = 0; i < obj.bindings.length; i++) {
req.input(`p${i}`, obj.bindings[i])
client._setReqInput(req, i, obj.bindings[i])
}
}
req.pipe(stream)
Expand All @@ -115,6 +116,7 @@ assign(Client_MSSQL.prototype, {
// Runs the query on the specified connection, providing the bindings
// and any other necessary prep work.
_query(connection, obj) {
const client = this;
if (!obj || typeof obj === 'string') obj = {sql: obj}
// convert ? params into positional bindings (@p1)
obj.sql = this.positionBindings(obj.sql);
Expand All @@ -127,7 +129,7 @@ assign(Client_MSSQL.prototype, {
req.multiple = true;
if (obj.bindings) {
for (let i = 0; i < obj.bindings.length; i++) {
req.input(`p${i}`, obj.bindings[i])
client._setReqInput(req, i, obj.bindings[i])
}
}
req.query(sql, function(err, recordset) {
Expand All @@ -138,6 +140,18 @@ assign(Client_MSSQL.prototype, {
})
},

// sets a request input parameter. Detects bigints and sets type appropriately.
_setReqInput(req, i, binding) {
if (typeof binding == 'number' && (binding < -2147483648 || binding > 2147483647)) {
if (binding < -9007199254740991 || binding > 9007199254740991) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice for the code to give names for the magic numbers used e.g. MIN/MAX_INT4 MIN/MAX_SAFE_INTEGER. Anyways we are using babel to compile sources to ES5 so I suppose Number.MAX_SAFE_INTEGER / Number.isSafeInteger() should work too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for the constants, isSafeInteger wasn't transpiled or polyfiled (I guess this is a matter of babel profile and plugin) Maybe constants would be the most compatible approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sounds good.

Copy link
Contributor Author

@sandorfr sandorfr May 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will do soon and add some unit tests.

throw new Error(`Bigint must be safe integer or must be passed as string, saw ${binding}`)
}
req.input(`p${i}`, this.driver.BigInt, binding)
} else {
req.input(`p${i}`, binding)
}
},

// Process the response as returned from the query.
processResponse(obj, runner) {
if (obj == null) return;
Expand Down