DBTransaction.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-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 
00017     final class DBTransaction extends BaseTransaction
00018     {
00019         private $started    = false;
00020         
00021         public function __destruct()
00022         {
00023             if ($this->isStarted())
00024                 $this->db->queryRaw("rollback;\n");
00025         }
00026         
00030         public function setDB(DB $db)
00031         {
00032             if ($this->isStarted())
00033                 throw new WrongStateException(
00034                     'transaction already started, can not switch to another db'
00035                 );
00036 
00037             return parent::setDB($db);
00038         }
00039         
00040         public function isStarted()
00041         {
00042             return $this->started;
00043         }
00044         
00048         public function add(Query $query)
00049         {
00050             if (!$this->isStarted()) {
00051                 $this->db->queryRaw($this->getBeginString());
00052                 $this->started = true;
00053             }
00054             
00055             $this->db->queryNull($query);
00056             
00057             return $this;
00058         }
00059         
00063         public function flush()
00064         {
00065             $this->started = false;
00066             
00067             try {
00068                 $this->db->queryRaw("commit;\n");
00069             } catch (DatabaseException $e) {
00070                 $this->db->queryRaw("rollback;\n");
00071                 throw $e;
00072             }
00073             
00074             return $this;
00075         }
00076     }
00077 ?>