Skip to content

Commit 046967a

Browse files
authored
fix: gather affected rows after query call failed (#9363)
* fix: gather affected rows after query call failed * update user guide * fix test * fix test * fix failing tests for mysqli - enable strict mode * cs fix
1 parent 3c851f1 commit 046967a

File tree

7 files changed

+78
-3
lines changed

7 files changed

+78
-3
lines changed

system/Database/BaseBuilder.php

+1-1
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

+4
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

+4
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

+30-1
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,41 @@ 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+
$db = $this->db;
106+
107+
if ($this->db->DBDriver === 'MySQLi') {
108+
// strict mode is required for MySQLi to throw an exception here
109+
$config = config('Database');
110+
$config->tests['strictOn'] = true;
111+
112+
$db = Database::connect($config->tests);
113+
}
114+
115+
$db->table('job')->insertBatch($data);
116+
}
117+
89118
public function testReplaceWithNoMatchingData(): void
90119
{
91120
$data = [

tests/system/Database/Live/TransactionTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,40 @@ 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+
$db = $this->db;
258+
259+
if ($this->db->DBDriver === 'MySQLi') {
260+
// strict mode is required for MySQLi to throw an exception here
261+
$config = config('Database');
262+
$config->tests['strictOn'] = true;
263+
264+
$db = Database::connect($config->tests);
265+
}
266+
267+
$db->transStrict(false)->transBegin();
268+
$db->table('job')->insertBatch($data);
269+
270+
$this->assertFalse($db->transStatus());
271+
272+
$db->transComplete();
273+
274+
$db->transStrict();
275+
276+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
277+
}
242278
}

user_guide_src/source/changelogs/v4.5.8.rst

+2
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.

user_guide_src/source/database/query_builder.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,7 @@ Class Reference
18701870
:param array $set: Data to insert
18711871
:param bool $escape: Whether to escape values
18721872
:param int $batch_size: Count of rows to insert at once
1873-
:returns: Number of rows inserted or ``false`` on failure
1873+
:returns: Number of rows inserted or ``false`` on no data to perform an insert operation
18741874
:rtype: int|false
18751875

18761876
Compiles and executes batch ``INSERT`` statements.

0 commit comments

Comments
 (0)