2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-11 12:25:39 +02:00
|
|
|
/*jslint node:true, nomen: true*/
|
|
|
|
var inherits = require('util').inherits;
|
|
|
|
var Readable = require('stream').Readable;
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
function OracleQueryStream(connection, sql, bindings, options) {
|
2014-11-26 17:45:11 -06:00
|
|
|
try {
|
|
|
|
Readable.call(this, _.merge({}, {
|
|
|
|
objectMode: true,
|
|
|
|
highWaterMark: 1000
|
|
|
|
}, options));
|
|
|
|
|
|
|
|
this.oracleReader = connection.reader(sql, bindings || []);
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inherits(OracleQueryStream, Readable);
|
|
|
|
|
2014-11-26 17:45:11 -06:00
|
|
|
OracleQueryStream.prototype._read = function() {
|
|
|
|
var self = this;
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2014-11-26 17:45:11 -06:00
|
|
|
function pushNull() {
|
|
|
|
process.nextTick(function() {
|
|
|
|
self.push(null);
|
|
|
|
});
|
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2014-11-26 17:45:11 -06:00
|
|
|
try {
|
|
|
|
this.oracleReader.nextRows(function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2014-11-26 17:45:11 -06:00
|
|
|
if (rows.length === 0) {
|
|
|
|
pushNull();
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < rows.length; i++) {
|
|
|
|
if (rows[i]) {
|
|
|
|
self.push(rows[i]);
|
|
|
|
} else {
|
|
|
|
pushNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
});
|
2014-11-26 17:45:11 -06:00
|
|
|
} catch (e) {
|
|
|
|
// Catch Error: invalid state: reader is busy with another nextRows call
|
|
|
|
// and return false to rate limit stream.
|
|
|
|
if (e.message ===
|
|
|
|
'invalid state: reader is busy with another nextRows call') {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.emit('error', e);
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
};
|
|
|
|
|
2014-11-26 17:45:11 -06:00
|
|
|
|
2014-08-11 12:25:39 +02:00
|
|
|
module.exports = OracleQueryStream;
|