Skip to content

Commit d9ac9a7

Browse files
committed
refactor insertBatch() and setInsertBatch()
1 parent 45e0e87 commit d9ac9a7

File tree

5 files changed

+17
-97
lines changed

5 files changed

+17
-97
lines changed

system/Database/BaseBuilder.php

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,117 +1981,28 @@ protected function getValues(array $values): array
19811981
*/
19821982
public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100)
19831983
{
1984-
if ($set === null) {
1985-
if (empty($this->QBSet)) {
1986-
if ($this->db->DBDebug) {
1987-
throw new DatabaseException('You must use the "set" method to update an entry.');
1988-
}
1989-
1990-
return false; // @codeCoverageIgnore
1991-
}
1992-
} elseif (empty($set)) {
1993-
if ($this->db->DBDebug) {
1994-
throw new DatabaseException('insertBatch() called with no data');
1995-
}
1996-
1997-
return false; // @codeCoverageIgnore
1998-
}
1999-
2000-
$hasQBSet = $set === null;
2001-
2002-
$table = $this->QBFrom[0];
2003-
2004-
$affectedRows = 0;
2005-
$savedSQL = [];
2006-
2007-
if ($hasQBSet) {
2008-
$set = $this->QBSet;
2009-
}
2010-
2011-
for ($i = 0, $total = count($set); $i < $total; $i += $batchSize) {
2012-
if ($hasQBSet) {
2013-
$QBSet = array_slice($this->QBSet, $i, $batchSize);
2014-
} else {
2015-
$this->setInsertBatch(array_slice($set, $i, $batchSize), '', $escape);
2016-
$QBSet = $this->QBSet;
2017-
}
2018-
$sql = $this->_insertBatch($this->db->protectIdentifiers($table, true, null, false), $this->QBKeys, $QBSet);
2019-
2020-
if ($this->testMode) {
2021-
$savedSQL[] = $sql;
2022-
} else {
2023-
$this->db->query($sql, null, false);
2024-
$affectedRows += $this->db->affectedRows();
2025-
}
2026-
2027-
if (! $hasQBSet) {
2028-
$this->resetRun([
2029-
'QBSet' => [],
2030-
'QBKeys' => [],
2031-
]);
2032-
}
2033-
}
2034-
2035-
$this->resetWrite();
2036-
2037-
return $this->testMode ? $savedSQL : $affectedRows;
1984+
return $this->batchExecute('_insertBatch', $set, $escape, $batchSize);
20381985
}
20391986

20401987
/**
20411988
* Generates a platform-specific insert string from the supplied data.
20421989
*/
20431990
protected function _insertBatch(string $table, array $keys, array $values): string
20441991
{
2045-
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
1992+
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $table
1993+
. ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $this->getValues($values));
20461994
}
20471995

20481996
/**
2049-
* Allows key/value pairs to be set for batch inserts
1997+
* Alias for setBatch()
20501998
*
20511999
* @param mixed $key
20522000
*
20532001
* @return $this|null
20542002
*/
20552003
public function setInsertBatch($key, string $value = '', ?bool $escape = null)
20562004
{
2057-
$key = $this->batchObjectToArray($key);
2058-
2059-
if (! is_array($key)) {
2060-
$key = [$key => $value];
2061-
}
2062-
2063-
$escape = is_bool($escape) ? $escape : $this->db->protectIdentifiers;
2064-
2065-
$keys = array_keys($this->objectToArray(current($key)));
2066-
sort($keys);
2067-
2068-
foreach ($key as $row) {
2069-
$row = $this->objectToArray($row);
2070-
if (array_diff($keys, array_keys($row)) !== [] || array_diff(array_keys($row), $keys) !== []) {
2071-
// batch function above returns an error on an empty array
2072-
$this->QBSet[] = [];
2073-
2074-
return null;
2075-
}
2076-
2077-
ksort($row); // puts $row in the same order as our keys
2078-
2079-
$clean = [];
2080-
2081-
foreach ($row as $rowValue) {
2082-
$clean[] = $escape ? $this->db->escape($rowValue) : $rowValue;
2083-
}
2084-
2085-
$row = $clean;
2086-
2087-
$this->QBSet[] = '(' . implode(',', $row) . ')';
2088-
}
2089-
2090-
foreach ($keys as $k) {
2091-
$this->QBKeys[] = $this->db->protectIdentifiers($k, false);
2092-
}
2093-
2094-
return $this;
2005+
return $this->setBatch($key, $value, $escape);
20952006
}
20962007

20972008
/**

system/Database/OCI8/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Builder extends BaseBuilder
6767
*/
6868
protected function _insertBatch(string $table, array $keys, array $values): string
6969
{
70+
$values = $this->getValues($values);
71+
7072
$insertKeys = implode(', ', $keys);
7173
$hasPrimaryKey = in_array('PRIMARY', array_column($this->db->getIndexData($table), 'type'), true);
7274

system/Database/Postgre/Builder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ protected function _insert(string $table, array $keys, array $unescapedKeys): st
196196
*/
197197
protected function _insertBatch(string $table, array $keys, array $values): string
198198
{
199-
return trim(sprintf('INSERT INTO %s (%s) VALUES %s %s', $table, implode(', ', $keys), implode(', ', $values), $this->compileIgnore('insert')));
199+
return trim(sprintf(
200+
'INSERT INTO %s (%s) VALUES %s %s',
201+
$table,
202+
implode(', ', $keys),
203+
implode(', ', $this->getValues($values)),
204+
$this->compileIgnore('insert')
205+
));
200206
}
201207

202208
/**

system/Database/SQLSRV/Builder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ protected function _insert(string $table, array $keys, array $unescapedKeys): st
180180
*/
181181
protected function _insertBatch(string $table, array $keys, array $values): string
182182
{
183-
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $this->getFullName($table) . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $values);
183+
return 'INSERT ' . $this->compileIgnore('insert') . 'INTO ' . $this->getFullName($table)
184+
. ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $this->getValues($values));
184185
}
185186

186187
/**

tests/system/Database/Builder/InsertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public function testInsertBatchThrowsExceptionOnEmptyData()
258258
$builder = $this->db->table('jobs');
259259

260260
$this->expectException(DatabaseException::class);
261-
$this->expectExceptionMessage('insertBatch() called with no data');
261+
$this->expectExceptionMessage('insertBatch()/upsertBatch called with no data');
262262
$builder->insertBatch([]);
263263
}
264264
}

0 commit comments

Comments
 (0)