|
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
|
|
|
|
|
11
|
|
|
class Update implements IUpdate |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array<int,string|Table> */ |
|
14
|
|
|
protected array $tables = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string[] */ |
|
17
|
|
|
protected array $modifiers = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array<string,mixed> */ |
|
20
|
|
|
protected array $rawValues = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var IQueryPart[] */ |
|
23
|
|
|
protected array $whereParts = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string|Table ...$tables |
|
27
|
|
|
* |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
13 |
|
public function addFrom(string|Table ...$tables): static |
|
31
|
|
|
{ |
|
32
|
13 |
|
$this->tables = array_merge($this->tables, $tables); |
|
33
|
|
|
|
|
34
|
13 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string ...$modifiers |
|
39
|
|
|
* |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function addModifier(string ...$modifiers): static |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->modifiers = array_merge($this->modifiers, $modifiers); |
|
45
|
|
|
|
|
46
|
1 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array<string,mixed> $values |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
11 |
|
public function setValues(array $values): static |
|
55
|
|
|
{ |
|
56
|
11 |
|
$this->rawValues = $values; |
|
57
|
|
|
|
|
58
|
11 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|IQueryPart ...$whereParts |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function addWhere(string|IQueryPart ...$whereParts): static |
|
67
|
|
|
{ |
|
68
|
11 |
|
foreach ($whereParts as $wherePart) { |
|
69
|
11 |
|
$this->whereParts[] = is_string($wherePart) ? new Expr($wherePart) : $wherePart; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
11 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function __toString(): string |
|
79
|
|
|
{ |
|
80
|
9 |
|
if (!$this->isValid()) { |
|
81
|
2 |
|
throw new \RuntimeException('Under-initialized UPDATE query. Table, values and where are necessary'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
$sqlParts = array_merge( |
|
85
|
7 |
|
[$this->getCommand()], |
|
86
|
7 |
|
$this->getSet(), |
|
87
|
7 |
|
$this->getWhere(), |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
7 |
|
$sqlParts = array_filter($sqlParts); |
|
91
|
|
|
|
|
92
|
7 |
|
return implode(PHP_EOL, $sqlParts); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
9 |
|
public function isValid(): bool |
|
96
|
|
|
{ |
|
97
|
9 |
|
return count($this->tables) === 1 && count($this->rawValues) > 0 && count($this->whereParts) > 0; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
7 |
|
protected function getCommand(): string |
|
104
|
|
|
{ |
|
105
|
7 |
|
$sql = []; |
|
106
|
7 |
|
$sql[] = 'UPDATE'; |
|
107
|
7 |
|
$sql[] = implode(' ', $this->modifiers); |
|
108
|
7 |
|
$sql[] = $this->tables[0]; |
|
109
|
|
|
|
|
110
|
7 |
|
$sql = array_filter($sql); |
|
111
|
|
|
|
|
112
|
7 |
|
return implode(' ', $sql); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string[] |
|
117
|
|
|
*/ |
|
118
|
7 |
|
protected function getSet(): array |
|
119
|
|
|
{ |
|
120
|
7 |
|
$values = []; |
|
121
|
7 |
|
foreach ($this->rawValues as $column => $value) { |
|
122
|
7 |
|
$values[] = sprintf('%s = %s', $column, $value); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
7 |
|
return ['SET ' . implode(', ', $values)]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string[] |
|
130
|
|
|
*/ |
|
131
|
7 |
|
protected function getWhere(): array |
|
132
|
|
|
{ |
|
133
|
7 |
|
$whereParts = []; |
|
134
|
7 |
|
foreach ($this->whereParts as $wherePart) { |
|
135
|
7 |
|
$whereParts[] = (string)$wherePart; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
7 |
|
return ['WHERE ' . implode(' AND ', $whereParts)]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function getParams(): array |
|
145
|
|
|
{ |
|
146
|
2 |
|
$params = []; |
|
147
|
|
|
|
|
148
|
2 |
|
foreach ($this->rawValues as $values) { |
|
149
|
2 |
|
if ($values instanceof IQueryPart) { |
|
150
|
2 |
|
$params = array_merge($params, $values->getParams()); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
2 |
|
foreach ($this->whereParts as $wherePart) { |
|
155
|
2 |
|
$params = array_merge($params, $wherePart->getParams()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
return $params; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function values(): array |
|
165
|
|
|
{ |
|
166
|
2 |
|
return array_values($this->rawValues); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|