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 */
2
2
/*globals include md5 node exports */
3
3
4
4
include ( 'util.js' ) ;
@@ -15,7 +15,6 @@ Array.prototype.add_header = function (code) {
15
15
return stream . concat ( this ) ;
16
16
} ;
17
17
18
-
19
18
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
20
19
var formatter = {
21
20
CopyData : function ( ) {
@@ -76,7 +75,7 @@ var formatter = {
76
75
}
77
76
} ;
78
77
79
-
78
+ // Parse response streams from the server
80
79
function parse_response ( code , stream ) {
81
80
var type , args ;
82
81
args = [ ] ;
@@ -150,17 +149,17 @@ function parse_response(code, stream) {
150
149
break ;
151
150
case 'D' :
152
151
type = "DataRow" ;
153
- row = [ ] ;
152
+ var data = [ ] ;
154
153
var num_cols = stream . parse_int16 ( ) ;
155
154
for ( i = 0 ; i < num_cols ; i += 1 ) {
156
155
var size = stream . parse_int32 ( ) ;
157
156
if ( size === - 1 ) {
158
- row . push ( null ) ;
157
+ data . push ( null ) ;
159
158
} else {
160
- row . push ( stream . parse_raw_string ( size ) ) ;
159
+ data . push ( stream . parse_raw_string ( size ) ) ;
161
160
}
162
161
}
163
- args = [ row ] ;
162
+ args = [ data ] ;
164
163
break ;
165
164
case 'C' :
166
165
type = "CommandComplete" ;
@@ -173,6 +172,7 @@ function parse_response(code, stream) {
173
172
return { type : type , args : args } ;
174
173
}
175
174
175
+
176
176
exports . Connection = function ( database , username , password ) {
177
177
178
178
var connection = node . tcp . createConnection ( 5432 ) ;
@@ -274,10 +274,25 @@ exports.Connection = function (database, username, password) {
274
274
} ) ;
275
275
events . addListener ( "DataRow" , function ( data ) {
276
276
var row = { } ;
277
- data . each_with_index ( function ( i , cell ) {
277
+ for ( var i = 0 , l = data . length ; i < l ; i += 1 ) {
278
278
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
+ }
281
296
results . push ( row ) ;
282
297
} ) ;
283
298
events . addListener ( 'CommandComplete' , function ( data ) {
0 commit comments