Skip to content

Commit 0d90429

Browse files
committed
Add use of setData() with upsert() and allow for getCompiledUpsert() on batch
Allowing the use of setData() along with set() on upsert(). This allows getCompiledUpsert() to be called with batches set with setData()
1 parent a520a6a commit 0d90429

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

system/Database/BaseBuilder.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,35 +1885,28 @@ public function getCompiledUpsert()
18851885
*/
18861886
public function upsert($set = null, ?bool $escape = null)
18871887
{
1888+
// if set() function was used then we need to convert to setData()
18881889
if ($set === null && ! empty($this->binds)) {
1889-
$set = [array_map(static fn ($columnName) => $columnName[0], $this->binds)];
1890-
1890+
$set = [array_map(static fn ($columnName) => $columnName[0], $this->binds)];
18911891
$this->binds = [];
1892-
1893-
$this->resetRun([
1894-
'QBSet' => [],
1895-
'QBKeys' => [],
1896-
]);
18971892
} elseif ($set === null && ! empty($this->QBSet)) {
1898-
$set = [$this->QBSet];
1899-
1900-
$this->resetRun([
1901-
'QBSet' => [],
1902-
'QBKeys' => [],
1903-
]);
1904-
} else {
1905-
$set = [$set];
1893+
$set = $this->QBSet;
19061894
}
19071895

1896+
$this->resetRun([
1897+
'QBSet' => [],
1898+
'QBKeys' => [],
1899+
]);
1900+
19081901
$this->setData($set, $escape);
19091902

1910-
return $this->batchExecute('_upsertBatch', 1);
1903+
return $this->batchExecute('_upsertBatch');
19111904
}
19121905

19131906
/**
19141907
* Compiles batch upsert strings and runs the queries
19151908
*
1916-
* @param array|object|string|null $set a dataset or select query
1909+
* @param array|object|null $set a dataset
19171910
*
19181911
* @throws DatabaseException
19191912
*

tests/system/Database/Live/UpsertTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ public function testGetCompiledUpsert()
242242
->getCompiledUpsert());
243243
}
244244

245+
public function testGetCompiledUpsertBatch()
246+
{
247+
$userData = [
248+
[
249+
'email' => '[email protected]',
250+
'name' => 'Ahmadinejad',
251+
'country' => 'Iran',
252+
],
253+
[
254+
'email' => '[email protected]',
255+
'name' => 'Pedro',
256+
'country' => 'El Salvador',
257+
],
258+
];
259+
260+
$sql = $this->db->table('user')
261+
->setData($userData)
262+
->getCompiledUpsert();
263+
264+
$pos = strpos($sql, '[email protected]');
265+
$this->assertTrue(($pos !== false));
266+
267+
$pos = strpos($sql, '[email protected]');
268+
$this->assertTrue(($pos !== false));
269+
}
270+
245271
public function testUpsertCauseConstraintError()
246272
{
247273
$userData = [

0 commit comments

Comments
 (0)