66#include < stdlib.h>
77
88#define LOG (msg ) printf(" %s\n " ,msg);
9- #define TRACE (msg ) // printf(" %s\n" , msg);
9+ #define TRACE (msg ) // printf(%s\n, msg);
1010
1111
1212#define THROW (msg ) return ThrowException(Exception::Error(String::New(msg)));
@@ -434,12 +434,15 @@ class Connection : public ObjectWrap {
434434
435435 if (revents & UV_READABLE) {
436436 TRACE (" revents & UV_READABLE" );
437+ TRACE (" about to consume input" );
437438 if (PQconsumeInput (connection_) == 0 ) {
439+ TRACE (" could not read, terminating" );
438440 End ();
439441 EmitLastError ();
440442 // LOG("Something happened, consume input is 0");
441443 return ;
442444 }
445+ TRACE (" Consumed" );
443446
444447 // declare handlescope as this method is entered via a libuv callback
445448 // and not part of the public v8 interface
@@ -450,8 +453,11 @@ class Connection : public ObjectWrap {
450453 if (!this ->copyInMode_ && !this ->copyOutMode_ && PQisBusy (connection_) == 0 ) {
451454 PGresult *result;
452455 bool didHandleResult = false ;
456+ TRACE (" PQgetResult" );
453457 while ((result = PQgetResult (connection_))) {
458+ TRACE (" HandleResult" );
454459 didHandleResult = HandleResult (result);
460+ TRACE (" PQClear" );
455461 PQclear (result);
456462 if (!didHandleResult) {
457463 // this means that we are in copy in or copy out mode
@@ -469,6 +475,7 @@ class Connection : public ObjectWrap {
469475 }
470476
471477 PGnotify *notify;
478+ TRACE (" PQnotifies" );
472479 while ((notify = PQnotifies (connection_))) {
473480 Local<Object> result = Object::New ();
474481 result->Set (channel_symbol, String::New (notify->relname ));
@@ -515,6 +522,7 @@ class Connection : public ObjectWrap {
515522 }
516523 bool HandleResult (PGresult* result)
517524 {
525+ TRACE (" PQresultStatus" );
518526 ExecStatusType status = PQresultStatus (result);
519527 switch (status) {
520528 case PGRES_TUPLES_OK:
@@ -526,6 +534,7 @@ class Connection : public ObjectWrap {
526534 break ;
527535 case PGRES_FATAL_ERROR:
528536 {
537+ TRACE (" HandleErrorResult" );
529538 HandleErrorResult (result);
530539 return true ;
531540 }
@@ -610,8 +619,15 @@ class Connection : public ObjectWrap {
610619 {
611620 HandleScope scope;
612621 // instantiate the return object as an Error with the summary Postgres message
613- Local<Object> msg = Local<Object>::Cast (Exception::Error (String::New (PQresultErrorField (result, PG_DIAG_MESSAGE_PRIMARY))));
614-
622+ TRACE (" ReadResultField" );
623+ const char * errorMessage = PQresultErrorField (result, PG_DIAG_MESSAGE_PRIMARY);
624+ if (!errorMessage) {
625+ // there is no error, it has already been consumed in the last
626+ // read-loop callback
627+ return ;
628+ }
629+ Local<Object> msg = Local<Object>::Cast (Exception::Error (String::New (errorMessage)));
630+ TRACE (" AttachErrorFields" );
615631 // add the other information returned by Postgres to the error object
616632 AttachErrorField (result, msg, severity_symbol, PG_DIAG_SEVERITY);
617633 AttachErrorField (result, msg, code_symbol, PG_DIAG_SQLSTATE);
@@ -625,6 +641,7 @@ class Connection : public ObjectWrap {
625641 AttachErrorField (result, msg, line_symbol, PG_DIAG_SOURCE_LINE);
626642 AttachErrorField (result, msg, routine_symbol, PG_DIAG_SOURCE_FUNCTION);
627643 Handle<Value> m = msg;
644+ TRACE (" EmitError" );
628645 Emit (" _error" , &m);
629646 }
630647
@@ -638,9 +655,11 @@ class Connection : public ObjectWrap {
638655
639656 void End ()
640657 {
658+ TRACE (" stopping read & write" );
641659 StopRead ();
642660 StopWrite ();
643661 DestroyConnection ();
662+ Emit (" _end" );
644663 }
645664
646665private:
@@ -719,7 +738,7 @@ class Connection : public ObjectWrap {
719738 void StopWrite ()
720739 {
721740 TRACE (" write STOP" );
722- if (ioInitialized_) {
741+ if (ioInitialized_ && writing_ ) {
723742 uv_poll_stop (&write_watcher_);
724743 writing_ = false ;
725744 }
@@ -739,7 +758,7 @@ class Connection : public ObjectWrap {
739758 void StopRead ()
740759 {
741760 TRACE (" read STOP" );
742- if (ioInitialized_) {
761+ if (ioInitialized_ && reading_ ) {
743762 uv_poll_stop (&read_watcher_);
744763 reading_ = false ;
745764 }
0 commit comments