]> source.dussan.org Git - nextcloud-server.git/commitdiff
allow selecting multiple columns with SELECT DISTINCT 24230/head
authorRobin Appelman <robin@icewind.nl>
Mon, 9 Nov 2020 16:43:29 +0000 (17:43 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Thu, 19 Nov 2020 21:41:13 +0000 (21:41 +0000)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/DB/QueryBuilder/QueryBuilder.php
tests/lib/DB/QueryBuilder/QueryBuilderTest.php

index 0b4f9831292c22f66d9c20a39decc5af0085a25a..53025f8a5b4f7bde89c80be864fca949df0bcc87 100644 (file)
@@ -435,8 +435,14 @@ class QueryBuilder implements IQueryBuilder {
         * @return $this This QueryBuilder instance.
         */
        public function selectDistinct($select) {
+               if (!is_array($select)) {
+                       $select = [$select];
+               }
+
+               $quotedSelect = $this->helper->quoteColumnNames($select);
+
                $this->queryBuilder->addSelect(
-                       'DISTINCT ' . $this->helper->quoteColumnName($select)
+                       'DISTINCT ' . implode(', ', $quotedSelect)
                );
 
                return $this;
index faf06360e17587cfb7cc21f054ee456617d013d4..fe87a201d573f7c622511ad7b51ad809109644c4 100644 (file)
@@ -314,6 +314,44 @@ class QueryBuilderTest extends \Test\TestCase {
                $this->deleteTestingRows('testFirstResult2');
        }
 
+       public function testSelectDistinctMultiple() {
+               $this->deleteTestingRows('testFirstResult1');
+               $this->deleteTestingRows('testFirstResult2');
+               $this->createTestingRows('testFirstResult1');
+               $this->createTestingRows('testFirstResult2');
+
+               $this->queryBuilder->selectDistinct(['appid', 'configkey']);
+
+               $this->queryBuilder->from('*PREFIX*appconfig')
+                       ->where($this->queryBuilder->expr()->eq(
+                               'appid',
+                               $this->queryBuilder->expr()->literal('testFirstResult1')
+                       ))
+                       ->orderBy('appid', 'DESC');
+
+               $query = $this->queryBuilder->execute();
+               $rows = $query->fetchAll();
+               $query->closeCursor();
+
+               $this->assertEquals(
+                       [
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing1'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing2'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing3'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing4'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing5'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing6'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing7'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing8'],
+                               ['appid' => 'testFirstResult1', 'configkey' => 'testing9'],
+                       ],
+                       $rows
+               );
+
+               $this->deleteTestingRows('testFirstResult1');
+               $this->deleteTestingRows('testFirstResult2');
+       }
+
        public function dataAddSelect() {
                $config = $this->createMock(SystemConfig::class);
                $logger = $this->createMock(ILogger::class);