Skip to content

Commit 2331cc6

Browse files
committed
Add in initial type conversion code. Ints and bools now come back as native json ints and bools
1 parent cd5ac36 commit 2331cc6

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

bits.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ Array.prototype.parse_multi_cstring = function () {
9292
return fields;
9393
};
9494

95-
95+
//// Example usage
96+
//var portal = "some_name";
97+
//var stream = ['E'.charCodeAt(0)].add_int32(4 + portal.length + 1 + 4).add_cstring(portal).add_int32(0);
98+
//p(stream);
99+
//// output [69,0,0,0,18,115,111,109,101,95,110,97,109,101,0,0,0,0,0]

postgres.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: false, plusplus: true, regexp: true, undef: true, white: true, indent: 2 */
1+
/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 2 */
22
/*globals include md5 node exports */
33

44
include('util.js');
@@ -15,7 +15,6 @@ Array.prototype.add_header = function (code) {
1515
return stream.concat(this);
1616
};
1717

18-
1918
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
2019
var formatter = {
2120
CopyData: function () {
@@ -76,7 +75,7 @@ var formatter = {
7675
}
7776
};
7877

79-
78+
// Parse response streams from the server
8079
function parse_response(code, stream) {
8180
var type, args;
8281
args = [];
@@ -150,17 +149,17 @@ function parse_response(code, stream) {
150149
break;
151150
case 'D':
152151
type = "DataRow";
153-
row = [];
152+
var data = [];
154153
var num_cols = stream.parse_int16();
155154
for (i = 0; i < num_cols; i += 1) {
156155
var size = stream.parse_int32();
157156
if (size === -1) {
158-
row.push(null);
157+
data.push(null);
159158
} else {
160-
row.push(stream.parse_raw_string(size));
159+
data.push(stream.parse_raw_string(size));
161160
}
162161
}
163-
args = [row];
162+
args = [data];
164163
break;
165164
case 'C':
166165
type = "CommandComplete";
@@ -173,6 +172,7 @@ function parse_response(code, stream) {
173172
return {type: type, args: args};
174173
}
175174

175+
176176
exports.Connection = function (database, username, password) {
177177

178178
var connection = node.tcp.createConnection(5432);
@@ -274,10 +274,25 @@ exports.Connection = function (database, username, password) {
274274
});
275275
events.addListener("DataRow", function (data) {
276276
var row = {};
277-
data.each_with_index(function (i, cell) {
277+
for (var i = 0, l = data.length; i < l; i += 1) {
278278
var description = row_description[i];
279-
row[description.field] = cell;
280-
});
279+
var value = data[i];
280+
if (value !== null) {
281+
// TODO: investigate to see if these numbers are stable across databases or
282+
// if we need to dynamically pull them from the pg_types table
283+
switch (description.type_id) {
284+
case 16: // bool
285+
value = value === 't';
286+
break;
287+
case 20: // int8
288+
case 21: // int2
289+
case 23: // int4
290+
value = parseInt(value, 10);
291+
break;
292+
}
293+
}
294+
row[description.field] = value;
295+
}
281296
results.push(row);
282297
});
283298
events.addListener('CommandComplete', function (data) {

0 commit comments

Comments
 (0)