diff --git a/classes/db_pdo.php b/classes/db_pdo.php index c0c1358..491ae32 100644 --- a/classes/db_pdo.php +++ b/classes/db_pdo.php @@ -51,6 +51,13 @@ abstract class DB_PDO extends PDO { */ public $util; + /** + * Last query executed + * + * @var string + */ + public $last_query; + /** * PDO constructor wrapper * @@ -443,9 +450,16 @@ abstract class DB_PDO extends PDO { */ public function num_rows() { - return isset($this->statement) && is_object($this->statement) - ? $this->statement->rowCount() - : FALSE; + $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; + $output = array(); + + 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; } // ------------------------------------------------------------------------- diff --git a/classes/query_builder.php b/classes/query_builder.php index 68f8405..80550a7 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -1511,7 +1511,7 @@ class Query_Builder { // Set the where string if ( ! empty($this->query_map)) { - foreach($this->query_map as &$q) + foreach($this->query_map as $q) { $sql .= $q['conjunction'] . $q['string']; } @@ -1524,7 +1524,7 @@ class Query_Builder { // Set the where string if ( ! empty($this->query_map)) { - foreach($this->query_map as &$q) + foreach($this->query_map as $q) { $sql .= $q['conjunction'] . $q['string']; } @@ -1533,8 +1533,12 @@ class Query_Builder { break; } + // Add the query to the list of executed queries $this->queries[] = $sql; + // Set the last query to get rowcounts properly + $this->db->last_query = $sql; + // echo $sql . '
'; return $sql; diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index e0b539b..c6db784 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -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() { if (empty($this->db)) return; diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index a8d6c35..761b13d 100644 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ