Skip to content

Commit

Permalink
Merge pull request laravel#5 from JoostK/5.0-builder
Browse files Browse the repository at this point in the history
Extend aggregate support with groupings
  • Loading branch information
GrahamCampbell committed Mar 24, 2015
2 parents 255f7e5 + 2764750 commit d1ac237
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1428,10 +1428,7 @@ public function getCountForPagination()

$total = $this->count();

if(isset($this->groups))
{
$total = count($total);
}
if (is_array($total)) $total = count($total);

$this->restoreFieldsForCount();

Expand Down Expand Up @@ -1576,7 +1573,9 @@ public function count($columns = '*')
$columns = array($columns);
}

return $this->aggregate(__FUNCTION__, $columns);
$count = $this->aggregate(__FUNCTION__, $columns);

return is_array($count) ? array_map('intval', $count) : (int) $count;
}

/**
Expand Down Expand Up @@ -1647,20 +1646,23 @@ public function aggregate($function, $columns = array('*'))

$this->columns = $previousColumns;

// Once we have used groupBy function, $result may have more than one
// record, so we need to get all of $results elements in array.
if (count($results) > 1)
$results = array_map(function($result)
{
$results = array_change_key_case((array) $results);
$result = array_change_key_case((array) $result);

return array_pluck($results, 'aggregate');
}
return $result['aggregate'];
}, (array) $results);

if (isset($results[0]))
// If any groupings have been specified, we return the aggregate results as
// is such that we give information about all groups. Otherwise we check
// if the aggregate had any results and return the value as a scalar.
if (isset($this->groups))
{
$result = array_change_key_case((array) $results[0]);

return $result['aggregate'];
return $results;
}
elseif (isset($results[0]))
{
return $results[0];
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,12 @@ public function testAggregateFunctions()
$results = $builder->from('users')->count();
$this->assertEquals(1, $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', array(), true)->andReturn(null);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->count();
$this->assertEquals(0, $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users" limit 1', array(), true)->andReturn(array(array('aggregate' => 1)));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
Expand All @@ -836,6 +842,34 @@ public function testAggregateFunctions()
}


public function testAggregateFunctionsWithGroups()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users" group by "name"', array(), true)->andReturn(array(array('aggregate' => '3'), array('Aggregate' => '2')));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->groupBy('name')->count();
$this->assertSame(array(3, 2), $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select max("id") as aggregate from "users" group by "name"', array(), true)->andReturn(array(array('aggregate' => 3), array('aggregate' => 2)));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->groupBy('name')->max('id');
$this->assertEquals(array(3, 2), $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select min("id") as aggregate from "users" group by "name"', array(), true)->andReturn(array(array('aggregate' => 3), array('aggregate' => 2)));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->groupBy('name')->min('id');
$this->assertEquals(array(3, 2), $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users" group by "name"', array(), true)->andReturn(array(array('aggregate' => 3), array('aggregate' => 2)));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->groupBy('name')->sum('id');
$this->assertEquals(array(3, 2), $results);
}


public function testAggregateResetFollowedByGet()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit d1ac237

Please sign in to comment.