@@ -331,12 +331,22 @@ function message () {
331
331
}
332
332
return false ;
333
333
}
334
+ this . on ( "Error" , function ( err ) {
335
+ if ( errback !== null && errback !== undefined ) {
336
+ errback ( err ) ;
337
+ }
338
+ else {
339
+ // We don't have an error handler? Odd..
340
+ // Pass it up to our connection/transaction.
341
+ conn . emit ( "Error" , err ) ;
342
+ }
343
+ } ) ;
334
344
}
335
345
336
346
message . prototype = new process . EventEmitter ;
337
347
// message.prototype.constructor = message;
338
348
339
- function Query ( sql , callback ) {
349
+ function Query ( sql , callback , errback ) {
340
350
message . call ( this ) ;
341
351
this . sql = sql ;
342
352
var q = this ;
@@ -358,18 +368,18 @@ function Query(sql, callback) {
358
368
// args: []
359
369
// }
360
370
] ) ;
361
- q . addListener ( "newRow" , function ( row ) {
371
+ q . on ( "newRow" , function ( row ) {
362
372
q . results . push ( row ) ;
363
373
} ) ;
364
- q . addListener ( "Complete" , function ( data ) {
374
+ q . on ( "Complete" , function ( data ) {
365
375
if ( exports . DEBUG > 2 ) {
366
376
sys . debug ( "Callback: " + callback ) ;
367
377
}
368
378
369
379
callback ( q . results ) ;
370
380
} ) ;
371
381
q . toString = function ( ) { return "Query: " + q . length } ;
372
- q . addListener ( "RowDescription" , function ( desc ) {
382
+ q . on ( "RowDescription" , function ( desc ) {
373
383
q . row_description = desc ;
374
384
if ( exports . DEBUG > 2 ) {
375
385
sys . debug ( "Caught RowDescription message." ) ;
@@ -418,7 +428,7 @@ function Prepared(sql, conn /*, use_named */) {
418
428
419
429
self . setMessages ( arr ) ;
420
430
421
- self . addListener ( "ParseComplete" , function ( ) {
431
+ self . on ( "ParseComplete" , function ( ) {
422
432
// Execute can now be run successfully.
423
433
// Until this point, we can't assume that there's a matching query.
424
434
// Anyway, we now run a DESCRIBE operation, and store the row
@@ -441,9 +451,6 @@ function Prepared(sql, conn /*, use_named */) {
441
451
self . parameters = desc ;
442
452
} ) ;
443
453
444
- self . on ( "NoData" , function ( ) {
445
- self . noData = true ;
446
- } )
447
454
self . on ( "newRow" , function ( row ) {
448
455
currExec . emit ( "newRow" , row ) ;
449
456
} ) ;
@@ -473,6 +480,12 @@ function Prepared(sql, conn /*, use_named */) {
473
480
}
474
481
} ) ;
475
482
483
+ self . on ( "NoData" , function ( ) {
484
+ if ( currExec !== null ) {
485
+ currExec . noData = true ;
486
+ }
487
+ } ) ;
488
+
476
489
self . on ( "BindComplete" , function ( ) {
477
490
if ( currExec != null ) {
478
491
currExec . emit ( "BindComplete" ) ;
@@ -574,6 +587,7 @@ function Prepared(sql, conn /*, use_named */) {
574
587
if ( exports . DEBUG > 0 ) {
575
588
sys . debug ( "Results length " + eP . results . length ) ;
576
589
}
590
+ sys . debug ( "noData is: " + eP . noData ) ;
577
591
if ( eP . results . length > 0 && eP . bound ) {
578
592
if ( exports . DEBUG > 0 ) {
579
593
sys . debug ( "Execute Complete: Calling with results" ) ;
@@ -595,6 +609,7 @@ function Prepared(sql, conn /*, use_named */) {
595
609
eP . bound = true ;
596
610
} ) ;
597
611
eP . on ( "NoData" , function ( ) {
612
+
598
613
eP . noData = true ;
599
614
} ) ;
600
615
eP . on ( "newRow" , function ( row ) {
@@ -605,6 +620,9 @@ function Prepared(sql, conn /*, use_named */) {
605
620
}
606
621
eB . push ( eP ) ;
607
622
}
623
+ self . on ( "Error" , function ( err ) {
624
+
625
+ } ) ;
608
626
}
609
627
Prepared . prototype = new message ( ) ;
610
628
Prepared . prototype . constructor = Prepared ;
@@ -754,34 +772,43 @@ function Connection(args) {
754
772
}
755
773
756
774
// Set up tcp client
757
- connection . addListener ( "connect" , function ( ) {
775
+ connection . on ( "connect" , function ( ) {
758
776
sendMessage ( 'StartupMessage' , [ { user : args . username , database : args . database } ] ) ;
759
777
} ) ;
760
- connection . addListener ( "data" , function ( data ) {
778
+ connection . on ( "data" , function ( data ) {
761
779
if ( exports . DEBUG > 2 ) {
762
780
sys . debug ( "<-" + data . inspect ( ) ) ;
763
781
}
764
782
queue . push ( data ) ;
765
783
checkInput ( ) ;
766
784
} ) ;
767
- connection . addListener ( "end" , function ( data ) {
785
+ connection . on ( "end" , function ( data ) {
768
786
connection . end ( ) ;
769
787
} ) ;
770
- connection . addListener ( "disconnect" , function ( had_error ) {
788
+ connection . on ( "disconnect" , function ( had_error ) {
771
789
if ( had_error ) {
772
790
sys . debug ( "CONNECTION DIED WITH ERROR" ) ;
773
791
}
774
792
} ) ;
775
793
776
794
// Set up callbacks to automatically do the login and other logic
777
- events . addListener ( 'AuthenticationMD5Password' , function ( salt ) {
795
+ events . on ( 'AuthenticationMD5Password' , function ( salt ) {
778
796
var result = "md5" + md5 ( md5 ( args . password + args . username ) + salt . toString ( "binary" ) ) ;
779
797
sendMessage ( 'PasswordMessage' , [ result ] ) ;
780
798
} ) ;
781
- events . addListener ( 'AuthenticationCleartextPassword' , function ( ) {
799
+ events . on ( 'AuthenticationCleartextPassword' , function ( ) {
782
800
sendMessage ( 'PasswordMessage' , [ args . password ] ) ;
783
801
} ) ;
784
- events . addListener ( 'ErrorResponse' , function ( e ) {
802
+
803
+ /*
804
+ Errors shuld be handled at the TX layer.
805
+ This allows for a given TX set (which contains 1..n message sets) to
806
+ catch and gracefully recover from an error state, and not attempt to
807
+ continue to slam messages onto the wire when there isn't an ability to
808
+ do so.
809
+ */
810
+
811
+ events . on ( 'ErrorResponse' , function ( e ) {
785
812
conn . emit ( 'error' , e . S + ": " + e . M ) ;
786
813
if ( e . S === 'FATAL' ) {
787
814
connection . end ( ) ;
@@ -863,21 +890,13 @@ function Connection(args) {
863
890
readyState = false ;
864
891
sendMessage . apply ( conn , [ msg . type , msg . args ] ) ;
865
892
}
866
- // else {
867
- // sys.debug("getting next query, nM");
868
- // events.emit("nextQuery");
869
- // }
870
893
if ( exports . DEBUG > 2 ) {
871
894
sys . debug ( "waiting for RFQ" ) ;
872
895
}
873
896
}
874
- // else {
875
- // // wait
876
- //
877
- // }
878
897
} ) ;
879
898
880
- events . addListener ( "nextQuery" , function ( ) {
899
+ events . on ( "nextQuery" , function ( ) {
881
900
if ( exports . DEBUG > 0 ) {
882
901
sys . debug ( "got nextQuery" ) ;
883
902
}
@@ -936,7 +955,7 @@ function Connection(args) {
936
955
937
956
938
957
// This should always be caught by the current query.
939
- events . addListener ( "RowDescription" , function ( data ) {
958
+ events . on ( "RowDescription" , function ( data ) {
940
959
row_description = data ;
941
960
results = [ ] ;
942
961
} ) ;
@@ -945,7 +964,7 @@ function Connection(args) {
945
964
// Data row is handled by the connection for the time
946
965
// being, even though we should be looking at handling it in the
947
966
// query object, where the RowDescription lives.
948
- events . addListener ( "DataRow" , function ( data ) {
967
+ events . on ( "DataRow" , function ( data ) {
949
968
var row , i , l , description , value ;
950
969
row = { } ;
951
970
l = data . length ;
@@ -982,7 +1001,7 @@ function Connection(args) {
982
1001
} ) ;
983
1002
984
1003
985
- events . addListener ( 'CommandComplete' , function ( data , results ) {
1004
+ events . on ( 'CommandComplete' , function ( data , results ) {
986
1005
if ( results != null && results . length > 0 ) {
987
1006
// To allow for insert..returning
988
1007
current_query . emit ( "Complete" , results , data ) ;
@@ -1116,7 +1135,7 @@ function Connection(args) {
1116
1135
1117
1136
Easy, and very nifty.
1118
1137
*/
1119
- conn . addListener ( 'newListener' , function ( e , listener ) {
1138
+ conn . on ( 'newListener' , function ( e , listener ) {
1120
1139
if ( e === 'String' ) {
1121
1140
// It's a string.
1122
1141
if ( ! ( e in [ 'newListener' ] ) ) {
@@ -1162,7 +1181,7 @@ function Transaction (connection /*, params */) {
1162
1181
var wrap = function ( func ) {
1163
1182
return ( function ( ) {
1164
1183
if ( exports . DEBUG > 3 ) {
1165
- sys . debug ( func ) ;
1184
+ sys . debug ( "Wrapping function: " + func ) ;
1166
1185
}
1167
1186
func . apply ( func , arguments ) ;
1168
1187
} ) ;
@@ -1226,7 +1245,7 @@ function Transaction (connection /*, params */) {
1226
1245
events . emit ( "queryAdded" ) ;
1227
1246
wrap ( callback ) ( p , thisp ) ;
1228
1247
if ( exports . DEBUG == 4 ) {
1229
- sys . debug ( "Prepared messages: " + sys . inspect ( messages ) ) ;
1248
+ sys . debug ( "Prepared messages: " + sys . inspect ( messages , 4 ) ) ;
1230
1249
}
1231
1250
// conn.emit.call(conn, "queryAdded");
1232
1251
}
@@ -1270,7 +1289,7 @@ function Transaction (connection /*, params */) {
1270
1289
messages . push ( msg ) ;
1271
1290
this . emit ( "queryAdded" ) ;
1272
1291
if ( exports . DEBUG > 0 ) {
1273
- sys . debug ( "Added message of " + msg + " to TX" ) ;
1292
+ sys . debug ( "Added message of " + sys . inspect ( msg ) + " to TX" ) ;
1274
1293
}
1275
1294
}
1276
1295
else {
@@ -1291,6 +1310,14 @@ function Transaction (connection /*, params */) {
1291
1310
}
1292
1311
return null ;
1293
1312
}
1313
+ this . on ( "Error" , function ( e ) {
1314
+ /* Global transaction error response.
1315
+ This should push a SYNC message into the buffer immediately,
1316
+ as well as a ROLLBACK command.
1317
+ This will free up the wire from whatever the last message set was,
1318
+ and allow for a given piece of code to recover gracefully.
1319
+ */
1320
+ } ) ;
1294
1321
}
1295
1322
1296
1323
Transaction . prototype = new process . EventEmitter ( ) ;
0 commit comments