Added count_all method to query_builder

This commit is contained in:
Timothy Warren 2012-04-17 08:29:28 -04:00
parent 49503dfb3d
commit f7d18b19fd
8 changed files with 54 additions and 32 deletions

View File

@ -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

View File

@ -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 . '<br />';
//echo $sql . '<br />';
return $sql;
}

View File

@ -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();
}
// --------------------------------------------------------------------------

View File

@ -135,6 +135,18 @@ class Firebird_Result {
// --------------------------------------------------------------------------
/**
* Return the number of rows for the select query
*
* @return int
*/
public function num_rows()
{
return count($this->fetchAll());
}
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
*

View File

@ -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;
}
// --------------------------------------------------------------------------

View File

@ -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

View File

@ -17,6 +17,8 @@
*/
abstract class QBTest extends UnitTestCase {
// ! Get Tests
function TestGet()
{
if (empty($this->db)) return;
@ -44,6 +46,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()
{
if (empty($this->db)) return;
@ -118,15 +138,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()
{
if (empty($this->db)) return;
@ -162,6 +173,8 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! Query modifier tests
function TestOrderBy()
{
if (empty($this->db)) return;
@ -248,6 +261,8 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement');
}
// ! DB update tests
function TestInsert()
{
if (empty($this->db)) return;
@ -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;
$query = $this->db->count_all('create_test');
$this->assertTrue(is_array($this->db->get_views()));
$this->assertTrue(is_numeric($query));
}
}
// End of db_qb_test.php

BIN
tests/db_files/FB_TEST_DB.FDB Executable file → Normal file

Binary file not shown.