make data conversion the same between native & javascript

This commit is contained in:
brianc 2012-07-05 22:08:18 -05:00
parent e4a5f2edad
commit fa80b4e3fa
5 changed files with 26 additions and 24 deletions

View File

@ -154,7 +154,6 @@ p.bind = function(config, more) {
if(val === null || typeof val === "undefined") { if(val === null || typeof val === "undefined") {
buffer.addInt32(-1); buffer.addInt32(-1);
} else { } else {
val = val.toString();
buffer.addInt32(Buffer.byteLength(val)); buffer.addInt32(Buffer.byteLength(val));
buffer.addString(val); buffer.addString(val);
} }

View File

@ -1,7 +1,8 @@
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var util = require('util'); var util = require('util');
var types = require(__dirname + "/../types"); var types = require(__dirname + '/../types');
var utils = require(__dirname + '/../utils');
//event emitter proxy //event emitter proxy
var NativeQuery = function(text, values, callback) { var NativeQuery = function(text, values, callback) {
@ -31,21 +32,7 @@ var NativeQuery = function(text, values, callback) {
//normalize values //normalize values
if(this.values) { if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) { for(var i = 0, len = this.values.length; i < len; i++) {
var item = this.values[i]; this.values[i] = utils.prepareValue(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();
}
} }
} }

View File

@ -1,8 +1,9 @@
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var util = require('util'); var util = require('util');
var Result = require(__dirname + "/result"); var Result = require(__dirname + '/result');
var Types = require(__dirname + "/types"); var Types = require(__dirname + '/types');
var utils = require(__dirname + '/utils');
var Query = function(config) { var Query = function(config) {
this.text = config.text; this.text = config.text;
@ -129,9 +130,9 @@ p.prepare = function(connection) {
//TODO is there some better way to prepare values for the database? //TODO is there some better way to prepare values for the database?
if(self.values) { if(self.values) {
self.values = self.values.map(function(val) { for(var i = 0, len = self.values.length; i < len; i++) {
return (val instanceof Date) ? JSON.stringify(val) : val; self.values[i] = utils.prepareValue(self.values[i]);
}); }
} }
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY //http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

View File

@ -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 = { module.exports = {
normalizeConnectionInfo: normalizeConnectionInfo, normalizeConnectionInfo: normalizeConnectionInfo,
//only exported here to make testing of this method possible //only exported here to make testing of this method possible
//since it contains quite a bit of logic and testing for //since it contains quite a bit of logic and testing for
//each connection scenario in an integration test is impractical //each connection scenario in an integration test is impractical
buildLibpqConnectionString: getLibpgConString, buildLibpqConnectionString: getLibpgConString,
parseConnectionString: parseConnectionString parseConnectionString: parseConnectionString,
prepareValue: prepareValue
} }

View File

@ -94,7 +94,7 @@ test('bind messages', function() {
con.bind({ con.bind({
portal: 'bang', portal: 'bang',
statement: 'woo', statement: 'woo',
values: [1, 'hi', null, 'zing'] values: ['1', 'hi', null, 'zing']
}); });
var expectedBuffer = new BufferList() var expectedBuffer = new BufferList()
.addCString('bang') //portal name .addCString('bang') //portal name