File tree 7 files changed +78
-3
lines changed
tests/system/Database/Live
7 files changed +78
-3
lines changed Original file line number Diff line number Diff line change @@ -2198,7 +2198,7 @@ protected function formatValues(array $values): array
2198
2198
*
2199
2199
* @param array|object|null $set a dataset
2200
2200
*
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
2202
2202
*/
2203
2203
public function insertBatch ($ set = null , ?bool $ escape = null , int $ batchSize = 100 )
2204
2204
{
Original file line number Diff line number Diff line change @@ -227,6 +227,10 @@ protected function getDriverFunctionPrefix(): string
227
227
*/
228
228
public function affectedRows (): int
229
229
{
230
+ if ($ this ->resultID === false ) {
231
+ return 0 ;
232
+ }
233
+
230
234
return pg_affected_rows ($ this ->resultID );
231
235
}
232
236
Original file line number Diff line number Diff line change @@ -444,6 +444,10 @@ public function error(): array
444
444
*/
445
445
public function affectedRows (): int
446
446
{
447
+ if ($ this ->resultID === false ) {
448
+ return 0 ;
449
+ }
450
+
447
451
return sqlsrv_rows_affected ($ this ->resultID );
448
452
}
449
453
Original file line number Diff line number Diff line change 13
13
14
14
namespace CodeIgniter \Database \Live ;
15
15
16
+ use CodeIgniter \Database \Exceptions \DatabaseException ;
16
17
use CodeIgniter \Database \Forge ;
17
18
use CodeIgniter \Database \RawSql ;
18
19
use CodeIgniter \Test \CIUnitTestCase ;
@@ -79,13 +80,41 @@ public function testInsertBatch(): void
79
80
],
80
81
];
81
82
82
- $ this ->db ->table ($ table )->insertBatch ($ data );
83
+ $ count = $ this ->db ->table ($ table )->insertBatch ($ data );
84
+
85
+ $ this ->assertSame (2 , $ count );
83
86
84
87
$ expected = $ data ;
85
88
$ this ->seeInDatabase ($ table , $ expected [0 ]);
86
89
$ this ->seeInDatabase ($ table , $ expected [1 ]);
87
90
}
88
91
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
+
89
118
public function testReplaceWithNoMatchingData (): void
90
119
{
91
120
$ data = [
Original file line number Diff line number Diff line change @@ -239,4 +239,40 @@ public function testTransStrictFalseAndDBDebugFalse(): void
239
239
240
240
$ this ->enableDBDebug ();
241
241
}
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
+ }
242
278
}
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ Deprecations
30
30
Bugs Fixed
31
31
**********
32
32
33
+ - **Database: ** Fixed a bug where ``Builder::affectedRows() `` threw an error when the previous query call failed in ``Postgre `` and ``SQLSRV `` drivers.
34
+
33
35
See the repo's
34
36
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md >`_
35
37
for a complete list of bugs fixed.
Original file line number Diff line number Diff line change @@ -1870,7 +1870,7 @@ Class Reference
1870
1870
:param array $set: Data to insert
1871
1871
:param bool $escape: Whether to escape values
1872
1872
: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
1874
1874
:rtype: int|false
1875
1875
1876
1876
Compiles and executes batch ``INSERT `` statements.
You can’t perform that action at this time.
0 commit comments