Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: $builder->ignore()->insertBatch() only ignores on first iteration #5672

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,10 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
}

if (! $hasQBSet) {
$this->resetWrite();
$this->resetRun([
'QBSet' => [],
'QBKeys' => [],
]);
}
}

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