From a8063be6a518a10d2ebc5fa92c258f43056f5ca5 Mon Sep 17 00:00:00 2001 From: Sasha Aliashkevich Date: Wed, 17 Jun 2015 18:53:51 +0200 Subject: [PATCH 1/3] custom handler for server side notices/warnings --- src/connection.cc | 22 ++++++++++++++++++---- src/connection.h | 4 +++- test/notice.js | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/notice.js diff --git a/src/connection.cc b/src/connection.cc index d2bd33d..3ecab21 100644 --- a/src/connection.cc +++ b/src/connection.cc @@ -789,6 +789,8 @@ bool Connection::ConnectDB(const char* paramString) { uv_poll_init(uv_default_loop(), &(this->read_watcher), fd); uv_poll_init(uv_default_loop(), &(this->write_watcher), fd); + PQsetNoticeProcessor(this->pq, NoticeProcessor, (void *) this); + TRACE("Connection::ConnectSync::Success"); return true; } @@ -900,7 +902,11 @@ void Connection::DeleteCStringArray(char** array, int length) { delete [] array; } -void Connection::Emit(const char* message) { +void Connection::Emit(const char* event) { + this->EmitMessage(event, ""); +} + +void Connection::EmitMessage(const char* event, const char* message) { NanScope(); TRACE("ABOUT TO EMIT EVENT"); @@ -910,13 +916,21 @@ void Connection::Emit(const char* message) { assert(emit_v->IsFunction()); v8::Local emit_f = emit_v.As(); - v8::Local eventName = NanNew(message); - v8::Handle args[1] = { eventName }; + v8::Local eventName = NanNew(event); + v8::Local eventMessage = NanNew(message); + v8::Handle args[2] = { eventName, eventMessage }; TRACE("CALLING EMIT"); v8::TryCatch tc; - emit_f->Call(NanObjectWrapHandle(this), 1, args); + emit_f->Call(NanObjectWrapHandle(this), 2, args); if(tc.HasCaught()) { node::FatalException(tc); } } + +void Connection::NoticeProcessor(void *arg, const char *message) +{ + fprintf(stderr, "%s", message); + + ((Connection *) arg)->EmitMessage("notice", message); +} diff --git a/src/connection.h b/src/connection.h index cbd1b77..4983ca9 100644 --- a/src/connection.h +++ b/src/connection.h @@ -76,7 +76,9 @@ class Connection : public node::ObjectWrap { static char* NewCString(v8::Handle val); static char** NewCStringArray(v8::Handle jsParams); static void DeleteCStringArray(char** array, int length); - void Emit(const char* message); + void Emit(const char* event); + void EmitMessage(const char* event, const char* message); + static void NoticeProcessor(void *arg, const char *message); }; #endif diff --git a/test/notice.js b/test/notice.js new file mode 100644 index 0000000..ffd419f --- /dev/null +++ b/test/notice.js @@ -0,0 +1,19 @@ +var PQ = require('../') +var assert = require('assert'); + +describe('server notices', function() { + it('works', function(done) { + var pq = new PQ(); + pq.connect(function(err) { + assert.ifError(err); + notices = [] + pq.on('notice', function(msg){notices.push(msg);}); + pq.exec("DO $$BEGIN RAISE NOTICE 'test1'; RAISE WARNING 'test2'; END;$$"); + assert.equal(notices.length, 2); + assert.equal(notices[0], 'NOTICE: test1\n'); + assert.equal(notices[1], 'WARNING: test2\n'); + done(); + }); + }); + +}); From 25e6de241c306bc71c73325b3b94a9df2fba3e62 Mon Sep 17 00:00:00 2001 From: Sasha Aliashkevich Date: Fri, 26 Feb 2016 10:55:31 +0100 Subject: [PATCH 2/3] uv_poll_init_socket isntead of uv_poll_init --- src/connection.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connection.cc b/src/connection.cc index 3ecab21..d5fa302 100644 --- a/src/connection.cc +++ b/src/connection.cc @@ -786,8 +786,8 @@ bool Connection::ConnectDB(const char* paramString) { } int fd = PQsocket(this->pq); - uv_poll_init(uv_default_loop(), &(this->read_watcher), fd); - uv_poll_init(uv_default_loop(), &(this->write_watcher), fd); + uv_poll_init_socket(uv_default_loop(), &(this->read_watcher), fd); + uv_poll_init_socket(uv_default_loop(), &(this->write_watcher), fd); PQsetNoticeProcessor(this->pq, NoticeProcessor, (void *) this); From c05e29a292f519161da49bef00c4f932bd0605cb Mon Sep 17 00:00:00 2001 From: Sasha Aliashkevich Date: Sun, 27 Nov 2016 20:48:11 +0100 Subject: [PATCH 3/3] Bump node-nan to 2.3.x - adds support for node@6.x --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 041fc41..0d34a1c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "author": "Brian M. Carlson", "license": "MIT", "dependencies": { - "nan": "^1.8.4", + "nan": "^2.3.0", "bindings": "1.2.1" }, "devDependencies": {