CVS update: /cowiki/includes/cowiki/class/dao/

[email protected] 12 May 2005 19:47:30 -0000
Newsgroups gmane.comp.php.cowiki.cvs
Message-ID <[email protected]>
User: dgorski 
Date: 2005/05/12 12:47:30

Modified:
   cowiki/includes/cowiki/class/dao/class.StorageMySQL.php

Log:
 Rewrite
 Added begin(), commit() and rollback() methods

File Changes:

Directory: /cowiki/includes/cowiki/class/dao/
=============================================

File [changed]: class.StorageMySQL.php
Url: http://cowiki.tigris.org/source/browse/cowiki/includes/cowiki/class/dao/class.StorageMySQL.php?r1=1.34&r2=1.35
Delta lines:  +54 -53
---------------------
--- class.StorageMySQL.php	7 May 2005 00:30:53 -0000	1.34
+++ class.StorageMySQL.php	12 May 2005 19:47:28 -0000	1.35
@@ -2,7 +2,7 @@
 
 /**
  *
- * $Id: class.StorageMySQL.php,v 1.34 2005/05/07 00:30:53 dgorski Exp $
+ * $Id: class.StorageMySQL.php,v 1.35 2005/05/12 19:47:28 dgorski Exp $
  *
  * This file is part of coWiki. coWiki is free software under the terms of
  * the GNU General Public License (GPL). Read the LICENSE file. If you did
@@ -17,7 +17,7 @@
  * @author      Daniel T. Gorski, <[email protected]>
  * @copyright   (C) Daniel T. Gorski, {@link http://www.develnet.org}
  * @license     http://www.gnu.org/licenses/gpl.html
- * @version     $Revision: 1.34 $
+ * @version     $Revision: 1.35 $
  *
  */
 
@@ -42,9 +42,7 @@
 
     protected
         $rLink    = null,
-        $rResult  = null,
-        $aLocked  = array(),
-        $sLastId  = 0;
+        $rResult  = null;
 
     protected
         $sHost    = null,
@@ -427,12 +425,6 @@
 
     // --------------------------------------------------------------------
 
-    protected function setLastInsertId() {
-        $this->sLastId = @mysql_insert_id($this->rLink);
-    }
-
-    // --------------------------------------------------------------------
-
     /**
      * Get last insert id
      *
@@ -447,7 +439,7 @@
      * @todo    [D11N]  Check return type
      */
     public function getLastInsertId($sTableName) {
-        return $this->sLastId;
+        return @mysql_insert_id($this->rLink);
     }
 
     // --------------------------------------------------------------------
@@ -484,18 +476,12 @@
      * @todo    [D11N]  Check description
      */
     public function remove($aData) {
-        $this->beginTransaction($aData['table']);
-
         $sQuery = 'DELETE FROM '.$aData['table'].' WHERE '.$aData['where'];
 
         // {{{ DEBUG }}}
         Logger::sql($sQuery);
 
-        $rResult = $this->query($sQuery, 'DELETE');
-
-        $this->endTransaction($aData['table']);
-
-        return $rResult;
+        return $this->query($sQuery, 'DELETE');
     }
 
     // --------------------------------------------------------------------
@@ -513,8 +499,6 @@
      * @todo    [D11N]  Check description
      */
     public function insert($aData) {
-        $this->beginTransaction($aData['table']);
-
         $sQuery = 'INSERT INTO ' . $aData['table'];
         $sValue = '';
 
@@ -539,15 +523,7 @@
         // {{{ DEBUG }}}
         Logger::sql($sQuery . $sValue);
 
-        $rResult = $this->query($sQuery . $sValue, 'INSERT');
-
-        // Save the "last insert id", it would be falsified by the
-        // "endTransaction()" method otherwise.
-        $this->setLastInsertId();
-
-        $this->endTransaction($aData['table']);
-
-        return $rResult;
+        return $this->query($sQuery . $sValue, 'INSERT');
     }
 
     // --------------------------------------------------------------------
@@ -565,8 +541,6 @@
      * @todo    [D11N]  Check description
      */
     public function update($aData) {
-        $this->beginTransaction($aData['table']);
-
         $sQuery = 'UPDATE ' . $aData['table'] . ' SET ';
 
         while (list($key, $value) = each($aData['fields'])) {
@@ -583,11 +557,7 @@
         // {{{ DEBUG }}}
         Logger::sql($sQuery);
 
-        $rResult = $this->query($sQuery, 'UPDATE');
-
-        $this->endTransaction($aData['table']);
-
-        return $rResult;
+        return $this->query($sQuery, 'UPDATE');
     }
 
     // --------------------------------------------------------------------
@@ -639,44 +609,75 @@
      * @return  void
      *
      * @author  Daniel T. Gorski, <[email protected]>
-     * @since   coWiki 0.3.0
+     * @since   coWiki 0.4.0
      *
      * @todo    [D11N]  Check description
      */
-    public function beginTransaction($sTableName) {
-        if (!isset($this->aLocked[$sTableName])) {
-            $this->aLocked[$sTableName] = 0;
-        }
-
-        $this->aLocked[$sTableName]++;
+    public function begin() {
+        if ($this->nTrans == 0) {
+            // {{{ DEBUG }}}
+            Logger::sql('BEGIN');
 
-        if ($this->aLocked[$sTableName] == 1) {
-            $this->query('LOCK TABLES '.$sTableName.' WRITE', 'LOCK');
+            $this->query('BEGIN');
         }
+
+        $this->nTrans++;
     }
 
     // --------------------------------------------------------------------
 
     /**
-     * End transaction
+     * Commit transaction
      *
      * @access  public
      * @param   string
      * @return  void
      *
      * @author  Daniel T. Gorski, <[email protected]>
-     * @since   coWiki 0.3.0
+     * @since   coWiki 0.4.0
      *
      * @todo    [D11N]  Check description
      */
-    public function endTransaction($sTableName) {
-        if ($this->aLocked[$sTableName] != 0) {
-            $this->aLocked[$sTableName]--;
+    public function commit() {
+        $this->nTrans--;
+
+        if ($this->nTrans < 0) {
+            $this->rollback();
+
+            throw new StorageException(
+                'Tried to commit after rollback or no transaction started.'
+            );
         }
 
-        if (array_sum($this->aLocked) == 0) {
-            $this->query('UNLOCK TABLES', 'UNLOCK');
+        if ($this->nTrans == 0) {
+            // {{{ DEBUG }}}
+            Logger::sql('COMMIT');
+
+            $this->query('COMMIT');
+        }
         }
+
+    // --------------------------------------------------------------------
+
+    /**
+     * Rollback transaction
+     *
+     * @access  public
+     * @param   string
+     * @return  void
+     *
+     * @author  Daniel T. Gorski, <[email protected]>
+     * @since   coWiki 0.4.0
+     *
+     * @todo    [D11N]  Check description
+     */
+    public function rollback() {
+        $this->nTrans = 0;
+
+        // {{{ DEBUG }}}
+        Logger::sql('ROLLBACK');
+
+        $this->query('ROLLBACK');
     }
 
     // --------------------------------------------------------------------