Moving the insertId() and affectedRows() methods from the result class to the databas...
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 22 Sep 2007 18:37:42 +0000 (14:37 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 22 Sep 2007 18:37:42 +0000 (14:37 -0400)
* Db.php
* DbMySql.php
* DbMySqlI.php
* DbPostgreSql.php
* UnitTest/DatabaseTestAbstract.php

Db.php
DbMySql.php
DbMySqlI.php
DbPostgreSql.php
UnitTest/DatabaseTestAbstract.php

diff --git a/Db.php b/Db.php
index 56ce0b06f3466633b9474a081d66450373a6961e..bc78fccfa6677c339ffc873ae775564f82528a3a 100644 (file)
--- a/Db.php
+++ b/Db.php
@@ -284,6 +284,47 @@ abstract class BSDb
        */
        public abstract function _errorString();
        
+       // ###################################################################
+       /**
+       * Fetch the unique ID of the record just inserted
+       *
+       * @return       integer Insert-ID
+       */
+       public function insertId()
+       {
+               return $this->_insertID();
+       }
+       
+       /**
+       * Abstract function that returns the ID of the most recently-inserted
+       * record
+       *
+       * @return       integer Insertion ID
+       */
+       protected abstract function _insertId();
+       
+       // ###################################################################
+       /**
+       * Fetch the number of rows affected by the query
+       *
+       * @param        integer Result
+       *
+       * @return       integer Number of affected rows
+       */
+       public function affectedRows()
+       {
+               return $this->_affectedRows($this->result);
+       }
+       
+       /**
+       * Abstract function that returns the number of affected rows in the result
+       *
+       * @param        integer Result ID
+       *
+       * @return       integer Number of rows
+       */
+       protected abstract function _affectedRows($result);
+       
        // ###################################################################
        /**
        * Sends the command to start a transaction. This command should never
@@ -399,7 +440,7 @@ abstract class BSDbResult
        */
        public function free()
        {
-               $this->_freeResult($result);
+               $this->_freeResult($this->result);
        }
        
        /**
@@ -409,25 +450,6 @@ abstract class BSDbResult
        */
        protected abstract function _freeResult($result);
        
-       // ###################################################################
-       /**
-       * Fetch the unique ID of the record just inserted
-       *
-       * @return       integer Insert-ID
-       */
-       public function insertId()
-       {
-               return $this->_insertID();
-       }
-       
-       /**
-       * Abstract function that returns the ID of the most recently-inserted
-       * record
-       *
-       * @return       integer Insertion ID
-       */
-       protected abstract function _insertId();
-               
        // ###################################################################
        /**
        * Fetch the number of rows in the result
@@ -449,28 +471,6 @@ abstract class BSDbResult
        * @return       integer Number of rows
        */
        protected abstract function _numRows($result);
-       
-       // ###################################################################
-       /**
-       * Fetch the number of rows affected by the query
-       *
-       * @param        integer Result
-       *
-       * @return       integer Number of affected rows
-       */
-       public function affectedRows()
-       {
-               return $this->_affectedRows($this->result);
-       }
-       
-       /**
-       * Abstract function that returns the number of affected rows in the result
-       *
-       * @param        integer Result ID
-       *
-       * @return       integer Number of rows
-       */
-       protected abstract function _affectedRows($result);
 }
 
 /**
index c5c0ed27b0c87e31cb92d6537b15e66fac97b065..814ae8dabc4d59e83568a03c6428e49dde705100 100644 (file)
@@ -113,6 +113,24 @@ class BSDbMySql extends BSDb
                return $string;
        }
        
+       // ###################################################################
+       /**
+       * Wrapper: mysql_insert_id
+       */
+       protected function _insertId()
+       {
+               return mysql_insert_id($this->dblink);
+       }
+       
+       // ###################################################################
+       /**
+       * Wrapper: mysql_affected_rows
+       */
+       protected function _affectedRows($result)
+       {
+               return mysql_affected_rows($this->dblink);
+       }
+       
        // ###################################################################
        /**
        * Starts a database transaction
@@ -211,15 +229,6 @@ class BSDbMySqlResult extends BSDbResult
                mysql_free_result($result);
        }
        
-       // ###################################################################
-       /**
-       * Wrapper: mysql_insert_id
-       */
-       protected function _insertId()
-       {
-               return mysql_insert_id($this->dblink);
-       }
-       
        // ###################################################################
        /**
        * Wrapper: mysql_num_rows
@@ -228,15 +237,6 @@ class BSDbMySqlResult extends BSDbResult
        {
                return mysql_num_rows($result);
        }
-       
-       // ###################################################################
-       /**
-       * Wrapper: mysql_affected_rows
-       */
-       protected function _affectedRows($result)
-       {
-               return mysql_affected_rows($this->dblink);
-       }
 }
 
 ?>
\ No newline at end of file
index fb29745b2b72a956fdcabd1a28da4a78bffcd283..9d5ff31a7b2b0f96986bc88bd6ebaa121b884fb1 100644 (file)
@@ -85,6 +85,24 @@ class BSDbMySqlI extends BSDb
                return $string;
        }
        
+       // ###################################################################
+       /**
+       * Wrapper: mysql_insert_id
+       */
+       protected function _insertId()
+       {
+               return mysqli_insert_id($this->dblink);
+       }
+       
+       // ###################################################################
+       /**
+       * Wrapper: mysql_affected_rows
+       */
+       protected function _affectedRows($result)
+       {
+               return mysqli_affected_rows($this->dblink);
+       }
+       
        // ###################################################################
        /**
        * Starts a database transaction
@@ -183,15 +201,6 @@ class BSDbMySqlIResult extends BSDbResult
                mysqli_free_result($result);
        }
        
-       // ###################################################################
-       /**
-       * Wrapper: mysql_insert_id
-       */
-       protected function _insertId()
-       {
-               return mysqli_insert_id($this->dblink);
-       }
-       
        // ###################################################################
        /**
        * Wrapper: mysql_num_rows
@@ -200,15 +209,6 @@ class BSDbMySqlIResult extends BSDbResult
        {
                return mysqli_num_rows($result);
        }
-       
-       // ###################################################################
-       /**
-       * Wrapper: mysql_affected_rows
-       */
-       protected function _affectedRows($result)
-       {
-               return mysqli_affected_rows($this->dblink);
-       }
 }
 
 ?>
\ No newline at end of file
index e9ced45884b524102f69084f3b824b55141455f5..6ce94b602e19cfd194aaff9b266f05e0831624b8 100644 (file)
@@ -98,11 +98,32 @@ class BSDbPostgreSql extends BSDb
                return -1;
        }
        
+       // ###################################################################
+       /**
+       * Overload: insertId
+       */
+       public function insertId($table, $field)
+       {
+               return $this->queryFirst("SELECT last_value FROM {$table}_{$field}_seq")->fetchObject()->last_value;
+       }
+       
+       protected function _insertId()
+       {}
+       
+       // ###################################################################
+       /**
+       * Wrapper: pg_affected_rows
+       */
+       protected function _affectedRows($result)
+       {
+               return pg_affected_rows($result);
+       }
+       
        // ###################################################################
        /**
        * Starts a database transaction
        */
-       public function start()
+       public function begin()
        {
                $this->query("BEGIN");
        }
@@ -159,19 +180,7 @@ class BSDbPostgreSql extends BSDb
  *
  */
 class BSDbPostgreSqlResult extends BSDbResult
-{
-       // ###################################################################
-       /**
-       * Overload: insertId
-       */
-       public function insertId($table, $field)
-       {
-               return $this->queryFirst("SELECT last_value FROM {$table}_{$field}_seq")->fetchObject()->last_value;
-       }
-       
-       protected function _insertId()
-       {}
-       
+{      
        // ###################################################################
        /**
        * Wrapper: pg_fetch_assoc
@@ -216,15 +225,6 @@ class BSDbPostgreSqlResult extends BSDbResult
        {
                return pg_num_rows($result);
        }
-       
-       // ###################################################################
-       /**
-       * Wrapper: pg_affected_rows
-       */
-       protected function _affectedRows($result)
-       {
-               return pg_affected_rows($result);
-       }
 }
 
 ?>
\ No newline at end of file
index fee34559ef73bd82924eafd326798e5c8df34a6b..8970121008c8b148d33aae5925c11fcef3f743f6 100644 (file)
@@ -121,10 +121,10 @@ abstract class DatabaseTestAbstract extends PHPUnit_Framework_TestCase
                        $this->fixture->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('789')");
                        
                        $res = $this->fixture->query("SELECT * FROM test");
-                       $this->assertEquals(3, $res->numRows());
+                       $this->assertEquals(3, $res->size());
                        
                        $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = '--invalid value--'");
-                       $this->assertEquals(0, $res->numRows());
+                       $this->assertEquals(0, $res->size());
                }
                catch (BSDbException $e)
                {
@@ -139,10 +139,10 @@ abstract class DatabaseTestAbstract extends PHPUnit_Framework_TestCase
                        $this->fixture->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('123')");
                        
                        $res = $this->fixture->query("UPDATE test SET textstuff = 'abc' WHERE textstuff = '123'");
-                       $this->assertEquals(2, $res->affectedRows());
+                       $this->assertEquals(2, $this->fixture->affectedRows());
                        
                        $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'abc'");
-                       $this->assertEquals(2, $res->numRows());
+                       $this->assertEquals(2, $res->size());
                }
                catch (BSDbException $e)
                {
@@ -170,17 +170,17 @@ abstract class DatabaseTestAbstract extends PHPUnit_Framework_TestCase
                $this->fixture->commit();
                
                $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'foo'");
-               $this->assertEquals(1, $res->numRows());
+               $this->assertEquals(1, $res->size());
                
                $this->fixture->begin();
                $this->fixture->query("UPDATE test SET textstuff = 'abc'");
                $this->fixture->rollback();
                
                $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'abc'");
-               $this->assertEquals(0, $res->numRows());
+               $this->assertEquals(0, $res->size());
                
                $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'foo'");
-               $this->assertEquals(1, $res->numRows());
+               $this->assertEquals(1, $res->size());
        }
 }