Skip to content

Commit 5fb20ae

Browse files
committed
fix: gather affected rows after query call failed
1 parent 3c851f1 commit 5fb20ae

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ protected function formatValues(array $values): array
21982198
*
21992199
* @param array|object|null $set a dataset
22002200
*
2201-
* @return false|int|list<string> Number of rows inserted or FALSE on failure, SQL array when testMode
2201+
* @return false|int|list<string> Number of rows inserted or FALSE on no data to perform an insert operation, SQL array when testMode
22022202
*/
22032203
public function insertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
22042204
{

system/Database/Postgre/Connection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ protected function getDriverFunctionPrefix(): string
227227
*/
228228
public function affectedRows(): int
229229
{
230+
if ($this->resultID === false) {
231+
return 0;
232+
}
233+
230234
return pg_affected_rows($this->resultID);
231235
}
232236

system/Database/SQLSRV/Connection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ public function error(): array
444444
*/
445445
public function affectedRows(): int
446446
{
447+
if ($this->resultID === false) {
448+
return 0;
449+
}
450+
447451
return sqlsrv_rows_affected($this->resultID);
448452
}
449453

tests/system/Database/Live/InsertTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Database\Live;
1515

16+
use CodeIgniter\Database\Exceptions\DatabaseException;
1617
use CodeIgniter\Database\Forge;
1718
use CodeIgniter\Database\RawSql;
1819
use CodeIgniter\Test\CIUnitTestCase;
@@ -79,13 +80,31 @@ public function testInsertBatch(): void
7980
],
8081
];
8182

82-
$this->db->table($table)->insertBatch($data);
83+
$count = $this->db->table($table)->insertBatch($data);
84+
85+
$this->assertSame(2, $count);
8386

8487
$expected = $data;
8588
$this->seeInDatabase($table, $expected[0]);
8689
$this->seeInDatabase($table, $expected[1]);
8790
}
8891

92+
public function testInsertBatchFailed(): void
93+
{
94+
$this->expectException(DatabaseException::class);
95+
96+
$data = [
97+
[
98+
'name' => 'Grocery Sales',
99+
],
100+
[
101+
'name' => null,
102+
],
103+
];
104+
105+
$this->db->table('job')->insertBatch($data);
106+
}
107+
89108
public function testReplaceWithNoMatchingData(): void
90109
{
91110
$data = [

tests/system/Database/Live/TransactionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,28 @@ public function testTransStrictFalseAndDBDebugFalse(): void
239239

240240
$this->enableDBDebug();
241241
}
242+
243+
/**
244+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9362
245+
*/
246+
public function testTransInsertBatchFailed(): void
247+
{
248+
$data = [
249+
[
250+
'name' => 'Grocery Sales',
251+
],
252+
[
253+
'name' => null,
254+
],
255+
];
256+
257+
$this->db->transBegin();
258+
$this->db->table('job')->insertBatch($data);
259+
260+
$this->assertFalse($this->db->transStatus());
261+
262+
$this->db->transRollback();
263+
264+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
265+
}
242266
}

user_guide_src/source/changelogs/v4.5.8.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Database:** Fixed a bug where ``Builder::affectedRows()`` threw an error when the previous query call failed in ``Postgre`` and ``SQLSRV`` drivers.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)