Skip to content

Commit

Permalink
Support MySQL rollup keyword for group by clause
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 9, 2015
1 parent 37b4d09 commit a65f00b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/SQLBuilder/Universal/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class SelectQuery implements ToSqlInterface

protected $lockModifier;

protected $rollupModifier;

public function __construct()
{
$this->having = new Conditions;
Expand Down Expand Up @@ -231,15 +233,22 @@ public function clearGroupBy()
/**
* Note: SELECT FOR UPDATE does not work when used in select statement with a subquery.
*/
public function forUpdate() {
public function forUpdate()
{
$this->lockModifier = 'FOR UPDATE';
return $this;
}

public function lockInShareMode() {
public function lockInShareMode()
{
$this->lockModifier = 'LOCK IN SHARE MODE';
}

public function rollup()
{
$this->rollupModifier = 'WITH ROLLUP';
}

/****************************************************************
* Builders
***************************************************************/
Expand Down Expand Up @@ -319,6 +328,13 @@ public function buildGroupByClause(BaseDriver $driver, ArgumentArray $args) {
if ($this->groupByModifiers) {
$sql .= ' ' . join(' ', $this->groupByModifiers);
}

if ($this->rollupModifier) {
if (!$driver instanceof MySQLDriver) {
throw new Exception("Incompatible Query Usage: rollup is only supported in MySQL.");
}
$sql .= ' ' . $this->rollupModifier;
}
return $sql;
}

Expand Down

0 comments on commit a65f00b

Please sign in to comment.