forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteStatement.php
More file actions
40 lines (32 loc) · 1.2 KB
/
DeleteStatement.php
File metadata and controls
40 lines (32 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Tempest\Database\QueryStatements;
use Tempest\Database\Builder\TableDefinition;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Exceptions\DeleteStatementWasInvalid;
use Tempest\Database\QueryStatement;
use Tempest\Support\Arr\ImmutableArray;
use function Tempest\Support\arr;
final class DeleteStatement implements QueryStatement, HasWhereStatements
{
public function __construct(
public readonly TableDefinition $table,
public ImmutableArray $where = new ImmutableArray(),
public bool $allowAll = false,
) {}
public function compile(DatabaseDialect $dialect): string
{
if ($this->allowAll === false && $this->where->isEmpty()) {
throw new DeleteStatementWasInvalid();
}
$query = arr([
sprintf('DELETE FROM %s', $dialect->quoteIdentifier($this->table->name)),
]);
if ($this->where->isNotEmpty()) {
$query[] = 'WHERE ' . $this->where
->map(fn (QueryStatement $where) => $where->compile($dialect))
->filter(fn (string $compiled) => $compiled !== '')
->implode(' ');
}
return $query->implode(' ');
}
}