Skip to content

Commit

Permalink
updating streaming support.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 20, 2015
1 parent 6e15e17 commit d59f50f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ function $extend(ctx, obj) {
return obj.query(query, values, 'result');
};

// TODO: Needs proper documentation;
// Has no expectation for the data;
// Has no expectation for the return data;
// Resolves when the streaming has finished;
// qs - stream-like object;
// init - stream initialization callback.
obj.stream = function (qs, init) {
return obj.query(qs, init, 'stream');
};
Expand Down Expand Up @@ -303,9 +305,16 @@ function $stream(ctx, qs, init) {
return $p.reject("Invalid stream initialization callback.");
}
// TODO: qs.text - the sql query to be reported;
var stream;
var stream, start = Date.now(), count = 0;
try {
stream = ctx.db.client.query(qs);
var oldCB = stream._fetch;
stream._fetch = function (size, func) {
oldCB.call(stream, size, function (err, rows) {
count += rows.length;
func(err, rows);
});
};
init(stream); // initialize the stream;
} catch (err) {
return $p.reject(err);
Expand All @@ -318,7 +327,10 @@ function $stream(ctx, qs, init) {
stream.once('end', function () {
// TODO: Need to report the query?
// TODO: Number of rows?
resolve();
resolve({
count: count,
duration: Date.now() - start
});
});
stream.once('error', function (error) {
// TODO: Need to report the error?
Expand Down

0 comments on commit d59f50f

Please sign in to comment.