From f7d18b19fd6e9a80f346e1f4f55f36c725800bdd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Apr 2012 08:29:28 -0400 Subject: [PATCH] Added count_all method to query_builder --- README.md | 2 +- classes/query_builder.php | 6 ++-- drivers/firebird/firebird_driver.php | 19 +++--------- drivers/firebird/firebird_result.php | 12 ++++++++ drivers/pgsql/pgsql_driver.php | 2 +- drivers/sqlite/sqlite_driver.php | 2 +- tests/core/db_qb_test.php | 43 +++++++++++++++++++-------- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 8 files changed, 54 insertions(+), 32 deletions(-) mode change 100755 => 100644 tests/db_files/FB_TEST_DB.FDB diff --git a/README.md b/README.md index 1b15ca9..df7b354 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Create a connection array or object similar to this: The parameters required depend on the database. ### Running Queries -Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `count_all_results`, `having`, `or_having`, `insert_batch`, `update_batch`, or `count_all` methods. +Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `count_all_results`, `having`, `or_having`, `insert_batch`, `update_batch` methods. #### Retrieving Results diff --git a/classes/query_builder.php b/classes/query_builder.php index 17f8553..79e8671 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -1043,7 +1043,9 @@ class Query_Builder { */ public function count_all($table) { - //@todo Implement count_all + $sql = 'SELECT * FROM '.$this->quote_ident($table); + $res = $this->query($sql); + return count($res->fetchAll()); } // -------------------------------------------------------------------------- @@ -1290,7 +1292,7 @@ class Query_Builder { break; } - // echo $sql . '
'; + //echo $sql . '
'; return $sql; } diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index f46899a..30071fa 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -17,7 +17,7 @@ * * PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions. */ -class firebird extends DB_PDO { +class Firebird extends DB_PDO { protected $statement, $statement_link, $trans, $count, $result, $conn; @@ -33,11 +33,11 @@ class firebird extends DB_PDO { $this->conn = fbird_connect($dbpath, $user, $pass, 'utf-8'); // Throw an exception to make this match other pdo classes - /*if ( ! is_resource($this->conn)) + if ( ! is_resource($this->conn)) { throw new PDOException(fbird_errmsg()); die(); - }*/ + } $class = __CLASS__."_sql"; $this->sql = new $class; @@ -92,8 +92,6 @@ class firebird extends DB_PDO { return new FireBird_Result($this->statement_link); } - - // -------------------------------------------------------------------------- /** @@ -124,16 +122,7 @@ class firebird extends DB_PDO { */ public function num_rows() { - // @todo: Redo this similar to the codeigniter driver - if(isset($this->result)) - { - return count($this->result); - } - - //Fetch all the rows for the result - $this->result = $this->statement->fetchAll(); - - return count($this->result); + return $this->statement->num_rows(); } // -------------------------------------------------------------------------- diff --git a/drivers/firebird/firebird_result.php b/drivers/firebird/firebird_result.php index 3d08fa4..d28f99b 100644 --- a/drivers/firebird/firebird_result.php +++ b/drivers/firebird/firebird_result.php @@ -132,6 +132,18 @@ class Firebird_Result { { return fbird_affected_rows(); } + + // -------------------------------------------------------------------------- + + /** + * Return the number of rows for the select query + * + * @return int + */ + public function num_rows() + { + return count($this->fetchAll()); + } // -------------------------------------------------------------------------- diff --git a/drivers/pgsql/pgsql_driver.php b/drivers/pgsql/pgsql_driver.php index ec1d380..e9fb11b 100644 --- a/drivers/pgsql/pgsql_driver.php +++ b/drivers/pgsql/pgsql_driver.php @@ -71,7 +71,7 @@ class pgSQL extends DB_PDO { */ public function num_rows() { - return (isset($this->statement)) ? $this->statement->rowCount : FALSE; + return (isset($this->statement)) ? $this->statement->rowCount() : FALSE; } // -------------------------------------------------------------------------- diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index 215f95b..4364b63 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -139,7 +139,7 @@ SQL; */ public function num_rows() { - return (isset($this->statement)) ? $this->statement->rowCount : FALSE; + return (isset($this->statement)) ? $this->statement->rowCount() : FALSE; } } //End of sqlite_driver.php \ No newline at end of file diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 2f8c5e0..28ea6e3 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -17,6 +17,8 @@ */ abstract class QBTest extends UnitTestCase { + // ! Get Tests + function TestGet() { if (empty($this->db)) return; @@ -43,6 +45,24 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } + + function TestGetWhere() + { + if (empty($this->db)) return; + + $query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetViews() + { + if (empty($this->db)) return; + + $this->assertTrue(is_array($this->db->get_views())); + } + + // ! Select Tests function TestSelectWhereGet() { @@ -117,15 +137,6 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } - - function TestGetWhere() - { - if (empty($this->db)) return; - - $query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } function TestSelectGet() { @@ -161,6 +172,8 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } + + // ! Query modifier tests function TestOrderBy() { @@ -247,6 +260,8 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } + + // ! DB update tests function TestInsert() { @@ -282,12 +297,16 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } - function TestGetViews() + // ! Non-data read queries + + function TestCountAll() { if (empty($this->db)) return; - - $this->assertTrue(is_array($this->db->get_views())); + $query = $this->db->count_all('create_test'); + + $this->assertTrue(is_numeric($query)); } + } // End of db_qb_test.php \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB old mode 100755 new mode 100644 index e4f6dfcaf6800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