Skip to content

Commit

Permalink
Adding groupBy() to PdoDataSet and SqlBuilder.
Browse files Browse the repository at this point in the history
$node = new Node();
$nodeSet = $node->all()->groupBy('name')->limit(10);

Signed-off-by: Recess PHP Framework <kris@recessframework.org>
  • Loading branch information
kevburnsjr authored and recess committed Aug 24, 2009
1 parent ac4ccd2 commit 4ca5ff9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions recess/recess/database/pdo/PdoDataSet.class.php
Expand Up @@ -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; }
}
?>
43 changes: 43 additions & 0 deletions recess/recess/database/sql/SqlBuilder.class.php
Expand Up @@ -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) )
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -378,6 +384,7 @@ protected function addCondition($column, $value, $operator) {
protected $offset;
protected $distinct;
protected $orderBy = array();
protected $groupBy = array();
protected $usingAliases = false;

/**
Expand All @@ -402,6 +409,8 @@ public function select() {

$sql .= $this->orderByHelper();

$sql .= $this->groupByHelper();

$sql .= $this->rangeHelper();

return $sql;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 4ca5ff9

Please sign in to comment.