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

BigInt as Parameter works, but does this look right? #490

Closed
beautifulcoder opened this issue Jan 9, 2017 · 4 comments
Closed

BigInt as Parameter works, but does this look right? #490

beautifulcoder opened this issue Jan 9, 2017 · 4 comments

Comments

@beautifulcoder
Copy link

beautifulcoder commented Jan 9, 2017

Hello, when I add a BigInt type as a parameter I get weird behavior.

Say:

commandBigInt.addParameter('Id', TYPES.BigInt, '9223372036854775807');

SQL Server tells me that a -9223372036854775808 was put in this parameter. This seems weird because it is a string type in JavaScript.

But, when I do this instead:

commandBigInt.addParameter('Id', TYPES.VarChar, '9223372036854775806');

It works! But, Tedious requires a VarChar type? I get that it has to be a string in JavaScript, but why in Tedious?

@tvrprasad
Copy link
Contributor

Can you please put together a complete script? I can take a look. Thanks.

@beautifulcoder
Copy link
Author

beautifulcoder commented Jan 10, 2017

You bet.

Create a basic table with a BitInt.

CREATE DATABASE FunWithBigIntInNode;
GO

USE FunWithBigIntInNode;
GO

CREATE TABLE dbo.BigIntInNodeTest (
Id BIGINT NOT NULL,
IsAtMax BIT NOT NULL,
CONSTRAINT PK_BigIntInNodeTest_Id PRIMARY KEY(Id));
GO

Then, use this script:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var config = require('./sqlConfig');

var connection = new Connection(config);

var commandBigInt = new Request('dbo.InsertBigInt', function onRequest() {
connection.close();
});

commandBigInt.addParameter('Id', TYPES.BigInt, '9223372036854775807');
commandBigInt.addParameter('IsAtMax', TYPES.Bit, true);

connection.on('connect', function onConnect() {
connection.callProcedure(commandBigInt);
});

sqlConfig is a JSON key / value pair file, as such:

{
"userName": "",
"password": "",
"server": "",
"options": {
"database": "FunWithBigIntInNode",
"encrypt": true
}
}

@tvrprasad
Copy link
Contributor

@beautifulcoder Got around to checking this out. This is essentially a JavaScript Numeric range limitation. You lose precision outside the range -(253 - 1) and (253 - 1). Using VarChar for values outside this range is the right behavior.

I sent a PR to validate range on BigInt - #504.

@tvrprasad
Copy link
Contributor

@beautifulcoder This change is now merged. Happy coding :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants