Skip to content

Commit c1724a6

Browse files
committed
add selectDistinct() method and tests
1 parent b5fc299 commit c1724a6

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/QueryBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,23 @@ public function select($table, $fields = '*', bool $dist = false): QueryBuilder
598598
return $this;
599599
}
600600

601+
/**
602+
* @param array|string $table
603+
* @param array|string $fields
604+
* @return QueryBuilder
605+
*/
606+
public function selectDistinct($table, $fields = '*'): QueryBuilder
607+
{
608+
if (!empty($table) && !empty($fields)) {
609+
$this->select($table, $fields, true);
610+
} else {
611+
$this->setError('Empty $table or $fields in ' . __METHOD__);
612+
return $this;
613+
}
614+
615+
return $this;
616+
}
617+
601618
/**
602619
* @param array|string $where
603620
* @param string $addition

tests/SqlSelectTest.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void
2121
}
2222
}
2323

24-
public function testEmptyTable()
24+
public function testSelectEmptyTable()
2525
{
2626
$result = $this->qb->select('', 'param');
2727

@@ -30,7 +30,7 @@ public function testEmptyTable()
3030
$this->assertSame($result->getErrorMessage(), 'Empty $table or $fields in QueryBuilder::select');
3131
}
3232

33-
public function testEmptyFields()
33+
public function testSelectEmptyFields()
3434
{
3535
$result = $this->qb->select('users', []);
3636

@@ -39,7 +39,7 @@ public function testEmptyFields()
3939
$this->assertSame($result->getErrorMessage(), 'Empty $table or $fields in QueryBuilder::select');
4040
}
4141

42-
public function testEmptyTableAndFields()
42+
public function testSelectEmptyTableAndFields()
4343
{
4444
$result = $this->qb->select('', []);
4545

@@ -68,7 +68,7 @@ public function testGetSqlNoValues()
6868
$this->assertSame([1], $result->getParams());
6969
}
7070

71-
public function testSelectAll()
71+
public function testSelect()
7272
{
7373
$result = $this->qb->select('users');
7474

@@ -77,6 +77,15 @@ public function testSelectAll()
7777
$this->assertSame([], $result->getParams());
7878
}
7979

80+
public function testSelectAliasAll()
81+
{
82+
$result = $this->qb->select(['u' => 'users'], 'u.*');
83+
84+
$this->assertSame(false, $result->hasError());
85+
$this->assertSame("SELECT u.* FROM `users` AS `u`", $result->getSql());
86+
$this->assertSame([], $result->getParams());
87+
}
88+
8089
public function testSelectWhereEq()
8190
{
8291
$result = $this->qb->select('users')->where([['id', '=', 10]]);
@@ -257,6 +266,33 @@ public function testSelectCounter()
257266
$this->assertSame([], $result->getParams());
258267
}
259268

269+
public function testSelectDistinctMethodEmptyTable()
270+
{
271+
$result = $this->qb->selectDistinct('', 'param');
272+
273+
$this->assertSame($this->qb, $result);
274+
$this->assertSame(true, $result->hasError());
275+
$this->assertSame($result->getErrorMessage(), 'Empty $table or $fields in QueryBuilder::selectDistinct');
276+
}
277+
278+
public function testSelectDistinctMethodEmptyFields()
279+
{
280+
$result = $this->qb->selectDistinct('users', []);
281+
282+
$this->assertSame($this->qb, $result);
283+
$this->assertSame(true, $result->hasError());
284+
$this->assertSame($result->getErrorMessage(), 'Empty $table or $fields in QueryBuilder::selectDistinct');
285+
}
286+
287+
public function testSelectDistinctMethodEmptyTableAndFields()
288+
{
289+
$result = $this->qb->selectDistinct('', []);
290+
291+
$this->assertSame($this->qb, $result);
292+
$this->assertSame(true, $result->hasError());
293+
$this->assertSame($this->qb->getErrorMessage(), 'Empty $table or $fields in QueryBuilder::selectDistinct');
294+
}
295+
260296
public function testSelectDistinctOrderBy()
261297
{
262298
$result = $this->qb->select('customers', 'city', true)->orderBy('city');
@@ -266,6 +302,15 @@ public function testSelectDistinctOrderBy()
266302
$this->assertSame([], $result->getParams());
267303
}
268304

305+
public function testSelectDistinctMethodOrderBy()
306+
{
307+
$result = $this->qb->selectDistinct('customers', 'city')->orderBy('city');
308+
309+
$this->assertSame(false, $result->hasError());
310+
$this->assertSame("SELECT DISTINCT `city` FROM `customers` ORDER BY `city` ASC", $result->getSql());
311+
$this->assertSame([], $result->getParams());
312+
}
313+
269314
public function testSelectDistinctOrderBy2Col()
270315
{
271316
$result = $this->qb->select('customers', ['city', 'country'], true)->orderBy('country desc');
@@ -275,6 +320,15 @@ public function testSelectDistinctOrderBy2Col()
275320
$this->assertSame([], $result->getParams());
276321
}
277322

323+
public function testSelectDistinctMethodOrderBy2Col()
324+
{
325+
$result = $this->qb->selectDistinct('customers', ['city', 'country'])->orderBy('country desc');
326+
327+
$this->assertSame(false, $result->hasError());
328+
$this->assertSame("SELECT DISTINCT `city`, `country` FROM `customers` ORDER BY `country` DESC", $result->getSql());
329+
$this->assertSame([], $result->getParams());
330+
}
331+
278332
public function testSelectOrderByTwoParams()
279333
{
280334
$result = $this->qb->select(['b' => 'branches'], ['b.id', 'b.name'])
@@ -347,7 +401,7 @@ public function testSelectGroupByHavingCount()
347401
$this->assertSame(['Nevada', 20], $result->getParams());
348402
}
349403

350-
public function testSelectSumm()
404+
public function testSelectSum()
351405
{
352406
$result = $this->qb->select("1+5 as 'res'");
353407

0 commit comments

Comments
 (0)