Skip to content

Commit 161a4f9

Browse files
committed
allow selecting multiple columns with SELECT DISTINCT
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 4686deb commit 161a4f9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/private/DB/QueryBuilder/QueryBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,14 @@ public function selectAlias($select, $alias) {
436436
* @return $this This QueryBuilder instance.
437437
*/
438438
public function selectDistinct($select) {
439+
if (!is_array($select)) {
440+
$select = [$select];
441+
}
442+
443+
$quotedSelect = $this->helper->quoteColumnNames($select);
444+
439445
$this->queryBuilder->addSelect(
440-
'DISTINCT ' . $this->helper->quoteColumnName($select)
446+
'DISTINCT ' . implode(', ', $quotedSelect)
441447
);
442448

443449
return $this;

tests/lib/DB/QueryBuilder/QueryBuilderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,44 @@ public function testSelectDistinct() {
314314
$this->deleteTestingRows('testFirstResult2');
315315
}
316316

317+
public function testSelectDistinctMultiple() {
318+
$this->deleteTestingRows('testFirstResult1');
319+
$this->deleteTestingRows('testFirstResult2');
320+
$this->createTestingRows('testFirstResult1');
321+
$this->createTestingRows('testFirstResult2');
322+
323+
$this->queryBuilder->selectDistinct(['appid', 'configkey']);
324+
325+
$this->queryBuilder->from('*PREFIX*appconfig')
326+
->where($this->queryBuilder->expr()->eq(
327+
'appid',
328+
$this->queryBuilder->expr()->literal('testFirstResult1')
329+
))
330+
->orderBy('appid', 'DESC');
331+
332+
$query = $this->queryBuilder->execute();
333+
$rows = $query->fetchAll();
334+
$query->closeCursor();
335+
336+
$this->assertEquals(
337+
[
338+
['appid' => 'testFirstResult1', 'configkey' => 'testing1'],
339+
['appid' => 'testFirstResult1', 'configkey' => 'testing2'],
340+
['appid' => 'testFirstResult1', 'configkey' => 'testing3'],
341+
['appid' => 'testFirstResult1', 'configkey' => 'testing4'],
342+
['appid' => 'testFirstResult1', 'configkey' => 'testing5'],
343+
['appid' => 'testFirstResult1', 'configkey' => 'testing6'],
344+
['appid' => 'testFirstResult1', 'configkey' => 'testing7'],
345+
['appid' => 'testFirstResult1', 'configkey' => 'testing8'],
346+
['appid' => 'testFirstResult1', 'configkey' => 'testing9'],
347+
],
348+
$rows
349+
);
350+
351+
$this->deleteTestingRows('testFirstResult1');
352+
$this->deleteTestingRows('testFirstResult2');
353+
}
354+
317355
public function dataAddSelect() {
318356
$config = $this->createMock(SystemConfig::class);
319357
$logger = $this->createMock(ILogger::class);

0 commit comments

Comments
 (0)