From f827f56ed27800c40a8372448ad6cccc19d02815 Mon Sep 17 00:00:00 2001 From: sevastos Date: Thu, 23 May 2013 20:10:08 +0300 Subject: [PATCH] BigInt parsing bullet-proofing --- lib/types/binaryParsers.js | 8 +++----- lib/types/textParsers.js | 8 +++++++- package.json | 3 ++- test/integration/client/type-coercion-tests.js | 2 +- test/unit/client/typed-query-results-tests.js | 8 ++++---- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/types/binaryParsers.js b/lib/types/binaryParsers.js index ca55cc4..e47ab1e 100644 --- a/lib/types/binaryParsers.js +++ b/lib/types/binaryParsers.js @@ -1,3 +1,5 @@ +var ref = require('ref'); + var parseBits = function(data, bits, offset, invert, callback) { offset = offset || 0; invert = invert || false; @@ -106,11 +108,7 @@ var parseInt32 = function(value) { }; var parseInt64 = function(value) { - if (parseBits(value, 1) == 1) { - return -1 * (parseBits(value, 63, 1, true) + 1); - } - - return parseBits(value, 63, 1); + return String(ref.readInt64BE(value, 0)); }; var parseFloat32 = function(value) { diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index 558ce73..1e76dd9 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -162,8 +162,14 @@ var parseInteger = function(val) { return parseInt(val, 10); }; +var parseBigInteger = function(val) { + var valStr = String(val); + if (/^\d+$/.test(valStr)) { return valStr; } + return val; +}; + var init = function(register) { - register(20, parseInteger); + register(20, parseBigInteger); register(21, parseInteger); register(23, parseInteger); register(26, parseInteger); diff --git a/package.json b/package.json index d78afbb..b6689ec 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "main": "./lib", "dependencies": { "generic-pool": "2.0.3", - "buffer-writer": "1.0.0" + "buffer-writer": "1.0.0", + "ref": "0.1.3" }, "devDependencies": { "jshint": "1.1.0", diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 61204cf..cc61da5 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -46,7 +46,7 @@ var types = [{ values: [-1, 0, 1, null] },{ name: 'bigint', - values: [-10000, 0, 10000, null] + values: ['-9223372036854775808', '-9007199254740992', '0', '72057594037928030', '9007199254740992', '9223372036854775807', null] },{ name: 'varchar(5)', values: ['yo', '', 'zomg!', null] diff --git a/test/unit/client/typed-query-results-tests.js b/test/unit/client/typed-query-results-tests.js index ab48137..89f79b3 100644 --- a/test/unit/client/typed-query-results-tests.js +++ b/test/unit/client/typed-query-results-tests.js @@ -30,8 +30,8 @@ test('typed results', function() { name: 'bigint/int8', format: 'text', dataTypeID: 20, - actual: '102', - expected: 102 + actual: '9223372036854775807', + expected: '9223372036854775807' },{ name: 'oid', format: 'text', @@ -222,13 +222,13 @@ test('typed results', function() { format: 'binary', dataTypeID: 20, actual: [0, 0, 0, 0, 0, 0, 0, 102], - expected: 102 + expected: '102' },{ name: 'binary-bigint/int8-full', format: 'binary', dataTypeID: 20, actual: [1, 0, 0, 0, 0, 0, 0, 102], - expected: 72057594037928030 + expected: '72057594037928038' },{ name: 'binary-oid', format: 'binary',