diff --git a/doc/api.md b/doc/api.md index f22d07f..8b7fa43 100644 --- a/doc/api.md +++ b/doc/api.md @@ -81,7 +81,7 @@ $result2 = $result->where($array) // for each key-value pair, call $result->w $result2 = $result->whereNot($array) // for each key-value pair, call $result->whereNot($key, $value) ``` -## Selected columns, Order and Limit +## Selected columns, Group, Order and Limit Note that you can order association results, but you cannot use `LIMIT` on them. @@ -89,6 +89,9 @@ Note that you can order association results, but you cannot use `LIMIT` on them. $result2 = $result->select($expr) // identfiers NOT escaped, so expressions are possible // multiple calls are joined with a comma +// $column group by +$result2 = $result->groupBy($column); + // $column will be escaped $result2 = $result->orderBy($column); $result2 = $result->orderBy($column, 'ASC'); diff --git a/src/LessQL/Database.php b/src/LessQL/Database.php index b56e325..d2f08b3 100644 --- a/src/LessQL/Database.php +++ b/src/LessQL/Database.php @@ -453,6 +453,7 @@ public function setIdentifierDelimiter($d) * @param string $table * @param mixed $exprs * @param array $where + * @param array $groupBy * @param array $orderBy * @param int|null $limitCount * @param int|null $limitOffset @@ -464,6 +465,7 @@ public function select($table, $options = array()) $options = array_merge(array( 'expr' => null, 'where' => array(), + 'groupBy' => array(), 'orderBy' => array(), 'limitCount' => null, 'limitOffset' => null, @@ -484,7 +486,7 @@ public function select($table, $options = array()) $table = $this->rewriteTable($table); $query .= " FROM " . $this->quoteIdentifier($table); - $query .= $this->getSuffix($options['where'], $options['orderBy'], $options['limitCount'], $options['limitOffset']); + $query .= $this->getSuffix($options['where'], $options['groupBy'], $options['orderBy'], $options['limitCount'], $options['limitOffset']); $this->onQuery($query, $options['params']); @@ -762,12 +764,13 @@ public function delete($table, $where = array(), $params = array()) * Return WHERE/LIMIT/ORDER suffix for queries * * @param array $where + * @param array $groupBy * @param array $orderBy * @param int|null $limitCount * @param int|null $limitOffset * @return string */ - public function getSuffix($where, $orderBy = array(), $limitCount = null, $limitOffset = null) + public function getSuffix($where,$groupBy = array(), $orderBy = array(), $limitCount = null, $limitOffset = null) { $suffix = ""; @@ -775,6 +778,10 @@ public function getSuffix($where, $orderBy = array(), $limitCount = null, $limit $suffix .= " WHERE (" . implode(") AND (", $where) . ")"; } + if (!empty($groupBy)) { + $suffix .= " GROUP BY " . implode(", ", $groupBy); + } + if (!empty($orderBy)) { $suffix .= " ORDER BY " . implode(", ", $orderBy); }