forked from perl5-dbi/DBD-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for $sth->{ParamValues} attrib
confirming behavior of this attribute before and after execution on prepared statements, with and without bound values. and confirming that no segfaults happen (gh perl5-dbi#447)
- Loading branch information
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use DBI; | ||
use lib 't', '.'; | ||
require 'lib.pl'; | ||
|
||
my ($row, $sth, $dbh); | ||
my ($def, $rows, $errstr, $ret_ref); | ||
use vars qw($test_dsn $test_user $test_password); | ||
my $table = 'dbd_mysql_gh447'; | ||
|
||
eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password, | ||
{ RaiseError => 1, AutoCommit => 1});}; | ||
|
||
if ($@) { | ||
plan skip_all => | ||
"no database connection"; | ||
} | ||
|
||
# in case exit early, ensure we clean up | ||
END { | ||
if ($dbh) { | ||
$dbh->do("DROP TABLE IF EXISTS $table"); | ||
$dbh->disconnect(); | ||
} | ||
} | ||
|
||
# ------ set up | ||
ok(defined $dbh, "Connected to database"); | ||
$dbh->do("DROP TABLE IF EXISTS $table"); | ||
$dbh->do("CREATE TABLE $table (id INT(4), name VARCHAR(64))"); | ||
|
||
|
||
# test prepare/execute statement without a placeholder | ||
|
||
$sth = $dbh->prepare("SHOW TABLES LIKE '$table'"); | ||
is_deeply($sth->{ParamValues}, {}, "ParamValues is empty hashref before SHOW"); | ||
$sth->execute(); | ||
|
||
is_deeply($sth->{ParamValues}, {}, "ParamValues is still empty after execution"); | ||
|
||
$sth->finish; | ||
is_deeply($sth->{ParamValues}, {}, "ParamValues empty after finish"); | ||
undef $sth; | ||
|
||
|
||
# test prepare/execute statement with a placeholder | ||
|
||
$sth = $dbh->prepare("INSERT INTO $table values (?, ?)"); | ||
is_deeply($sth->{ParamValues}, {1 => undef, 2 => undef}, | ||
"ParamValues is correct hashref before INSERT"); | ||
|
||
# insert rows with placeholder | ||
my %rowdata; | ||
my @chars = grep !/[0O1Iil]/, 0..9, 'A'..'Z', 'a'..'z'; | ||
|
||
for (my $i = 1 ; $i < 4; $i++) { | ||
my $word = join '', map { $chars[rand @chars] } 0 .. 16; | ||
$rowdata{$i} = $word; # save for later | ||
$rows = $sth->execute($i, $word); | ||
is($rows, 1, "Should have inserted one row"); | ||
my $attrib = $sth->{ParamValues}; | ||
is_deeply($attrib, {1 => $i, 2 => $word}, "row $i ParamValues hashref as expected"); | ||
# we're checking here that list context doesnt cause segfault | ||
my %copy = %$attrib; | ||
is_deeply(\%copy, {1 => $i, 2 => $word}, "...and copied hashref as well"); | ||
} | ||
|
||
$sth->finish; | ||
is_deeply($sth->{ParamValues}, {}, "ParamValues empty after finish"); | ||
undef $sth; | ||
|
||
|
||
# test prepare/execute with bind_param | ||
|
||
$sth = $dbh->prepare("SELECT * FROM $table WHERE id = ? OR name = ?"); | ||
is_deeply($sth->{ParamValues}, {1 => undef, 2 => undef}, | ||
"ParamValues is hashref with keys before bind_param"); | ||
$sth->bind_param(1, 1, DBI::SQL_INTEGER); | ||
$sth->bind_param(2, $rowdata{1}); | ||
is_deeply($sth->{ParamValues}, {1 => 1, 2 => $rowdata{1}}, | ||
"ParamValues contains bound values after bind_param"); | ||
|
||
my %copy = do { my $attrib = $sth->{ParamValues}; %$attrib }; | ||
ok( %copy, 'copied ParamValues without segfault'); | ||
|
||
$rows = $sth->execute; | ||
is($rows, 1, 'execute selected 1 row'); | ||
is_deeply($sth->{ParamValues}, {1 => 1, 2 => $rowdata{1}}, | ||
"ParamValues still contains values after execute"); | ||
%copy = do { my $attrib = $sth->{ParamValues}; %$attrib }; | ||
ok( %copy, 'copied ParamValues without segfault'); | ||
|
||
# try changing one parameter only | ||
$sth->bind_param(2, $rowdata{2}); | ||
is_deeply($sth->{ParamValues}, {1 => 1, 2 => $rowdata{2}}, | ||
"ParamValues updated with another bind_param"); | ||
$rows = $sth->execute; | ||
is($rows, 2, 'execute selected 2 rows because changed param value'); | ||
|
||
# try execute with args (the bound values take precedent?) | ||
$rows = $sth->execute(3, $rowdata{3}); | ||
is($rows, 2, 'execute used bound params, ignored exec args'); | ||
is_deeply($sth->{ParamValues}, {1 => 1, 2 => $rowdata{2}}, | ||
"ParamValues reflect bound params -- execute args ignored"); | ||
|
||
$sth->bind_param(1, undef, DBI::SQL_INTEGER); | ||
is_deeply($sth->{ParamValues}, {1 => undef, 2 => $rowdata{2}}, | ||
"ParamValues includes undef param after binding"); | ||
%copy = do { my $attrib = $sth->{ParamValues}; %$attrib }; | ||
ok( %copy, 'copied ParamValues without segfault'); | ||
|
||
$rows = $sth->execute(1); | ||
is($rows, 1, 'execute used bound undef value, not exec arg'); | ||
is_deeply($sth->{ParamValues}, {1 => undef, 2 => $rowdata{2}}, | ||
"ParamValues unchanged after execution"); | ||
|
||
undef $sth; | ||
|
||
|
||
# clean up | ||
$dbh->do("DROP TABLE IF EXISTS $table"); | ||
|
||
# Install a handler so that a warning about unfreed resources gets caught | ||
$SIG{__WARN__} = sub { die @_ }; | ||
|
||
$dbh->disconnect(); | ||
|
||
undef $dbh; | ||
|
||
done_testing(); | ||
|