Skip to content

Commit 461e674

Browse files
committed
make data conversion the same between native & javascript
1 parent 84569ab commit 461e674

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

lib/connection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ p.bind = function(config, more) {
154154
if(val === null || typeof val === "undefined") {
155155
buffer.addInt32(-1);
156156
} else {
157-
val = val.toString();
158157
buffer.addInt32(Buffer.byteLength(val));
159158
buffer.addString(val);
160159
}

lib/native/query.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var EventEmitter = require('events').EventEmitter;
22
var util = require('util');
33

4-
var types = require(__dirname + "/../types");
4+
var types = require(__dirname + '/../types');
5+
var utils = require(__dirname + '/../utils');
56

67
//event emitter proxy
78
var NativeQuery = function(text, values, callback) {
@@ -31,21 +32,7 @@ var NativeQuery = function(text, values, callback) {
3132
//normalize values
3233
if(this.values) {
3334
for(var i = 0, len = this.values.length; i < len; i++) {
34-
var item = this.values[i];
35-
switch(typeof item) {
36-
case 'undefined':
37-
this.values[i] = null;
38-
break;
39-
case 'object':
40-
this.values[i] = item === null ? null : JSON.stringify(item);
41-
break;
42-
case 'string':
43-
//value already string
44-
break;
45-
default:
46-
//numbers
47-
this.values[i] = item.toString();
48-
}
35+
this.values[i] = utils.prepareValue(this.values[i]);
4936
}
5037
}
5138

lib/query.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var EventEmitter = require('events').EventEmitter;
22
var util = require('util');
33

4-
var Result = require(__dirname + "/result");
5-
var Types = require(__dirname + "/types");
4+
var Result = require(__dirname + '/result');
5+
var Types = require(__dirname + '/types');
6+
var utils = require(__dirname + '/utils');
67

78
var Query = function(config) {
89
this.text = config.text;
@@ -129,9 +130,9 @@ p.prepare = function(connection) {
129130

130131
//TODO is there some better way to prepare values for the database?
131132
if(self.values) {
132-
self.values = self.values.map(function(val) {
133-
return (val instanceof Date) ? JSON.stringify(val) : val;
134-
});
133+
for(var i = 0, len = self.values.length; i < len; i++) {
134+
self.values[i] = utils.prepareValue(self.values[i]);
135+
}
135136
}
136137

137138
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

lib/utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,26 @@ var getLibpgConString = function(config, callback) {
9191
}
9292
}
9393

94+
//converts values from javascript types
95+
//to their 'raw' counterparts for use as a postgres parameter
96+
//note: you can override this function to provide your own conversion mechanism
97+
//for complex types, etc...
98+
var prepareValue = function(val) {
99+
if(val instanceof Date) {
100+
return JSON.stringify(val);
101+
}
102+
if(typeof val === 'undefined') {
103+
return null;
104+
}
105+
return val === null ? null : val.toString();
106+
}
107+
94108
module.exports = {
95109
normalizeConnectionInfo: normalizeConnectionInfo,
96110
//only exported here to make testing of this method possible
97111
//since it contains quite a bit of logic and testing for
98112
//each connection scenario in an integration test is impractical
99113
buildLibpqConnectionString: getLibpgConString,
100-
parseConnectionString: parseConnectionString
114+
parseConnectionString: parseConnectionString,
115+
prepareValue: prepareValue
101116
}

test/test-helper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ var args = require(__dirname + '/cli');
156156
if(args.binary) process.stdout.write(' (binary)');
157157
if(args.native) process.stdout.write(' (native)');
158158

159-
process.on('exit', console.log)
159+
process.on('exit', function() {
160+
console.log('')
161+
})
160162

161163
process.on('uncaughtException', function(err) {
162164
console.error("\n %s", err.stack || err.toString())

test/unit/connection/outbound-sending-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test('bind messages', function() {
9494
con.bind({
9595
portal: 'bang',
9696
statement: 'woo',
97-
values: [1, 'hi', null, 'zing']
97+
values: ['1', 'hi', null, 'zing']
9898
});
9999
var expectedBuffer = new BufferList()
100100
.addCString('bang') //portal name

0 commit comments

Comments
 (0)