fixed boneheaded failure to properly propogate txn_do
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated.pm
index c95a084..a21c413 100644 (file)
@@ -224,11 +224,13 @@ has 'write_handler' => (
         update
         delete
         dbh
+        txn_do
         txn_commit
         txn_rollback
         sth
         deploy
         schema
+        reload_row
     /],
 );
 
@@ -385,9 +387,10 @@ Example:
         my $name = shift @_;
         $schema->resultset('User')->create({name=>$name});
         my $user_rs = $schema->resultset('User')->find({name=>$name}); 
+        return $user_rs;
     };
 
-    $schema->storage->execute_reliably($reliably, 'John');
+    my $user_rs = $schema->storage->execute_reliably($reliably, 'John');
 
 Use this when you must be certain of your database state, such as when you just
 inserted something and need to get a resultset including it, etc.
@@ -411,16 +414,31 @@ sub execute_reliably {
     $self->read_handler($master);
     
     ## do whatever the caller needs
+    my @result;
+    my $want_array = wantarray;
+    
     eval {
-        $coderef->(@args);
+           if($want_array) {
+               @result = $coderef->(@args);
+           }
+           elsif(defined $want_array) {
+               ($result[0]) = ($coderef->(@args));
+           } else {
+               $coderef->(@args);
+           }           
     };
     
+    ##Reset to the original state
+    $self->read_handler($current); 
+    
+    ##Exception testing has to come last, otherwise you might leave the 
+    ##read_handler set to master.
+    
     if($@) {
         $self->throw_exception("coderef returned an error: $@");
+    } else {
+       return $want_array ? @result : $result[0];
     }
-  
-    ##Reset to the original state
-    $self->schema->storage->read_handler($current);    
 }
 
 =head2 set_reliable_storage
@@ -453,7 +471,7 @@ sub set_balanced_storage {
     $schema->storage->read_handler($write_handler);
 }
 
-=head2 txn_do ($coderef)
+=head2 around: txn_do ($coderef)
 
 Overload to the txn_do method, which is delegated to whatever the
 L<write_handler> is set to.  We overload this in order to wrap in inside a
@@ -461,10 +479,24 @@ L</execute_reliably> method.
 
 =cut
 
-sub txn_do {
-       my($self, $coderef, @args) = @_;
-       $self->execute_reliably($coderef, @args);
-}
+around 'txn_do' => sub {
+    my($txn_do, $self, $coderef, @args) = @_;
+    $self->execute_reliably(sub {$self->$txn_do($coderef, @args)});    
+};
+
+=head2 reload_row ($row)
+
+Overload to the reload_row method so that the reloading is always directed to
+the master storage.
+
+=cut
+
+around 'reload_row' => sub {
+       my ($reload_row, $self, $row) = @_;
+       return $self->execute_reliably(sub {
+               return $self->$reload_row(shift);
+       }, $row);
+};
 
 =head2 connected