From 902b6b7cb3d34663415d51b7809757d52bdcd7d3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Apr 2012 11:37:39 -0400 Subject: [PATCH] Implemented count_all_results method in Query Builder --- README.md | 2 +- classes/query_builder.php | 32 ++++++++++++++++++++++++++++++-- tests/core/db_qb_test.php | 22 ++++++++++++++++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index df7b354..9f3089b 100644 --- a/README.md +++ b/README.md @@ -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` 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 diff --git a/classes/query_builder.php b/classes/query_builder.php index 79e8671..0ee79a1 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -1045,7 +1045,7 @@ class Query_Builder { { $sql = 'SELECT * FROM '.$this->quote_ident($table); $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='') { - // @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; } // -------------------------------------------------------------------------- diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 28ea6e3..8bb51db 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -307,6 +307,28 @@ abstract class QBTest extends UnitTestCase { $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 \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 3f49ff60e0149701ff02fac80e65df3ff1deda3e..9d4bfa6de69421a6bf748d63ec1d12bcd85aa29b 100644 GIT binary patch delta 917 zcmZva&uddb5XWakEcT;jBV z-a41T5@A^2P9eG{H4TKA+ag)I$R zdTi;nrO%dKw)ESw+m_oFWxy-NcEXz^7mY-UUr~#x@mPJNQnxLj;KQ4^I{JOelA_t+Hkbt9E6&eN?%Q;^R4yZ%md_U zp@L^bo_tjtWiz*qaY|J&%KkoME>JYVd^Vrf( z9(w?0I=MAVqaDoKDoW40MFV6z zVVqo+dg=_zQnJpltST}_pX<1f{r?tumqu2YtK+!b#38L6E*x%iER~1Y{F~|I`X)t& zKCUn~Lp`g^&8Q+{bZ3>h8O0qJw`qfC!g`;3cw=d;b-fw7@DrD4ejRty!3HMiPm{g& z&Cq-UCH<;msPv(MMbCEK1kNS%ofT+yjq@nbw>69i=LQc@w22}9b)tzwTdlR^D%D}_ TD7tmrj%)Qut367bMC9y0PB-JE delta 921 zcmZ9K&ubG=5Xa}`N0Z$l^oc}@VoaLElq#6e`V%~a9)gHAhuTZ9X^kP2Sk$yoh#rFA z-g6pI5B>v!C}~t^z35d>UIZa&lhz!XQ1Bpv;OuT*8r;Y3yzjnw^O37M80NGI_@()vp@$*P+N(vO#oB!CdVTQ1 zOLrQ0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