diff --git a/recess/recess/database/pdo/PdoDataSet.class.php b/recess/recess/database/pdo/PdoDataSet.class.php index 28e8319..46eeabf 100644 --- a/recess/recess/database/pdo/PdoDataSet.class.php +++ b/recess/recess/database/pdo/PdoDataSet.class.php @@ -341,5 +341,11 @@ function range($start, $finish) { $copy = clone $this; $copy->sqlBuilder->range( * @return PdoDataSet */ function orderBy($clause) { $copy = clone $this; $copy->sqlBuilder->orderBy($clause); return $copy; } + + /** + * @see SqlBuilder::groupBy + * @return PdoDataSet + */ + function groupBy($clause) { $copy = clone $this; $copy->sqlBuilder->groupBy($clause); return $copy; } } ?> \ No newline at end of file diff --git a/recess/recess/database/sql/SqlBuilder.class.php b/recess/recess/database/sql/SqlBuilder.class.php index 7ec26b7..c5ec351 100644 --- a/recess/recess/database/sql/SqlBuilder.class.php +++ b/recess/recess/database/sql/SqlBuilder.class.php @@ -73,6 +73,8 @@ protected function insertSanityCheck() { throw new RecessException('Insert does not use joins.', get_defined_vars()); if( !empty($this->orderBy) ) throw new RecessException('Insert does not use order by.', get_defined_vars()); + if( !empty($this->groupBy) ) + throw new RecessException('Insert does not use group by.', get_defined_vars()); if( isset($this->limit) ) throw new RecessException('Insert does not use limit.', get_defined_vars()); if( isset($this->offset) ) @@ -140,6 +142,8 @@ protected function deleteSanityCheck() { throw new RecessException('Delete does not use joins.', get_defined_vars()); if( !empty($this->orderBy) ) throw new RecessException('Delete does not use order by.', get_defined_vars()); + if( !empty($this->groupBy) ) + throw new RecessException('Delete does not use group by.', get_defined_vars()); if( isset($this->limit) ) throw new RecessException('Delete does not use limit.', get_defined_vars()); if( isset($this->offset) ) @@ -180,6 +184,8 @@ protected function updateSanityCheck() { throw new RecessException('Update does not use joins.', get_defined_vars()); if( !empty($this->orderBy) ) throw new RecessException('Update (in Recess) does not use order by.', get_defined_vars()); + if( !empty($this->groupBy) ) + throw new RecessException('Update (in Recess) does not use group by.', get_defined_vars()); if( isset($this->limit) ) throw new RecessException('Update (in Recess) does not use limit.', get_defined_vars()); if( isset($this->offset) ) @@ -378,6 +384,7 @@ protected function addCondition($column, $value, $operator) { protected $offset; protected $distinct; protected $orderBy = array(); + protected $groupBy = array(); protected $usingAliases = false; /** @@ -402,6 +409,8 @@ public function select() { $sql .= $this->orderByHelper(); + $sql .= $this->groupByHelper(); + $sql .= $this->rangeHelper(); return $sql; @@ -469,6 +478,27 @@ public function orderBy($clause) { return $this; } + /** + * Add an GROUP BY expression to sql string. Example: ->groupBy('name') + * + * @param string $clause + * @return SqlBuilder + */ + public function groupBy($clause) { + if(($spacePos = strpos($clause,' ')) !== false) { + $name = substr($clause,0,$spacePos); + } else { + $name = $clause; + } + + if(isset($this->table) && strpos($clause,'.') === false && strpos($name,'(') === false && !array_key_exists($name, $this->selectAs)) { + $this->groupBy[] = $this->tableAsPrefix() . '.' . $clause; + } else { + $this->groupBy[] = $clause; + } + return $this; + } + /** * Helper method which returns the current table even when it * is aliased due to joins between the same table. @@ -643,6 +673,19 @@ protected function orderByHelper() { return $sql; } + protected function groupByHelper() { + $sql = ''; + if(!empty($this->groupBy)){ + $sql = ' GROUP BY '; + $first = true; + foreach($this->groupBy as $order){ + if(!$first) { $sql .= ', '; } else { $first = false; } + $sql .= $order; + } + } + return $sql; + } + protected function rangeHelper() { $sql = ''; if(isset($this->limit)){ $sql .= ' LIMIT ' . $this->limit; }