From b8771dc9e8dfb403d10f3b0fc7bcad48ffc0ff5b Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 14 Jun 2024 13:54:24 +0200 Subject: [PATCH] Default to distinct union queries --- docs/en/reference/query-builder.rst | 8 ++++---- src/Query/QueryBuilder.php | 2 +- tests/Functional/Query/QueryBuilderTest.php | 15 ++++++++++++++- tests/Query/QueryBuilderTest.php | 9 +++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/en/reference/query-builder.rst b/docs/en/reference/query-builder.rst index 30b6ec642d7..c34a97fa00a 100644 --- a/docs/en/reference/query-builder.rst +++ b/docs/en/reference/query-builder.rst @@ -322,7 +322,7 @@ To combine multiple ``SELECT`` queries into one result-set you can pass SQL Part or QueryBuilder instances to one of the following methods: * ``union(string|QueryBuilder $part)`` -* ``addUnion(string|QueryBuilder $part, UnionType $type)`` +* ``addUnion(string|QueryBuilder $part, UnionType $type = UnionType::DISTINCT)`` .. code-block:: php @@ -330,9 +330,9 @@ or QueryBuilder instances to one of the following methods: $queryBuilder ->union('SELECT 1 AS field') - ->addUnion('SELECT 2 AS field', UnionType::DISTINCT) - ->addUnion('SELECT 3 AS field', UnionType::DISTINCT) - ->addUnion('SELECT 3 as field', UnionType::DISTINCT); + ->addUnion('SELECT 2 AS field') + ->addUnion('SELECT 3 AS field') + ->addUnion('SELECT 3 as field'); $queryBuilder ->union('SELECT 1 AS field') diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 6141c6cc46b..0d684c9289b 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -542,7 +542,7 @@ public function union(string|QueryBuilder $part): self * * @return $this */ - public function addUnion(string|QueryBuilder $part, UnionType $type): self + public function addUnion(string|QueryBuilder $part, UnionType $type = UnionType::DISTINCT): self { $this->type = QueryType::UNION; diff --git a/tests/Functional/Query/QueryBuilderTest.php b/tests/Functional/Query/QueryBuilderTest.php index c1c51f0b13d..b54a9376804 100644 --- a/tests/Functional/Query/QueryBuilderTest.php +++ b/tests/Functional/Query/QueryBuilderTest.php @@ -124,7 +124,7 @@ public function testUnionAllReturnsExpectedResult(): void self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative()); } - public function testUnionReturnsExpectedResult(): void + public function testUnionDistinctReturnsExpectedResult(): void { $expectedRows = $this->prepareExpectedRows([['field_one' => 1], ['field_one' => 2]]); $platform = $this->connection->getDatabasePlatform(); @@ -137,6 +137,19 @@ public function testUnionReturnsExpectedResult(): void self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative()); } + public function testUnionIsDistinctByDefault(): void + { + $expectedRows = $this->prepareExpectedRows([['field_one' => 1], ['field_one' => 2]]); + $platform = $this->connection->getDatabasePlatform(); + $qb = $this->connection->createQueryBuilder(); + $qb->union($platform->getDummySelectSQL('2 as field_one')) + ->addUnion($platform->getDummySelectSQL('1 as field_one')) + ->addUnion($platform->getDummySelectSQL('1 as field_one')) + ->orderBy('field_one', 'ASC'); + + self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative()); + } + public function testUnionWithDescOrderByReturnsExpectedResult(): void { $expectedRows = $this->prepareExpectedRows([['field_one' => 2], ['field_one' => 1]]); diff --git a/tests/Query/QueryBuilderTest.php b/tests/Query/QueryBuilderTest.php index 096eb8d1e13..5cfa4cb6b01 100644 --- a/tests/Query/QueryBuilderTest.php +++ b/tests/Query/QueryBuilderTest.php @@ -1472,6 +1472,15 @@ public function testUnionAndAddUnionReturnsUnionQuery(): void self::assertSame('SELECT 1 AS field_one UNION SELECT 2 as field_one', $qb->getSQL()); } + public function testUnionIsDistinctByDefault(): void + { + $qb = new QueryBuilder($this->conn); + $qb->union('SELECT 1 AS field_one') + ->addUnion('SELECT 2 as field_one'); + + self::assertSame('SELECT 1 AS field_one UNION SELECT 2 as field_one', $qb->getSQL()); + } + public function testUnionAndOrderByReturnsUnionQueryWithOrderBy(): void { $qb = new QueryBuilder($this->conn);