@@ -51,27 +51,45 @@ function encoder(header) {
51
51
52
52
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
53
53
var formatter = {
54
- Bind : function ( portal , prepared_name , args ) {
54
+ Bind : function ( portal , prepared_name , params , args ) {
55
55
var builder = ( encoder ( 'B' ) )
56
56
. push . cstring ( portal )
57
57
. push . cstring ( prepared_name )
58
- . push . int16 ( 0 ) // 0 format codes. We don't want to declare types .
59
- . push . int16 ( args . length ) ;
58
+ . push . int16 ( args . length ) ; // declare our format codes as expected .
59
+
60
60
for ( var i = 0 ; i < args . length ; i ++ ) {
61
61
switch ( typeof args [ i ] ) {
62
62
case "number" :
63
- builder . push . int32 ( 4 ) ; // 4 bytes. int32.
64
- builder . push . int32 ( args [ i ] ) ;
63
+ case "boolean" :
64
+ case "object" :
65
+ builder . push . int16 ( 1 ) ; // binary
65
66
break ;
66
67
case "string" :
67
- builder . push . int32 ( args [ i ] . length ) ;
68
- builder . push . string ( args [ i ] ) ; // Not a cstring. Don't \0
68
+ builder . push . int16 ( 0 ) ;
69
+ break ;
70
+ }
71
+ }
72
+
73
+ builder . push . int16 ( args . length ) ;
74
+ for ( var i = 0 ; i < args . length ; i ++ ) {
75
+ switch ( typeof args [ i ] ) {
76
+ case "number" :
77
+ builder . push . int32 ( 4 ) // 4 bytes. int32.
78
+ . push . int32 ( args [ i ] ) ;
79
+ break ;
80
+ case "string" :
81
+ builder . push . int32 ( args [ i ] . length )
82
+ . push . string ( args [ i ] ) ; // Not a cstring. Don't \0
69
83
break ;
70
84
case "boolean" :
71
- builder . push . int32 ( 1 ) ; // One byte.
72
- builder . push . string ( args [ i ] ? 1 : 0 ) ;
85
+ builder . push . int32 ( 1 ) // One byte.
86
+ . push . string ( args [ i ] ? 1 : 0 ) ;
73
87
break ;
74
- }
88
+ case "object" :
89
+ if ( args [ i ] === null ) {
90
+ builder . push . int32 ( - 1 ) ;
91
+ }
92
+ } ;
75
93
}
76
94
builder . push . int16 ( 0 ) ; // They should all use text. Don't declare return
77
95
// types, as we already have the types from the
@@ -193,7 +211,7 @@ function parse_response(code, buffer) {
193
211
for ( var i = 0 ; i < num_fields ; i ++ ) {
194
212
data . push ( reader . int32 ( ) ) ;
195
213
}
196
- args = data ;
214
+ args = [ data ] ;
197
215
break ;
198
216
case 'S' :
199
217
type = "ParameterStatus" ;
@@ -409,11 +427,11 @@ function Prepared(sql, conn, callback) {
409
427
if ( typeof ( arguments [ 1 ] ) == 'function' ) {
410
428
callback = arguments [ 1 ] ;
411
429
}
412
- eP = new execPrepared ( portal , prepared_name , arguments [ 0 ] , arguments [ 1 ] ) ;
430
+ eP = new execPrepared ( portal , prepared_name , q . param_description , arguments [ 0 ] , q . row_description , conn , arguments [ 1 ] ) ;
413
431
}
414
432
}
415
433
else if ( typeof ( arguments [ 0 ] ) == 'function' ) {
416
- eP = new execPrepared ( portal , prepared_name , [ ] , arguments [ 0 ] ) ;
434
+ eP = new execPrepared ( portal , prepared_name , q . param_description , [ ] , q . row_description , conn , arguments [ 0 ] ) ;
417
435
// Announcing that my slot is released in favour of this new query.
418
436
// The connection doesn't advance the buffer, but *does* replace
419
437
// the current query with this one.
@@ -437,20 +455,25 @@ function Prepared(sql, conn, callback) {
437
455
}
438
456
Prepared . prototype = new process . EventEmitter ;
439
457
440
- function execPrepared ( portal , prepared , args , callback ) {
458
+ function execPrepared ( portal , prepared , params , args , row_desc , conn , callback ) {
441
459
var q = this ;
460
+ q . row_description = row_desc ;
442
461
var results = [ ] ;
443
462
var arr = [
444
463
{
445
464
type : "Execute" ,
446
465
args : [ portal , 0 ] , // No limit. Get all the rows.
466
+ } ,
467
+ {
468
+ type : "Flush" ,
469
+ args : [ portal , 0 ]
447
470
}
448
471
] ;
449
472
// If we have args, unshift the Bind.
450
473
if ( args instanceof Array && args . length >= 1 ) {
451
474
arr . unshift ( {
452
475
type : "Bind" ,
453
- args :[ portal , prepared , args ] ,
476
+ args :[ portal , prepared , params , args ] ,
454
477
callback : callback
455
478
} ) ;
456
479
}
@@ -473,13 +496,20 @@ function execPrepared (portal, prepared, args, callback) {
473
496
// If it was a SELECT, args will be an array of rows,
474
497
// If it was an INSERT, etc, it'll be the type, and the number of
475
498
// affected rows.
499
+ if ( exports . DEBUG > 0 ) {
500
+ sys . debug ( "Results length " + results . length ) ;
501
+ }
476
502
if ( results . length >= 1 ) {
477
- callback . call ( data , results ) ;
503
+ if ( exports . DEBUG > 0 ) {
504
+ sys . debug ( "Calling with results" ) ;
505
+ }
506
+ callback . call ( q , results ) ;
478
507
}
479
508
else {
480
509
this . type = data . type ;
481
- callback . call ( data ) ;
510
+ callback . call ( q ) ;
482
511
}
512
+ conn . release ( q ) ;
483
513
} ) ;
484
514
var pos = 0 ;
485
515
q . next = function ( ) {
@@ -717,7 +747,11 @@ function Connection(args) {
717
747
}
718
748
row [ description . field ] = value ;
719
749
}
720
- if ( current_query . listeners ( "newRow" ) > 0 ) {
750
+ if ( exports . DEBUG > 0 ) {
751
+ sys . debug ( current_query . listeners ( "newRow" ) . length ) ;
752
+ sys . debug ( current_query ) ;
753
+ }
754
+ if ( current_query . listeners ( "newRow" ) . length > 0 ) {
721
755
current_query . emit ( "newRow" , row ) ;
722
756
}
723
757
else {
0 commit comments