@@ -212,9 +212,6 @@ public function testUnionWithLimitAndOffsetClauseReturnsExpectedResult(): void
212
212
{
213
213
$ expectedRows = $ this ->prepareExpectedRows ([['field_one ' => 2 ]]);
214
214
$ platform = $ this ->connection ->getDatabasePlatform ();
215
- $ plainSelect1 = $ platform ->getDummySelectSQL ('1 as field_one ' );
216
- $ plainSelect2 = $ platform ->getDummySelectSQL ('2 as field_one ' );
217
- $ plainSelect3 = $ platform ->getDummySelectSQL ('1 as field_one ' );
218
215
$ qb = $ this ->connection ->createQueryBuilder ();
219
216
$ qb ->union ($ platform ->getDummySelectSQL ('1 as field_one ' ))
220
217
->addUnion ($ platform ->getDummySelectSQL ('2 as field_one ' ), UnionType::DISTINCT )
@@ -332,6 +329,122 @@ public function testUnionAndAddUnionWorksWithQueryBuilderPartsAndReturnsExpected
332
329
self ::assertSame ($ expectedRows , $ qb ->executeQuery ()->fetchAllAssociative ());
333
330
}
334
331
332
+ public function testUnionAndAddUnionWorksWithBindingNamedParametersToQueryBuilderParts (): void
333
+ {
334
+ $ expectedRows = $ this ->prepareExpectedRows ([['id ' => 2 ], ['id ' => 1 ], ['id ' => 1 ]]);
335
+ $ qb = $ this ->connection ->createQueryBuilder ();
336
+
337
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
338
+ $ subQueryBuilder1 ->select ('id ' )
339
+ ->from ('for_update ' )
340
+ ->where ('id = :id1 ' )
341
+ ->setParameter ('id1 ' , 1 , ParameterType::INTEGER );
342
+
343
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
344
+ $ subQueryBuilder2 ->select ('id ' )
345
+ ->from ('for_update ' )
346
+ ->where ('id = :id2 ' )
347
+ ->setParameter ('id2 ' , 2 , ParameterType::INTEGER );
348
+
349
+ $ subQueryBuilder3 = $ this ->connection ->createQueryBuilder ();
350
+ $ subQueryBuilder3 ->select ('id ' )
351
+ ->from ('for_update ' )
352
+ ->where ('id = :id3 ' )
353
+ ->setParameter ('id3 ' , 1 , ParameterType::INTEGER );
354
+
355
+ $ qb ->union ($ subQueryBuilder1 )
356
+ ->addUnion ($ subQueryBuilder2 )
357
+ ->addUnion ($ subQueryBuilder3 , UnionType::ALL )
358
+ ->orderBy ('id ' , 'DESC ' );
359
+
360
+ self ::assertSame ($ expectedRows , $ qb ->executeQuery ()->fetchAllAssociative ());
361
+ }
362
+
363
+ public function testUnionAndAddUnionWorksWithBindingPositionalParametersToQueryBuilderParts (): void
364
+ {
365
+ $ expectedRows = $ this ->prepareExpectedRows ([['id ' => 1 ], ['id ' => 1 ], ['id ' => 2 ]]);
366
+ $ qb = $ this ->connection ->createQueryBuilder ();
367
+
368
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
369
+ $ subQueryBuilder1 ->select ('id ' )
370
+ ->from ('for_update ' )
371
+ ->where ('id = ? ' )
372
+ ->setParameter (0 , 1 , ParameterType::INTEGER );
373
+
374
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
375
+ $ subQueryBuilder2 ->select ('id ' )
376
+ ->from ('for_update ' )
377
+ ->where ($ subQueryBuilder2 ->expr ()->eq (
378
+ 'id ' ,
379
+ $ subQueryBuilder2 ->createPositionalParameter (2 , ParameterType::INTEGER ),
380
+ ));
381
+
382
+ $ subQueryBuilder3 = $ this ->connection ->createQueryBuilder ();
383
+ $ subQueryBuilder3 ->select ('id ' )
384
+ ->from ('for_update ' )
385
+ ->where ('id = ? ' )
386
+ ->setParameter (0 , 1 , ParameterType::INTEGER );
387
+
388
+ $ qb ->union ($ subQueryBuilder1 )
389
+ ->addUnion ($ subQueryBuilder2 )
390
+ ->addUnion ($ subQueryBuilder3 , UnionType::ALL )
391
+ ->orderBy ('id ' , 'ASC ' );
392
+
393
+ self ::assertSame ($ expectedRows , $ qb ->executeQuery ()->fetchAllAssociative ());
394
+ }
395
+
396
+ public function testUnionAndAddUnionThrowsExceptionWithDuplicatedParametersNames (): void
397
+ {
398
+ $ qb = $ this ->connection ->createQueryBuilder ();
399
+
400
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
401
+ $ subQueryBuilder1 ->select ('id ' )
402
+ ->from ('for_update ' )
403
+ ->where ('id = :id ' )
404
+ ->setParameter ('id ' , 1 , ParameterType::INTEGER );
405
+
406
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
407
+ $ subQueryBuilder2 ->select ('id ' )
408
+ ->from ('for_update ' )
409
+ ->where ('id = :id ' )
410
+ ->setParameter ('id ' , 2 , ParameterType::INTEGER );
411
+
412
+ $ qb ->union ($ subQueryBuilder1 )
413
+ ->addUnion ($ subQueryBuilder2 );
414
+
415
+ self ::expectExceptionMessage ('Found duplicated parameter in query. The duplicated parameter names are: "id". ' );
416
+ $ qb ->executeQuery ();
417
+ }
418
+
419
+ public function testUnionAndAddUnionThrowsExceptionWithDuplicatedCreatedParametersNames (): void
420
+ {
421
+ $ qb = $ this ->connection ->createQueryBuilder ();
422
+
423
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
424
+ $ subQueryBuilder1 ->select ('id ' )
425
+ ->from ('for_update ' )
426
+ ->where ($ subQueryBuilder1 ->expr ()->eq (
427
+ 'id ' ,
428
+ $ subQueryBuilder1 ->createNamedParameter (1 , ParameterType::INTEGER ),
429
+ ));
430
+
431
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
432
+ $ subQueryBuilder2 ->select ('id ' )
433
+ ->from ('for_update ' )
434
+ ->where ($ subQueryBuilder2 ->expr ()->eq (
435
+ 'id ' ,
436
+ $ subQueryBuilder2 ->createNamedParameter (2 , ParameterType::INTEGER ),
437
+ ));
438
+
439
+ $ qb ->union ($ subQueryBuilder1 )
440
+ ->addUnion ($ subQueryBuilder2 );
441
+
442
+ self ::expectExceptionMessage (
443
+ 'Found duplicated parameter in query. The duplicated parameter names are: "dcValue1". ' ,
444
+ );
445
+ $ qb ->executeQuery ();
446
+ }
447
+
335
448
/**
336
449
* @param array<array<string, int>> $rows
337
450
*
0 commit comments