Skip to content

Commit 03fb06c

Browse files
committed
Add SQLite _deleteBatch()
1 parent 2329b14 commit 03fb06c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

system/Database/SQLite3/Builder.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,72 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
211211

212212
return str_replace('{:_table_:}', $data, $sql);
213213
}
214+
215+
/**
216+
* Generates a platform-specific batch update string from the supplied data
217+
*/
218+
protected function _deleteBatch(string $table, array $keys, array $values): string
219+
{
220+
$sql = $this->QBOptions['sql'] ?? '';
221+
222+
// if this is the first iteration of batch then we need to build skeleton sql
223+
if ($sql === '') {
224+
$constraints = $this->QBOptions['constraints'] ?? [];
225+
226+
if ($constraints === []) {
227+
if ($this->db->DBDebug) {
228+
throw new DatabaseException('You must specify a constraint to match on for batch deletes.'); // @codeCoverageIgnore
229+
}
230+
231+
return ''; // @codeCoverageIgnore
232+
}
233+
234+
$alias = $this->QBOptions['alias'] ?? '_u';
235+
236+
$sql = 'DELETE FROM ' . $table . "\n";
237+
238+
if (current($constraints) instanceof RawSql) {
239+
if ($this->db->DBDebug) {
240+
throw new DatabaseException('You cannot use RawSql for constraint in SQLite.'); // @codeCoverageIgnore
241+
}
242+
}
243+
244+
if (is_string(current(array_keys($constraints)))) {
245+
$concat1 = implode(' || ', array_keys($constraints));
246+
$concat2 = implode(' || ', array_values($constraints));
247+
} else {
248+
$concat1 = implode(' || ', $constraints);
249+
$concat2 = $concat1;
250+
}
251+
252+
$sql .= "WHERE {$concat1} IN (SELECT {$concat2} FROM (\n{:_table_:}))";
253+
254+
// where is not supported
255+
if ($this->QBWhere !== []) {
256+
if ($this->db->DBDebug) {
257+
throw new DatabaseException('You cannot use WHERE with SQLite.'); // @codeCoverageIgnore
258+
}
259+
}
260+
261+
$this->QBOptions['sql'] = $sql;
262+
}
263+
264+
if (isset($this->QBOptions['fromQuery'])) {
265+
$data = $this->QBOptions['fromQuery'];
266+
} else {
267+
$data = implode(
268+
" UNION ALL\n",
269+
array_map(
270+
static fn ($value) => 'SELECT ' . implode(', ', array_map(
271+
static fn ($key, $index) => $index . ' ' . $key,
272+
$keys,
273+
$value
274+
)),
275+
$values
276+
)
277+
) . "\n";
278+
}
279+
280+
return str_replace('{:_table_:}', $data, $sql);
281+
}
214282
}

0 commit comments

Comments
 (0)