From: Rob Kinyon Date: Sun, 3 Jan 2010 05:25:30 +0000 (-0500) Subject: Added infrastructure to support transactions in ::DBI, but turned it off (for now) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bd6b4f3cc1bf5f5448dc37a362d324118bbc82c5;p=dbsrgits%2FDBM-Deep.git Added infrastructure to support transactions in ::DBI, but turned it off (for now) --- diff --git a/lib/DBM/Deep/Engine/DBI.pm b/lib/DBM/Deep/Engine/DBI.pm index b00b98a..10f5b17 100644 --- a/lib/DBM/Deep/Engine/DBI.pm +++ b/lib/DBM/Deep/Engine/DBI.pm @@ -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; } diff --git a/lib/DBM/Deep/Storage/DBI.pm b/lib/DBM/Deep/Storage/DBI.pm index 819e3cd..8cb9c02 100644 --- a/lib/DBM/Deep/Storage/DBI.pm +++ b/lib/DBM/Deep/Storage/DBI.pm @@ -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) = @_;