@@ -85,28 +85,25 @@ Client.prototype.connect = function(callback) {
8585 //hook up query handling events to connection
8686 //after the connection initially becomes ready for queries
8787 con . once ( 'readyForQuery' , function ( ) {
88- //delegate row descript to active query
88+
89+ //delegate rowDescription to active query
8990 con . on ( 'rowDescription' , function ( msg ) {
9091 self . activeQuery . handleRowDescription ( msg ) ;
9192 } ) ;
9293
93- //delegate datarow to active query
94+ //delegate dataRow to active query
9495 con . on ( 'dataRow' , function ( msg ) {
9596 self . activeQuery . handleDataRow ( msg ) ;
9697 } ) ;
9798
98- //TODO should query gain access to connection?
99+ //delegate portalSuspended to active query
99100 con . on ( 'portalSuspended' , function ( msg ) {
100- self . activeQuery . getRows ( con ) ;
101+ self . activeQuery . handlePortalSuspended ( con ) ;
101102 } ) ;
102103
104+ //delegate commandComplete to active query
103105 con . on ( 'commandComplete' , function ( msg ) {
104- //delegate command complete to query
105- self . activeQuery . handleCommandComplete ( msg ) ;
106- //need to sync after each command complete of a prepared statement
107- if ( self . activeQuery . isPreparedStatement ) {
108- con . sync ( ) ;
109- }
106+ self . activeQuery . handleCommandComplete ( msg , con ) ;
110107 } ) ;
111108
112109 con . on ( 'copyInResponse' , function ( msg ) {
@@ -128,60 +125,46 @@ Client.prototype.connect = function(callback) {
128125 self . activeQuery . handleCopyFromChunk ( msg . chunk ) ;
129126 } ) ;
130127
131- if ( ! callback ) {
132- self . emit ( 'connect' ) ;
133- } else {
134- callback ( null , self ) ;
135- //remove callback for proper error handling after the connect event
136- callback = null ;
137- }
138-
139128 con . on ( 'notification' , function ( msg ) {
140129 self . emit ( 'notification' , msg ) ;
141130 } ) ;
142131
132+ //process possible callback argument to Client#connect
133+ if ( callback ) {
134+ callback ( null , self ) ;
135+ //remove callback for proper error handling
136+ //after the connect event
137+ callback = null ;
138+ }
139+ self . emit ( 'connect' ) ;
143140 } ) ;
144141
145142 con . on ( 'readyForQuery' , function ( ) {
146- var error ;
147- if ( self . activeQuery ) {
148- //try/catch/rethrow to ensure exceptions don't prevent the queryQueue from
149- //being processed
150- try {
151- self . activeQuery . handleReadyForQuery ( ) ;
152- } catch ( e ) {
153- error = e ;
154- }
155- }
143+ var activeQuery = self . activeQuery ;
156144 self . activeQuery = null ;
157145 self . readyForQuery = true ;
158146 self . _pulseQueryQueue ( ) ;
159- if ( error ) {
160- throw error ;
147+ if ( activeQuery ) {
148+ activeQuery . handleReadyForQuery ( ) ;
161149 }
162150 } ) ;
163151
164152 con . on ( 'error' , function ( error ) {
165- if ( ! self . activeQuery ) {
166- if ( ! callback ) {
167- self . emit ( 'error' , error ) ;
168- } else {
169- callback ( error ) ;
170- }
171- } else {
172- //need to sync after error during a prepared statement
173- if ( self . activeQuery . isPreparedStatement ) {
174- con . sync ( ) ;
175- }
153+ if ( self . activeQuery ) {
176154 var activeQuery = self . activeQuery ;
177155 self . activeQuery = null ;
178- activeQuery . handleError ( error ) ;
156+ return activeQuery . handleError ( error , con ) ;
157+ }
158+ if ( ! callback ) {
159+ return self . emit ( 'error' , error ) ;
179160 }
161+ callback ( error ) ;
180162 } ) ;
181163
182164 con . once ( 'end' , function ( ) {
183165 if ( self . activeQuery ) {
184- self . activeQuery . handleError ( new Error ( 'Stream unexpectedly ended during query execution' ) ) ;
166+ var disconnectError = new Error ( 'Stream unexpectedly ended during query execution' )
167+ self . activeQuery . handleError ( disconnectError ) ;
185168 self . activeQuery = null ;
186169 }
187170 self . emit ( 'end' ) ;
0 commit comments