diff --git a/Changes b/Changes index dee5aeea..964d2474 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,9 @@ RT refers to rt.cpan.org - Support binding native boolean false on Perl 5.36 and newer [Dagfinn Ilmari Mannsåker] + - Respect pg_bool_tf when binding native booleans on Perl 5.36 and newer + [Dagfinn Ilmari Mannsåker] + Version 3.18.0 (released December 6, 2023) - Support new PQclosePrepared function, added in Postgres 17 diff --git a/Pg.pm b/Pg.pm index 80c15f35..4c48d1ef 100644 --- a/Pg.pm +++ b/Pg.pm @@ -3234,7 +3234,9 @@ should the query fail (see C). =head3 B (boolean) DBD::Pg specific attribute. If true, boolean values will be returned -as the characters 't' and 'f' instead of '1' and '0'. +as the characters 't' and 'f' instead of '1' and '0'. On Perl 5.36 +and newer, distinguished boolean values (see L) will +also be sent as 't' and 'f' when used as placeholder values. =head3 B (boolean) @@ -4456,11 +4458,14 @@ Boolean values can be passed to PostgreSQL as TRUE, 't', 'true', 'y', 'yes' or '1' for true and FALSE, 'f', 'false', 'n', 'no' or '0' for false. On Perl 5.36 and newer, distinguished boolean values (see -L) can be used as placeholder values. On older -versions of Perl, false values returned by built-in operators (such -as C) must be converted to one of the above false values, or -bound with C<< pg_type => PG_BOOL >>, since they stringify to the empty -string. +L) can be used as placeholder values. They will be +sent as C<1> and C<0>, or C or C if C is set to a +true value. + +On older versions of Perl, false values returned by built-in operators +(such as C) must be converted to one of the above false values, +or bound with C<< pg_type => PG_BOOL >>, since they stringify to the +empty string. =head2 Schema support diff --git a/dbdimp.c b/dbdimp.c index 2bfab354..32211bb3 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -2630,8 +2630,10 @@ int dbd_bind_ph (SV * sth, imp_sth_t * imp_sth, SV * ph_name, SV * newvalue, IV if (SvOK(newvalue)) { if (SvIsBOOL(newvalue)) { - /* bind native booleans as 1/0 */ - value_string = SvTRUE(newvalue) ? "1" : "0"; + /* bind native booleans as 1/0 or t/f if pg_bool_tf is set */ + value_string = SvTRUE(newvalue) + ? imp_dbh->pg_bool_tf ? "t" : "1" + : imp_dbh->pg_bool_tf ? "f" : "0"; currph->valuelen = 1; } else { diff --git a/t/12placeholders.t b/t/12placeholders.t index 8b0c3dba..366334d0 100644 --- a/t/12placeholders.t +++ b/t/12placeholders.t @@ -17,7 +17,7 @@ my $dbh = connect_database(); if (! $dbh) { plan skip_all => 'Connection to database failed, cannot continue testing'; } -plan tests => 264; +plan tests => 266; my $t='Connect to database for placeholder testing'; isnt ($dbh, undef, $t); @@ -894,11 +894,19 @@ is_deeply ($sth->fetch, [104,'f'], $t); $dbh->{pg_bool_tf} = 0; SKIP: { - skip 'Cannot test native false without builtin::is_bool', 1 unless defined &builtin::is_bool; + skip 'Cannot test native false without builtin::is_bool', 3 unless defined &builtin::is_bool; $t = q{Inserting into a boolean column with native false works}; $sth = $dbh->prepare($SQL); $sth->execute(105, !!0, 'Boolean native false'); is_deeply ($sth->fetch, [105, 0], $t); + + local $dbh->{pg_boo_tf} = 1;; + $t = q{Inserting into a boolean column with native false works (pg_bool_tf on)}; + $sth = $dbh->prepare($SQL); + $sth->execute(105, !!1, 'Boolean native true (pg_bool_tf on)'); + is_deeply ($sth->fetch, [106, 't'], $t); + $sth->execute(105, !!0, 'Boolean native false (pg_bool_tf on)'); + is_deeply ($sth->fetch, [107, 'f'], $t); } ## Test of placeholder escaping. Enabled by default, so let's jump right in