diff --git a/lib/copystream.js b/lib/copystream.js index 88d44e9..baca17d 100644 --- a/lib/copystream.js +++ b/lib/copystream.js @@ -114,12 +114,12 @@ CopyToStream.prototype._outputDataChunk = function () { } if (this.buffer.length) { if (this._encoding) { - this.emit('data', this.buffer.toString(encoding)); + this.emit('data', this.buffer.toString(this._encoding)); } else { this.emit('data', this.buffer); } this.buffer = new Buffer(0); - } + } }; CopyToStream.prototype._readable = function () { return !this._finished && !this._error; diff --git a/lib/native/query.js b/lib/native/query.js index c6dc0f9..e0d2df3 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -82,7 +82,7 @@ p.streamData = function (connection) { p.handleCopyFromChunk = function (chunk) { if ( this.stream ) { this.stream.handleChunk(chunk); - } + } //if there are no stream (for example when copy to query was sent by //query method instead of copyTo) error will be handled //on copyOutResponse event, so silently ignore this error here diff --git a/src/binding.cc b/src/binding.cc index c26adc1..bc76cd4 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -487,7 +487,7 @@ protected: copied = PQgetCopyData(connection_, &buffer, 1); while (copied > 0) { chunk = Buffer::New(buffer, copied); - Handle node_chunk = chunk->handle_; + Local node_chunk = Local::New(chunk->handle_); Emit("copyData", &node_chunk); PQfreemem(buffer); copied = PQgetCopyData(connection_, &buffer, 1); diff --git a/test/native/copyto-largedata-tests.js b/test/native/copyto-largedata-tests.js index 518514e..8c87948 100644 --- a/test/native/copyto-largedata-tests.js +++ b/test/native/copyto-largedata-tests.js @@ -9,15 +9,15 @@ test("COPY TO large amount of data from postgres", function () { var con = new Client(helper.config), rowCount = 100000, stdoutStream = con.copyTo('COPY (select generate_series(1, ' + rowCount + ')) TO STDOUT'); - con.connect(); stdoutStream.on('data', function () { - rowCount --; + rowCount--; }); stdoutStream.on('end', function () { - assert.equal(rowCount, 1, "copy to should load exactly requested number of rows" + rowCount); + assert.equal(rowCount, 0, "copy to should load exactly requested number of rows"); con.query("SELECT 1", assert.calls(function (error, result) { assert.ok(!error && result, "loading large amount of data by copy to should not break connection"); con.end(); })); }); + con.connect(); });