[PATCH] notify support for DBD::Pg

From: Benjamin(dot)Feinstein(at)guardent(dot)com
To: pgsql-interfaces(at)postgresql(dot)org
Cc: alex(at)pilosoft(dot)com, E(dot)Mergl(at)bawue(dot)de
Subject: [PATCH] notify support for DBD::Pg
Date: 2002-01-10 00:05:53
Message-ID: 397E0659AA2DD411843500508B64F1CE0287DE1F@USBOSMX01
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Hey ya'll,

Attached is a patch to add support for asynchronous client notifications to
the DBD::Pg interface. The original patch was written by Alex Pilosov
<alex(at)pilosoft(dot)com> and sent to this list on 10 Jun 2001. This is an updated
version that fixes some problems I encountered when running Alex's patch
against DBD-Pg-1.01. Alex's original patch was generated from a diff of
DBD-Pg-1.00.

The following description of the patch is from Alex's original email:

Added following functions (self-documented in Pg.pm):

$ret = $dbh->func('notifies');

Returns either undef or a reference to two-element array
[ $table, $backend_pid ] of asynchronous notifications received.

$fd = $dbh->func('getfd');

Returns fd of the actual connection to server. Can be used with
select() and func('notifies').

Also modified test.pl to test all of this functionality in the following
sequence (listen, select, notify, select).

Cheers,
Ben

diff -r DBD-Pg-1.01/Pg.pm DBD-Pg-1.01.ben/Pg.pm
631a632,640
> $ret = $dbh->func('notifies');
>
> Returns either undef or a reference to two-element array
> [ $table, $backend_pid ] of asynchronous notifications received.
>
> $fd = $dbh->func('getfd');
>
> Returns fd of the actual connection to server. Can be used with
> select() and func('notifies').
diff -r DBD-Pg-1.01/Pg.xs DBD-Pg-1.01.ben/Pg.xs
85a86,103
> getfd(dbh)
> SV * dbh
> CODE:
> int ret;
> D_imp_dbh(dbh);
>
> ret = dbd_db_getfd(dbh, imp_dbh);
> ST(0) = sv_2mortal( newSViv( ret ) );
>
> void
> notifies(dbh)
> SV * dbh
> CODE:
> D_imp_dbh(dbh);
>
> ST(0) = dbd_db_notifies(dbh, imp_dbh);
>
> void
diff -r DBD-Pg-1.01/dbdimp.c DBD-Pg-1.01.ben/dbdimp.c
199a200,240
> int
> dbd_db_getfd (dbh, imp_dbh)
> SV *dbh;
> imp_dbh_t *imp_dbh;
> {
> char id;
> SV* retsv;
>
> if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_getfd\n"); }
>
> return PQsocket(imp_dbh->conn);
> }
>
> SV *
> dbd_db_notifies (dbh, imp_dbh)
> SV *dbh;
> imp_dbh_t *imp_dbh;
> {
> char id;
> PGnotify* notify;
> AV* ret;
> SV* retsv;
>
> if (dbis->debug >= 1) { fprintf(DBILOGFP, "dbd_db_notifies\n"); }
>
> PQconsumeInput(imp_dbh->conn);
>
> notify = PQnotifies(imp_dbh->conn);
>
> if (!notify) return &sv_undef;
>
> ret=newAV();
>
> av_push(ret, newSVpv(notify->relname,0) );
> av_push(ret, newSViv(notify->be_pid) );
>
> retsv = newRV(sv_2mortal((SV*)ret));
>
> return retsv;
> }
>
diff -r DBD-Pg-1.01/dbdimp.h DBD-Pg-1.01.ben/dbdimp.h
72a73,74
> SV * dbd_db_notifies (SV *dbh, imp_dbh_t *imp_dbh);
>
diff -r DBD-Pg-1.01/test.pl DBD-Pg-1.01.ben/test.pl
43c43
< my ($dbh0, $dbh, $sth);
---
> my ($dbh0, $dbh, $dbh1, $sth);
447a448,449
> # compare large objects
>
452,453d453
< # compare large objects
<
457a458,497
> my $fd;
> ( $fd=$dbh->func( 'getfd') )
> and print "\$dbh->func(getfd) .......... ok\n"
> or print "\$dbh->func(getfd) .......... not ok\n";
>
> ( $dbh->do( 'LISTEN test ') )
> and print "\$dbh->do('LISTEN test') .... ok\n"
> or print "\$dbh->do('LISTEN test') .... not ok\n";
>
> ( $dbh1 = DBI->connect("$dsn_test", '', '', { AutoCommit => 1 }) )
> and print "DBI->connect (for notify)... ok\n"
> or die "DBI->connect (for notify)... not ok: ", $DBI::errstr;
>
> # there should be no data for read on $fd , until we send a notify
>
> my $rout;
> my $rin = '';
> vec($rin,$fd,1) = 1;
> my $nfound = select( $rout=$rin, undef, undef, 0);
>
> ( $nfound==0 )
> and print "select(\$fd) returns no data. ok\n"
> or die "select(\$fd) returns no data. not ok\n";
>
> ( $dbh1->do( 'NOTIFY test ') )
> and print "\$dbh1->do('NOTIFY test') ... ok\n"
> or print "\$dbh1->do('NOTIFY test') ... not ok\n";
>
> my $nfound2 = select( $rout=$rin, undef, undef, 1);
>
> ( $nfound2==1 )
> and print "select(\$fd) returns data.... ok\n"
> or die "select(\$fd) returns data.... not ok\n";
>
> my $notify_r;
>
> ( $notify_r = $dbh->func('notifies') )
> and print "\$dbh->func('notifies')...... ok\n"
> or die "\$dbh->func('notifies')...... not ok\n";
>
464a505,508
>
> ( $dbh1->disconnect )
> and print "\$dbh1->disconnect .......... ok\n"
> or die "\$dbh1->disconnect .......... not ok: ", $DBI::errstr;

> Ben Feinstein
> Software Development Engineer, R & D
> W: 678.585.7865 x6726 F: 770.645.8311 M: 678.772.4126
> 8302 Dunwoody Pl., Suite 320, Atlanta, GA 30350 www.guardent.com
> _____________________________________________________
> G U A R D E N T
> Enterprise Security and Privacy Programs
>

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Bruce Momjian 2002-01-10 00:28:39 Re: [PATCH] notify support for DBD::Pg
Previous Message Tim Nelson 2002-01-09 17:06:52 [ECPG] generates bad code on declare/open