CarefulDatabaseRunner.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: CarefulDatabaseRunner.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class CarefulDatabaseRunner implements CarefulCommand
00017     {
00018         private $command    = null;
00019         private $db         = null;
00020         
00021         private $running = false;
00022         
00023         final public function __construct(EditorCommand $command)
00024         {
00025             $this->command = $command;
00026         }
00027         
00032         public function run(Prototyped $subject, Form $form, HttpRequest $request)
00033         {
00034             Assert::isFalse($this->running, 'command already running');
00035             Assert::isTrue($subject instanceof DAOConnected);
00036             
00037             $this->db = DBPool::getByDao($subject->dao());
00038             
00039             $this->db->begin();
00040             
00041             try {
00042                 $mav = $this->command->run($subject, $form, $request);
00043                 
00044                 $this->running = true;
00045                 
00046                 return $mav;
00047             } catch (BaseException $e) {
00048                 $this->db->rollback();
00049                 
00050                 throw $e;
00051             }
00052             
00053             Assert::isUnreachable();
00054         }
00055         
00059         public function commit()
00060         {
00061             if ($this->running) {
00062                 $this->db->commit();
00063                 $this->running = false;
00064             }
00065             
00066             return $this;
00067         }
00068         
00072         public function rollback()
00073         {
00074             if ($this->running) {
00075                 try {
00076                     $this->db->rollback();
00077                 } catch (DatabaseException $e) {
00078                     // keep silence
00079                 }
00080                 
00081                 $this->running = false;
00082             }
00083             
00084             return $this;
00085         }
00086         
00087         public function __destruct()
00088         {
00089             if ($this->running) {
00090                 try {
00091                     $this->db->rollback();
00092                 } catch (BaseException $e) {
00093                     // fear of fatal error's
00094                 }
00095             }
00096         }
00097     }
00098 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4