Skip to content

Commit e359570

Browse files
committed
Add throw Exception in *Batch operations
Throwing an exception when $set is empty and $QBSet is empty. This is more informative to the user of what the problem is.
1 parent 413d0c2 commit e359570

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

system/Database/BaseBuilder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,8 +1949,14 @@ protected function getValues(array $values): array
19491949
*/
19501950
public function insertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
19511951
{
1952-
if ($set !== null) {
1952+
if ($set !== null && $set !== []) {
19531953
$this->setData($set, $escape);
1954+
} elseif (empty($this->QBSet)) {
1955+
if ($this->db->DBDebug) {
1956+
throw new DatabaseException('insertBatch() has no data.');
1957+
}
1958+
1959+
return false; // @codeCoverageIgnore
19541960
}
19551961

19561962
return $this->batchExecute('_insertBatch', $batchSize);
@@ -2289,8 +2295,14 @@ public function updateBatch($set = null, $constraints = null, int $batchSize = 1
22892295
{
22902296
$this->onConstraint($constraints);
22912297

2292-
if ($set !== null) {
2298+
if ($set !== null && $set !== []) {
22932299
$this->setData($set, true);
2300+
} elseif (empty($this->QBSet)) {
2301+
if ($this->db->DBDebug) {
2302+
throw new DatabaseException('updateBatch() has no data.');
2303+
}
2304+
2305+
return false; // @codeCoverageIgnore
22942306
}
22952307

22962308
return $this->batchExecute('_updateBatch', $batchSize);

tests/system/Database/Builder/InsertTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function testInsertBatchThrowsExceptionOnNoData()
265265
$builder = $this->db->table('jobs');
266266

267267
$this->expectException(DatabaseException::class);
268-
$this->expectExceptionMessage('No data availble to process.');
268+
$this->expectExceptionMessage('insertBatch() has no data.');
269269
$builder->insertBatch();
270270
}
271271

@@ -274,7 +274,7 @@ public function testInsertBatchThrowsExceptionOnEmptyData()
274274
$builder = $this->db->table('jobs');
275275

276276
$this->expectException(DatabaseException::class);
277-
$this->expectExceptionMessage('setData() has no data.');
277+
$this->expectExceptionMessage('insertBatch() has no data.');
278278
$builder->insertBatch([]);
279279
}
280280
}

tests/system/Database/Builder/UpdateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public function testUpdateBatchThrowsExceptionWithNoData()
290290
$builder = new BaseBuilder('jobs', $this->db);
291291

292292
$this->expectException(DatabaseException::class);
293-
$this->expectExceptionMessage('No data availble to process.');
293+
$this->expectExceptionMessage('updateBatch() has no data.');
294294

295295
$builder->updateBatch(null, 'id');
296296
}
@@ -323,7 +323,7 @@ public function testUpdateBatchThrowsExceptionWithEmptySetArray()
323323
$builder = new BaseBuilder('jobs', $this->db);
324324

325325
$this->expectException(DatabaseException::class);
326-
$this->expectExceptionMessage('setData() has no data.');
326+
$this->expectExceptionMessage('updateBatch() has no data.');
327327

328328
$builder->updateBatch([], 'id');
329329
}

0 commit comments

Comments
 (0)