This repository has been archived by the owner on Dec 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
73 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
- "0.12" | ||
- "4.2" | ||
- "6" | ||
- "7" | ||
- "8" | ||
env: | ||
- PGUSER=postgres PGDATABASE=postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,63 @@ | ||
'use strict'; | ||
var util = require('util') | ||
var Cursor = require('pg-cursor') | ||
var Readable = require('readable-stream').Readable | ||
var Readable = require('stream').Readable | ||
|
||
var QueryStream = module.exports = function(text, values, options) { | ||
var self = this | ||
this._reading = false | ||
this._closing = false | ||
options = options || { } | ||
Cursor.call(this, text, values) | ||
Readable.call(this, { | ||
objectMode: true, | ||
highWaterMark: options.highWaterMark || 1000 | ||
}) | ||
this.batchSize = options.batchSize || 100 | ||
this.once('end', function() { | ||
process.nextTick(function() { | ||
self.emit('close') | ||
}) | ||
}) | ||
} | ||
class PgQueryStream extends Readable { | ||
constructor(text, values, options) { | ||
super(Object.assign({ objectMode: true }, options)) | ||
this.cursor = new Cursor(text, values) | ||
this._reading = false | ||
this._closed = false | ||
this.batchSize = (options || { }).batchSize || 100 | ||
|
||
util.inherits(QueryStream, Readable) | ||
// delegate Submittable callbacks to cursor | ||
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor) | ||
this.handleDataRow = this.cursor.handleDataRow.bind(this.cursor) | ||
this.handlePortalSuspended = this.cursor.handlePortalSuspended.bind(this.cursor) | ||
this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor) | ||
this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor) | ||
this.handleError = this.cursor.handleError.bind(this.cursor) | ||
} | ||
|
||
//copy cursor prototype to QueryStream | ||
//so we can handle all the events emitted by the connection | ||
for(var key in Cursor.prototype) { | ||
if(key == 'read') { | ||
QueryStream.prototype._fetch = Cursor.prototype.read | ||
} else { | ||
QueryStream.prototype[key] = Cursor.prototype[key] | ||
submit(connection) { | ||
this.cursor.submit(connection) | ||
return this | ||
} | ||
} | ||
|
||
QueryStream.prototype.close = function(cb) { | ||
this._closing = true | ||
var self = this | ||
Cursor.prototype.close.call(this, function(err) { | ||
if (cb) { cb(err); } | ||
if(err) return self.emit('error', err) | ||
process.nextTick(function() { | ||
self.push(null) | ||
}) | ||
}) | ||
} | ||
close(callback) { | ||
this._closed = true | ||
const cb = callback || (() => this.emit('close')) | ||
this.cursor.close(cb) | ||
} | ||
|
||
QueryStream.prototype._read = function(n) { | ||
if(this._reading || this._closing) return false | ||
this._reading = true | ||
var self = this | ||
this._fetch(this.batchSize, function(err, rows) { | ||
if(err) { | ||
return self.emit('error', err) | ||
_read(size) { | ||
if (this._reading || this._closed) { | ||
return false | ||
} | ||
this._reading = true | ||
const readAmount = Math.max(size, this.batchSize) | ||
this.cursor.read(readAmount, (err, rows) => { | ||
if (this._closed) { | ||
return | ||
} | ||
if (err) { | ||
return this.emit('error', err) | ||
} | ||
// if we get a 0 length array we've read to the end of the cursor | ||
if (!rows.length) { | ||
this._closed = true | ||
setImmediate(() => this.emit('close')) | ||
return this.push(null) | ||
} | ||
|
||
if (self._closing) { return; } | ||
|
||
if(!rows.length) { | ||
process.nextTick(function() { | ||
self.push(null) | ||
}) | ||
return | ||
} | ||
self._reading = false | ||
for(var i = 0; i < rows.length; i++) { | ||
self.push(rows[i]) | ||
} | ||
}) | ||
// push each row into the stream | ||
this._reading = false | ||
for(var i = 0; i < rows.length; i++) { | ||
this.push(rows[i]) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = PgQueryStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
--no-exit | ||
--bail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters