From 9204611f8989bd5a992bc6350e0639cda8f5fe44 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 22 Sep 2007 14:37:42 -0400 Subject: [PATCH] Moving the insertId() and affectedRows() methods from the result class to the database one and fixed a broken BSDbResult::free * Db.php * DbMySql.php * DbMySqlI.php * DbPostgreSql.php * UnitTest/DatabaseTestAbstract.php --- Db.php | 84 +++++++++++++++---------------- DbMySql.php | 36 ++++++------- DbMySqlI.php | 36 ++++++------- DbPostgreSql.php | 46 ++++++++--------- UnitTest/DatabaseTestAbstract.php | 14 +++--- 5 files changed, 108 insertions(+), 108 deletions(-) diff --git a/Db.php b/Db.php index 56ce0b0..bc78fcc 100644 --- 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); } /** diff --git a/DbMySql.php b/DbMySql.php index c5c0ed2..814ae8d 100644 --- a/DbMySql.php +++ b/DbMySql.php @@ -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 diff --git a/DbMySqlI.php b/DbMySqlI.php index fb29745..9d5ff31 100644 --- a/DbMySqlI.php +++ b/DbMySqlI.php @@ -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 diff --git a/DbPostgreSql.php b/DbPostgreSql.php index e9ced45..6ce94b6 100644 --- a/DbPostgreSql.php +++ b/DbPostgreSql.php @@ -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 diff --git a/UnitTest/DatabaseTestAbstract.php b/UnitTest/DatabaseTestAbstract.php index fee3455..8970121 100644 --- a/UnitTest/DatabaseTestAbstract.php +++ b/UnitTest/DatabaseTestAbstract.php @@ -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()); } } -- 2.22.5