|
4 | 4 |
|
5 | 5 | namespace Doctrine\DBAL\Tests\Functional\Query;
|
6 | 6 |
|
| 7 | +use Doctrine\DBAL\ArrayParameterType; |
7 | 8 | use Doctrine\DBAL\DriverManager;
|
8 | 9 | use Doctrine\DBAL\Exception;
|
9 | 10 | use Doctrine\DBAL\ParameterType;
|
|
14 | 15 | use Doctrine\DBAL\Platforms\MySQLPlatform;
|
15 | 16 | use Doctrine\DBAL\Platforms\SQLitePlatform;
|
16 | 17 | use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode;
|
| 18 | +use Doctrine\DBAL\Query\QueryException; |
17 | 19 | use Doctrine\DBAL\Query\UnionType;
|
18 | 20 | use Doctrine\DBAL\Schema\Table;
|
19 | 21 | use Doctrine\DBAL\Tests\FunctionalTestCase;
|
@@ -332,6 +334,49 @@ public function testUnionAndAddUnionWorksWithQueryBuilderPartsAndReturnsExpected
|
332 | 334 | self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
|
333 | 335 | }
|
334 | 336 |
|
| 337 | + public function testWithNamedParameterCTE(): void |
| 338 | + { |
| 339 | + if (! $this->platformSupportsCTEs()) { |
| 340 | + self::markTestSkipped('The database platform does not support CTE.'); |
| 341 | + } |
| 342 | + |
| 343 | + $expectedRows = $this->prepareExpectedRows([['id' => 1]]); |
| 344 | + $qb = $this->connection->createQueryBuilder(); |
| 345 | + |
| 346 | + $cteQueryBuilder1 = $this->connection->createQueryBuilder(); |
| 347 | + $cteQueryBuilder1->select('id') |
| 348 | + ->from('for_update') |
| 349 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter(1, ParameterType::INTEGER))); |
| 350 | + |
| 351 | + $qb->with('filtered_for_update', $cteQueryBuilder1) |
| 352 | + ->select('id') |
| 353 | + ->from('filtered_for_update'); |
| 354 | + |
| 355 | + self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative()); |
| 356 | + } |
| 357 | + |
| 358 | + public function testWithPositionalParameterCTE(): void |
| 359 | + { |
| 360 | + if (! $this->platformSupportsCTEs()) { |
| 361 | + self::markTestSkipped('The database platform does not support CTE.'); |
| 362 | + } |
| 363 | + |
| 364 | + $expectedRows = $this->prepareExpectedRows([['id' => 1]]); |
| 365 | + $qb = $this->connection->createQueryBuilder(); |
| 366 | + |
| 367 | + $cteQueryBuilder1 = $this->connection->createQueryBuilder(); |
| 368 | + $cteQueryBuilder1->select('id') |
| 369 | + ->from('for_update') |
| 370 | + ->where($qb->expr()->in('id', $qb->createPositionalParameter([1, 2], ArrayParameterType::INTEGER))); |
| 371 | + |
| 372 | + $qb->with('filtered_for_update', $cteQueryBuilder1) |
| 373 | + ->select('id') |
| 374 | + ->from('filtered_for_update') |
| 375 | + ->where($qb->expr()->eq('id', $qb->createPositionalParameter(1, ParameterType::INTEGER))); |
| 376 | + |
| 377 | + self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative()); |
| 378 | + } |
| 379 | + |
335 | 380 | /**
|
336 | 381 | * @param array<array<string, int>> $rows
|
337 | 382 | *
|
@@ -380,4 +425,15 @@ private function platformSupportsSkipLocked(): bool
|
380 | 425 |
|
381 | 426 | return ! $platform instanceof SQLitePlatform;
|
382 | 427 | }
|
| 428 | + |
| 429 | + private function platformSupportsCTEs(): bool |
| 430 | + { |
| 431 | + $platform = $this->connection->getDatabasePlatform(); |
| 432 | + |
| 433 | + if (! $platform instanceof MySQLPlatform) { |
| 434 | + return true; |
| 435 | + } |
| 436 | + |
| 437 | + return $this->connection->getServerVersion() >= '8.0.11'; |
| 438 | + } |
383 | 439 | }
|
0 commit comments