Skip to content

Commit c8b7086

Browse files
committed
Add fromQuery() logic to upsertBatch()
1 parent 1b665ab commit c8b7086

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
@@ -1912,14 +1912,30 @@ public function upsert($set = null, ?bool $escape = null)
19121912
/**
19131913
* Compiles batch upsert strings and runs the queries
19141914
*
1915-
* @param array|object|null $set a dataset
1915+
* @param array|BaseBuilder|RawSql|object|null $set a dataset
19161916
*
19171917
* @return false|int|string[] Number of affected rows or FALSE on failure, SQL array when testMode
19181918
*
19191919
* @throws DatabaseException
19201920
*/
19211921
public function upsertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
19221922
{
1923+
$this->fromQuery($set);
1924+
1925+
if (isset($this->QBOptions['fromQuery'])) {
1926+
$sql = $this->_upsertBatch($this->QBFrom[0], $this->QBKeys, []);
1927+
1928+
if ($sql === '') {
1929+
return false; // @codeCoverageIgnore
1930+
}
1931+
1932+
$this->db->query($sql, null, false);
1933+
1934+
$this->resetWrite();
1935+
1936+
return $this->testMode ? $sql : $this->db->affectedRows();
1937+
}
1938+
19231939
if ($set !== null) {
19241940
$this->setData($set, $escape);
19251941
}

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)