StreamCopy getResults refactor

- changing get Result to getRowCount
- ensuring to and from independecy in StreamCopy
This commit is contained in:
Simon Martín 2018-06-21 13:39:33 +02:00
parent 2cecc54cb2
commit bafcb8051f
2 changed files with 25 additions and 8 deletions

View File

@ -80,11 +80,11 @@ function handleCopyTo (logger) {
.on('data', data => metrics.addSize(data.length))
.on('error', (err) => {
metrics.end(null, err);
pgstream.unpipe();
pgstream.unpipe(res);
return next(err);
})
.on('end', () => metrics.end(streamCopy.getResult()))
.on('end', () => metrics.end( streamCopy.getRowCount(StreamCopy.ACTION_TO) ))
.pipe(res)
.on('close', () => {
const err = new Error('Connection closed by client');
@ -125,9 +125,10 @@ function handleCopyFrom (logger) {
})
.on('close', () => {
const err = new Error('Connection closed by client');
pgstream.emit('cancelQuery', err);
metrics.end(null, err);
const connection = client.connection;
connection.sendCopyFail('CARTO SQL API: Connection closed by client');
req.unpipe(pgstream);
done();
next(err);

View File

@ -3,6 +3,9 @@ const copyTo = require('pg-copy-streams').to;
const copyFrom = require('pg-copy-streams').from;
const { Client } = require('pg');
const ACTION_TO = 'to';
const ACTION_FROM = 'from';
module.exports = class StreamCopy {
constructor(sql, userDbParams) {
this.pg = new PSQL(userDbParams);
@ -13,6 +16,14 @@ module.exports = class StreamCopy {
this.copyFromStream;
}
static get ACTION_TO() {
return ACTION_TO;
}
static get ACTION_FROM() {
return ACTION_FROM;
}
to(cb) {
this.pg.connect((err, client, done) => {
if (err) {
@ -49,16 +60,21 @@ module.exports = class StreamCopy {
pgstream
.on('end', () => done())
.on('error', err => done(err));
.on('error', err => done(err))
.on('cancelQuery', err => {
client.connection.sendCopyFail('CARTO SQL API: Connection closed by client');
});
cb(null, pgstream, copyFromStream, client, done);
});
}
getResult() {
if (this.copyToStream) {
getRowCount(action = ACTION_TO) {
if (action === ACTION_TO && this.copyToStream) {
return this.copyToStream.rowCount;
} else if (this.copyFromStream) {
}
if (action === ACTION_FROM && this.copyFromStream) {
return this.copyFromStream.rowCount;
}
}