Permalink
Browse files

Implemented count_all_results method in Query Builder

  • Loading branch information...
timw4mail committed Apr 17, 2012
1 parent 1211ed6 commit 902b6b7cb3d34663415d51b7809757d52bdcd7d3
Showing with 53 additions and 3 deletions.
  1. +1 −1 README.md
  2. +30 −2 classes/query_builder.php
  3. +22 −0 tests/core/db_qb_test.php
  4. BIN tests/db_files/FB_TEST_DB.FDB
View
@@ -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` 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 `having`, `or_having`, `insert_batch` or `update_batch` methods.
#### Retrieving Results
View
@@ -1045,7 +1045,7 @@ public function count_all($table)
{
$sql = 'SELECT * FROM '.$this->quote_ident($table);
$res = $this->query($sql);
- return count($res->fetchAll());
+ return (int) count($res->fetchAll());
}
// --------------------------------------------------------------------------
@@ -1059,7 +1059,35 @@ public function count_all($table)
*/
public function count_all_results($table='')
{
- // @todo Implement count_all_results
+ // Set the table
+ if ( ! empty($table))
+ {
+ $this->from($table);
+ }
+
+ $sql = $this->_compile();
+
+ // Do prepared statements for anything involving a "where" clause
+ if ( ! empty($this->query_map))
+ {
+ $result = $this->prepare_execute($sql, $this->values);
+ }
+ else
+ {
+ // Otherwise, a simple query will do.
+ $result = $this->query($sql);
+ }
+
+ // Reset for next query
+ $this->_reset();
+
+ $rows = $result->fetchAll();
+ $count = count($rows);
+
+ // Unset rows to save memory
+ $rows = NULL;
+
+ return (int) $count;
}
// --------------------------------------------------------------------------
View
@@ -307,6 +307,28 @@ function TestCountAll()
$this->assertTrue(is_numeric($query));
}
+ function TestCountAllResults()
+ {
+ if (empty($this->db)) return;
+ $query = $this->db->count_all_results('create_test');
+
+ $this->assertTrue(is_numeric($query));
+ }
+
+ function TestCountAllResults2()
+ {
+ if (empty($this->db)) return;
+
+ $query = $this->db->select('id, key as k, val')
+ ->from('create_test')
+ ->where(' id ', 1)
+ ->or_where('key >', 0)
+ ->limit(2, 1)
+ ->count_all_results();
+
+ $this->assertTrue(is_numeric($query));
+ }
+
}
// End of db_qb_test.php
Binary file not shown.

0 comments on commit 902b6b7

Please sign in to comment.