From 8495d05ffbc9b896a201b004f901cfe7e468fd58 Mon Sep 17 00:00:00 2001 From: Bingo-Soft Date: Fri, 11 Oct 2019 09:36:07 +0300 Subject: [PATCH 1/4] Add support for DISTINCT statement --- lib/Doctrine/DBAL/Query/QueryBuilder.php | 26 ++++++++++++++++++- .../Tests/DBAL/Query/QueryBuilderTest.php | 11 ++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index d1e6d31dab5..e50f6a0170b 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -62,6 +62,7 @@ class QueryBuilder * @var array */ private $sqlParts = [ + 'distinct' => false, 'select' => [], 'from' => [], 'join' => [], @@ -465,6 +466,28 @@ public function select($select = null) : self return $this->add('select', $selects); } + /** + * Adds a DISTINCT flag to this query. + * + * + * $qb = $conn->createQueryBuilder() + * ->select('u.id') + * ->distinct() + * ->from('users', 'u'); + * + * + * @param bool $flag + * + * @return $this This QueryBuilder instance. + */ + public function distinct($flag = true) + { + $this->sqlParts['distinct'] = (bool) $flag; + + return $this; + } + + /** * Adds an item that is to be returned in the query result. * @@ -1088,7 +1111,8 @@ public function resetQueryPart(string $queryPartName) : self */ private function getSQLForSelect() : string { - $query = 'SELECT ' . implode(', ', $this->sqlParts['select']); + $query = 'SELECT ' . ($this->sqlParts['distinct'] === true ? 'DISTINCT ' : '') . + implode(', ', $this->sqlParts['select']); $query .= ($this->sqlParts['from'] ? ' FROM ' . implode(', ', $this->getFromClauses()) : '') . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '') diff --git a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php index 233376890e2..1ec459411ac 100644 --- a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php @@ -64,6 +64,17 @@ public function testSelectWithSimpleWhere() : void self::assertEquals('SELECT u.id FROM users u WHERE u.nickname = ?', (string) $qb); } + public function testSimpleSelectWithDistinct() : void + { + $qb = new QueryBuilder($this->conn); + + $qb->select('u.id') + ->from('users', 'u') + ->distinct(); + + self::assertEquals('SELECT DISTINCT u.id FROM users u', (string) $qb); + } + public function testSelectWithLeftJoin() : void { $qb = new QueryBuilder($this->conn); From eff22d676761cbc4200e8602099fc6d6c4b75fdc Mon Sep 17 00:00:00 2001 From: Bingo-Soft Date: Fri, 11 Oct 2019 10:11:08 +0300 Subject: [PATCH 2/4] Fixed code style errors in Query Builder --- lib/Doctrine/DBAL/Query/QueryBuilder.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index e50f6a0170b..124b63645aa 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -480,14 +480,13 @@ public function select($select = null) : self * * @return $this This QueryBuilder instance. */ - public function distinct($flag = true) + public function distinct(bool $flag = true) { $this->sqlParts['distinct'] = (bool) $flag; return $this; } - /** * Adds an item that is to be returned in the query result. * From cd03fe28d2ae58e6b29cef5c9038d472bf44fbe6 Mon Sep 17 00:00:00 2001 From: Bingo-Soft Date: Fri, 11 Oct 2019 10:21:53 +0300 Subject: [PATCH 3/4] Fixed distinct method docstring --- lib/Doctrine/DBAL/Query/QueryBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index 124b63645aa..888668966fe 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -476,7 +476,7 @@ public function select($select = null) : self * ->from('users', 'u'); * * - * @param bool $flag + * @param bool $flag If true, then DISTINCT is added to the query * * @return $this This QueryBuilder instance. */ From 4956402a89d45e8294b18435c9801589ba1bc691 Mon Sep 17 00:00:00 2001 From: Bingo-Soft Date: Sat, 12 Oct 2019 19:01:46 +0300 Subject: [PATCH 4/4] Remove unnecessary type casting in QueryBuilder distinct method --- lib/Doctrine/DBAL/Query/QueryBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index 888668966fe..eda2a843fa6 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -482,7 +482,7 @@ public function select($select = null) : self */ public function distinct(bool $flag = true) { - $this->sqlParts['distinct'] = (bool) $flag; + $this->sqlParts['distinct'] = $flag; return $this; }