Skip to content

Commit be23873

Browse files
myrrhlindveeden
authored andcommitted
tests for $sth->{ParamValues} attrib (gh #447)
confirming behavior of this attribute before and after execution on prepared statements, with and without bound values.
1 parent 2870fe0 commit be23873

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

Diff for: t/gh447-paramvalues.t

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#! /bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
#"set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
7+
8+
use Data::Dumper;
9+
use Test::More;
10+
use DBI;
11+
use lib 't', '.';
12+
require 'lib.pl';
13+
14+
my ($row, $sth, $dbh);
15+
my ($def, $rows, $errstr, $ret_ref);
16+
use vars qw($test_dsn $test_user $test_password);
17+
my $table = 'dbd_mysql_gh447';
18+
19+
eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
20+
{ RaiseError => 1, AutoCommit => 1});};
21+
22+
if ($@) {
23+
plan skip_all => "no database connection";
24+
}
25+
26+
# in case of exit early, ensure we clean up
27+
END {
28+
if ($dbh) {
29+
$dbh->do("DROP TABLE IF EXISTS $table");
30+
$dbh->disconnect();
31+
}
32+
}
33+
34+
# ------ set up
35+
ok(defined $dbh, "Connected to database");
36+
$dbh->do("DROP TABLE IF EXISTS $table");
37+
$dbh->do("CREATE TABLE $table (id INT(4), name VARCHAR(64))");
38+
39+
# test prepare/execute statement without a placeholder
40+
41+
$sth = $dbh->prepare("SHOW TABLES LIKE '$table'");
42+
is_deeply($sth->{ParamValues}, {}, "ParamValues is empty hashref before SHOW");
43+
$sth->execute();
44+
45+
is_deeply($sth->{ParamValues}, {}, "ParamValues is still empty after execution");
46+
47+
$sth->finish;
48+
is_deeply($sth->{ParamValues}, {}, "ParamValues empty after finish");
49+
undef $sth;
50+
51+
52+
# test prepare/execute statement with a placeholder
53+
my $ofs = 0;
54+
55+
$sth = $dbh->prepare("INSERT INTO $table values (?, ?)");
56+
is_deeply($sth->{ParamValues}, {0+$ofs => undef, 1+$ofs => undef},
57+
"ParamValues is correct hashref before INSERT")
58+
|| print Dumper($sth->{ParamValues});
59+
60+
# insert rows with placeholder
61+
my %rowdata;
62+
my @chars = grep !/[0O1Iil]/, 0..9, 'A'..'Z', 'a'..'z';
63+
64+
for (my $i = 1 ; $i < 4; $i++) {
65+
my $word = join '', $i, '-', map { $chars[rand @chars] } 0 .. 16;
66+
$rowdata{$i} = $word; # save for later
67+
$rows = $sth->execute($i, $word);
68+
is($rows, 1, "Should have inserted one row");
69+
is_deeply($sth->{ParamValues}, {0+$ofs => $i, 1+$ofs => $word},
70+
"row $i ParamValues hashref as expected");
71+
}
72+
73+
$sth->finish;
74+
is_deeply($sth->{ParamValues}, {0+$ofs => 3, 1+$ofs => $rowdata{3}},
75+
"ParamValues still hold last values after finish");
76+
undef $sth;
77+
78+
79+
# test prepare/execute with bind_param
80+
81+
$sth = $dbh->prepare("SELECT * FROM $table WHERE id = ? OR name = ?");
82+
is_deeply($sth->{ParamValues}, {0+$ofs => undef, 1+$ofs => undef},
83+
"ParamValues is hashref with keys before bind_param");
84+
$sth->bind_param(1, 1, DBI::SQL_INTEGER);
85+
$sth->bind_param(2, $rowdata{1});
86+
is_deeply($sth->{ParamValues}, {0+$ofs => 1, 1+$ofs => $rowdata{1}},
87+
"ParamValues contains bound values after bind_param");
88+
89+
$rows = $sth->execute;
90+
is($rows, 1, 'execute selected 1 row');
91+
is_deeply($sth->{ParamValues}, {0+$ofs => 1, 1+$ofs => $rowdata{1}},
92+
"ParamValues still contains values after execute");
93+
94+
# try changing one parameter only (so still param 1 => 1)
95+
$sth->bind_param(2, $rowdata{2});
96+
is_deeply($sth->{ParamValues}, {0+$ofs => 1, 1+$ofs => $rowdata{2}},
97+
"ParamValues updated with another bind_param");
98+
$rows = $sth->execute;
99+
is($rows, 2, 'execute selected 2 rows because changed param value');
100+
101+
# try execute with args (the previously bound values are overridden)
102+
$rows = $sth->execute(3, $rowdata{3});
103+
is($rows, 1, 'execute used exec args, overrode bound params');
104+
is_deeply($sth->{ParamValues}, {0+$ofs => 3, 1+$ofs => $rowdata{3}},
105+
"ParamValues reflect execute args -- bound params overwritten");
106+
107+
$sth->bind_param(1, undef, DBI::SQL_INTEGER);
108+
is_deeply($sth->{ParamValues}, {0+$ofs => undef, 1+$ofs => $rowdata{3}},
109+
"ParamValues includes undef param after binding");
110+
111+
$rows = $sth->execute(1, $rowdata{2});
112+
is($rows, 2, 'execute used exec args, not bound values');
113+
is_deeply($sth->{ParamValues}, {0+$ofs => 1, 1+$ofs => $rowdata{2}},
114+
"ParamValues changed by execution");
115+
116+
undef $sth;
117+
118+
119+
# clean up
120+
$dbh->do("DROP TABLE IF EXISTS $table");
121+
122+
# Install a handler so that a warning about unfreed resources gets caught
123+
$SIG{__WARN__} = sub { die @_ };
124+
125+
$dbh->disconnect();
126+
127+
undef $dbh;
128+
129+
done_testing();
130+

0 commit comments

Comments
 (0)