2014-09-16 03:01:39 +08:00
|
|
|
module.exports = function(txt, options) {
|
|
|
|
return new CopyStreamQuery(txt, options)
|
2013-10-29 06:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var Transform = require('stream').Transform
|
|
|
|
var util = require('util')
|
2016-07-27 03:39:48 +08:00
|
|
|
var code = require('./message-formats')
|
2013-10-29 06:31:11 +08:00
|
|
|
|
2014-09-16 03:01:39 +08:00
|
|
|
var CopyStreamQuery = function(text, options) {
|
|
|
|
Transform.call(this, options)
|
2013-10-29 06:31:11 +08:00
|
|
|
this.text = text
|
2016-07-29 05:13:22 +08:00
|
|
|
this._gotCopyOutResponse = false
|
2013-10-29 10:50:45 +08:00
|
|
|
this.rowCount = 0
|
2013-10-29 06:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(CopyStreamQuery, Transform)
|
|
|
|
|
2014-04-06 18:49:25 +08:00
|
|
|
var eventTypes = ['close', 'data', 'end', 'error']
|
2014-03-28 16:17:05 +08:00
|
|
|
|
2013-10-29 06:31:11 +08:00
|
|
|
CopyStreamQuery.prototype.submit = function(connection) {
|
|
|
|
connection.query(this.text)
|
|
|
|
this.connection = connection
|
2016-05-04 02:20:04 +08:00
|
|
|
this.connection.removeAllListeners('copyData')
|
2013-10-29 06:31:11 +08:00
|
|
|
connection.stream.pipe(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyStreamQuery.prototype._detach = function() {
|
2016-05-04 02:20:04 +08:00
|
|
|
this.connection.stream.unpipe(this)
|
|
|
|
// Unpipe can drop us out of flowing mode
|
|
|
|
this.connection.stream.resume()
|
2013-10-29 06:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyStreamQuery.prototype._transform = function(chunk, enc, cb) {
|
|
|
|
var offset = 0
|
2016-07-29 05:13:22 +08:00
|
|
|
var Byte1Len = 1;
|
|
|
|
var Int32Len = 4;
|
2013-10-29 06:31:11 +08:00
|
|
|
if(this._remainder && chunk) {
|
|
|
|
chunk = Buffer.concat([this._remainder, chunk])
|
|
|
|
}
|
2016-07-29 05:13:22 +08:00
|
|
|
|
|
|
|
var length;
|
|
|
|
var messageCode;
|
|
|
|
var needPush = false;
|
|
|
|
|
2018-06-08 18:49:22 +08:00
|
|
|
var buffer = Buffer.alloc(chunk.length);
|
|
|
|
var buffer_offset = 0;
|
|
|
|
|
2018-06-11 18:14:28 +08:00
|
|
|
this.pushBufferIfneeded = function() {
|
2018-06-11 18:17:39 +08:00
|
|
|
if (needPush && buffer_offset > 0) {
|
2018-06-11 18:14:28 +08:00
|
|
|
this.push(buffer.slice(0, buffer_offset))
|
|
|
|
buffer_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 07:03:11 +08:00
|
|
|
while((chunk.length - offset) >= (Byte1Len + Int32Len)) {
|
2013-10-29 06:31:11 +08:00
|
|
|
var messageCode = chunk[offset]
|
2016-07-29 05:13:22 +08:00
|
|
|
|
2016-07-29 07:03:11 +08:00
|
|
|
//console.log('PostgreSQL message ' + String.fromCharCode(messageCode))
|
2016-07-29 05:13:22 +08:00
|
|
|
switch(messageCode) {
|
|
|
|
|
|
|
|
// detect COPY start
|
|
|
|
case code.CopyOutResponse:
|
|
|
|
if (!this._gotCopyOutResponse) {
|
|
|
|
this._gotCopyOutResponse = true
|
|
|
|
} else {
|
|
|
|
this.emit('error', new Error('Unexpected CopyOutResponse message (H)'))
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// meaningful row
|
|
|
|
case code.CopyData:
|
|
|
|
needPush = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// standard interspersed messages. discard
|
|
|
|
case code.ParameterStatus:
|
|
|
|
case code.NoticeResponse:
|
|
|
|
case code.NotificationResponse:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case code.ErrorResponse:
|
|
|
|
case code.CopyDone:
|
2018-06-11 18:14:28 +08:00
|
|
|
this.pushBufferIfneeded();
|
2016-07-29 05:13:22 +08:00
|
|
|
this._detach()
|
|
|
|
this.push(null)
|
|
|
|
return cb();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.emit('error', new Error('Unexpected PostgreSQL message ' + String.fromCharCode(messageCode)))
|
2013-10-29 06:31:11 +08:00
|
|
|
}
|
2016-07-29 05:13:22 +08:00
|
|
|
|
|
|
|
length = chunk.readUInt32BE(offset+Byte1Len)
|
2016-07-29 07:03:11 +08:00
|
|
|
if(chunk.length >= (offset + Byte1Len + length)) {
|
2016-07-29 05:13:22 +08:00
|
|
|
offset += Byte1Len + Int32Len
|
|
|
|
if (needPush) {
|
|
|
|
var row = chunk.slice(offset, offset + length - Int32Len)
|
|
|
|
this.rowCount++
|
2018-06-08 18:49:22 +08:00
|
|
|
row.copy(buffer, buffer_offset);
|
|
|
|
buffer_offset += row.length;
|
2016-07-29 05:13:22 +08:00
|
|
|
}
|
|
|
|
offset += (length - Int32Len)
|
2013-10-29 06:31:11 +08:00
|
|
|
} else {
|
2016-07-29 05:13:22 +08:00
|
|
|
// we need more chunks for a complete message
|
2013-10-29 06:31:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 05:13:22 +08:00
|
|
|
|
2018-06-11 18:14:28 +08:00
|
|
|
this.pushBufferIfneeded();
|
2018-06-08 18:49:22 +08:00
|
|
|
|
2013-10-29 06:31:11 +08:00
|
|
|
if(chunk.length - offset) {
|
|
|
|
var slice = chunk.slice(offset)
|
|
|
|
this._remainder = slice
|
|
|
|
} else {
|
|
|
|
this._remainder = false
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyStreamQuery.prototype.handleError = function(e) {
|
|
|
|
this.emit('error', e)
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:20:04 +08:00
|
|
|
CopyStreamQuery.prototype.handleCopyData = function(chunk) {
|
|
|
|
}
|
|
|
|
|
2013-10-29 06:31:11 +08:00
|
|
|
CopyStreamQuery.prototype.handleCommandComplete = function() {
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyStreamQuery.prototype.handleReadyForQuery = function() {
|
|
|
|
}
|