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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#!perl -w
#
# $Id: 50commit.t 11244 2008-05-11 15:13:10Z capttofu $
#
# This is testing the transaction support.
#
use DBI;
use Test::More;
use lib 't', '.';
require 'lib.pl';
use vars qw($got_warning $test_dsn $test_user $test_password $table);
my $dbh;
eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
{ RaiseError => 1, PrintError => 1, AutoCommit => 0 });};
if ($@) {
plan skip_all =>
"ERROR: $DBI::errstr. Can't continue test";
}
sub catch_warning ($) {
$got_warning = 1;
}
sub num_rows($$$) {
my($dbh, $table, $num) = @_;
my($sth, $got);
if (!($sth = $dbh->prepare("SELECT * FROM $table"))) {
return "Failed to prepare: err " . $dbh->err . ", errstr "
. $dbh->errstr;
}
if (!$sth->execute) {
return "Failed to execute: err " . $dbh->err . ", errstr "
. $dbh->errstr;
}
$got = 0;
while ($sth->fetchrow_arrayref) {
++$got;
}
if ($got ne $num) {
return "Wrong result: Expected $num rows, got $got.\n";
}
return '';
}
$have_transactions = have_transactions($dbh);
my $engine= $have_transactions ? 'InnoDB' : 'MyISAM';
if ($have_transactions) {
plan tests => 21;
ok $dbh->do("DROP TABLE IF EXISTS $table"), "drop table if exists $table";
my $create =<<EOT;
CREATE TABLE $table (
id INT(4) NOT NULL default 0,
name VARCHAR(64) NOT NULL default ''
) ENGINE=$engine
EOT
ok $dbh->do($create), 'create $table';
ok !$dbh->{AutoCommit}, "\$dbh->{AutoCommit} not defined |$dbh->{AutoCommit}|";
$dbh->{AutoCommit} = 0;
ok !$dbh->err;
ok !$dbh->errstr;
ok !$dbh->{AutoCommit};
ok $dbh->do("INSERT INTO $table VALUES (1, 'Jochen')"),
"insert into $table (1, 'Jochen')";
my $msg;
$msg = num_rows($dbh, $table, 1);
ok !$msg;
ok $dbh->rollback, 'rollback';
$msg = num_rows($dbh, $table, 0);
ok !$msg;
ok $dbh->do("DELETE FROM $table WHERE id = 1"), "delete from $table where id = 1";
$msg = num_rows($dbh, $table, 0);
ok !$msg;
ok $dbh->commit, 'commit';
$msg = num_rows($dbh, $table, 0);
ok !$msg;
# Check auto rollback after disconnect
ok $dbh->do("INSERT INTO $table VALUES (1, 'Jochen')");
$msg = num_rows($dbh, $table, 1);
ok !$msg;
ok $dbh->disconnect;
ok ($dbh = DBI->connect($test_dsn, $test_user, $test_password));
ok $dbh, "connected";
$msg = num_rows($dbh, $table, 0);
ok !$msg;
ok $dbh->{AutoCommit}, "\$dbh->{AutoCommit} $dbh->{AutoCommit}";
}
else {
plan tests => 11;
ok $dbh->do("DROP TABLE IF EXISTS $table"), "drop table if exists $table";
my $create =<<EOT;
CREATE TABLE $table (
id INT(4) NOT NULL default 0,
name VARCHAR(64) NOT NULL default ''
) ENGINE=$engine
EOT
ok $dbh->do($create), 'create $table';
# Tests for databases that don't support transactions
# Check whether AutoCommit mode works.
ok $dbh->do("INSERT INTO $table VALUES (1, 'Jochen')");
$msg = num_rows($dbh, $table, 1);
ok !$msg;
ok $dbh->disconnect;
ok ($dbh = DBI->connect($test_dsn, $test_user, $test_password));
$msg = num_rows($dbh, $table, 1);
ok !$msg;
ok $dbh->do("INSERT INTO $table VALUES (2, 'Tim')");
my $result;
$@ = '';
$SIG{__WARN__} = \&catch_warning;
$got_warning = 0;
eval { $result = $dbh->commit; };
$SIG{__WARN__} = 'DEFAULT';
ok $got_warning;
# Check whether rollback issues a warning in AutoCommit mode
# We accept error messages as being legal, because the DBI
# requirement of just issuing a warning seems scary.
ok $dbh->do("INSERT INTO $table VALUES (3, 'Alligator')");
$@ = '';
$SIG{__WARN__} = \&catch_warning;
$got_warning = 0;
eval { $result = $dbh->rollback; };
$SIG{__WARN__} = 'DEFAULT';
ok $got_warning, "Should be warning defined upon rollback of non-trx table";
ok $dbh->do("DROP TABLE $table");
ok $dbh->disconnect();
}
|