Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nio-dtp committed Dec 11, 2024
1 parent 19b6ad3 commit aab2338
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/SQL/Builder/WithSQLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function buildSQL(CommonTableExpression $firstExpression, CommonTableExpr
foreach (array_merge([$firstExpression], $otherExpressions) as $part) {
$ctePart = [$part->name];
if ($part->columns !== null && count($part->columns) > 0) {
$ctePart[] = '(' . implode(', ', $part->columns) . ')';
$ctePart[] = ' (' . implode(', ', $part->columns) . ')';
}

$ctePart[] = ' AS (' . $part->query . ')';
Expand Down
111 changes: 102 additions & 9 deletions tests/Functional/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode;
use Doctrine\DBAL\Query\UnionType;
Expand Down Expand Up @@ -340,8 +341,8 @@ public function testSelectWithCTENamedParameter(): void
self::markTestSkipped('The database platform does not support CTE.');
}

if (! $this->platformSupportsCTEColumnDefinition()) {
self::markTestSkipped('The database platform does not support CTE column definition.');
if (! $this->platformSupportsCTEColumnsDefinition()) {
self::markTestSkipped('The database platform does not support columns definition for CTE.');
}

$expectedRows = $this->prepareExpectedRows([['virtual_id' => 1]]);
Expand All @@ -350,7 +351,7 @@ public function testSelectWithCTENamedParameter(): void
$cteQueryBuilder = $this->connection->createQueryBuilder();
$cteQueryBuilder->select('id AS virtual_id')
->from('for_update')
->where('virtual_id = :id')
->where('id = :id')
->setParameter('id', 1);

$qb->with('cte_a', $cteQueryBuilder, ['virtual_id'])
Expand All @@ -360,14 +361,14 @@ public function testSelectWithCTENamedParameter(): void
self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testSelectWithCTEPositionalParametersBindForEachQuery(): void
public function testSelectWithCTEAndParametersBindForEachQuery(): void
{
if (! $this->platformSupportsCTEs()) {
self::markTestSkipped('The database platform does not support CTE.');
}

if (! $this->platformSupportsCTEColumnDefinition()) {
self::markTestSkipped('The database platform does not support CTE column definition.');
if (! $this->platformSupportsCTEColumnsDefinition()) {
self::markTestSkipped('The database platform does not support columns definition for CTE.');
}

$expectedRows = $this->prepareExpectedRows([['virtual_id' => 1]]);
Expand All @@ -387,9 +388,95 @@ public function testSelectWithCTEPositionalParametersBindForEachQuery(): void

$qb->with('cte_a', $cteQueryBuilder1, ['virtual_id'])
->with('cte_b', $cteQueryBuilder2, ['virtual_id'])
->select('a.virtual_id')
->with('cte_c', 'SELECT 1 AS virtual_id')
->select('c.virtual_id')
->from('cte_a', 'a')
->join('a', 'cte_b', 'b', 'a.virtual_id = b.virtual_id')
->join('b', 'cte_c', 'c', 'b.virtual_id = c.virtual_id')
->where($qb->expr()->eq('a.virtual_id', '?'))
->setParameter(0, 1, ParameterType::INTEGER);

self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testSelectWithCTEAndCreateNamedParametersForEachQuery(): void
{
if (! $this->platformSupportsCTEs()) {
self::markTestSkipped('The database platform does not support CTE.');
}

if (! $this->platformSupportsCTEColumnsDefinition()) {
self::markTestSkipped('The database platform does not support columns definition for CTE.');
}

$expectedRows = $this->prepareExpectedRows([['virtual_id' => 1]]);
$qb = $this->connection->createQueryBuilder();

$cteQueryBuilder1 = $this->connection->createQueryBuilder();
$cteQueryBuilder1->select('id AS virtual_id')
->from('for_update')
->where($cteQueryBuilder1->expr()->eq(
'id',
$cteQueryBuilder1->createNamedParameter(1, ParameterType::INTEGER, ':id1'),
));

$cteQueryBuilder2 = $this->connection->createQueryBuilder();
$cteQueryBuilder2->select('id AS virtual_id')
->from('for_update')
->where($cteQueryBuilder2->expr()->in(
'id',
$cteQueryBuilder2->createNamedParameter([1, 2], ArrayParameterType::INTEGER, ':id2'),
));

$qb->with('cte_a', $cteQueryBuilder1, ['virtual_id'])
->with('cte_b', $cteQueryBuilder2, ['virtual_id'])
->with('cte_c', 'SELECT 1 AS virtual_id')
->select('c.virtual_id')
->from('cte_a', 'a')
->join('a', 'cte_b', 'b', 'a.virtual_id = b.virtual_id')
->join('b', 'cte_c', 'c', 'b.virtual_id = c.virtual_id')
->where($qb->expr()->eq('a.virtual_id', '?'))
->setParameter(0, 1, ParameterType::INTEGER);

self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testSelectWithCTEAndCreatePositionalParametersForEachQuery(): void
{
if (! $this->platformSupportsCTEs()) {
self::markTestSkipped('The database platform does not support CTE.');
}

if (! $this->platformSupportsCTEColumnsDefinition()) {
self::markTestSkipped('The database platform does not support columns definition for CTE.');
}

$expectedRows = $this->prepareExpectedRows([['virtual_id' => 1]]);
$qb = $this->connection->createQueryBuilder();

$cteQueryBuilder1 = $this->connection->createQueryBuilder();
$cteQueryBuilder1->select('id AS virtual_id')
->from('for_update')
->where($cteQueryBuilder1->expr()->eq(
'id',
$cteQueryBuilder1->createPositionalParameter(1, ParameterType::INTEGER),
));

$cteQueryBuilder2 = $this->connection->createQueryBuilder();
$cteQueryBuilder2->select('id AS virtual_id')
->from('for_update')
->where($cteQueryBuilder2->expr()->in(
'id',
$cteQueryBuilder2->createPositionalParameter([1, 2], ArrayParameterType::INTEGER),
));

$qb->with('cte_a', $cteQueryBuilder1, ['virtual_id'])
->with('cte_b', $cteQueryBuilder2, ['virtual_id'])
->with('cte_c', 'SELECT 1 AS virtual_id')
->select('c.virtual_id')
->from('cte_a', 'a')
->join('a', 'cte_b', 'b', 'a.virtual_id = b.virtual_id')
->join('b', 'cte_c', 'c', 'b.virtual_id = c.virtual_id')
->where($qb->expr()->eq('a.virtual_id', '?'))
->setParameter(0, 1, ParameterType::INTEGER);

Expand Down Expand Up @@ -504,8 +591,14 @@ private function platformSupportsCTEs(): bool
return ! $platform instanceof MySQLPlatform || $platform instanceof MySQL80Platform;
}

private function platformSupportsCTEColumnDefinition(): bool
private function platformSupportsCTEColumnsDefinition(): bool
{
return $this->connection->getDatabasePlatform() instanceof SQLitePlatform;
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof DB2Platform || $platform instanceof OraclePlatform) {
return false;
}

return ! $platform instanceof MySQLPlatform || $platform instanceof MySQL80Platform;
}
}
2 changes: 1 addition & 1 deletion tests/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public function testSelectWithCTE(): void

self::assertEquals(
'WITH cte_a AS (SELECT ta.id, ta.name, ta.table_b_id FROM table_a ta WHERE ta.name LIKE :name)'
. ', cte_b(virtual_id, virtual_name) AS '
. ', cte_b (virtual_id, virtual_name) AS '
. '(SELECT ca.id AS virtual_id, ca.name AS virtual_name '
. 'FROM cte_a ca INNER JOIN table_b tb ON ca.table_b_id = tb.id) '
. 'SELECT cb.* FROM cte_b cb',
Expand Down

0 comments on commit aab2338

Please sign in to comment.