Skip to content

Commit 7576a55

Browse files
authored
Merge pull request #2160 from MGatner/select-count
Add BaseBuilder SelectCount
2 parents 95effac + 7065ed3 commit 7576a55

5 files changed

Lines changed: 90 additions & 4 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,24 @@ public function selectSum(string $select = '', string $alias = '')
384384
//--------------------------------------------------------------------
385385

386386
/**
387-
* SELECT [MAX|MIN|AVG|SUM]()
387+
* Select Count
388+
*
389+
* Generates a SELECT COUNT(field) portion of a query
390+
*
391+
* @param string $select The field
392+
* @param string $alias An alias
393+
*
394+
* @return BaseBuilder
395+
*/
396+
public function selectCount(string $select = '', string $alias = '')
397+
{
398+
return $this->maxMinAvgSum($select, $alias, 'COUNT');
399+
}
400+
401+
//--------------------------------------------------------------------
402+
403+
/**
404+
* SELECT [MAX|MIN|AVG|SUM|COUNT]()
388405
*
389406
* @used-by selectMax()
390407
* @used-by selectMin()
@@ -413,7 +430,7 @@ protected function maxMinAvgSum(string $select = '', string $alias = '', string
413430

414431
$type = strtoupper($type);
415432

416-
if (! in_array($type, ['MAX', 'MIN', 'AVG', 'SUM']))
433+
if (! in_array($type, ['MAX', 'MIN', 'AVG', 'SUM', 'COUNT']))
417434
{
418435
throw new DatabaseException('Invalid function type: ' . $type);
419436
}

tests/system/Database/Builder/SelectTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,32 @@ public function testSelectSumWithAlias()
199199

200200
//--------------------------------------------------------------------
201201

202+
public function testSelectCountWithNoAlias()
203+
{
204+
$builder = new BaseBuilder('invoices', $this->db);
205+
206+
$builder->selectCount('payments');
207+
208+
$expected = 'SELECT COUNT("payments") AS "payments" FROM "invoices"';
209+
210+
$this->assertEquals($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
211+
}
212+
213+
//--------------------------------------------------------------------
214+
215+
public function testSelectCountWithAlias()
216+
{
217+
$builder = new BaseBuilder('invoices', $this->db);
218+
219+
$builder->selectCount('payments', 'myAlias');
220+
221+
$expected = 'SELECT COUNT("payments") AS "myAlias" FROM "invoices"';
222+
223+
$this->assertEquals($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
224+
}
225+
226+
//--------------------------------------------------------------------
227+
202228
public function testSelectMinThrowsExceptionOnEmptyValue()
203229
{
204230
$builder = new BaseBuilder('invoices', $this->db);

tests/system/Database/Live/GroupTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,15 @@ public function testOrNotGroups()
123123

124124
//--------------------------------------------------------------------
125125

126+
public function testGroupByCount()
127+
{
128+
$result = $this->db->table('user')
129+
->selectCount('id', 'count')
130+
->groupBy('country')
131+
->orderBy('country', 'desc')
132+
->get()
133+
->getResult();
134+
135+
$this->assertEquals(2, $result[0]->count);
136+
}
126137
}

tests/system/Database/Live/SelectTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function testSelectAvg()
9191

9292
//--------------------------------------------------------------------
9393

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

@@ -109,7 +109,7 @@ public function testSelectSum()
109109

110110
//--------------------------------------------------------------------
111111

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

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

119119
//--------------------------------------------------------------------
120120

121+
public function testSelectCount()
122+
{
123+
$result = $this->db->table('job')->selectCount('id')->get()->getRow();
124+
125+
$this->assertEquals(4, $result->id);
126+
}
127+
128+
//--------------------------------------------------------------------
129+
130+
public function testSelectCountWithAlias()
131+
{
132+
$result = $this->db->table('job')->selectCount('id', 'xam')->get()->getRow();
133+
134+
$this->assertEquals(4, $result->xam);
135+
}
136+
137+
//--------------------------------------------------------------------
138+
121139
public function testSelectDistinctWorkTogether()
122140
{
123141
$users = $this->db->table('user')->select('country')->distinct()->get()->getResult();

user_guide_src/source/database/query_builder.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ Writes a "SELECT SUM(field)" portion for your query. As with
172172
selectMax(), You can optionally include a second parameter to rename
173173
the resulting field.
174174

175+
::
176+
177+
$builder->selectSum('age');
178+
$query = $builder->get(); // Produces: SELECT SUM(age) as age FROM mytable
179+
180+
**$builder->selectCount()**
181+
182+
Writes a "SELECT COUNT(field)" portion for your query. As with
183+
selectMax(), You can optionally include a second parameter to rename
184+
the resulting field.
185+
186+
.. note:: This method is particularly helpful when used with ``groupBy()``. For
187+
counting results generally see ``countAll()`` or ``countAllResults()``.
188+
175189
::
176190

177191
$builder->selectSum('age');

0 commit comments

Comments
 (0)