File tree Expand file tree Collapse file tree 6 files changed +55
-2
lines changed
tests/system/Database/Live
user_guide_src/source/changelogs Expand file tree Collapse file tree 6 files changed +55
-2
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1313
1414namespace CodeIgniter \Database \Live ;
1515
16+ use CodeIgniter \Database \Exceptions \DatabaseException ;
1617use CodeIgniter \Database \Forge ;
1718use CodeIgniter \Database \RawSql ;
1819use 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 = [
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ Deprecations
3030Bugs 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+
3335See the repo's
3436`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md >`_
3537for a complete list of bugs fixed.
You can’t perform that action at this time.
0 commit comments