make data conversion the same between native & javascript
This commit is contained in:
parent
e4a5f2edad
commit
fa80b4e3fa
@ -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);
|
||||
}
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
11
lib/query.js
11
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
|
||||
|
17
lib/utils.js
17
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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user