Skip to content

Commit bc222a8

Browse files
committed
Merge pull request brianc#457 from brianc/query-stream
Clean up internals
2 parents c612a39 + 894c60e commit bc222a8

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

lib/client.js

+27-44
Original file line numberDiff line numberDiff line change
@@ -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');
@@ -301,7 +284,7 @@ Client.prototype.copyTo = function (text) {
301284

302285
Client.prototype.query = function(config, values, callback) {
303286
//can take in strings, config object or query object
304-
var query = (config instanceof Query) ? config :
287+
var query = (typeof config.submit == 'function') ? config :
305288
new Query(config, values, callback);
306289
if(this.binary && !query.binary) {
307290
query.binary = true;

lib/query.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ Query.prototype.handleDataRow = function(msg) {
6868
}
6969
};
7070

71-
Query.prototype.handleCommandComplete = function(msg) {
71+
Query.prototype.handleCommandComplete = function(msg, con) {
7272
this._result.addCommandComplete(msg);
73+
//need to sync after each command complete of a prepared statement
74+
if(this.isPreparedStatement) {
75+
con.sync();
76+
}
7377
};
7478

7579
Query.prototype.handleReadyForQuery = function() {
@@ -82,7 +86,11 @@ Query.prototype.handleReadyForQuery = function() {
8286
this.emit('end', this._result);
8387
};
8488

85-
Query.prototype.handleError = function(err) {
89+
Query.prototype.handleError = function(err, connection) {
90+
//need to sync after error during a prepared statement
91+
if(this.isPreparedStatement) {
92+
connection.sync();
93+
}
8694
if(this._canceledDueToError) {
8795
err = this._canceledDueToError;
8896
this._canceledDueToError = false;
@@ -110,10 +118,14 @@ Query.prototype.hasBeenParsed = function(connection) {
110118
return this.name && connection.parsedStatements[this.name];
111119
};
112120

113-
Query.prototype.getRows = function(connection) {
121+
Query.prototype.handlePortalSuspended = function(connection) {
122+
this._getRows(connection, this.rows);
123+
};
124+
125+
Query.prototype._getRows = function(connection, rows) {
114126
connection.execute({
115127
portal: this.portalName,
116-
rows: this.rows
128+
rows: rows
117129
}, true);
118130
connection.flush();
119131
};
@@ -155,7 +167,7 @@ Query.prototype.prepare = function(connection) {
155167
name: self.portalName || ""
156168
}, true);
157169

158-
this.getRows(connection);
170+
this._getRows(connection, this.rows);
159171
};
160172

161173
Query.prototype.streamData = function (connection) {

0 commit comments

Comments
 (0)