From: "Peter Haworth" <pmh@edison.ioppublishing.com>
Subject: [PATCH] New pg_bool_tf attribute for DBD::Pg
To: dbi-dev@perl.org, dbdpg-general@gborg.postgresql.org
Date: Tue, 31 Dec 2002 18:47:40 +0000

------------=_1041360460-541-0
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Here's a patch against CVS to add a pg_bool_tf database handle attribute to
DBD::Pg, and a new test for it. This flag defaults to false, but when set to
true, booleans are returned as 't' and 'f', rather than '1' and '0'.

-- 
	Peter Haworth	pmh@edison.ioppublishing.com
"OK, see, the sad thing is that I really have no idea whether
 you're joking or not.  That's how wiggy this thread has gotten."
		-- Michael Lazzaro
------------=_1041360460-541-0
Content-Type: text/plain; name="pg.diff"
Content-Disposition: inline; filename="pg.diff"
Content-Transfer-Encoding: binary

diff -u -N -r DBD-Pg-1.20/dbdimp.c DBD-Pg-1.20-bool_tf/dbdimp.c
--- DBD-Pg-1.20/dbdimp.c	2002-11-27 02:02:39.000000000 +0000
+++ DBD-Pg-1.20-bool_tf/dbdimp.c	2002-12-31 18:03:35.000000000 +0000
@@ -191,6 +191,7 @@
 
     imp_dbh->init_commit = 1;			/* initialize AutoCommit */
     imp_dbh->pg_auto_escape = 1;		/* initialize pg_auto_escape */
+    imp_dbh->pg_bool_tf = 0;                    /* initialize pg_bool_tf */
 
     DBIc_IMPSET_on(imp_dbh);			/* imp_dbh set up now */
     DBIc_ACTIVE_on(imp_dbh);			/* call disconnect before freeing */
@@ -461,6 +462,8 @@
         return 1;
     } else if (kl==14 && strEQ(key, "pg_auto_escape")) {
         imp_dbh->pg_auto_escape = newval;
+    } else if (kl==10 && strEQ(key, "pg_bool_tf")) {
+	imp_dbh->pg_bool_tf = newval;
     } else {
         return 0;
     }
@@ -483,6 +486,8 @@
         retsv = boolSV(DBIc_has(imp_dbh, DBIcf_AutoCommit));
     } else if (kl==14 && strEQ(key, "pg_auto_escape")) {
         retsv = newSViv((IV)imp_dbh->pg_auto_escape);
+    } else if (kl==10 && strEQ(key, "pg_bool_tf")) {
+	retsv = newSViv((IV)imp_dbh->pg_bool_tf);
     } else if (kl==11 && strEQ(key, "pg_INV_READ")) {
         retsv = newSViv((IV)INV_READ);
     } else if (kl==12 && strEQ(key, "pg_INV_WRITE")) {
@@ -1326,6 +1331,7 @@
     SV *sth;
     imp_sth_t *imp_sth;
 {
+    D_imp_dbh_from_sth;
     int num_fields;
     int i;
     AV *av;
@@ -1357,7 +1363,7 @@
             char *val   = (char*)PQgetvalue(imp_sth->result, imp_sth->cur_tuple, i);
             int val_len = strlen(val);
             int  type   = PQftype(imp_sth->result, i); /* hopefully these hard coded values will not change */
-            if (16 == type) {
+            if (16 == type && ! imp_dbh->pg_bool_tf) {
                *val = (*val == 'f') ? '0' : '1'; /* bool: translate postgres into perl */
             }
             if (17 == type) {  /* decode \001 -> chr(1), etc, in-place */
diff -u -N -r DBD-Pg-1.20/dbdimp.h DBD-Pg-1.20-bool_tf/dbdimp.h
--- DBD-Pg-1.20/dbdimp.h	2002-11-27 10:21:46.000000000 +0000
+++ DBD-Pg-1.20-bool_tf/dbdimp.h	2002-12-31 17:57:00.000000000 +0000
@@ -22,6 +22,7 @@
     PGconn    * conn;		/* connection structure */
     int         init_commit;	/* initialize AutoCommit */
     int         pg_auto_escape;	/* initialize AutoEscape */
+    int         pg_bool_tf;     /* do bools return 't'/'f' */
 };
 
 /* Define sth implementor data structure */
diff -u -N -r DBD-Pg-1.20/t/16pgbooltf.t DBD-Pg-1.20-bool_tf/t/16pgbooltf.t
--- DBD-Pg-1.20/t/16pgbooltf.t	1970-01-01 01:00:00.000000000 +0100
+++ DBD-Pg-1.20-bool_tf/t/16pgbooltf.t	2002-12-31 18:30:32.000000000 +0000
@@ -0,0 +1,46 @@
+use strict;
+use DBI;
+use Test::More;
+
+if (defined $ENV{DBI_DSN}) {
+  plan tests => 10;
+} else {
+  plan skip_all => 'cannot test without DB info';
+}
+
+my $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS},
+                       {RaiseError => 1, AutoCommit => 0}
+                      );
+ok(defined $dbh,
+   'connect with transaction'
+  );
+
+ok(get('select 1=1') eq 1,'default true is 1');
+ok(get('select 1=0') eq 0,'default false is 0');
+ok(!defined get('select 1=null'),'null');
+
+$dbh->{pg_bool_tf}=0;
+
+ok(get('select 1=1') eq 1,'default true is 1');
+ok(get('select 1=0') eq 0,'default false is 0');
+ok(!defined get('select 1=null'),'null');
+
+$dbh->{pg_bool_tf}=1;
+
+ok(get('select 1=1') eq 't','tf true is t');
+ok(get('select 1=0') eq 'f','tf false is f');
+ok(!defined get('select 1=null'),'null');
+
+
+
+sub get{
+  my($sql)=@_;
+
+  my $sth=$dbh->prepare($sql);
+  $sth->execute;
+  my($ret)=$sth->fetchrow_array;
+  $sth->finish;
+
+  $ret;
+}
+

------------=_1041360460-541-0
Content-Type: text/plain; name="16pgbooltf.t"
Content-Disposition: inline; filename="16pgbooltf.t"
Content-Transfer-Encoding: binary

use strict;
use DBI;
use Test::More;

if (defined $ENV{DBI_DSN}) {
  plan tests => 10;
} else {
  plan skip_all => 'cannot test without DB info';
}

my $dbh = DBI->connect($ENV{DBI_DSN}, $ENV{DBI_USER}, $ENV{DBI_PASS},
                       {RaiseError => 1, AutoCommit => 0}
                      );
ok(defined $dbh,
   'connect with transaction'
  );

ok(get('select 1=1') eq 1,'default true is 1');
ok(get('select 1=0') eq 0,'default false is 0');
ok(!defined get('select 1=null'),'null');

$dbh->{pg_bool_tf}=0;

ok(get('select 1=1') eq 1,'default true is 1');
ok(get('select 1=0') eq 0,'default false is 0');
ok(!defined get('select 1=null'),'null');

$dbh->{pg_bool_tf}=1;

ok(get('select 1=1') eq 't','tf true is t');
ok(get('select 1=0') eq 'f','tf false is f');
ok(!defined get('select 1=null'),'null');



sub get{
  my($sql)=@_;

  my $sth=$dbh->prepare($sql);
  $sth->execute;
  my($ret)=$sth->fetchrow_array;
  $sth->finish;

  $ret;
}


------------=_1041360460-541-0--