Repository navigation
Expand file tree
/
Copy pathUpdate.php
More file actions
291 lines (249 loc) · 7.79 KB
/
Copy pathUpdate.php
File metadata and controls
291 lines (249 loc) · 7.79 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
namespace PhpDb\Sql;
use Closure;
use Laminas\Stdlib\PriorityList;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Driver\PdoDriverInterface;
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Sql\Predicate\PredicateInterface;
use function array_key_exists;
use function implode;
use function is_numeric;
use function is_scalar;
use function is_string;
use function str_replace;
use function strtolower;
/**
* @property Where $where
*/
class Update extends AbstractPreparableSql
{
/**@#++
* @const
*/
public const SPECIFICATION_UPDATE = 'update';
final public const SPECIFICATION_SET = 'set';
final public const SPECIFICATION_WHERE = 'where';
final public const SPECIFICATION_JOIN = 'joins';
final public const VALUES_MERGE = 'merge';
final public const VALUES_SET = 'set';
/** @#-* */
/** @var array<string, string>|array<string, array> */
protected array $specifications = [
self::SPECIFICATION_UPDATE => 'UPDATE %1$s',
self::SPECIFICATION_JOIN => [
'%1$s' => [
[3 => '%1$s JOIN %2$s ON %3$s', 'combinedby' => ' '],
],
],
self::SPECIFICATION_SET => 'SET %1$s',
self::SPECIFICATION_WHERE => 'WHERE %1$s',
];
protected TableIdentifier|string|array $table = '';
protected bool $emptyWhereProtection = true;
protected ?PriorityList $set = null;
protected ?Where $where = null;
protected ?Join $joins = null;
/**
* Constructor
*/
public function __construct(string|TableIdentifier|null $table = null)
{
if ($table) {
$this->table($table);
}
}
public function getRawState(?string $key = null): mixed
{
$rawState = [
'emptyWhereProtection' => $this->emptyWhereProtection,
'table' => $this->table,
'set' => $this->getSet()->toArray(),
'where' => $this->getWhere(),
'joins' => $this->getJoins(),
];
return null !== $key && array_key_exists($key, $rawState) ? $rawState[$key] : $rawState;
}
/**
* Create join clause
*
* @throws Exception\InvalidArgumentException
*/
public function join(array|string|TableIdentifier $name, string $on, string $type = Join::JOIN_INNER): static
{
$this->getJoins()->join($name, $on, [], $type);
return $this;
}
/**
* Set key/value pairs to update
*
* @param array $values Associative array of key values
* @param string $flag One of the VALUES_* constants
* @throws Exception\InvalidArgumentException
*/
public function set(array $values, string|int $flag = self::VALUES_SET): static
{
$set = $this->getSet();
if (self::VALUES_SET === $flag) {
$set->clear();
}
$priority = is_numeric($flag) ? $flag : 0;
foreach ($values as $k => $v) {
if (! is_string($k)) {
throw new Exception\InvalidArgumentException('set() expects a string for the value key');
}
$set->insert($k, $v, $priority);
}
return $this;
}
/**
* Specify table for statement
*/
public function table(TableIdentifier|string|array $table): static
{
$this->table = $table;
return $this;
}
/**
* Create where clause
*
* @throws Exception\InvalidArgumentException
*/
public function where(
PredicateInterface|array|Closure|string|Where $predicate,
string $combination = Predicate\PredicateSet::OP_AND,
): static {
if ($predicate instanceof Where) {
$this->where = $predicate;
} else {
$this->getWhere()->addPredicates($predicate, $combination);
}
return $this;
}
/** @return string[][][]|null */
protected function processJoins(
PlatformInterface $platform,
?DriverInterface $driver = null,
?ParameterContainer $parameterContainer = null,
): ?array {
return $this->processJoin($this->joins, $platform, $driver, $parameterContainer);
}
protected function processSet(
PlatformInterface $platform,
?DriverInterface $driver = null,
?ParameterContainer $parameterContainer = null,
): string {
$setSql = [];
$i = 0;
$isPdoDriver = $driver instanceof PdoDriverInterface;
foreach ($this->getSet() as $column => $value) {
$prefix = $this->resolveColumnValue(
[
'column' => $column,
'fromTable' => '',
'isIdentifier' => true,
],
$platform,
$driver,
$parameterContainer,
'column',
);
$prefix .= ' = ';
if (is_scalar($value) && $parameterContainer) {
// use incremental value instead of column name for PDO
// @see https://github.com/zendframework/zend-db/issues/35
if ($isPdoDriver) {
$column = 'c_' . $i++;
}
$setSql[] = $prefix . $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $value);
} else {
$setSql[] = $prefix
. $this->resolveColumnValue(
$value,
$platform,
$driver,
$parameterContainer,
);
}
}
return str_replace(
'%1$s',
implode(', ', $setSql),
$this->specifications[static::SPECIFICATION_SET],
);
}
protected function processUpdate(
PlatformInterface $platform,
?DriverInterface $driver = null,
?ParameterContainer $parameterContainer = null,
): string {
return str_replace(
'%1$s',
$this->resolveTable($this->table, $platform, $driver, $parameterContainer),
$this->specifications[static::SPECIFICATION_UPDATE],
);
}
protected function processWhere(
PlatformInterface $platform,
?DriverInterface $driver = null,
?ParameterContainer $parameterContainer = null,
): ?string {
if (null === $this->where || $this->where->count() === 0) {
return null;
}
return str_replace(
'%1$s',
$this->processExpression($this->where, $platform, $driver, $parameterContainer, 'where'),
$this->specifications[static::SPECIFICATION_WHERE],
);
}
private function getJoins(): Join
{
return $this->joins ??= new Join();
}
private function getSet(): PriorityList
{
if (null === $this->set) {
$this->set = new PriorityList();
$this->set->isLIFO(false);
}
return $this->set;
}
private function getWhere(): Where
{
return $this->where ??= new Where();
}
/**
* __clone
*
* Resets the where object each time the Update is cloned.
*
* @return void
*/
public function __clone()
{
if (null !== $this->where) {
$this->where = clone $this->where;
}
if (null !== $this->joins) {
$this->joins = clone $this->joins;
}
if (null !== $this->set) {
$this->set = clone $this->set;
}
}
/**
* Variable overloading
* Proxies to "where" only
*/
public function __get(string $name): ?Where
{
if (strtolower($name) === 'where') {
return $this->getWhere();
}
return null;
}
}