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 e4f6dfc..d1440b4 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ