Skip to content

Commit 1621f09

Browse files
committed
Add fromQuery() logic to upsertBatch()
1 parent 2525456 commit 1621f09

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

system/Database/BaseBuilder.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,14 +1914,30 @@ public function upsert($set = null, ?bool $escape = null)
19141914
/**
19151915
* Compiles batch upsert strings and runs the queries
19161916
*
1917-
* @param array|object|null $set a dataset
1917+
* @param array|BaseBuilder|RawSql|object|null $set a dataset
19181918
*
19191919
* @return false|int|string[] Number of affected rows or FALSE on failure, SQL array when testMode
19201920
*
19211921
* @throws DatabaseException
19221922
*/
19231923
public function upsertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
19241924
{
1925+
$this->fromQuery($set);
1926+
1927+
if (isset($this->QBOptions['fromQuery'])) {
1928+
$sql = $this->_upsertBatch($this->QBFrom[0], $this->QBKeys, []);
1929+
1930+
if ($sql === '') {
1931+
return false; // @codeCoverageIgnore
1932+
}
1933+
1934+
$this->db->query($sql, null, false);
1935+
1936+
$this->resetWrite();
1937+
1938+
return $this->testMode ? $sql : $this->db->affectedRows();
1939+
}
1940+
19251941
if ($set !== null) {
19261942
$this->setData($set, $escape);
19271943
}

tests/system/Database/Live/InsertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testInsertPasswordHash()
118118
$this->seeInDatabase('misc', ['value' => $hash]);
119119
}
120120

121-
public function testInsertWithQuery()
121+
public function testInsertBatchWithQuery()
122122
{
123123
$this->forge = Database::forge($this->DBGroup);
124124

0 commit comments

Comments
 (0)