Skip to content

Commit

Permalink
Respect pg_bool_tf when binding native booleans on 5.36 and newer
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmari committed Sep 28, 2024
1 parent 91b91cb commit 7da168c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3234,7 +3234,9 @@ should the query fail (see C<ShowErrorStatement>).
=head3 B<pg_bool_tf> (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<builtin/is_bool>) will
also be sent as 't' and 'f' when used as placeholder values.
=head3 B<ReadOnly> (boolean)
Expand Down Expand Up @@ -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<builtin/is_bool>) can be used as placeholder values. On older
versions of Perl, false values returned by built-in operators (such
as C<!!0>) 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<builtin/is_bool>) can be used as placeholder values. They will be
sent as C<1> and C<0>, or C<t> or C<f> if C<pg_bool_tf> is set to a
true value.
On older versions of Perl, false values returned by built-in operators
(such as C<!!0>) 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
Expand Down
6 changes: 4 additions & 2 deletions dbdimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions t/12placeholders.t
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7da168c

Please sign in to comment.