Author: turnstep
Date: Sun Apr 13 18:59:08 2008
New Revision: 11073

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/t/03dbmethod.t

Log:
Elevate pg_notifies to a real function. Add some real tests for LISTEN/NOTIFY.
Instead of $dbh->func('pg_notifies')', you can simply say $dbh->pg_notifies;


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes        (original)
+++ DBD-Pg/trunk/Changes        Sun Apr 13 18:59:08 2008
@@ -2,6 +2,8 @@
 
 2.5.2 Released April, 2008
 
+       - Make pg_notifies a true function, so that you can now
+               use $dbh->pg_notifies instead of $dbh->func('pg_notifies') [GSM]
        - Fix minor build issue with Strawberry Perl [GSM]
        - Various performance improvements [GSM]
 

Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm  (original)
+++ DBD-Pg/trunk/Pg.pm  Sun Apr 13 18:59:08 2008
@@ -124,6 +124,7 @@
                DBD::Pg::db->install_method("pg_getline");
                DBD::Pg::db->install_method("pg_getcopydata");
                DBD::Pg::db->install_method("pg_getcopydata_async");
+               DBD::Pg::db->install_method("pg_notifies");
                DBD::Pg::db->install_method("pg_putcopydata");
                DBD::Pg::db->install_method("pg_putcopyend");
                DBD::Pg::db->install_method("pg_ping");
@@ -2043,39 +2044,11 @@
 Exports a large object into a Unix file. Returns false upon failure, true
 otherwise.
 
-=item pg_notifies
-
-  $ret = $dbh->func('pg_notifies');
-
-Returns either C<undef> or a reference to two-element array [ $table,
-$backend_pid ] of asynchronous notifications received. Note that this does
-not check if the connection to the database is still valid - for that, 
-use the c<ping> method. Also note that you may need to commit if not in 
-autocommit mode - new notices will not be picked up while in the middle of 
-a transaction. An example:
-
-  $dbh->do("LISTEN abc");
-  $dbh->do("LISTEN def");
-
-  ## Hang around until we get the message we want
-  LISTENLOOP: {
-    while (my $notify = $dbh->func('pg_notifies')) {
-      my ($name, $pid) = @$notify;
-      print qq{I received notice "$name" from PID $pid\n};
-      ## Do something based on the notice received
-    }
-    $dbh->ping() or die qq{Ping failed!};
-    $dbh->commit();
-    sleep(5);
-    redo;
-  }
-
 =item getfd
 
   $fd = $dbh->func('getfd');
 
-Returns fd of the actual connection to server. Can be used with select() and
-func('pg_notifies'). Deprecated in favor of C<< $dbh->{pg_socket} >>.
+Deprecated, use C<< $dbh->{pg_socket} >> instead.
 
 =back
 
@@ -2542,6 +2515,33 @@
 
 Supported by this driver as proposed by DBI.
 
+=item pg_notifies
+
+  $ret = $dbh->pg_notifies;
+
+Returns either C<undef> or a reference to two-element array [ $table,
+$backend_pid ] of asynchronous notifications received. Note that this does
+not check if the connection to the database is still valid - for that,
+use the c<ping> method. Also note that you may need to commit if not in
+autocommit mode - new notices will not be picked up while in the middle of
+a transaction. An example:
+
+  $dbh->do("LISTEN abc");
+  $dbh->do("LISTEN def");
+
+  ## Hang around until we get the message we want
+  LISTENLOOP: {
+    while (my $notify = $dbh->pg_notifies) {
+      my ($name, $pid) = @$notify;
+      print qq{I received notice "$name" from PID $pid\n};
+      ## Do something based on the notice received
+    }
+    $dbh->ping() or die qq{Ping failed!};
+    $dbh->commit();
+    sleep(5);
+    redo;
+  }
+
 =item B<ping>
 
   $rc = $dbh->ping;

Modified: DBD-Pg/trunk/t/03dbmethod.t
==============================================================================
--- DBD-Pg/trunk/t/03dbmethod.t (original)
+++ DBD-Pg/trunk/t/03dbmethod.t Sun Apr 13 18:59:08 2008
@@ -25,7 +25,7 @@
 if (! defined $dbh) {
        plan skip_all => 'Connection to database failed, cannot continue 
testing';
 }
-plan tests => 218;
+plan tests => 220;
 
 isnt( $dbh, undef, 'Connect to database for database handle method testing');
 
@@ -1151,15 +1151,26 @@
 # Test of the "pg_notifies" database handle method
 #
 
-#   [-Perl::Critic::Policy::Bangs::ProhibitCommentedOutCode]$ret = 
$dbh->func('pg_notifies');
-# Returns either undef or a reference to two-element array [ $table,
-# $backend_pid ] of asynchronous notifications received.
-
 eval {
   $dbh->func('pg_notifies');
 };
 is( $@, q{}, 'DB handle method "pg_notifies" does not throw an error');
 
+$t=q{DB handle method "pg_notifies" (func) returns the correct values};
+my $notify_name = 'dbdpg_notify_test';
+my $pid = $dbh->selectall_arrayref('SELECT pg_backend_pid()')->[0][0];
+$dbh->do("LISTEN $notify_name");
+$dbh->do("NOTIFY $notify_name");
+$dbh->commit();
+my $info = $dbh->func('pg_notifies');
+is_deeply($info, [$notify_name, $pid], $t);
+
+$t=q{DB handle method "pg_notifies" returns the correct values};
+$dbh->do("NOTIFY $notify_name");
+$dbh->commit();
+my $info = $dbh->pg_notifies;;
+is_deeply($info, [$notify_name, $pid], $t);
+
 #
 # Test of the "getfd" database handle method
 #

Reply via email to