Skip to content

Commit 41a96ec

Browse files
committed
Add SQLite _deleteBatch()
1 parent 76ab59c commit 41a96ec

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
@@ -204,4 +204,72 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
204204

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

0 commit comments

Comments
 (0)