Skip to content

Commit

Permalink
fix: $builder->ignore()->insertBatch() only ignores on first iteratio…
Browse files Browse the repository at this point in the history
…n of batch
  • Loading branch information
kenjis committed Feb 10, 2022
1 parent e5c0d9c commit 99800ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,9 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
}

if (! $hasQBSet) {
$ignore = $this->QBIgnore;
$this->resetWrite();
$this->QBIgnore = $ignore;
}
}

Expand Down
5 changes: 5 additions & 0 deletions system/Test/Mock/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@

class MockBuilder extends BaseBuilder
{
protected $supportedIgnoreStatements = [
'update' => 'IGNORE',
'insert' => 'IGNORE',
'delete' => 'IGNORE',
];
}
37 changes: 37 additions & 0 deletions tests/system/Database/Builder/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,43 @@ public function testInsertBatch()
$this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5671
*/
public function testInsertBatchIgnore()
{
$builder = $this->db->table('jobs');

$insertData = [
[
'id' => 2,
'name' => 'Commedian',
'description' => 'There\'s something in your teeth',
],
[
'id' => 3,
'name' => 'Cab Driver',
'description' => 'I am yellow',
],
];

$this->db->shouldReturn('execute', 1)->shouldReturn('affectedRows', 1);
$builder->ignore()->insertBatch($insertData, true, 1);

$query = $this->db->getLastQuery();
$this->assertInstanceOf(Query::class, $query);

$raw = <<<'SQL'
INSERT IGNORE INTO "jobs" ("description", "id", "name") VALUES ('I am yellow',3,'Cab Driver')
SQL;
$this->assertSame($raw, str_replace("\n", ' ', $query->getOriginalQuery()));

$expected = <<<'SQL'
INSERT IGNORE INTO "jobs" ("description", "id", "name") VALUES ('I am yellow',3,'Cab Driver')
SQL;
$this->assertSame($expected, str_replace("\n", ' ', $query->getQuery()));
}

public function testInsertBatchWithoutEscape()
{
$builder = $this->db->table('jobs');
Expand Down

0 comments on commit 99800ac

Please sign in to comment.