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": { diff --git a/src/connection.cc b/src/connection.cc index d2bd33d..d5fa302 100644 --- a/src/connection.cc +++ b/src/connection.cc @@ -786,8 +786,10 @@ 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); 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(); + }); + }); + +});