Skip to content

Commit

Permalink
🐛 streaming reads should emit the dataset number for each dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffynuts committed Mar 13, 2024
1 parent 0d35bf9 commit b33ccc6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Query extends Command {
if (this.onResult) {
this._rows[this._resultIndex].push(row);
} else {
this.emit('result', row);
this.emit('result', row, this._resultIndex);
}
return Query.prototype.row;
}
Expand All @@ -268,11 +268,11 @@ class Query extends Command {
stream._read = () => {
this._connection && this._connection.resume();
};
this.on('result', row => {
this.on('result', (row, resultSetIndex) => {
if (!stream.push(row)) {
this._connection.pause();
}
stream.emit('result', row); // replicate old emitter
stream.emit('result', row, resultSetIndex); // replicate old emitter
});
this.on('error', err => {
stream.emit('error', err); // Pass on any errors
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint:typings": "npx prettier --check ./typings",
"lint:tests": "npx prettier --check ./test",
"test": "poku --debug --include=\"test/esm,test/unit,test/integration\"",
"test-me": "poku --debug --include=test/integration/test-multi-result-streaming.cjs",
"test:bun": "poku --debug --platform=\"bun\" --include=\"test/esm,test/unit,test/integration\"",
"test:tsc-build": "cd \"test/tsc-build\" && npx tsc -p \"tsconfig.json\"",
"coverage-test": "c8 -r cobertura -r lcov -r text npm run test",
Expand Down
61 changes: 61 additions & 0 deletions test/integration/test-multi-result-streaming.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { createConnection } = require('../common.test.cjs');
(async function() {
'use strict';

const { assert } = require('poku');

const
conn = createConnection({ multipleStatements: true }),
captured1 = [],
captured2 = [],
sql1 = 'select * from information_schema.columns order by table_schema, table_name, column_name limit 1;',
sql2 = 'select * from information_schema.columns order by table_schema, table_name limit 1;';

const compare1 = await conn.promise().query(
sql1
);
const compare2 = await conn.promise().query(
sql2
);

if (!compare1 || compare1.length < 1) {
assert.fail('no results for comparison 1');
}
if (!compare2 || compare2.length < 1) {
assert.fail('no results for comparison 2');
}

const stream = conn.query(`${ sql1 }\n${ sql2 }`).stream();
stream.on('result', (row, datasetIndex) => {
if (datasetIndex === 0) {
captured1.push(row);
} else {
captured2.push(row);
}
});
// note: this is very important:
// after each result set is complete,
// the stream will emit "readable" and if we don't
// read then 'end' won't be emitted and the
// test will hang.
stream.on("readable", () => {
stream.read();
});

await new Promise((resolve, reject) => {
stream.on('error', e => reject(e));
stream.on('end', () => resolve());
});

try {
assert.equal(captured1.length, 1);
assert.equal(captured2.length, 1);
assert.deepEqual(captured1[0], compare1[0][0]);
assert.deepEqual(captured2[0], compare2[0][0]);
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}

})();

0 comments on commit b33ccc6

Please sign in to comment.