From 1c55d64d85abe53b0eb802c5e81077db9baaff37 Mon Sep 17 00:00:00 2001 From: bbldn05 Date: Sun, 1 Mar 2026 01:16:11 +0300 Subject: [PATCH 1/2] feat: Added $distinct parameter to SelectQuery::count --- src/Query/SelectQuery.php | 6 ++++-- .../Database/Functional/Driver/Common/Schema/TableTest.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index 818dd6de..92ce213a 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -326,12 +326,14 @@ public function runChunks(int $limit, callable $callback): void * * @psalm-param non-empty-string $column Column to count by (every column by default). */ - public function count(string $column = '*'): int + public function count(string $column = '*', bool $distinct = false): int { $select = clone $this; //To be escaped in compiler - $select->columns = ["COUNT({$column})"]; + $select->columns = [ + $distinct === true ? "COUNT(DISTINCT({$column}))" : "COUNT({$column})", + ]; $select->orderBy = []; $select->groupBy = []; diff --git a/tests/Database/Functional/Driver/Common/Schema/TableTest.php b/tests/Database/Functional/Driver/Common/Schema/TableTest.php index 575dbf68..693bea0d 100644 --- a/tests/Database/Functional/Driver/Common/Schema/TableTest.php +++ b/tests/Database/Functional/Driver/Common/Schema/TableTest.php @@ -495,7 +495,7 @@ public function testCountDistinct(): void ], ); - $this->assertSame(4, $table->select()->count('DISTINCT(id)')); + $this->assertSame(4, $table->select()->count('id', true)); } public function setUp(): void From 724df7f96389200d264841569ceab1382191fac2 Mon Sep 17 00:00:00 2001 From: bbldn05 Date: Mon, 2 Mar 2026 22:11:53 +0300 Subject: [PATCH 2/2] Added an extra test to cover all syntaxes --- tests/Database/Functional/Driver/Common/Schema/TableTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Database/Functional/Driver/Common/Schema/TableTest.php b/tests/Database/Functional/Driver/Common/Schema/TableTest.php index 693bea0d..b0edebac 100644 --- a/tests/Database/Functional/Driver/Common/Schema/TableTest.php +++ b/tests/Database/Functional/Driver/Common/Schema/TableTest.php @@ -496,6 +496,7 @@ public function testCountDistinct(): void ); $this->assertSame(4, $table->select()->count('id', true)); + $this->assertSame(4, $table->select()->count('DISTINCT(id)')); } public function setUp(): void