5
# Verify behavior of interfaces which report number of rows affected
9
use Test::More tests => 84;
11
use vars qw($dbh $table);
14
require 't/tests-setup.pl';
17
if (defined $dbh and defined $table) {
18
eval { $dbh->do("DROP TABLE $table"); };
22
# is() with special case "zero but true" support
24
my ($value, $expected) = @_;
25
return ($value == $expected) unless $expected == 0;
27
return (($value == 0 and $value));
30
# == Test Initialization =========================================
32
($dbh) = connect_to_database({RaiseError => 1});
34
$table = find_new_table($dbh);
35
$dbh->do("CREATE TABLE $table(ID INTEGER NOT NULL, NAME VARCHAR(16) NOT NULL)");
36
pass("CREATE TABLE $table");
40
sql => qq|INSERT INTO $table (ID, NAME) VALUES (1, 'unu')|,
41
desc => 'literal insert',
45
sql => qq|INSERT INTO $table (ID, NAME) VALUES (?, ?)|,
46
desc => 'parameterized insert',
51
sql => qq|DELETE FROM $table WHERE 1=0|,
52
desc => 'DELETE WHERE (false)',
56
sql => qq|UPDATE $table SET NAME='nomo'|,
61
sql => qq|DELETE FROM $table|,
71
for my $spec (@TEST_PROGRAM) {
72
my @bind = @{$spec->{params}} if $spec->{params};
73
my $rv = $dbh->do($spec->{sql}, undef, @bind);
75
ok(is_maybe_zbt($rv, $spec->{expected}), "do($spec->{desc})");
76
# $DBI::rows is not guaranteed to be correct after $dbh->blah operations
79
# == 2a. single execute() and rows()
81
for my $spec (@TEST_PROGRAM) {
82
my @bind = @{$spec->{params}} if $spec->{params};
83
my $sth = $dbh->prepare($spec->{sql});
84
my $rv = $sth->execute(@bind);
86
ok(is_maybe_zbt($rv, $spec->{expected}), "execute($spec->{desc})");
87
is($DBI::rows, $spec->{expected}, "execute($spec->{desc}) (\$DBI::rows)");
88
is($sth->rows, $spec->{expected}, "\$sth->rows($spec->{desc})");
91
# == 2b. repeated execute() and rows()
94
my $sth = $dbh->prepare("INSERT INTO $table(ID, NAME) VALUES (?, ?)");
95
for my $name (qw|unu du tri kvar kvin ses sep ok naux dek|) {
96
my $rv = $sth->execute( ++$i, $name );
97
is( $rv, 1, "re-execute(INSERT one) -> 1" );
98
is( $DBI::rows, 1, "re-execute(INSERT one) -> 1 (\$DBI::rows)" );
99
is( $sth->rows, 1, "\$sth->rows(re-executed INSERT)" );
102
$sth = $dbh->prepare("DELETE FROM $table WHERE ID<?");
104
my $rv = $sth->execute($_);
105
is( $rv, 5, "re-execute(DELETE five) -> 1" );
106
is( $DBI::rows, 5, "re-execute(DELETE five) -> 1 (\$DBI::rows)" );
107
is( $sth->rows, 5, "\$sth->rows(re-executed DELETE)" );
109
my $rv = $sth->execute(16);
110
ok( is_maybe_zbt( $rv, 0 ), "re-execute(DELETE on empty) zero but true" );
112
"re-execute(DELETE on empty) (\$DBI::rows) zero but true" );
114
"\$sth->rows(re-executed DELETE on empty) zero but true" );
117
# == 3. special cases
118
# DBD::InterBase tracks the number of FETCHes on a SELECT statement
119
# in $sth->rows() as an extension to the DBI.
123
for my $name (qw|unu du tri kvar kvin ses sep ok naux dek|) {
124
$dbh->do( "INSERT INTO $table(ID, NAME) VALUES (?, ?)",
125
undef, ++$i, $name );
127
my $sth = $dbh->prepare("SELECT ID, NAME FROM $table");
128
my $rv = $sth->execute;
129
ok( is_maybe_zbt( $rv, 0 ), "execute(SELECT) -> zero but true" );
130
is( $DBI::rows, 0, "execute(SELECT) zero but true (\$DBI::rows)" );
131
is( $sth->rows, 0, "\$sth->rows(SELECT) zero but true" );
134
while ( $sth->fetch ) {
135
is( ++$fetched, $sth->rows, "\$sth->rows incrementing on SELECT" );
136
is( $fetched, $DBI::rows, "\$DBI::rows incrementing on SELECT" );