1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#!/usr/bin/perl
# This tests, whether the number of rows can be retrieved.
use strict;
use warnings;
use Test::More;
BEGIN { use_ok ("DBI") }
require "t/lib.pl";
sub TrueRows
{
my $sth = shift;
my $count = 0;
$count++ while $sth->fetch;
$count;
} # TrueRows
my @tbl_def = (
[ "id", "INTEGER", 4, 0 ],
[ "name", "CHAR", 64, 0 ],
);
my ($sth, $rows);
ok (my $dbh = Connect (), "connect");
ok (my $tbl = FindNewTable ($dbh), "find new test table");
like (my $def = TableDefinition ($tbl, @tbl_def),
qr{^create table $tbl}i, "table definition");
ok ($dbh->do ($def), "create table");
ok ($dbh->do ("INSERT INTO $tbl VALUES (1, 'Alligator Descartes')"), "insert");
ok ($sth = $dbh->prepare ("SELECT * FROM $tbl WHERE id = 1"), "prepare");
ok ($sth->execute, "execute");
is ($sth->rows, 1, "numrows");
is (TrueRows ($sth), 1, "true rows");
ok ($sth->finish, "finish");
undef $sth;
ok ($dbh->do ("INSERT INTO $tbl VALUES (2, 'Jochen Wiedman')"), "insert");
ok ($sth = $dbh->prepare ("SELECT * FROM $tbl WHERE id >= 1"), "prepare");
ok ($sth->execute, "execute");
$rows = $sth->rows;
ok ($rows == 2 || $rows == -1, "rows");
is (TrueRows ($sth), 2, "true rows");
ok ($sth->finish, "finish");
undef $sth;
ok ($dbh->do ("INSERT INTO $tbl VALUES (3, 'Tim Bunce')"), "insert");
ok ($sth = $dbh->prepare ("SELECT * FROM $tbl WHERE id >= 2"), "prepare");
ok ($sth->execute, "execute");
$rows = $sth->rows;
ok ($rows == 2 || $rows == -1, "rows");
is (TrueRows ($sth), 2, "true rows");
ok ($sth->finish, "finish");
undef $sth;
ok ($dbh->do ("DROP TABLE $tbl"), "drop");
ok ($dbh->disconnect, "disconnect");
done_testing ();
|