From e4ce36bda800be140c98115e85a30cd90cb6df90 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Thu, 14 Apr 2011 22:53:44 -0500 Subject: [PATCH] support for 'payload' of notification in postgres >=9.0 --- src/binding.cc | 7 ++++++- test/integration/client/notice-tests.js | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 3a008aa4..724c39b3 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -34,6 +34,8 @@ static Persistent routine_symbol; static Persistent name_symbol; static Persistent value_symbol; static Persistent type_symbol; +static Persistent channel_symbol; +static Persistent payload_symbol; class Connection : public EventEmitter { @@ -70,6 +72,8 @@ public: name_symbol = NODE_PSYMBOL("name"); value_symbol = NODE_PSYMBOL("value"); type_symbol = NODE_PSYMBOL("type"); + channel_symbol = NODE_PSYMBOL("channel"); + payload_symbol = NODE_PSYMBOL("payload"); NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); @@ -390,7 +394,8 @@ protected: PGnotify *notify; while ((notify = PQnotifies(connection_))) { Local result = Object::New(); - result->Set(String::New("channel"), String::New(notify->relname)); + result->Set(channel_symbol, String::New(notify->relname)); + result->Set(payload_symbol, String::New(notify->extra)); Handle res = (Handle)result; Emit((Handle)String::New("notification"), 1, &res); PQfreemem(notify); diff --git a/test/integration/client/notice-tests.js b/test/integration/client/notice-tests.js index b78ac6e0..f2f059ce 100644 --- a/test/integration/client/notice-tests.js +++ b/test/integration/client/notice-tests.js @@ -16,10 +16,16 @@ test('emits notify message', function() { client.query('LISTEN boom', assert.calls(function() { var otherClient = helper.client(); otherClient.query('LISTEN boom', assert.calls(function() { - client.query('NOTIFY boom'); + client.query("NOTIFY boom, 'omg!'"); assert.emits(client, 'notification', function(msg) { - assert.equal(msg.channel, 'boom'); - client.end() + + //make sure PQfreemem doesn't invalidate string pointers + setTimeout(function() { + assert.equal(msg.channel, 'boom'); + assert.ok(msg.payload == 'omg!' /*9.x*/ || msg.payload == '' /*8.x*/, "expected blank payload or correct payload but got " + msg.message) + client.end() + }, 500) + }); assert.emits(otherClient, 'notification', function(msg) { assert.equal(msg.channel, 'boom');