Skip to content

Commit 209f304

Browse files
committed
Add OCI8 _deleteBatch()
1 parent 41a96ec commit 209f304

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

system/Database/OCI8/Builder.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,81 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
410410

411411
return str_replace('{:_table_:}', $data, $sql);
412412
}
413+
414+
/**
415+
* Generates a platform-specific batch update string from the supplied data
416+
*/
417+
protected function _deleteBatch(string $table, array $keys, array $values): string
418+
{
419+
$sql = $this->QBOptions['sql'] ?? '';
420+
421+
// if this is the first iteration of batch then we need to build skeleton sql
422+
if ($sql === '') {
423+
$constraints = $this->QBOptions['constraints'] ?? [];
424+
425+
if ($constraints === []) {
426+
if ($this->db->DBDebug) {
427+
throw new DatabaseException('You must specify a constraint to match on for batch deletes.'); // @codeCoverageIgnore
428+
}
429+
430+
return ''; // @codeCoverageIgnore
431+
}
432+
433+
$alias = $this->QBOptions['alias'] ?? '_u';
434+
435+
$sql = 'DELETE ' . $table . "\n";
436+
437+
$sql .= "WHERE EXISTS (SELECT * FROM (\n{:_table_:}";
438+
439+
$sql .= ') ' . $alias . "\n";
440+
441+
$sql .= 'WHERE ' . implode(
442+
' AND ',
443+
array_map(
444+
static fn ($key) => ($key instanceof RawSql ?
445+
$key :
446+
$table . '.' . $key . ' = ' . $alias . '.' . $key),
447+
$constraints
448+
)
449+
);
450+
451+
// convert binds in where
452+
foreach ($this->QBWhere as $key => $where) {
453+
foreach ($this->binds as $field => $bind) {
454+
$this->QBWhere[$key]['condition'] = str_replace(':' . $field . ':', $bind[0], $where['condition']);
455+
}
456+
}
457+
458+
// remove database prefix from alias in where
459+
$sql .= ' ' . str_replace(
460+
'WHERE ',
461+
'AND ',
462+
str_replace(
463+
$this->db->DBPrefix . trim($alias, $this->db->escapeChar),
464+
trim($alias, $this->db->escapeChar),
465+
$this->compileWhereHaving('QBWhere')
466+
)
467+
) . ')';
468+
469+
$this->QBOptions['sql'] = $sql;
470+
}
471+
472+
if (isset($this->QBOptions['fromQuery'])) {
473+
$data = $this->QBOptions['fromQuery'];
474+
} else {
475+
$data = implode(
476+
" FROM DUAL UNION ALL\n",
477+
array_map(
478+
static fn ($value) => 'SELECT ' . implode(', ', array_map(
479+
static fn ($key, $index) => $index . ' ' . $key,
480+
$keys,
481+
$value
482+
)),
483+
$values
484+
)
485+
) . " FROM DUAL\n";
486+
}
487+
488+
return str_replace('{:_table_:}', $data, $sql);
489+
}
413490
}

system/Database/Postgre/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
448448
// remove database prefix from alias in where
449449
$sql .= ' ' . str_replace(
450450
'WHERE ',
451-
' AND ',
451+
'AND ',
452452
str_replace(
453453
$this->db->DBPrefix . trim($alias, $this->db->escapeChar),
454454
trim($alias, $this->db->escapeChar),

0 commit comments

Comments
 (0)