Skip to content

Commit 1fe5d73

Browse files
authored
Merge pull request #364 from sushantdhiman/bn-to-long
Bn.js to Long
2 parents cd2155f + ae7e5ca commit 1fe5d73

File tree

5 files changed

+73
-11
lines changed

5 files changed

+73
-11
lines changed

lib/packets/packet.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// var BigNumber = require("bignumber.js");
22
var ErrorCodeToName = require('../constants/errors.js');
33

4-
var bn = require('bn.js');
4+
var Long = require('long');
55

66
function Packet (id, buffer, start, end)
77
{
@@ -106,15 +106,19 @@ Packet.prototype.eofWarningCount = function () {
106106
return this.buffer.readInt16LE(this.offset + 1);
107107
};
108108

109-
Packet.prototype.readLengthCodedNumber = function (bigNumberStrings) {
109+
Packet.prototype.readLengthCodedNumber = function (bigNumberStrings, signed) {
110110
var byte1 = this.buffer[this.offset++];
111111
if (byte1 < 251) {
112112
return byte1;
113113
}
114-
return this.readLengthCodedNumberExt(byte1, bigNumberStrings);
114+
return this.readLengthCodedNumberExt(byte1, bigNumberStrings, signed);
115115
};
116116

117-
Packet.prototype.readLengthCodedNumberExt = function (tag, bigNumberStrings) {
117+
Packet.prototype.readLengthCodedNumberSigned = function (bigNumberStrings) {
118+
return this.readLengthCodedNumber(bigNumberStrings, true);
119+
};
120+
121+
Packet.prototype.readLengthCodedNumberExt = function (tag, bigNumberStrings, signed) {
118122
var word0, word1;
119123
var res;
120124
if (tag == 0xfb) {
@@ -142,9 +146,16 @@ Packet.prototype.readLengthCodedNumberExt = function (tag, bigNumberStrings) {
142146
return word1 * 0x100000000 + word0;
143147
}
144148

145-
res = (new bn(word1)).ishln(32).iaddn(word0);
149+
res = new Long(word0, word1, !signed); // Long need unsigned
150+
151+
var resNumber = res.toNumber()
152+
, resString = res.toString();
153+
154+
res = resNumber.toString() === resString
155+
? resNumber
156+
: resString;
146157

147-
return bigNumberStrings ? res.toString() : res;
158+
return bigNumberStrings ? resString : res;
148159
}
149160

150161
console.trace();

lib/packets/resultset_header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function ResultSetHeader (packet, bigNumberStrings)
99
} else {
1010
this.fieldCount = packet.readInt8(); // skip OK byte
1111
this.affectedRows = packet.readLengthCodedNumber(bigNumberStrings);
12-
this.insertId = packet.readLengthCodedNumber(bigNumberStrings);
12+
this.insertId = packet.readLengthCodedNumberSigned(bigNumberStrings);
1313
this.serverStatus = packet.readInt16();
1414
this.warningStatus = packet.readInt16();
1515
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
"author": "Andrey Sidorov <[email protected]>",
2929
"license": "MIT",
3030
"dependencies": {
31-
"bn.js": "4.11.6",
3231
"cardinal": "0.7.0",
3332
"double-ended-queue": "2.1.0-0",
33+
"long": "^3.2.0",
3434
"named-placeholders": "1.1.1",
3535
"readable-stream": "2.1.4",
3636
"seq-queue": "0.0.5",

test/integration/connection/test-insert-bigint.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var common = require('../../common');
22
var connection = common.createConnection();
33
var assert = require('assert');
4-
var bn = require('bn.js');
4+
var Long = require('long');
55

66
var table = 'insert_test';
77
connection.query([
@@ -26,10 +26,10 @@ connection.query('INSERT INTO bigs SET title=\'test1\'', function (err, result)
2626
// big int
2727
connection.query('INSERT INTO bigs SET title=\'test\', id=9007199254740992');
2828
connection.query('INSERT INTO bigs SET title=\'test3\'', function (err, result) {
29-
assert.strictEqual((new bn('9007199254740993')).cmp(result.insertId), 0);
29+
assert.strictEqual((Long.fromString('9007199254740993')).compare(result.insertId), 0);
3030
connection.query('INSERT INTO bigs SET title=\'test\', id=90071992547409924');
3131
connection.query('INSERT INTO bigs SET title=\'test4\'', function (err, result) {
32-
assert.strictEqual((new bn('90071992547409925')).cmp(result.insertId), 0);
32+
assert.strictEqual((Long.fromString('90071992547409925')).compare(result.insertId), 0);
3333
connection.query('select * from bigs', function (err, result) {
3434
assert.strictEqual(result[0].id, 123);
3535
assert.strictEqual(result[1].id, 124);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection();
3+
var assert = require('assert');
4+
5+
var testTable = 'neg-ai-test';
6+
var testData = 'test negative ai';
7+
8+
var selectResult, insertResult;
9+
10+
var prepareAndTest = function (cb) {
11+
connection.query(
12+
'CREATE TEMPORARY TABLE `' + testTable + '` (' +
13+
'`id` int(11) signed NOT NULL AUTO_INCREMENT,' +
14+
'`title` varchar(255),' +
15+
'PRIMARY KEY (`id`)' +
16+
') ENGINE=InnoDB DEFAULT CHARSET=utf8', testNegativeAI);
17+
};
18+
19+
var testNegativeAI = function (err) {
20+
assert.ifError(err);
21+
// insert the negative AI
22+
connection.query(
23+
'INSERT INTO `' + testTable + '`' +
24+
' (id, title) values (-999, "' + testData + '")'
25+
, function (err, result) {
26+
27+
assert.ifError(err);
28+
insertResult = result;
29+
30+
// select the row with negative AI
31+
connection.query('SELECT * FROM `' + testTable + '`' +
32+
' WHERE id = ' + result.insertId
33+
,function (err, result_) {
34+
35+
assert.ifError(err);
36+
selectResult = result_;
37+
38+
connection.end();
39+
});
40+
});
41+
};
42+
43+
prepareAndTest();
44+
45+
process.on('exit', function () {
46+
assert.strictEqual(insertResult.insertId, -999);
47+
assert.strictEqual(selectResult.length, 1);
48+
49+
assert.equal(selectResult[0].id, String(insertResult.insertId));
50+
assert.equal(selectResult[0].title, testData);
51+
});

0 commit comments

Comments
 (0)