1
0
Fork 0
mirror of https://github.com/DBD-SQLite/DBD-SQLite synced 2025-06-07 22:28:47 -04:00

avoid confusion by manual commit/rollback

This commit is contained in:
Kenichi Ishigaki 2010-01-01 11:59:39 +00:00
parent c8502a2bbf
commit 8d4756d31c
2 changed files with 204 additions and 172 deletions

View file

@ -570,6 +570,26 @@ sqlite_st_execute(SV *sth, imp_sth_t *imp_sth)
} }
} }
} }
else if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
char *sql = sqlite3_sql(imp_sth->stmt);
if (((sql[0] == 'C' || sql[0] == 'c') &&
(sql[1] == 'O' || sql[1] == 'o') &&
(sql[2] == 'M' || sql[2] == 'm') &&
(sql[3] == 'M' || sql[3] == 'm') &&
(sql[4] == 'I' || sql[4] == 'i') &&
(sql[5] == 'T' || sql[5] == 't')) ||
((sql[0] == 'R' || sql[0] == 'r') &&
(sql[1] == 'O' || sql[1] == 'o') &&
(sql[2] == 'L' || sql[2] == 'l') &&
(sql[3] == 'L' || sql[3] == 'l') &&
(sql[4] == 'B' || sql[4] == 'b') &&
(sql[5] == 'A' || sql[5] == 'a') &&
(sql[6] == 'C' || sql[6] == 'c') &&
(sql[7] == 'K' || sql[7] == 'k'))) {
DBIc_off(imp_dbh, DBIcf_BegunWork);
DBIc_on(imp_dbh, DBIcf_AutoCommit);
}
}
imp_sth->nrow = 0; imp_sth->nrow = 0;

View file

@ -7,7 +7,7 @@ BEGIN {
} }
use t::lib::Test; use t::lib::Test;
use Test::More tests => 94; use Test::More tests => 92 * 4 + 2;
use Test::NoWarnings; use Test::NoWarnings;
my $dbh = connect_ok( my $dbh = connect_ok(
@ -18,15 +18,24 @@ my $dbh = connect_ok(
$dbh->do('create table foo (id)'); $dbh->do('create table foo (id)');
my @funcs = (
sub { shift->rollback },
sub { shift->commit },
sub { shift->do('rollback') },
sub { shift->do('commit') },
);
foreach my $func (@funcs) {
# scenario 1: AutoCommit => 1 and no begin_work # scenario 1: AutoCommit => 1 and no begin_work
eval { $dbh->{AutoCommit} = 1 }; # initialize
ok $dbh->{AutoCommit}, "AutoCommit is on"; ok $dbh->{AutoCommit}, "AutoCommit is on";
ok !$dbh->{BegunWork}, "BegunWork is off"; ok !$dbh->{BegunWork}, "BegunWork is off";
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'a statement works'; ok !$@, 'a statement works';
diag $@ if $@; diag $@ if $@;
# eval { $dbh->rollback }; # eval { $func->($dbh) };
# ok !$@, "rollback ignored: nothing to be rolled back"; # ok !$@, "commit/rollback ignored";
# diag $@ if $@; # diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is still on"; ok $dbh->{AutoCommit}, "AutoCommit is still on";
ok !$dbh->{BegunWork}, "BegunWork is still off"; ok !$dbh->{BegunWork}, "BegunWork is still off";
@ -42,8 +51,8 @@ like $@ => qr/Already in a transaction/, "but second begin_work should fail";
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, "other statement should work"; ok !$@, "other statement should work";
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is turned on"; ok $dbh->{AutoCommit}, "AutoCommit is turned on";
ok !$dbh->{BegunWork}, "BegunWork is turned off"; ok !$dbh->{BegunWork}, "BegunWork is turned off";
@ -64,8 +73,8 @@ like $@ => qr/Already in a transaction/, "and second begin_work also should fail
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is turned on now"; ok $dbh->{AutoCommit}, "AutoCommit is turned on now";
ok !$dbh->{BegunWork}, "BegunWork is turned off"; ok !$dbh->{BegunWork}, "BegunWork is turned off";
@ -85,8 +94,8 @@ like $@ => qr/Already in a transaction/, "and second begin_work also should fail
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is turned on now"; ok $dbh->{AutoCommit}, "AutoCommit is turned on now";
ok !$dbh->{BegunWork}, "BegunWork is turned off"; ok !$dbh->{BegunWork}, "BegunWork is turned off";
@ -104,8 +113,8 @@ like $@ => qr/cannot start a transaction/, "second BEGIN should fail";
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is turned on now"; ok $dbh->{AutoCommit}, "AutoCommit is turned on now";
ok !$dbh->{BegunWork}, "BegunWork is turned off"; ok !$dbh->{BegunWork}, "BegunWork is turned off";
@ -125,8 +134,8 @@ like $@ => qr/Already in a transaction/, "and second begin_work also should fail
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok $dbh->{AutoCommit}, "AutoCommit is turned on now"; ok $dbh->{AutoCommit}, "AutoCommit is turned on now";
ok !$dbh->{BegunWork}, "BegunWork is turned off"; ok !$dbh->{BegunWork}, "BegunWork is turned off";
@ -150,8 +159,8 @@ like $@ => qr/Already in a transaction/, "and begin_work also should fail";
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok !$dbh->{AutoCommit}, "AutoCommit is still off"; ok !$dbh->{AutoCommit}, "AutoCommit is still off";
ok !$dbh->{BegunWork}, "BegunWork is still off"; ok !$dbh->{BegunWork}, "BegunWork is still off";
@ -177,8 +186,8 @@ like $@ => qr/Already in a transaction/, "and second begin_work also should fail
eval { $dbh->do('insert into foo (id) values (1)'); }; eval { $dbh->do('insert into foo (id) values (1)'); };
ok !$@, 'other statement should work'; ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok !$dbh->{AutoCommit}, "AutoCommit is still off"; ok !$dbh->{AutoCommit}, "AutoCommit is still off";
ok !$dbh->{BegunWork}, "BegunWork is still off"; ok !$dbh->{BegunWork}, "BegunWork is still off";
@ -195,8 +204,11 @@ ok !$@, 'other statement should work';
diag $@ if $@; diag $@ if $@;
ok !$dbh->{AutoCommit}, "AutoCommit is still off"; ok !$dbh->{AutoCommit}, "AutoCommit is still off";
ok !$dbh->{BegunWork}, "BegunWork is still off"; ok !$dbh->{BegunWork}, "BegunWork is still off";
eval { $dbh->rollback }; eval { $func->($dbh) };
ok !$@, 'rolled back'; ok !$@, 'rolled back/committed';
diag $@ if $@; diag $@ if $@;
ok !$dbh->{AutoCommit}, "AutoCommit is still off"; ok !$dbh->{AutoCommit}, "AutoCommit is still off";
ok !$dbh->{BegunWork}, "BegunWork is still off"; ok !$dbh->{BegunWork}, "BegunWork is still off";
}
eval { $dbh->{AutoCommit} = 1 }; # to end transaction
$dbh->disconnect;