@@ -30,6 +30,7 @@ static Persistent<String> type_symbol;
30
30
static Persistent<String> channel_symbol;
31
31
static Persistent<String> payload_symbol;
32
32
static Persistent<String> emit_symbol;
33
+ static Persistent<String> command_symbol;
33
34
34
35
class Connection : public ObjectWrap {
35
36
@@ -62,6 +63,7 @@ class Connection : public ObjectWrap {
62
63
type_symbol = NODE_PSYMBOL (" type" );
63
64
channel_symbol = NODE_PSYMBOL (" channel" );
64
65
payload_symbol = NODE_PSYMBOL (" payload" );
66
+ command_symbol = NODE_PSYMBOL (" command" );
65
67
66
68
67
69
NODE_SET_PROTOTYPE_METHOD (t, " connect" , Connect);
@@ -437,32 +439,46 @@ class Connection : public ObjectWrap {
437
439
}
438
440
}
439
441
440
- void HandleResult (const PGresult* result)
442
+ void HandleResult (PGresult* result)
441
443
{
442
444
ExecStatusType status = PQresultStatus (result);
443
445
switch (status) {
444
446
case PGRES_TUPLES_OK:
445
- HandleTuplesResult (result);
447
+ {
448
+ HandleTuplesResult (result);
449
+ EmitCommandMetaData (result);
450
+ }
446
451
break ;
447
452
case PGRES_FATAL_ERROR:
448
453
HandleErrorResult (result);
449
454
break ;
450
455
case PGRES_COMMAND_OK:
451
- case PGRES_EMPTY_QUERY:
452
- // do nothing
456
+ case PGRES_EMPTY_QUERY:
457
+ EmitCommandMetaData (result);
453
458
break ;
454
459
default :
455
460
printf (" Unrecogized query status: %s\n " , PQresStatus (status));
456
461
break ;
457
462
}
458
463
}
459
464
465
+ void EmitCommandMetaData (PGresult* result)
466
+ {
467
+ HandleScope scope;
468
+ Local<Object> info = Object::New ();
469
+ info->Set (command_symbol, String::New (PQcmdStatus (result)));
470
+ info->Set (value_symbol, String::New (PQcmdTuples (result)));
471
+ Handle <Value> e = (Handle <Value>)info;
472
+ Emit (" _cmdStatus" , &e);
473
+ }
474
+
460
475
// maps the postgres tuple results to v8 objects
461
476
// and emits row events
462
477
// TODO look at emitting fewer events because the back & forth between
463
478
// javascript & c++ might introduce overhead (requires benchmarking)
464
479
void HandleTuplesResult (const PGresult* result)
465
480
{
481
+ HandleScope scope;
466
482
int rowCount = PQntuples (result);
467
483
for (int rowNumber = 0 ; rowNumber < rowCount; rowNumber++) {
468
484
// create result object for this row
@@ -489,7 +505,6 @@ class Connection : public ObjectWrap {
489
505
row->Set (Integer::New (fieldNumber), field);
490
506
}
491
507
492
- // not sure about what to dealloc or scope#Close here
493
508
Handle <Value> e = (Handle <Value>)row;
494
509
Emit (" _row" , &e);
495
510
}
@@ -564,30 +579,30 @@ class Connection : public ObjectWrap {
564
579
{
565
580
PostgresPollingStatusType status = PQconnectPoll (connection_);
566
581
switch (status) {
567
- case PGRES_POLLING_READING:
568
- TRACE (" Polled: PGRES_POLLING_READING" );
569
- StopWrite ();
570
- StartRead ();
571
- break ;
572
- case PGRES_POLLING_WRITING:
573
- TRACE (" Polled: PGRES_POLLING_WRITING" );
574
- StopRead ();
575
- StartWrite ();
576
- break ;
577
- case PGRES_POLLING_FAILED:
578
- StopRead ();
579
- StopWrite ();
580
- TRACE (" Polled: PGRES_POLLING_FAILED" );
581
- EmitLastError ();
582
- break ;
583
- case PGRES_POLLING_OK:
584
- TRACE (" Polled: PGRES_POLLING_OK" );
585
- connecting_ = false ;
586
- StartRead ();
587
- Emit (" connect" );
588
- default :
589
- // printf("Unknown polling status: %d\n", status);
590
- break ;
582
+ case PGRES_POLLING_READING:
583
+ TRACE (" Polled: PGRES_POLLING_READING" );
584
+ StopWrite ();
585
+ StartRead ();
586
+ break ;
587
+ case PGRES_POLLING_WRITING:
588
+ TRACE (" Polled: PGRES_POLLING_WRITING" );
589
+ StopRead ();
590
+ StartWrite ();
591
+ break ;
592
+ case PGRES_POLLING_FAILED:
593
+ StopRead ();
594
+ StopWrite ();
595
+ TRACE (" Polled: PGRES_POLLING_FAILED" );
596
+ EmitLastError ();
597
+ break ;
598
+ case PGRES_POLLING_OK:
599
+ TRACE (" Polled: PGRES_POLLING_OK" );
600
+ connecting_ = false ;
601
+ StartRead ();
602
+ Emit (" connect" );
603
+ default :
604
+ // printf("Unknown polling status: %d\n", status);
605
+ break ;
591
606
}
592
607
}
593
608
0 commit comments