Skip to content

Commit 5cec8cc

Browse files
committed
- Add Select::distinct()
1 parent ff3c94c commit 5cec8cc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Builder/Select.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ class Select extends Statement {
3030
private $calcFoundRows = false;
3131
/** @var bool */
3232
private $forUpdate = false;
33+
/** @var bool */
34+
private $distinct = false;
35+
36+
/**
37+
* @param bool $distinct
38+
* @return $this
39+
*/
40+
public function distinct($distinct = true) {
41+
$this->distinct = $distinct;
42+
return $this;
43+
}
3344

3445
/**
3546
* @param string $expression
@@ -120,6 +131,9 @@ public function __toString() {
120131
if ($this->calcFoundRows) {
121132
$query .= " SQL_CALC_FOUND_ROWS";
122133
}
134+
if ($this->distinct) {
135+
$query .= " DISTINCT";
136+
}
123137
$query .= "\n";
124138
$query = $this->buildFields($query);
125139
if (count($this->getTables())) {

tests/Builder/SelectTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,15 @@ public function testOrderByValues() {
196196

197197
$this->assertEquals("SELECT\n\tt1.field\nFROM\n\ttest1 t1\nINNER JOIN\n\ttest2 t2 ON t1.id=t2.id\nORDER BY\n\tCASE `t1`.`field`\n\t\tWHEN '5' THEN '0'\n\t\tWHEN '1' THEN '1'\n\t\tWHEN '66' THEN '2'\n\t\tWHEN '183' THEN '3'\n\t\tWHEN '99' THEN '4'\n\t\tWHEN '2' THEN '5'\n\t\tWHEN '6' THEN '6'\n\tEND ASC\n", $query);
198198
}
199+
200+
public function testDistinct() {
201+
$query = TestSelect::create()
202+
->distinct()
203+
->field('t1.field1')
204+
->field('t1.field2')
205+
->from('t1', 'test1')
206+
->asString();
207+
208+
$this->assertEquals("SELECT DISTINCT\n\tt1.field1,\n\tt1.field2\nFROM\n\ttest1 t1\n", $query);
209+
}
199210
}

0 commit comments

Comments
 (0)