Browse files

Merge pull request #15 from rjbs/svp-failure

Make the SQLite driver work and fix bad method call in Driver.pm.
  • Loading branch information...
2 parents 19b8452 + 9e1285c commit e39f76b222d50f6282bf4958c100386cc418fdde @theory committed Feb 14, 2012
Showing with 63 additions and 23 deletions.
  1. +1 −1 lib/DBIx/Connector/Driver.pm
  2. +24 −21 lib/DBIx/Connector/Driver/SQLite.pm
  3. +38 −1 t/svp_live.t
View
2 lib/DBIx/Connector/Driver.pm
@@ -58,7 +58,7 @@ sub _rollback_and_release {
local $@;
eval {
$self->rollback_to($dbh, $name);
- $self->rollback_release($dbh, $name);
+ $self->release($dbh, $name);
};
return $@ ? DBIx::Connector::SvpRollbackError->new(
error => $err,
View
45 lib/DBIx/Connector/Driver/SQLite.pm
@@ -5,27 +5,30 @@ use warnings;
use base 'DBIx::Connector::Driver';
our $VERSION = '0.50';
-BEGIN {
- # Only install support for savepoints if SQLite supports them.
- my ($x, $y, $z) = split /[.]/ => $DBD::SQLite::sqlite_version || 0;
- return unless $x >= 3 && $y >= 6 && $z >= 8;
- eval q{
- sub savepoint {
- my ($self, $dbh, $name) = @_;
- $dbh->do("SAVEPOINT $name");
- }
-
- sub release {
- my ($self, $dbh, $name) = @_;
- $dbh->do("RELEASE SAVEPOINT $name");
- }
-
- sub rollback_to {
- my ($self, $dbh, $name) = @_;
- $dbh->do("ROLLBACK TO SAVEPOINT $name");
- }
- };
- die $@ if $@;
+sub _connect {
+ my ($self, $dbh, $dsn, $username, $password, $attrs) = @_;
+
+ my ($x, $y, $z) = split /[.]/ => $dbh->{sqlite_version};
+ $self->{_sqlite_is_new_enough} = ($x >= 3 && $y >= 6 && $z >= 8) ? 1 : 0;
+ return $dbh;
+}
+
+sub savepoint {
+ my ($self, $dbh, $name) = @_;
+ return unless $self->{_sqlite_is_new_enough};
+ $dbh->do("SAVEPOINT $name");
+}
+
+sub release {
+ my ($self, $dbh, $name) = @_;
+ return unless $self->{_sqlite_is_new_enough};
+ $dbh->do("RELEASE SAVEPOINT $name");
+}
+
+sub rollback_to {
+ my ($self, $dbh, $name) = @_;
+ return unless $self->{_sqlite_is_new_enough};
+ $dbh->do("ROLLBACK TO SAVEPOINT $name");
}
1;
View
39 t/svp_live.t
@@ -38,7 +38,7 @@ if (exists $ENV{DBICTEST_DSN}) {
plan skip_all => 'Set DBICTEST_DSN _USER and _PASS to run savepoint tests';
}
-plan tests => 34;
+plan tests => 38;
ok my $conn = DBIx::Connector->new($dsn, $user, $pass, {
PrintError => 0,
@@ -129,3 +129,40 @@ $conn->svp (sub {
});
is $dbh->selectrow_array($sel), 'Miff', 'Savepoint worked: name is "Muff"';
+
+$conn->txn(fixup => sub {
+ my ($dbh) = @_;
+ $dbh->do("DELETE FROM artist;");
+ $dbh->do("INSERT INTO artist (name) VALUES ('All-Time Quarterback');");
+
+ my $token = \do { my $x = "TURN IT OFF" };
+
+ my $ok = eval {
+ $conn->svp(sub {
+ my ($dbh) = @_;
+ $dbh->do("INSERT INTO artist (name) VALUES ('Britney Spears');");
+ die $token;
+ });
+ 1;
+ };
+ my $error = $@;
+
+ ok( ! $ok, "we didn't survive our svp");
+ ok(
+ (ref $error && ref $error eq 'SCALAR' && $error == $token),
+ "we got the expected error, too"
+ ) or diag "got error: $error";
+
+ $dbh->do("INSERT INTO artist (name) VALUES ('Cyndi Lauper');");
+});
+
+$conn->txn(sub {
+ my ($dbh) = @_;
+ my $rows = $dbh->selectcol_arrayref("SELECT name FROM artist ORDER BY name");
+ is(@$rows, 2, "we inserted 2 rows");
+ is_deeply(
+ $rows,
+ [ 'All-Time Quarterback', 'Cyndi Lauper' ],
+ "...and we omitted the bad one",
+ );
+});

0 comments on commit e39f76b

Please sign in to comment.