Skip to content

Commit

Permalink
allow expressions in GROUP BY
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Jan 27, 2016
1 parent 5a462dc commit f0a62cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 19 additions & 1 deletion framework/db/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,21 @@ public function buildWhere($condition, &$params)
*/
public function buildGroupBy($columns)
{
return empty($columns) ? '' : 'GROUP BY ' . $this->buildColumns($columns);
if (empty($columns)) {
return '';
}
foreach ($columns as $i => $column) {
if ($column instanceof Expression) {
$columns[$i] = $column->expression;
// TODO
// foreach ($direction->params as $n => $v) {
// $params[$n] = $v;
// }
} elseif (strpos($column, '(') === false) {
$columns[$i] = $this->db->quoteColumnName($column);
}
}
return 'GROUP BY ' . implode(', ', $columns);
}

/**
Expand Down Expand Up @@ -800,6 +814,10 @@ public function buildOrderBy($columns)
foreach ($columns as $name => $direction) {
if ($direction instanceof Expression) {
$orders[] = $direction->expression;
// TODO
// foreach ($direction->params as $n => $v) {
// $params[$n] = $v;
// }
} else {
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
}
Expand Down
8 changes: 4 additions & 4 deletions tests/framework/db/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public function testOrderBy()
->from('operations')
->orderBy('name ASC, date DESC');
list ($sql, $params) = $this->getQueryBuilder()->build($query);
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY `name`, `date` DESC');
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC');
$this->assertEquals($expected, $sql);
$this->assertEmpty($params);

Expand All @@ -611,7 +611,7 @@ public function testOrderBy()
->from('operations')
->orderBy(['name' => SORT_ASC, 'date' => SORT_DESC]);
list ($sql, $params) = $this->getQueryBuilder()->build($query);
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY `name`, `date` DESC');
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC');
$this->assertEquals($expected, $sql);
$this->assertEmpty($params);

Expand Down Expand Up @@ -645,7 +645,7 @@ public function testGroupBy()
->from('operations')
->groupBy('name, date');
list ($sql, $params) = $this->getQueryBuilder()->build($query);
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY `name`, `date`');
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]');
$this->assertEquals($expected, $sql);
$this->assertEmpty($params);

Expand All @@ -655,7 +655,7 @@ public function testGroupBy()
->from('operations')
->groupBy(['name', 'date']);
list ($sql, $params) = $this->getQueryBuilder()->build($query);
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY `name`, `date`');
$expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]');
$this->assertEquals($expected, $sql);
$this->assertEmpty($params);

Expand Down

0 comments on commit f0a62cd

Please sign in to comment.