|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AtlasPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/atlasphp/Atlas.Query |
|
14
|
|
|
* @license https://github.com/atlasphp/Atlas.Qyert/blob/1.x/LICENSE.md |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\DataMapper\Query; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\DataMapper\Pdo\Connection; |
|
22
|
|
|
|
|
23
|
|
|
use function array_merge; |
|
24
|
|
|
use function is_int; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Update |
|
28
|
|
|
*/ |
|
29
|
|
|
class Update extends AbstractConditions |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Update constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Connection $connection |
|
35
|
|
|
* @param Bind $bind |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Connection $connection, Bind $bind) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct($connection, $bind); |
|
40
|
|
|
|
|
41
|
|
|
$this->store["FROM"] = ""; |
|
42
|
|
|
$this->store["RETURNING"] = []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets a column for the `UPDATE` query |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $column |
|
49
|
|
|
* @param mixed|null $value |
|
50
|
|
|
* @param int $type |
|
51
|
|
|
* |
|
52
|
|
|
* @return Update |
|
53
|
|
|
*/ |
|
54
|
|
|
public function column( |
|
55
|
|
|
string $column, |
|
56
|
|
|
$value = null, |
|
57
|
|
|
int $type = -1 |
|
58
|
|
|
): Update { |
|
59
|
|
|
$this->store["COLUMNS"][$column] = ":" . $column; |
|
60
|
|
|
|
|
61
|
|
|
if (null !== $value) { |
|
62
|
|
|
$this->bind->setValue($column, $value, $type); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Mass sets columns and values for the `UPDATE` |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $columns |
|
72
|
|
|
* |
|
73
|
|
|
* @return Update |
|
74
|
|
|
*/ |
|
75
|
|
|
public function columns(array $columns): Update |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($columns as $column => $value) { |
|
78
|
|
|
if (is_int($column)) { |
|
79
|
|
|
$this->column($value); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->column($column, $value); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Adds table(s) in the query |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $table |
|
92
|
|
|
* |
|
93
|
|
|
* @return Update |
|
94
|
|
|
*/ |
|
95
|
|
|
public function from(string $table): Update |
|
96
|
|
|
{ |
|
97
|
|
|
$this->store["FROM"] = $table; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getStatement(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return "UPDATE" |
|
108
|
|
|
. $this->buildFlags() |
|
109
|
|
|
. " " . $this->store["FROM"] |
|
110
|
|
|
. $this->buildColumns() |
|
111
|
|
|
. $this->buildCondition("WHERE") |
|
112
|
|
|
. $this->buildReturning(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Whether the query has columns or not |
|
117
|
|
|
* |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
public function hasColumns(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return count($this->store["COLUMNS"]) > 0; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Adds the `RETURNING` clause |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $columns |
|
129
|
|
|
* |
|
130
|
|
|
* @return Update |
|
131
|
|
|
*/ |
|
132
|
|
|
public function returning(array $columns): Update |
|
133
|
|
|
{ |
|
134
|
|
|
$this->store["RETURNING"] = array_merge( |
|
135
|
|
|
$this->store["RETURNING"], |
|
136
|
|
|
$columns |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Resets the internal store |
|
144
|
|
|
*/ |
|
145
|
|
|
public function reset(): void |
|
146
|
|
|
{ |
|
147
|
|
|
parent::reset(); |
|
148
|
|
|
|
|
149
|
|
|
$this->store["FROM"] = ""; |
|
150
|
|
|
$this->store["RETURNING"] = []; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets a column = value condition |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $column |
|
157
|
|
|
* @param mixed|null $value |
|
158
|
|
|
* |
|
159
|
|
|
* @return Update |
|
160
|
|
|
*/ |
|
161
|
|
|
public function set(string $column, $value = null): Update |
|
162
|
|
|
{ |
|
163
|
|
|
if (null === $value) { |
|
164
|
|
|
$value = "NULL"; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->store["COLUMNS"][$column] = $value; |
|
168
|
|
|
|
|
169
|
|
|
$this->bind->remove($column); |
|
170
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Builds the column list |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
private function buildColumns(): string |
|
180
|
|
|
{ |
|
181
|
|
|
$assignments = []; |
|
182
|
|
|
|
|
183
|
|
|
foreach ($this->store["COLUMNS"] as $column => $value) { |
|
184
|
|
|
$assignments[] = $this->quoteIdentifier($column) . " = " . $value; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return " SET" . $this->indent($assignments, ","); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|