summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Holywell <[email protected]>2012-11-12 11:20:46 +0000
committerSimon Holywell <[email protected]>2012-11-12 11:20:46 +0000
commit22e24b17b4888e62fc7ee59e3516f1d063d342b1 (patch)
tree3331dd77cbd0e027c85c2c89c07bf2faa13f09e9
parent8896487378dfee87883e2fa4a260812796bb6848 (diff)
Issue #16 Aggregate function support
-rw-r--r--README.markdown11
-rw-r--r--idiorm.php53
-rw-r--r--test/test_queries.php16
3 files changed, 77 insertions, 3 deletions
diff --git a/README.markdown b/README.markdown
index a2b2ac7..f303457 100644
--- a/README.markdown
+++ b/README.markdown
@@ -34,6 +34,7 @@ Changelog
* Allow an associative array to be passed to `configure` method [[jordanlev](http://github.com/jordanlev)]
* Patch to allow empty Paris models to be saved ([[j4mie/paris](http://github.com/j4mie/paris)]) issue #58
* Add `select_many` and `select_many_expr` - closing issues #49 and #69
+* Add support for `MIN`, `AVG`, `MAX` and `SUM` - closes issue #16
#### 1.1.1 - release 2011-01-30
@@ -343,6 +344,16 @@ The `join` methods also take an optional third parameter, which is an `alias` fo
->join('person', array('p1.parent', '=', 'p2.id'), 'p2')
->find_many();
+#### Aggregate functions ####
+
+There is support for `MIN`, `AVG`, `MAX` and `SUM` in addition to `COUNT` (documented earlier).
+
+To return a minimum value of column, call the `min()` method.
+
+ $min = ORM::for_table('person')->min('height');
+
+The other functions (`AVG`, `MAX` and `SUM`) work in exactly the same manner. Supply a column name to perform the aggregate function on and it will return an integer.
+
#### Raw queries ####
If you need to perform more complex queries, you can completely specify the query to execute by using the `raw_query` method. This method takes a string and optionally an array of parameters. The string can contain placeholders, either in question mark or named placeholder syntax, which will be used to bind the parameters to the query.
diff --git a/idiorm.php b/idiorm.php
index 62332d5..f5178b5 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -398,10 +398,57 @@
* Will return an integer representing the number of
* rows returned.
*/
- public function count() {
- $this->select_expr('COUNT(*)', 'count');
+ public function count($column = '*') {
+ return $this->_call_aggregate_db_function(__FUNCTION__, $column);
+ }
+
+ /**
+ * Tell the ORM that you wish to execute a MAX query.
+ * Will return the max value of the choosen column.
+ */
+ public function max($column) {
+ return $this->_call_aggregate_db_function(__FUNCTION__, $column);
+ }
+
+ /**
+ * Tell the ORM that you wish to execute a MIN query.
+ * Will return the min value of the choosen column.
+ */
+ public function min($column) {
+ return $this->_call_aggregate_db_function(__FUNCTION__, $column);
+ }
+
+ /**
+ * Tell the ORM that you wish to execute a AVG query.
+ * Will return the average value of the choosen column.
+ */
+ public function avg($column) {
+ return $this->_call_aggregate_db_function(__FUNCTION__, $column);
+ }
+
+ /**
+ * Tell the ORM that you wish to execute a SUM query.
+ * Will return the sum of the choosen column.
+ */
+ public function sum($column) {
+ return $this->_call_aggregate_db_function(__FUNCTION__, $column);
+ }
+
+ /**
+ * Execute an aggregate query on the current connection.
+ * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc
+ * @param string $column The column to execute the aggregate query against
+ * @return int
+ */
+ protected function _call_aggregate_db_function($sql_function, $column) {
+ $alias = strtolower($sql_function);
+ $sql_function = strtoupper($sql_function);
+ if('*' != $column) {
+ $column = $this->_quote_identifier($column);
+ }
+ $this->select_expr("$sql_function($column)", $alias);
$result = $this->find_one();
- return ($result !== false && isset($result->count)) ? (int) $result->count : 0;
+ return ($result !== false && isset($result->$alias)) ? (int) $result->$alias : 0;
}
/**
diff --git a/test/test_queries.php b/test/test_queries.php
index 73b05e1..dc01103 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -36,6 +36,22 @@
$expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1";
Tester::check_equal("COUNT query", $expected);
+ ORM::for_table('person')->max('height');
+ $expected = "SELECT MAX(`height`) AS `max` FROM `person` LIMIT 1";
+ Tester::check_equal("MAX query", $expected);
+
+ ORM::for_table('person')->min('height');
+ $expected = "SELECT MIN(`height`) AS `min` FROM `person` LIMIT 1";
+ Tester::check_equal("MIN query", $expected);
+
+ ORM::for_table('person')->avg('height');
+ $expected = "SELECT AVG(`height`) AS `avg` FROM `person` LIMIT 1";
+ Tester::check_equal("AVG query", $expected);
+
+ ORM::for_table('person')->sum('height');
+ $expected = "SELECT SUM(`height`) AS `sum` FROM `person` LIMIT 1";
+ Tester::check_equal("SUM query", $expected);
+
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1";
Tester::check_equal("Single where clause", $expected);