From 823e3abcfe845ffcf73b401114614f0a1899d0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Thu, 21 Jun 2018 15:16:47 +0200 Subject: [PATCH] unify to and from method in only one getPGStream --- app/services/stream_copy.js | 40 +++++++++++-------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/app/services/stream_copy.js b/app/services/stream_copy.js index 243f7ca5..9348c28d 100644 --- a/app/services/stream_copy.js +++ b/app/services/stream_copy.js @@ -11,7 +11,6 @@ module.exports = class StreamCopy { this.pg = new PSQL(userDbParams); this.sql = sql; this.connectionClosedByClient = false; - this.stream = null; } @@ -23,45 +22,30 @@ module.exports = class StreamCopy { return ACTION_FROM; } - to(cb) { + getStream(action, cb) { this.pg.connect((err, client, done) => { if (err) { return cb(err); } - this.stream = copyTo(this.sql); + let streamMaker = action === ACTION_TO ? copyTo : copyFrom; + this.stream = streamMaker(this.sql); const pgstream = client.query(this.stream); pgstream .on('end', () => done()) .on('error', err => done(err)) .on('cancelQuery', err => { - // See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY - const cancelingClient = new Client(client.connectionParameters); - cancelingClient.cancel(client, pgstream); + if(action === ACTION_TO) { + // See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY + const cancelingClient = new Client(client.connectionParameters); + cancelingClient.cancel(client, pgstream); - // see https://node-postgres.com/api/pool#releasecallback - done(err); - }); - - cb(null, pgstream); - }); - } - - from(cb) { - this.pg.connect((err, client, done) => { - if (err) { - return cb(err); - } - - this.stream = copyFrom(this.sql); - const pgstream = client.query(this.stream); - - pgstream - .on('end', () => done()) - .on('error', err => done(err)) - .on('cancelQuery', () => { - client.connection.sendCopyFail('CARTO SQL API: Connection closed by client'); + // see https://node-postgres.com/api/pool#releasecallback + done(err); + } else if (action === ACTION_FROM) { + client.connection.sendCopyFail('CARTO SQL API: Connection closed by client'); + } }); cb(null, pgstream);