Skip to content

Commit ebf5c5c

Browse files
committed
support for limiting number of rows returned at a time from a cursor
1 parent e61ec5d commit ebf5c5c

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

lib/query.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ p.submit = function(connection) {
6161
};
6262

6363
var onReadyForQuery = function() {
64-
//remove all listeners
64+
//remove all listeners
6565
connection.removeListener('rowDescription', handleRowDescription);
6666
connection.removeListener('dataRow', handleDatarow);
6767
connection.removeListener('readyForQuery', onReadyForQuery);
@@ -110,29 +110,28 @@ p.prepare = function(connection) {
110110
name: self.name || ""
111111
});
112112

113-
//TODO test for & support multpile row requests
114-
connection.execute({
115-
portal: self.name,
116-
rows: self.rows
117-
});
113+
var getRows = function() {
114+
connection.execute({
115+
portal: self.name,
116+
rows: self.rows
117+
});
118+
connection.flush();
119+
};
118120

119-
connection.flush();
121+
getRows();
120122

121123
//TODO support EmptyQueryResponse, ErrorResponse, and PortalSuspended
122124
var onCommandComplete = function() {
123-
connection.removeListener('error', onError);
125+
connection.removeListener('error', onCommandComplete);
124126
connection.removeListener('commandComplete', onCommandComplete);
127+
connection.removeListener('portalSuspended', getRows);
125128
connection.sync();
126129
};
127130

128-
var onError = function() {
129-
connection.removeListener('error', onError);
130-
connection.removeListener('commandComplete', onCommandComplete);
131-
connection.sync();
132-
};
131+
connection.on('portalSuspended', getRows);
133132

134133
connection.on('commandComplete', onCommandComplete);
135-
connection.on('error', onError);
134+
connection.on('error', onCommandComplete);
136135
};
137136

138137
var dateParser = function(isoDate) {

test/integration/client/prepared-statement-tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,33 @@ test("prepared statements on different clients", function() {
152152

153153
});
154154

155+
test('prepared statement', function() {
156+
var client = helper.client();
157+
client.on('drain', client.end.bind(client));
158+
client.query('CREATE TEMP TABLE zoom(name varchar(100));');
159+
client.query("INSERT INTO zoom (name) VALUES ('zed')");
160+
client.query("INSERT INTO zoom (name) VALUES ('postgres')");
161+
client.query("INSERT INTO zoom (name) VALUES ('node postgres')");
162+
163+
test('with small row count', function() {
164+
var q = client.query({
165+
name: 'get names',
166+
text: "SELECT name FROM zoom ORDER BY name",
167+
rows: 1
168+
});
169+
test('row callback fires for each result', function() {
170+
assert.emits(q, 'row', function(row) {
171+
assert.equal(row.name, 'node postgres');
172+
173+
assert.emits(q, 'row', function(row) {
174+
assert.equal(row.name, 'postgres');
175+
176+
assert.emits(q, 'row', function(row) {
177+
assert.equal(row.name, 'zed');
178+
})
179+
});
180+
})
181+
})
182+
183+
})
184+
})

0 commit comments

Comments
 (0)