@@ -85,28 +85,25 @@ Client.prototype.connect = function(callback) {
85
85
//hook up query handling events to connection
86
86
//after the connection initially becomes ready for queries
87
87
con . once ( 'readyForQuery' , function ( ) {
88
- //delegate row descript to active query
88
+
89
+ //delegate rowDescription to active query
89
90
con . on ( 'rowDescription' , function ( msg ) {
90
91
self . activeQuery . handleRowDescription ( msg ) ;
91
92
} ) ;
92
93
93
- //delegate datarow to active query
94
+ //delegate dataRow to active query
94
95
con . on ( 'dataRow' , function ( msg ) {
95
96
self . activeQuery . handleDataRow ( msg ) ;
96
97
} ) ;
97
98
98
- //TODO should query gain access to connection?
99
+ //delegate portalSuspended to active query
99
100
con . on ( 'portalSuspended' , function ( msg ) {
100
- self . activeQuery . getRows ( con ) ;
101
+ self . activeQuery . handlePortalSuspended ( con ) ;
101
102
} ) ;
102
103
104
+ //delegate commandComplete to active query
103
105
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 ) ;
110
107
} ) ;
111
108
112
109
con . on ( 'copyInResponse' , function ( msg ) {
@@ -128,60 +125,46 @@ Client.prototype.connect = function(callback) {
128
125
self . activeQuery . handleCopyFromChunk ( msg . chunk ) ;
129
126
} ) ;
130
127
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
-
139
128
con . on ( 'notification' , function ( msg ) {
140
129
self . emit ( 'notification' , msg ) ;
141
130
} ) ;
142
131
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' ) ;
143
140
} ) ;
144
141
145
142
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 ;
156
144
self . activeQuery = null ;
157
145
self . readyForQuery = true ;
158
146
self . _pulseQueryQueue ( ) ;
159
- if ( error ) {
160
- throw error ;
147
+ if ( activeQuery ) {
148
+ activeQuery . handleReadyForQuery ( ) ;
161
149
}
162
150
} ) ;
163
151
164
152
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 ) {
176
154
var activeQuery = self . activeQuery ;
177
155
self . activeQuery = null ;
178
- activeQuery . handleError ( error ) ;
156
+ return activeQuery . handleError ( error , con ) ;
157
+ }
158
+ if ( ! callback ) {
159
+ return self . emit ( 'error' , error ) ;
179
160
}
161
+ callback ( error ) ;
180
162
} ) ;
181
163
182
164
con . once ( 'end' , function ( ) {
183
165
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 ) ;
185
168
self . activeQuery = null ;
186
169
}
187
170
self . emit ( 'end' ) ;
@@ -301,7 +284,7 @@ Client.prototype.copyTo = function (text) {
301
284
302
285
Client . prototype . query = function ( config , values , callback ) {
303
286
//can take in strings, config object or query object
304
- var query = ( config instanceof Query ) ? config :
287
+ var query = ( typeof config . submit == 'function' ) ? config :
305
288
new Query ( config , values , callback ) ;
306
289
if ( this . binary && ! query . binary ) {
307
290
query . binary = true ;
0 commit comments