Since it's very postgres specific I'd suggest it's added to DBD::Pg
as a set of new driver-specific methods:
$dbh = DBI->connect($data_source, $username, $auth, \%attr);
$dbh->pg_register_listener("foo",\&foo); # on "foo" notification call foo sub
$dbh->pg_register_listener("bar",\&foo); # on "bar" notification call foo sub
$dbh->pg_register_listener("bar", undef); # unregister callback for "bar"
$dbh->pg_register_listener("baz", sub { print "baz\n" } );
$dbh->pg_listen(); # loops forever waiting for events
Ideally pg_listen should integrate with existing perl event loops
so it can be used in Tk, POE etc.
Tim.
On Thu, Mar 29, 2007 at 07:06:13PM +0300, luben karavelov wrote:
> Martin Evans wrote:
> >I don't know exactly what postgres async notifications are but I know
> >DBD::ODBC has limited support for a asynchronous mode which is pretty
> >useful for picking up debug output etc from procedures.
> >
> >DBD::ODBC has odbc_async_exec flag which is described as:
> >
> >Allow asynchronous execution of queries. Right now, this causes a
> >spin-loop (with a small "sleep") until the sql is complete. This is
> >useful, however, if you want the error handling and asynchronous
> >messages (see the err_handler) below. See t/20SQLServer.t for an example
> >of this.
> >
> >Not sure if this helps you at all or not but you did ask about other
> >databases.
> >
> >Martin
>
> It is different concept. In psql you could register listener in one
> process with :
>
> LISTEN FOO;
>
> Then if another process executes
>
> NOTIFY FOO;
>
> The first process receives notification.
>
> I use it mostly to execute some code on the system when there is data
> inserted, deleted or updated in some table.
>
>
> Here is an example :
>
> use DBI;
> use DBI::Listen::pg;
>
> #Callback function
> sub foo {
> $dbh = shift;
> #... etc.
> }
>
> $dbh = DBI->connect($data_source, $username, $auth, \%attr);
> $dbl = DBI::Listen::pg->new($dbh);
> $dbl->register(???foo???,\&foo); # on "foo" notification call foo sub
> $dbl->register(???bar???,\&foo); # on "bar" notification call foo sub
> $dbl->unregister(???bar???); # unregister callback for "bar"
> $dbl->register("baz", sub { print "baz\n" } );
> $dbl->run(); # loops forever waiting for events
>
> #------ end
>
> NOTIFY could be executed by query or stored procedure/trigger.
>
>
> I hope this clarification helps
>
> luben
>
>
>
>
>
>
>