Author: turnstep
Date: Tue Mar 18 10:39:19 2008
New Revision: 10937

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/t/03smethod.t

Log:
Add pg_bound and pg_numbound statement handle attributes, with tests.


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes        (original)
+++ DBD-Pg/trunk/Changes        Tue Mar 18 10:39:19 2008
@@ -1,7 +1,8 @@
 ('GSM' is Greg Sabino Mullane, [EMAIL PROTECTED])
 
-???
+2.3.0
 
+       - Add $sth->pg_bound() and $sth->pg_numbound [GSM]
        - Fix broken fetch of $sth->{pg_segments} [GSM]
 
 2.2.2 Released March 3, 2008 (subversion r10873)

Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm  (original)
+++ DBD-Pg/trunk/Pg.pm  Tue Mar 18 10:39:19 2008
@@ -3264,6 +3264,17 @@
 
 Not supported by this driver.
 
+=item B<pg_numbound>  (integer, read-only)
+
+PostgreSQL specific attribute. Returns the number of placeholders
+that are currently bound (via bind_param).
+
+=item B<pg_bound>  (hash-ref, read-only)
+
+PostgreSQL specific attribute. Returns a hash of all named placeholders. The
+key is the name of the placeholder, and the value is a 0 or a 1, indicating if
+the placeholder has been bound yet (e.g. via bind_param)
+
 =item B<pg_size>  (array-ref, read-only)
 
 PostgreSQL specific attribute. It returns a reference to an array of integer

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c       (original)
+++ DBD-Pg/trunk/dbdimp.c       Tue Mar 18 10:39:19 2008
@@ -868,6 +868,22 @@
        /* Some can be done before we have a result: */
        switch (kl) {
 
+       case 8: /* pg_bound */
+
+               if (strEQ("pg_bound", key)) {
+                       HV *pvhv = newHV();
+                       ph_t *currph;
+                       int i;
+                       for (i=0,currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph,i++) {
+                               (void)hv_store_ent 
+                                       (pvhv,
+                                        (3==imp_sth->placeholder_type ? 
newSVpv(currph->fooname,0) : newSViv(i+1)),
+                                        newSViv(NULL == currph->bind_type ? 0 
: 1), 0);
+                       }
+                       retsv = newRV_noinc((SV*)pvhv);
+               }
+               break;
+
        case 9: /* pg_direct */
 
                if (strEQ("pg_direct", key))
@@ -896,7 +912,7 @@
                }
                break;
 
-       case 11: /* ParamValues pg_segments */
+       case 11: /* ParamValues pg_segments pg_numbound */
 
                if (strEQ("ParamValues", key)) {
                        HV *pvhv = newHV();
@@ -927,6 +943,14 @@
                        }
                        retsv = newRV_noinc((SV*)arr);
                }
+               else if (strEQ("pg_numbound", key)) {
+                       ph_t *currph;
+                       int i = 0;
+                       for (currph=imp_sth->ph; NULL != currph; 
currph=currph->nextph) {
+                               i += NULL == currph->bind_type ? 0 : 1;
+                       }
+                       retsv = newSViv(i);
+               }
                break;
 
        case 14: /* pg_prepare_now */

Modified: DBD-Pg/trunk/t/03smethod.t
==============================================================================
--- DBD-Pg/trunk/t/03smethod.t  (original)
+++ DBD-Pg/trunk/t/03smethod.t  Tue Mar 18 10:39:19 2008
@@ -19,7 +19,7 @@
 if (! defined $dbh) {
        plan skip_all => 'Connection to database failed, cannot continue 
testing';
 }
-plan tests => 75;
+plan tests => 87;
 
 isnt( $dbh, undef, 'Connect to database for statement handle method testing');
 
@@ -470,6 +470,64 @@
 }
 
 
+#
+# Test of the statement handle method "pg_numbound"
+#
+
+$dbh->rollback();
+$t=q{Statement handle attribute pg_numbound returns 0 if no placeholders};
+$sth = $dbh->prepare('SELECT 123');
+is($sth->{pg_numbound}, 0, $t);
+
+$sth->execute();
+is($sth->{pg_numbound}, 0, $t);
+
+$t=q{Statement handle attribute pg_numbound returns 0 if no placeholders bound 
yet};
+$sth = $dbh->prepare('SELECT 123 WHERE 1 > ? AND 2 > ?');
+is($sth->{pg_numbound}, 0, $t);
+
+$t=q{Statement handle attribute pg_numbound returns 1 if one placeholder 
bound};
+$sth->bind_param(1, 123);
+is($sth->{pg_numbound}, 1, $t);
+
+$t=q{Statement handle attribute pg_numbound returns 2 if two placeholders 
bound};
+$sth->bind_param(2, 345);
+is($sth->{pg_numbound}, 2, $t);
+
+$t=q{Statement handle attribute pg_numbound returns 1 if one placeholders 
bound as NULL};
+$sth = $dbh->prepare('SELECT 123 WHERE 1 > ? AND 2 > ?');
+$sth->bind_param(1, undef);
+is($sth->{pg_numbound}, 1, $t);
+
+#
+# Test of the statement handle method "pg_bound"
+#
+
+$t=q{Statement handle attribute pg_bound returns an empty hash if no 
placeholders};
+$sth = $dbh->prepare('SELECT 123');
+is_deeply($sth->{pg_bound}, {}, $t);
+
+$sth->execute();
+is_deeply($sth->{pg_bound}, {}, $t);
+
+$t=q{Statement handle attribute pg_bound returns correct value if no 
placeholders bound yet};
+$sth = $dbh->prepare('SELECT 123 WHERE 1 > ? AND 2 > ?');
+is_deeply($sth->{pg_bound}, {1=>0, 2=>0}, $t);
+
+$t=q{Statement handle attribute pg_bound returns correct value if one 
placeholder bound};
+$sth->bind_param(2, 123);
+is_deeply($sth->{pg_bound}, {1=>0, 2=>1}, $t);
+
+$t=q{Statement handle attribute pg_bound returns correct value if two 
placeholders bound};
+$sth->bind_param(1, 123);
+is_deeply($sth->{pg_bound}, {1=>1, 2=>1}, $t);
+
+$t=q{Statement handle attribute pg_numbound returns 1 if one placeholders 
bound as NULL};
+$sth = $dbh->prepare('SELECT 123 WHERE 1 > ? AND 2 > ?');
+$sth->bind_param(1, undef);
+is_deeply($sth->{pg_bound}, {1=>1, 2=>0}, $t);
+
+
 cleanup_database($dbh,'test');
 $dbh->rollback();
 $dbh->disconnect();

Reply via email to