Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom handler for server side notices/warnings #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Brian M. Carlson",
"license": "MIT",
"dependencies": {
"nan": "^1.8.4",
"nan": "^2.3.0",
"bindings": "1.2.1"
},
"devDependencies": {
Expand Down
26 changes: 20 additions & 6 deletions src/connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -910,13 +916,21 @@ void Connection::Emit(const char* message) {
assert(emit_v->IsFunction());
v8::Local<v8::Function> emit_f = emit_v.As<v8::Function>();

v8::Local<v8::String> eventName = NanNew<v8::String>(message);
v8::Handle<v8::Value> args[1] = { eventName };
v8::Local<v8::String> eventName = NanNew<v8::String>(event);
v8::Local<v8::String> eventMessage = NanNew<v8::String>(message);
v8::Handle<v8::Value> 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);
}
4 changes: 3 additions & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class Connection : public node::ObjectWrap {
static char* NewCString(v8::Handle<v8::Value> val);
static char** NewCStringArray(v8::Handle<v8::Array> 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
19 changes: 19 additions & 0 deletions test/notice.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

});