Skip to content

Commit b59e004

Browse files
committed
native bindings compatible with v0.5.x
1 parent 675bb52 commit b59e004

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

lib/native/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
var EventEmitter = require('events').EventEmitter;
33
var utils = require(__dirname + "/../utils");
44

5-
var binding = require(__dirname + '/../../build/default/binding');
5+
var binding;
6+
7+
try{
8+
//v0.5.x
9+
binding = require(__dirname + '/../../build/Release/binding.node');
10+
} catch(e) {
11+
//v0.4.x
12+
binding = require(__dirname + '/../../build/default/binding');
13+
}
14+
615
var Connection = binding.Connection;
716
var types = require(__dirname + "/../types");
817
var NativeQuery = require(__dirname + '/query');
918

19+
var EventEmitter = require('events').EventEmitter;
1020
var p = Connection.prototype;
21+
for(var k in EventEmitter.prototype) {
22+
p[k] = EventEmitter.prototype[k];
23+
}
1124

1225
var nativeConnect = p.connect;
1326

src/binding.cc

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include <libpq-fe.h>
22
#include <node.h>
3-
#include <node_events.h>
43
#include <string.h>
54
#include <assert.h>
65
#include <stdlib.h>
76

8-
#define LOG(msg) printf("%s\n",msg)
7+
#define LOG(msg) printf("%s\n",msg);
98
#define TRACE(msg) //printf("%s\n", msg);
109

1110

@@ -14,11 +13,6 @@
1413
using namespace v8;
1514
using namespace node;
1615

17-
static Persistent<String> connect_symbol;
18-
static Persistent<String> error_symbol;
19-
static Persistent<String> ready_symbol;
20-
static Persistent<String> row_symbol;
21-
static Persistent<String> notice_symbol;
2216
static Persistent<String> severity_symbol;
2317
static Persistent<String> code_symbol;
2418
static Persistent<String> detail_symbol;
@@ -35,8 +29,9 @@ static Persistent<String> value_symbol;
3529
static Persistent<String> type_symbol;
3630
static Persistent<String> channel_symbol;
3731
static Persistent<String> payload_symbol;
32+
static Persistent<String> emit_symbol;
3833

39-
class Connection : public EventEmitter {
34+
class Connection : public ObjectWrap {
4035

4136
public:
4237

@@ -47,15 +42,10 @@ class Connection : public EventEmitter {
4742
HandleScope scope;
4843
Local<FunctionTemplate> t = FunctionTemplate::New(New);
4944

50-
t->Inherit(EventEmitter::constructor_template);
5145
t->InstanceTemplate()->SetInternalFieldCount(1);
5246
t->SetClassName(String::NewSymbol("Connection"));
5347

54-
connect_symbol = NODE_PSYMBOL("connect");
55-
error_symbol = NODE_PSYMBOL("_error");
56-
ready_symbol = NODE_PSYMBOL("_readyForQuery");
57-
notice_symbol = NODE_PSYMBOL("notice");
58-
row_symbol = NODE_PSYMBOL("_row");
48+
emit_symbol = NODE_PSYMBOL("emit");
5949
severity_symbol = NODE_PSYMBOL("severity");
6050
code_symbol = NODE_PSYMBOL("code");
6151
detail_symbol = NODE_PSYMBOL("detail");
@@ -229,7 +219,7 @@ class Connection : public EventEmitter {
229219
ev_io write_watcher_;
230220
PGconn *connection_;
231221
bool connecting_;
232-
Connection () : EventEmitter ()
222+
Connection () : ObjectWrap ()
233223
{
234224
connection_ = NULL;
235225
connecting_ = false;
@@ -348,7 +338,7 @@ class Connection : public EventEmitter {
348338
{
349339
HandleScope scope;
350340
Handle<Value> notice = String::New(message);
351-
Emit(notice_symbol, 1, &notice);
341+
Emit("notice", &notice);
352342
}
353343

354344
//called to process io_events from libev
@@ -386,7 +376,7 @@ class Connection : public EventEmitter {
386376
}
387377
//might have fired from notification
388378
if(didHandleResult) {
389-
Emit(ready_symbol, 0, NULL);
379+
Emit("_readyForQuery");
390380
}
391381
}
392382

@@ -396,7 +386,7 @@ class Connection : public EventEmitter {
396386
result->Set(channel_symbol, String::New(notify->relname));
397387
result->Set(payload_symbol, String::New(notify->extra));
398388
Handle<Value> res = (Handle<Value>)result;
399-
Emit((Handle<String>)String::New("notification"), 1, &res);
389+
Emit("notification", &res);
400390
PQfreemem(notify);
401391
}
402392

@@ -464,7 +454,7 @@ class Connection : public EventEmitter {
464454

465455
//not sure about what to dealloc or scope#Close here
466456
Handle<Value> e = (Handle<Value>)row;
467-
Emit(row_symbol, 1, &e);
457+
Emit("_row", &e);
468458
}
469459
}
470460

@@ -487,7 +477,7 @@ class Connection : public EventEmitter {
487477
AttachErrorField(result, msg, line_symbol, PG_DIAG_SOURCE_LINE);
488478
AttachErrorField(result, msg, routine_symbol, PG_DIAG_SOURCE_FUNCTION);
489479
Handle<Value> m = msg;
490-
Emit(error_symbol, 1, &m);
480+
Emit("_error", &m);
491481
}
492482

493483
void AttachErrorField(const PGresult *result, const Local<Object> msg, const Persistent<String> symbol, int fieldcode)
@@ -506,6 +496,33 @@ class Connection : public EventEmitter {
506496
}
507497

508498
private:
499+
//EventEmitter was removed from c++ in node v0.5.x
500+
void Emit(char* message) {
501+
HandleScope scope;
502+
Handle<Value> args[1] = { String::New(message) };
503+
Emit(1, args);
504+
}
505+
506+
void Emit(char* message, Handle<Value>* arg) {
507+
HandleScope scope;
508+
Handle<Value> args[2] = { String::New(message), *arg };
509+
Emit(2, args);
510+
}
511+
512+
void Emit(int length, Handle<Value> *args) {
513+
HandleScope scope;
514+
515+
Local<Value> emit_v = this->handle_->Get(emit_symbol);
516+
assert(emit_v->IsFunction());
517+
Local<Function> emit_f = emit_v.As<Function>();
518+
519+
TryCatch tc;
520+
emit_f->Call(this->handle_, length, args);
521+
if(tc.HasCaught()) {
522+
FatalException(tc);
523+
}
524+
}
525+
509526
void HandleConnectionIO()
510527
{
511528
PostgresPollingStatusType status = PQconnectPoll(connection_);
@@ -530,7 +547,7 @@ class Connection : public EventEmitter {
530547
TRACE("Polled: PGRES_POLLING_OK");
531548
connecting_ = false;
532549
StartRead();
533-
Emit(connect_symbol, 0, NULL);
550+
Emit("connect");
534551
default:
535552
//printf("Unknown polling status: %d\n", status);
536553
break;
@@ -540,7 +557,7 @@ class Connection : public EventEmitter {
540557
void EmitError(const char *message)
541558
{
542559
Local<Value> exception = Exception::Error(String::New(message));
543-
Emit(error_symbol, 1, &exception);
560+
Emit("_error", &exception);
544561
}
545562

546563
void EmitLastError()
@@ -624,8 +641,7 @@ class Connection : public EventEmitter {
624641
};
625642

626643

627-
extern "C" void
628-
init (Handle<Object> target)
644+
extern "C" void init (Handle<Object> target)
629645
{
630646
HandleScope scope;
631647
Connection::Init(target);

0 commit comments

Comments
 (0)