Implemented count_all_results method in Query Builder

This commit is contained in:
Timothy Warren 2012-04-17 11:37:39 -04:00
parent 1211ed6347
commit 902b6b7cb3
4 changed files with 53 additions and 3 deletions

View File

@ -45,7 +45,7 @@ Create a connection array or object similar to this:
The parameters required depend on the database. The parameters required depend on the database.
### Running Queries ### 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 #### Retrieving Results

View File

@ -1045,7 +1045,7 @@ class Query_Builder {
{ {
$sql = 'SELECT * FROM '.$this->quote_ident($table); $sql = 'SELECT * FROM '.$this->quote_ident($table);
$res = $this->query($sql); $res = $this->query($sql);
return count($res->fetchAll()); return (int) count($res->fetchAll());
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -1059,7 +1059,35 @@ class Query_Builder {
*/ */
public function count_all_results($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 File

@ -307,6 +307,28 @@ abstract class QBTest extends UnitTestCase {
$this->assertTrue(is_numeric($query)); $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 // End of db_qb_test.php

Binary file not shown.