From fa80b4e3fa7ca1efb477e645cf407176993709ca Mon Sep 17 00:00:00 2001 From: brianc Date: Thu, 5 Jul 2012 22:08:18 -0500 Subject: [PATCH] make data conversion the same between native & javascript --- lib/connection.js | 1 - lib/native/query.js | 19 +++---------------- lib/query.js | 11 ++++++----- lib/utils.js | 17 ++++++++++++++++- .../unit/connection/outbound-sending-tests.js | 2 +- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 140cbf3..de0d6fc 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -154,7 +154,6 @@ p.bind = function(config, more) { if(val === null || typeof val === "undefined") { buffer.addInt32(-1); } else { - val = val.toString(); buffer.addInt32(Buffer.byteLength(val)); buffer.addString(val); } diff --git a/lib/native/query.js b/lib/native/query.js index 4473100..3d515d7 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -1,7 +1,8 @@ var EventEmitter = require('events').EventEmitter; var util = require('util'); -var types = require(__dirname + "/../types"); +var types = require(__dirname + '/../types'); +var utils = require(__dirname + '/../utils'); //event emitter proxy var NativeQuery = function(text, values, callback) { @@ -31,21 +32,7 @@ var NativeQuery = function(text, values, callback) { //normalize values if(this.values) { for(var i = 0, len = this.values.length; i < len; i++) { - var item = this.values[i]; - switch(typeof item) { - case 'undefined': - this.values[i] = null; - break; - case 'object': - this.values[i] = item === null ? null : JSON.stringify(item); - break; - case 'string': - //value already string - break; - default: - //numbers - this.values[i] = item.toString(); - } + this.values[i] = utils.prepareValue(this.values[i]); } } diff --git a/lib/query.js b/lib/query.js index c7fadee..3e0311a 100644 --- a/lib/query.js +++ b/lib/query.js @@ -1,8 +1,9 @@ var EventEmitter = require('events').EventEmitter; var util = require('util'); -var Result = require(__dirname + "/result"); -var Types = require(__dirname + "/types"); +var Result = require(__dirname + '/result'); +var Types = require(__dirname + '/types'); +var utils = require(__dirname + '/utils'); var Query = function(config) { this.text = config.text; @@ -129,9 +130,9 @@ p.prepare = function(connection) { //TODO is there some better way to prepare values for the database? if(self.values) { - self.values = self.values.map(function(val) { - return (val instanceof Date) ? JSON.stringify(val) : val; - }); + for(var i = 0, len = self.values.length; i < len; i++) { + self.values[i] = utils.prepareValue(self.values[i]); + } } //http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY diff --git a/lib/utils.js b/lib/utils.js index 0b98e99..07a3792 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -91,11 +91,26 @@ var getLibpgConString = function(config, callback) { } } +//converts values from javascript types +//to their 'raw' counterparts for use as a postgres parameter +//note: you can override this function to provide your own conversion mechanism +//for complex types, etc... +var prepareValue = function(val) { + if(val instanceof Date) { + return JSON.stringify(val); + } + if(typeof val === 'undefined') { + return null; + } + return val === null ? null : val.toString(); +} + module.exports = { normalizeConnectionInfo: normalizeConnectionInfo, //only exported here to make testing of this method possible //since it contains quite a bit of logic and testing for //each connection scenario in an integration test is impractical buildLibpqConnectionString: getLibpgConString, - parseConnectionString: parseConnectionString + parseConnectionString: parseConnectionString, + prepareValue: prepareValue } diff --git a/test/unit/connection/outbound-sending-tests.js b/test/unit/connection/outbound-sending-tests.js index d711a9a..731a46d 100644 --- a/test/unit/connection/outbound-sending-tests.js +++ b/test/unit/connection/outbound-sending-tests.js @@ -94,7 +94,7 @@ test('bind messages', function() { con.bind({ portal: 'bang', statement: 'woo', - values: [1, 'hi', null, 'zing'] + values: ['1', 'hi', null, 'zing'] }); var expectedBuffer = new BufferList() .addCString('bang') //portal name