From e744d05df7da2680fa3276624a54800a7c5d8311 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Thu, 29 Aug 2013 00:04:27 -0500 Subject: [PATCH] Add ability to opt-in to int8 parsing Switching the result of all COUNT operations to a string is a pretty nasty breaking change, and the majority of us aren't going to be hitting numbers larger than Number.MAX_VALUE --- lib/defaults.js | 7 ++++++- test/integration/client/parse-int-8-tests.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/integration/client/parse-int-8-tests.js diff --git a/lib/defaults.js b/lib/defaults.js index d7fe17c..e49006f 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -1,4 +1,4 @@ -module.exports = { +var defaults = module.exports = { // database host defaults to localhost host: 'localhost', @@ -38,3 +38,8 @@ module.exports = { client_encoding: "" }; + +//parse int8 so you can get your count values as actual numbers +module.exports.__defineSetter__("parseInt8", function(val) { + require('./types').setTypeParser(20, 'text', val ? parseInt : function(val) { return val; }); +}); diff --git a/test/integration/client/parse-int-8-tests.js b/test/integration/client/parse-int-8-tests.js new file mode 100644 index 0000000..7028e90 --- /dev/null +++ b/test/integration/client/parse-int-8-tests.js @@ -0,0 +1,18 @@ + +var helper = require(__dirname + '/../test-helper'); +var pg = helper.pg; +test('ability to turn on and off parser', function() { + if(helper.args.binary) return false; + pg.connect(helper.config, assert.success(function(client, done) { + pg.defaults.parseInt8 = true; + client.query('CREATE TEMP TABLE asdf(id SERIAL PRIMARY KEY)'); + client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) { + pg.defaults.parseInt8 = false; + client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) { + done(); + assert.strictEqual("0", res.rows[0].count); + pg.end(); + })); + })); + })); +});