Skip to content

Commit

Permalink
Merge pull request #2160 from MGatner/select-count
Browse files Browse the repository at this point in the history
Add BaseBuilder SelectCount
  • Loading branch information
lonnieezell committed Aug 26, 2019
2 parents 95effac + 7065ed3 commit 7576a55
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
21 changes: 19 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,24 @@ public function selectSum(string $select = '', string $alias = '')
//--------------------------------------------------------------------

/**
* SELECT [MAX|MIN|AVG|SUM]()
* Select Count
*
* Generates a SELECT COUNT(field) portion of a query
*
* @param string $select The field
* @param string $alias An alias
*
* @return BaseBuilder
*/
public function selectCount(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias, 'COUNT');
}

//--------------------------------------------------------------------

/**
* SELECT [MAX|MIN|AVG|SUM|COUNT]()
*
* @used-by selectMax()
* @used-by selectMin()
Expand Down Expand Up @@ -413,7 +430,7 @@ protected function maxMinAvgSum(string $select = '', string $alias = '', string

$type = strtoupper($type);

if (! in_array($type, ['MAX', 'MIN', 'AVG', 'SUM']))
if (! in_array($type, ['MAX', 'MIN', 'AVG', 'SUM', 'COUNT']))
{
throw new DatabaseException('Invalid function type: ' . $type);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/system/Database/Builder/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,32 @@ public function testSelectSumWithAlias()

//--------------------------------------------------------------------

public function testSelectCountWithNoAlias()
{
$builder = new BaseBuilder('invoices', $this->db);

$builder->selectCount('payments');

$expected = 'SELECT COUNT("payments") AS "payments" FROM "invoices"';

$this->assertEquals($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

//--------------------------------------------------------------------

public function testSelectCountWithAlias()
{
$builder = new BaseBuilder('invoices', $this->db);

$builder->selectCount('payments', 'myAlias');

$expected = 'SELECT COUNT("payments") AS "myAlias" FROM "invoices"';

$this->assertEquals($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

//--------------------------------------------------------------------

public function testSelectMinThrowsExceptionOnEmptyValue()
{
$builder = new BaseBuilder('invoices', $this->db);
Expand Down
11 changes: 11 additions & 0 deletions tests/system/Database/Live/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@ public function testOrNotGroups()

//--------------------------------------------------------------------

public function testGroupByCount()
{
$result = $this->db->table('user')
->selectCount('id', 'count')
->groupBy('country')
->orderBy('country', 'desc')
->get()
->getResult();

$this->assertEquals(2, $result[0]->count);
}
}
22 changes: 20 additions & 2 deletions tests/system/Database/Live/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testSelectAvg()

//--------------------------------------------------------------------

public function testSelectAvgWitAlias()
public function testSelectAvgWithAlias()
{
$result = $this->db->table('job')->selectAvg('id', 'xam')->get()->getRow();

Expand All @@ -109,7 +109,7 @@ public function testSelectSum()

//--------------------------------------------------------------------

public function testSelectSumWitAlias()
public function testSelectSumWithAlias()
{
$result = $this->db->table('job')->selectSum('id', 'xam')->get()->getRow();

Expand All @@ -118,6 +118,24 @@ public function testSelectSumWitAlias()

//--------------------------------------------------------------------

public function testSelectCount()
{
$result = $this->db->table('job')->selectCount('id')->get()->getRow();

$this->assertEquals(4, $result->id);
}

//--------------------------------------------------------------------

public function testSelectCountWithAlias()
{
$result = $this->db->table('job')->selectCount('id', 'xam')->get()->getRow();

$this->assertEquals(4, $result->xam);
}

//--------------------------------------------------------------------

public function testSelectDistinctWorkTogether()
{
$users = $this->db->table('user')->select('country')->distinct()->get()->getResult();
Expand Down
14 changes: 14 additions & 0 deletions user_guide_src/source/database/query_builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ Writes a "SELECT SUM(field)" portion for your query. As with
selectMax(), You can optionally include a second parameter to rename
the resulting field.

::

$builder->selectSum('age');
$query = $builder->get(); // Produces: SELECT SUM(age) as age FROM mytable

**$builder->selectCount()**

Writes a "SELECT COUNT(field)" portion for your query. As with
selectMax(), You can optionally include a second parameter to rename
the resulting field.

.. note:: This method is particularly helpful when used with ``groupBy()``. For
counting results generally see ``countAll()`` or ``countAllResults()``.

::

$builder->selectSum('age');
Expand Down

0 comments on commit 7576a55

Please sign in to comment.