1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\Generic\Statement; |
6
|
|
|
|
7
|
|
|
use QB\Generic\Clause\Table; |
8
|
|
|
use QB\Generic\Expr\Expr; |
9
|
|
|
use QB\Generic\IQueryPart; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
class Delete implements IDelete |
13
|
|
|
{ |
14
|
|
|
/** @var array<int,Table|string> */ |
15
|
|
|
protected array $tables = []; |
16
|
|
|
|
17
|
|
|
/** @var string[] */ |
18
|
|
|
protected array $modifiers = []; |
19
|
|
|
|
20
|
|
|
/** @var IQueryPart[] */ |
21
|
|
|
protected array $whereParts = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Table|string ...$tables |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
12 |
|
public function from(Table|string ...$tables): static |
29
|
|
|
{ |
30
|
12 |
|
$this->tables = array_merge($this->tables, $tables); |
31
|
|
|
|
32
|
12 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param IQueryPart|string ...$whereParts |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
8 |
|
public function where(IQueryPart|string ...$whereParts): static |
41
|
|
|
{ |
42
|
8 |
|
foreach ($whereParts as $wherePart) { |
43
|
8 |
|
$wherePart = is_string($wherePart) ? new Expr($wherePart) : $wherePart; |
44
|
|
|
|
45
|
8 |
|
$this->whereParts[] = $wherePart; |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
10 |
|
public function __toString(): string |
55
|
|
|
{ |
56
|
10 |
|
if (!$this->isValid()) { |
57
|
2 |
|
throw new RuntimeException('Under-initialized DELETE query. Table and where are necessary'); |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
$delete = $this->delete(); |
61
|
|
|
|
62
|
8 |
|
$sqlParts = array_merge( |
63
|
8 |
|
[$delete], |
64
|
8 |
|
$this->getWhere(), |
65
|
|
|
); |
66
|
|
|
|
67
|
8 |
|
$sqlParts = array_filter($sqlParts); |
68
|
|
|
|
69
|
8 |
|
return implode(PHP_EOL, $sqlParts); |
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
public function isValid(): bool |
73
|
|
|
{ |
74
|
10 |
|
return count($this->tables) === 1 || count($this->whereParts) > 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
protected function delete(): string |
78
|
|
|
{ |
79
|
8 |
|
$sql = []; |
80
|
8 |
|
$sql[] = 'DELETE'; |
81
|
8 |
|
$sql[] = $this->getModifiers(); |
82
|
8 |
|
$sql[] = 'FROM'; |
83
|
8 |
|
$sql[] = $this->tables[0]; |
84
|
|
|
|
85
|
8 |
|
$sql = array_filter($sql); |
86
|
|
|
|
87
|
8 |
|
return implode(' ', $sql); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
protected function getModifiers(): string |
91
|
|
|
{ |
92
|
8 |
|
if (empty($this->modifiers)) { |
93
|
7 |
|
return ''; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return implode(' ', $this->modifiers); |
97
|
|
|
} |
98
|
|
|
|
99
|
8 |
|
protected function getWhere(): array |
100
|
|
|
{ |
101
|
8 |
|
if (count($this->whereParts) === 0) { |
102
|
2 |
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
$whereParts = []; |
106
|
6 |
|
foreach ($this->whereParts as $wherePart) { |
107
|
6 |
|
$whereParts[] = (string)$wherePart; |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
return ['WHERE ' . implode(' AND ', $whereParts)]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
2 |
|
public function getParams(): array |
117
|
|
|
{ |
118
|
2 |
|
$params = []; |
119
|
|
|
|
120
|
2 |
|
foreach ($this->whereParts as $wherePart) { |
121
|
2 |
|
$params = array_merge($params, $wherePart->getParams()); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return $params; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|