Fix num_rows() method

This commit is contained in:
Timothy Warren 2012-09-27 17:41:29 +00:00
parent 742a4e7ecd
commit 27318506d0
4 changed files with 35 additions and 5 deletions

View File

@ -51,6 +51,13 @@ abstract class DB_PDO extends PDO {
*/ */
public $util; public $util;
/**
* Last query executed
*
* @var string
*/
public $last_query;
/** /**
* PDO constructor wrapper * PDO constructor wrapper
* *
@ -443,9 +450,16 @@ abstract class DB_PDO extends PDO {
*/ */
public function num_rows() public function num_rows()
{ {
return isset($this->statement) && is_object($this->statement) $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
? $this->statement->rowCount() $output = array();
: FALSE;
if (preg_match($regex, $this->last_query, $output) > 0)
{
$stmt = $this->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
return $stmt->fetchColumn();
}
return FALSE;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -1511,7 +1511,7 @@ class Query_Builder {
// Set the where string // Set the where string
if ( ! empty($this->query_map)) if ( ! empty($this->query_map))
{ {
foreach($this->query_map as &$q) foreach($this->query_map as $q)
{ {
$sql .= $q['conjunction'] . $q['string']; $sql .= $q['conjunction'] . $q['string'];
} }
@ -1524,7 +1524,7 @@ class Query_Builder {
// Set the where string // Set the where string
if ( ! empty($this->query_map)) if ( ! empty($this->query_map))
{ {
foreach($this->query_map as &$q) foreach($this->query_map as $q)
{ {
$sql .= $q['conjunction'] . $q['string']; $sql .= $q['conjunction'] . $q['string'];
} }
@ -1533,8 +1533,12 @@ class Query_Builder {
break; break;
} }
// Add the query to the list of executed queries
$this->queries[] = $sql; $this->queries[] = $sql;
// Set the last query to get rowcounts properly
$this->db->last_query = $sql;
// echo $sql . '<br />'; // echo $sql . '<br />';
return $sql; return $sql;

View File

@ -33,6 +33,18 @@ abstract class QBTest extends UnitTestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetWNumRows()
{
if (empty($this->db)) return;
$query = $this->db->get('create_test');
$numrows = count($query->fetchAll(PDO::FETCH_NUM));
$this->assertEqual($this->db->num_rows(), $numrows);
}
// --------------------------------------------------------------------------
public function TestGetLimit() public function TestGetLimit()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;

Binary file not shown.