12
12
namespace CodeIgniter \Database \Live ;
13
13
14
14
use CodeIgniter \Database \Exceptions \DatabaseException ;
15
+ use CodeIgniter \Database \Forge ;
15
16
use CodeIgniter \Database \RawSql ;
16
17
use CodeIgniter \Test \CIUnitTestCase ;
17
18
use CodeIgniter \Test \DatabaseTestTrait ;
@@ -27,6 +28,11 @@ final class UpdateTest extends CIUnitTestCase
27
28
{
28
29
use DatabaseTestTrait;
29
30
31
+ /**
32
+ * @var Forge|mixed
33
+ */
34
+ public $ forge ;
35
+
30
36
protected $ refresh = true ;
31
37
protected $ seed = CITestSeeder::class;
32
38
@@ -496,4 +502,89 @@ public function testNoConstraintFound()
496
502
$ this ->db ->table ('job ' )
497
503
->updateBatch ($ jobData );
498
504
}
505
+
506
+ public function testUpdateBatchWithQuery ()
507
+ {
508
+ $ this ->forge = Database::forge ($ this ->DBGroup );
509
+
510
+ $ this ->forge ->dropTable ('user2 ' , true );
511
+
512
+ $ this ->forge ->addField ([
513
+ 'id ' => ['type ' => 'INTEGER ' , 'constraint ' => 3 , 'auto_increment ' => true ],
514
+ 'name ' => ['type ' => 'VARCHAR ' , 'constraint ' => 80 ],
515
+ 'email ' => ['type ' => 'VARCHAR ' , 'constraint ' => 100 ],
516
+ 'country ' => ['type ' => 'VARCHAR ' , 'constraint ' => 40 ],
517
+ 'created_at ' => ['type ' => 'DATETIME ' , 'null ' => true ],
518
+ 'updated_at ' => ['type ' => 'DATETIME ' , 'null ' => true ],
519
+ 'deleted_at ' => ['type ' => 'DATETIME ' , 'null ' => true ],
520
+ 'last_loggin ' => ['type ' => 'DATETIME ' , 'null ' => true ],
521
+ ])->addKey ('id ' , true )->addUniqueKey ('email ' )->addKey ('country ' )->createTable ('user2 ' , true );
522
+
523
+ $ data = [
524
+ [
525
+ 'name ' => 'Derek Jones user2 ' ,
526
+
527
+ 'country ' => 'France ' ,
528
+ ],
529
+ [
530
+ 'name ' => 'Ahmadinejad user2 ' ,
531
+
532
+ 'country ' => 'Greece ' ,
533
+ ],
534
+ [
535
+ 'name ' => 'Richard A Causey user2 ' ,
536
+
537
+ 'country ' => 'France ' ,
538
+ ],
539
+ [
540
+ 'name ' => 'Chris Martin user2 ' ,
541
+
542
+ 'country ' => 'Greece ' ,
543
+ ],
544
+ [
545
+ 'name ' => 'New User user2 ' ,
546
+
547
+ 'country ' => 'US ' ,
548
+ ],
549
+ [
550
+ 'name ' => 'New User2 user2 ' ,
551
+
552
+ 'country ' => 'US ' ,
553
+ ],
554
+ ];
555
+ $ this ->db ->table ('user2 ' )->insertBatch ($ data );
556
+
557
+ if ($ this ->db ->DBDriver === 'SQLite3 ' && ! (version_compare ($ this ->db ->getVersion (), '3.33.0 ' ) >= 0 )) {
558
+ $ this ->markTestSkipped ('Only SQLite 3.33 and newer can complete this test. ' );
559
+ }
560
+
561
+ $ updateFields = ['country ' , 'updated_at ' => new RawSql ('CURRENT_TIMESTAMP ' )];
562
+
563
+ $ subQuery = $ this ->db ->table ('user2 ' )
564
+ ->select ('email, country ' )
565
+ ->where ('country ' , 'France ' );
566
+
567
+ $ affectedRows = $ this ->db ->table ('user ' )
568
+ ->updateFields ($ updateFields , true )
569
+ ->updateBatch ($ subQuery , 'email ' );
570
+
571
+ $ this ->assertSame (2 , (int ) $ affectedRows );
572
+
573
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Derek Jones ' , 'country ' => 'France ' ]);
574
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Ahmadinejad ' , 'country ' => 'Iran ' ]);
575
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Richard A Causey ' , 'country ' => 'France ' ]);
576
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Chris Martin ' , 'country ' => 'UK ' ]);
577
+
578
+ $ result = $ this ->db ->table ('user ' )->get ()->getResultArray ();
579
+
580
+ foreach ($ result as $ row ) {
581
+ if (
$ row[
'email ' ] ===
'[email protected] ' ||
$ row[
'email ' ] ===
'[email protected] ' ) {
582
+ $ this ->assertNotNull ($ row ['updated_at ' ]);
583
+ } else {
584
+ $ this ->assertNull ($ row ['updated_at ' ]);
585
+ }
586
+ }
587
+
588
+ $ this ->forge ->dropTable ('user2 ' , true );
589
+ }
499
590
}
0 commit comments