pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex Pilosov <alex@pilosoft.com>
To: e.mergl@bawue.de
Cc: pgsql-interfaces@postgresql.org
Subject: [PATCH] notify support for DBD::Pg
Date: Sun, 10 Jun 2001 15:59:45 -0400 (EDT)
Message-ID: <Pine.BSO.4.10.10106101552000.23782-200000@spider.pilosoft.com> (raw)

Edmund & all,

Attached is a patch to add support for asynchronous notifications to
DBD::Pg interface. 

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).

Edmund, please consider adding to next release.

Thanks

-alex

Attachments:

dbdpg.patch (text/plain, 4.9K) download diff -r --unified DBD-Pg-1.00/Pg.pm DBD-Pg-1.00.alex/Pg.pm --- DBD-Pg-1.00/Pg.pm Sun May 27 10:10:13 2001 +++ DBD-Pg-1.00.alex/Pg.pm Sun Jun 10 15:14:32 2001 @@ -629,6 +629,15 @@ dump a complete table. See test.pl for an example on how to use this function. + $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'). =back diff -r --unified DBD-Pg-1.00/Pg.xs DBD-Pg-1.00.alex/Pg.xs --- DBD-Pg-1.00/Pg.xs Mon Jul 10 13:47:51 2000 +++ DBD-Pg-1.00.alex/Pg.xs Sun Jun 10 15:12:35 2001 @@ -83,6 +83,24 @@ } void +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 commit(dbh) SV * dbh CODE: diff -r --unified DBD-Pg-1.00/dbdimp.c DBD-Pg-1.00.alex/dbdimp.c --- DBD-Pg-1.00/dbdimp.c Sun May 27 10:10:13 2001 +++ DBD-Pg-1.00.alex/dbdimp.c Sun Jun 10 15:12:35 2001 @@ -197,6 +197,47 @@ } +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; +} + int dbd_db_ping (dbh) SV *dbh; diff -r --unified DBD-Pg-1.00/dbdimp.h DBD-Pg-1.00.alex/dbdimp.h --- DBD-Pg-1.00/dbdimp.h Mon Apr 9 13:44:18 2001 +++ DBD-Pg-1.00.alex/dbdimp.h Sun Jun 10 15:12:35 2001 @@ -70,4 +70,6 @@ }; +SV * dbd_db_notifies (SV *dbh, imp_dbh_t *imp_dbh); + /* end of dbdimp.h */ diff -r --unified DBD-Pg-1.00/test.pl DBD-Pg-1.00.alex/test.pl --- DBD-Pg-1.00/test.pl Sun May 27 10:10:13 2001 +++ DBD-Pg-1.00.alex/test.pl Sun Jun 10 15:38:09 2001 @@ -40,7 +40,7 @@ my $dsn_main = "dbi:Pg:dbname=$dbmain"; my $dsn_test = "dbi:Pg:dbname=$dbtest"; -my ($dbh0, $dbh, $sth); +my ($dbh0, $dbh, $dbh1, $sth); #DBI->trace(3); # make your choice @@ -445,16 +445,56 @@ # end transaction $dbh->{AutoCommit} = 1; +# compare large objects + ( $dbh->func($lobjId, 'lo_unlink') ) and print "\$dbh->func(lo_unlink) ...... ok\n" or print "\$dbh->func(lo_unlink) ...... not ok\n"; -# compare large objects - ( $pgin cmp $buf and $pgin cmp $blob ) and print "compare blobs .............. not ok\n" or print "compare blobs .............. ok\n"; +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 $nfound = select( $rout=$rin, undef, undef, 1); + +( $nfound==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"; + ######################### disconnect and drop test database # disconnect @@ -462,6 +502,10 @@ ( $dbh->disconnect ) and print "\$dbh->disconnect ........... ok\n" or die "\$dbh->disconnect ........... not ok: ", $DBI::errstr; + +( $dbh1->disconnect ) + and print "\$dbh1->disconnect .......... ok\n" + or die "\$dbh1->disconnect .......... not ok: ", $DBI::errstr; $dbh0->do("DROP DATABASE $dbtest"); $dbh0->disconnect;

Message-ID: <Pine.BSO.4.10.10106101552000.23782-200000@spider.pilosoft.com>
Permalink:  ../Pine.BSO.4.10.10106101552000.23782-200000@spider.pilosoft.com/
Also on:    postgresql.org/message-id/Pine.BSO.4.10.10106101552000.23782-200000@spider.pilosoft.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: alex@pilosoft.com, e.mergl@bawue.de
  Subject: Re: [PATCH] notify support for DBD::Pg
  In-Reply-To: <Pine.BSO.4.10.10106101552000.23782-200000@spider.pilosoft.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox