knex/lib/dialects/oracle/stream.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-08-11 12:25:39 +02:00
/*jslint node:true, nomen: true*/
2015-05-09 14:01:19 -04:00
'use strict';
2015-05-09 13:58:18 -04:00
var inherits = require('inherits');
var merge = require('lodash/object/merge');
var Readable = require('stream').Readable;
2014-08-11 12:25:39 +02:00
function OracleQueryStream(connection, sql, bindings, options) {
Readable.call(this, merge({}, {
objectMode: true,
highWaterMark: 1000
2015-05-09 13:58:18 -04:00
}, options));
this.oracleReader = connection.reader(sql, bindings || []);
2014-08-11 12:25:39 +02:00
}
2015-05-09 13:58:18 -04:00
inherits(OracleQueryStream, Readable);
2014-08-11 12:25:39 +02:00
2015-05-09 13:58:18 -04:00
OracleQueryStream.prototype._read = function () {
2015-05-09 14:07:58 -04:00
var _this = this;
2014-11-26 17:45:11 -06:00
function pushNull() {
2015-05-09 14:07:58 -04:00
var _this2 = this;
2015-05-09 13:58:18 -04:00
process.nextTick(function () {
2015-05-09 14:07:58 -04:00
_this2.push(null);
2015-05-09 13:58:18 -04:00
});
2014-11-26 17:45:11 -06:00
}
try {
2015-05-09 13:58:18 -04:00
this.oracleReader.nextRows(function (err, rows) {
2015-05-09 14:07:58 -04:00
if (err) return _this.emit('error', err);
2014-11-26 17:45:11 -06:00
if (rows.length === 0) {
2015-05-09 13:58:18 -04:00
pushNull();
2014-11-26 17:45:11 -06:00
} else {
for (var i = 0; i < rows.length; i++) {
if (rows[i]) {
2015-05-09 14:07:58 -04:00
_this.push(rows[i]);
2014-11-26 17:45:11 -06:00
} else {
2015-05-09 13:58:18 -04:00
pushNull();
2014-11-26 17:45:11 -06:00
}
}
}
2015-05-09 13:58:18 -04: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.
2015-05-09 13:58:18 -04:00
if (e.message === 'invalid state: reader is busy with another nextRows call') {
return false;
2014-11-26 17:45:11 -06:00
} else {
2015-05-09 13:58:18 -04:00
this.emit('error', e);
2014-11-26 17:45:11 -06:00
}
}
2015-05-09 13:58:18 -04:00
};
2014-11-26 17:45:11 -06:00
2015-05-09 13:58:18 -04:00
module.exports = OracleQueryStream;