1
1
#include < libpq-fe.h>
2
2
#include < node.h>
3
- #include < node_events.h>
4
3
#include < string.h>
5
4
#include < assert.h>
6
5
#include < stdlib.h>
7
6
8
- #define LOG (msg ) printf(" %s\n " ,msg)
7
+ #define LOG (msg ) printf(" %s\n " ,msg);
9
8
#define TRACE (msg ) // printf("%s\n", msg);
10
9
11
10
14
13
using namespace v8 ;
15
14
using namespace node ;
16
15
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;
22
16
static Persistent<String> severity_symbol;
23
17
static Persistent<String> code_symbol;
24
18
static Persistent<String> detail_symbol;
@@ -35,8 +29,9 @@ static Persistent<String> value_symbol;
35
29
static Persistent<String> type_symbol;
36
30
static Persistent<String> channel_symbol;
37
31
static Persistent<String> payload_symbol;
32
+ static Persistent<String> emit_symbol;
38
33
39
- class Connection : public EventEmitter {
34
+ class Connection : public ObjectWrap {
40
35
41
36
public:
42
37
@@ -47,15 +42,10 @@ class Connection : public EventEmitter {
47
42
HandleScope scope;
48
43
Local<FunctionTemplate> t = FunctionTemplate::New (New);
49
44
50
- t->Inherit (EventEmitter::constructor_template);
51
45
t->InstanceTemplate ()->SetInternalFieldCount (1 );
52
46
t->SetClassName (String::NewSymbol (" Connection" ));
53
47
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" );
59
49
severity_symbol = NODE_PSYMBOL (" severity" );
60
50
code_symbol = NODE_PSYMBOL (" code" );
61
51
detail_symbol = NODE_PSYMBOL (" detail" );
@@ -229,7 +219,7 @@ class Connection : public EventEmitter {
229
219
ev_io write_watcher_;
230
220
PGconn *connection_;
231
221
bool connecting_;
232
- Connection () : EventEmitter ()
222
+ Connection () : ObjectWrap ()
233
223
{
234
224
connection_ = NULL ;
235
225
connecting_ = false ;
@@ -348,7 +338,7 @@ class Connection : public EventEmitter {
348
338
{
349
339
HandleScope scope;
350
340
Handle <Value> notice = String::New (message);
351
- Emit (notice_symbol, 1 , ¬ice);
341
+ Emit (" notice " , ¬ice);
352
342
}
353
343
354
344
// called to process io_events from libev
@@ -386,7 +376,7 @@ class Connection : public EventEmitter {
386
376
}
387
377
// might have fired from notification
388
378
if (didHandleResult) {
389
- Emit (ready_symbol, 0 , NULL );
379
+ Emit (" _readyForQuery " );
390
380
}
391
381
}
392
382
@@ -396,7 +386,7 @@ class Connection : public EventEmitter {
396
386
result->Set (channel_symbol, String::New (notify->relname ));
397
387
result->Set (payload_symbol, String::New (notify->extra ));
398
388
Handle <Value> res = (Handle <Value>)result;
399
- Emit (( Handle <String>) String::New ( " notification" ), 1 , &res);
389
+ Emit (" notification" , &res);
400
390
PQfreemem (notify);
401
391
}
402
392
@@ -464,7 +454,7 @@ class Connection : public EventEmitter {
464
454
465
455
// not sure about what to dealloc or scope#Close here
466
456
Handle <Value> e = (Handle <Value>)row;
467
- Emit (row_symbol, 1 , &e);
457
+ Emit (" _row " , &e);
468
458
}
469
459
}
470
460
@@ -487,7 +477,7 @@ class Connection : public EventEmitter {
487
477
AttachErrorField (result, msg, line_symbol, PG_DIAG_SOURCE_LINE);
488
478
AttachErrorField (result, msg, routine_symbol, PG_DIAG_SOURCE_FUNCTION);
489
479
Handle <Value> m = msg;
490
- Emit (error_symbol, 1 , &m);
480
+ Emit (" _error " , &m);
491
481
}
492
482
493
483
void AttachErrorField (const PGresult *result, const Local<Object> msg, const Persistent<String> symbol, int fieldcode)
@@ -506,6 +496,33 @@ class Connection : public EventEmitter {
506
496
}
507
497
508
498
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
+
509
526
void HandleConnectionIO ()
510
527
{
511
528
PostgresPollingStatusType status = PQconnectPoll (connection_);
@@ -530,7 +547,7 @@ class Connection : public EventEmitter {
530
547
TRACE (" Polled: PGRES_POLLING_OK" );
531
548
connecting_ = false ;
532
549
StartRead ();
533
- Emit (connect_symbol, 0 , NULL );
550
+ Emit (" connect " );
534
551
default :
535
552
// printf("Unknown polling status: %d\n", status);
536
553
break ;
@@ -540,7 +557,7 @@ class Connection : public EventEmitter {
540
557
void EmitError (const char *message)
541
558
{
542
559
Local<Value> exception = Exception::Error (String::New (message));
543
- Emit (error_symbol, 1 , &exception );
560
+ Emit (" _error " , &exception );
544
561
}
545
562
546
563
void EmitLastError ()
@@ -624,8 +641,7 @@ class Connection : public EventEmitter {
624
641
};
625
642
626
643
627
- extern " C" void
628
- init (Handle <Object> target)
644
+ extern " C" void init (Handle <Object> target)
629
645
{
630
646
HandleScope scope;
631
647
Connection::Init (target);
0 commit comments