Added infrastructure to support transactions in ::DBI, but turned it off (for now)
Rob Kinyon [Sun, 3 Jan 2010 05:25:30 +0000 (00:25 -0500)]
lib/DBM/Deep/Engine/DBI.pm
lib/DBM/Deep/Storage/DBI.pm

index b00b98a..10f5b17 100644 (file)
@@ -311,15 +311,68 @@ sub write_value {
     return 1;
 }
 
-sub begin_work { die "Transactions are not supported by this engine" } 
-sub rollback   { die "Transactions are not supported by this engine" } 
-sub commit     { die "Transactions are not supported by this engine" }
+sub begin_work {
+    my $self = shift;
+    die "Transactions are not supported by this engine"
+        unless $self->supports('transactions');
+
+    if ( $self->in_txn ) {
+        DBM::Deep->_throw_error( "Cannot begin_work within an active transaction" );
+    }
+
+    $self->storage->begin_work;
+
+    $self->in_txn( 1 );
+
+    return 1;
+} 
+
+sub rollback {
+    my $self = shift;
+    die "Transactions are not supported by this engine"
+        unless $self->supports('transactions');
+
+    if ( !$self->in_txn ) {
+        DBM::Deep->_throw_error( "Cannot rollback without an active transaction" );
+    }
+
+    $self->storage->rollback;
+
+    $self->in_txn( 0 );
+
+    return 1;
+} 
+
+sub commit {
+    my $self = shift;
+    die "Transactions are not supported by this engine"
+        unless $self->supports('transactions');
+
+    if ( !$self->in_txn ) {
+        DBM::Deep->_throw_error( "Cannot commit without an active transaction" );
+    }
+
+    $self->storage->commit;
+
+    $self->in_txn( 0 );
+
+    return 1;
+}
+
+sub in_txn {
+    my $self = shift;
+    $self->{in_txn} = shift if @_;
+    $self->{in_txn};
+}
 
 sub supports {
-    shift;
+    my $self = shift;
     my ($feature) = @_;
 
-    return if $feature eq 'transactions';
+    if ( $feature eq 'transactions' ) {
+#        return 1 if $self->storage->driver eq 'sqlite';
+        return;
+    }
     return;
 }
 
index 819e3cd..8cb9c02 100644 (file)
@@ -49,7 +49,7 @@ sub open {
 
     $self->{dbh} = DBI->connect(
         $self->{dbi}{dsn}, $self->{dbi}{username}, $self->{dbi}{password}, {
-            AutoCommit => 0,
+            AutoCommit => 1,
             PrintError => 0,
             RaiseError => 1,
             %{ $self->{dbi}{connect_args} || {} },
@@ -92,6 +92,21 @@ sub unlock {
     $self->{dbh}->commit;
 }
 
+sub begin_work {
+    my $self = shift;
+    $self->{dbh}->begin_work;
+}
+
+sub commit {
+    my $self = shift;
+    $self->{dbh}->commit;
+}
+
+sub rollback {
+    my $self = shift;
+    $self->{dbh}->rollback;
+}
+
 sub read_from {
     my $self = shift;
     my ($table, $cond, @cols) = @_;