Skip to content

Commit

Permalink
Added count_all method to query_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Apr 17, 2012
1 parent 49503df commit f7d18b1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions classes/query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,9 @@ public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE)
*/
public function count_all($table)
{
//@todo Implement count_all
$sql = 'SELECT * FROM '.$this->quote_ident($table);
$res = $this->query($sql);
return count($res->fetchAll());
}

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -1290,7 +1292,7 @@ private function _compile($type='', $table="")
break;
}

// echo $sql . '<br />';
//echo $sql . '<br />';

return $sql;
}
Expand Down
19 changes: 4 additions & 15 deletions drivers/firebird/firebird_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,11 +33,11 @@ public function __construct($dbpath, $user='sysdba', $pass='masterkey')
$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;
Expand Down Expand Up @@ -92,8 +92,6 @@ public function query($sql)
return new FireBird_Result($this->statement_link);
}



// --------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -124,16 +122,7 @@ public function prepare($query, $options=NULL)
*/
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();
}

// --------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions drivers/firebird/firebird_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public function rowCount()
{
return fbird_affected_rows();
}

// --------------------------------------------------------------------------

/**
* Return the number of rows for the select query
*
* @return int
*/
public function num_rows()
{
return count($this->fetchAll());
}

// --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion drivers/pgsql/pgsql_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function truncate($table)
*/
public function num_rows()
{
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
return (isset($this->statement)) ? $this->statement->rowCount() : FALSE;
}

// --------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion drivers/sqlite/sqlite_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function unload_database($name)
*/
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
43 changes: 31 additions & 12 deletions tests/core/db_qb_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
abstract class QBTest extends UnitTestCase {

// ! Get Tests

function TestGet()
{
if (empty($this->db)) return;
Expand All @@ -43,6 +45,24 @@ function TestGetLimitSkip()

$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()
{
Expand Down Expand Up @@ -117,15 +137,6 @@ function TestSelectDistinct()

$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()
{
Expand Down Expand Up @@ -161,6 +172,8 @@ function TestSelectFromLimitGet()

$this->assertIsA($query, 'PDOStatement');
}

// ! Query modifier tests

function TestOrderBy()
{
Expand Down Expand Up @@ -247,6 +260,8 @@ function TestJoin()

$this->assertIsA($query, 'PDOStatement');
}

// ! DB update tests

function TestInsert()
{
Expand Down Expand Up @@ -282,12 +297,16 @@ function TestDelete()
$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
Binary file modified tests/db_files/FB_TEST_DB.FDB
100755 → 100644
Binary file not shown.

0 comments on commit f7d18b1

Please sign in to comment.