@@ -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,120 @@ 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 ' => 1 ], ['id ' => 2 ], ['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
+
359
+ self ::assertSame ($ expectedRows , $ qb ->executeQuery ()->fetchAllAssociative ());
360
+ }
361
+
362
+ public function testUnionAndAddUnionWorksWithBindingPositionalParametersToQueryBuilderParts (): void
363
+ {
364
+ $ expectedRows = $ this ->prepareExpectedRows ([['id ' => 1 ], ['id ' => 2 ], ['id ' => 1 ]]);
365
+ $ qb = $ this ->connection ->createQueryBuilder ();
366
+
367
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
368
+ $ subQueryBuilder1 ->select ('id ' )
369
+ ->from ('for_update ' )
370
+ ->where ('id = ? ' )
371
+ ->setParameter (0 , 1 , ParameterType::INTEGER );
372
+
373
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
374
+ $ subQueryBuilder2 ->select ('id ' )
375
+ ->from ('for_update ' )
376
+ ->where ($ subQueryBuilder2 ->expr ()->eq (
377
+ 'id ' ,
378
+ $ subQueryBuilder2 ->createPositionalParameter (2 , ParameterType::INTEGER ),
379
+ ));
380
+
381
+ $ subQueryBuilder3 = $ this ->connection ->createQueryBuilder ();
382
+ $ subQueryBuilder3 ->select ('id ' )
383
+ ->from ('for_update ' )
384
+ ->where ('id = ? ' )
385
+ ->setParameter (0 , 1 , ParameterType::INTEGER );
386
+
387
+ $ qb ->union ($ subQueryBuilder1 )
388
+ ->addUnion ($ subQueryBuilder2 )
389
+ ->addUnion ($ subQueryBuilder3 , UnionType::ALL );
390
+
391
+ self ::assertSame ($ expectedRows , $ qb ->executeQuery ()->fetchAllAssociative ());
392
+ }
393
+
394
+ public function testUnionAndAddUnionThrowsExceptionWithDuplicatedParametersNames (): void
395
+ {
396
+ $ qb = $ this ->connection ->createQueryBuilder ();
397
+
398
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
399
+ $ subQueryBuilder1 ->select ('id ' )
400
+ ->from ('for_update ' )
401
+ ->where ('id = :id ' )
402
+ ->setParameter ('id ' , 1 , ParameterType::INTEGER );
403
+
404
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
405
+ $ subQueryBuilder2 ->select ('id ' )
406
+ ->from ('for_update ' )
407
+ ->where ('id = :id ' )
408
+ ->setParameter ('id ' , 2 , ParameterType::INTEGER );
409
+
410
+ $ qb ->union ($ subQueryBuilder1 )
411
+ ->addUnion ($ subQueryBuilder2 );
412
+
413
+ self ::expectExceptionMessage ('Found duplicated parameter in query. The duplicated parameter names are: "id". ' );
414
+ $ qb ->executeQuery ();
415
+ }
416
+
417
+ public function testUnionAndAddUnionThrowsExceptionWithDuplicatedCreatedParametersNames (): void
418
+ {
419
+ $ qb = $ this ->connection ->createQueryBuilder ();
420
+
421
+ $ subQueryBuilder1 = $ this ->connection ->createQueryBuilder ();
422
+ $ subQueryBuilder1 ->select ('id ' )
423
+ ->from ('for_update ' )
424
+ ->where ($ subQueryBuilder1 ->expr ()->eq (
425
+ 'id ' ,
426
+ $ subQueryBuilder1 ->createNamedParameter (1 , ParameterType::INTEGER ),
427
+ ));
428
+
429
+ $ subQueryBuilder2 = $ this ->connection ->createQueryBuilder ();
430
+ $ subQueryBuilder2 ->select ('id ' )
431
+ ->from ('for_update ' )
432
+ ->where ($ subQueryBuilder2 ->expr ()->eq (
433
+ 'id ' ,
434
+ $ subQueryBuilder2 ->createNamedParameter (2 , ParameterType::INTEGER ),
435
+ ));
436
+
437
+ $ qb ->union ($ subQueryBuilder1 )
438
+ ->addUnion ($ subQueryBuilder2 );
439
+
440
+ self ::expectExceptionMessage (
441
+ 'Found duplicated parameter in query. The duplicated parameter names are: "dcValue1". ' ,
442
+ );
443
+ $ qb ->executeQuery ();
444
+ }
445
+
335
446
/**
336
447
* @param array<array<string, int>> $rows
337
448
*
0 commit comments