1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\SqlQuery\Common; |
10
|
|
|
|
11
|
|
|
use Aura\SqlQuery\AbstractDmlQuery; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* An object for UPDATE queries. |
16
|
|
|
* |
17
|
|
|
* @package Aura.SqlQuery |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class Update extends AbstractDmlQuery implements UpdateInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* The table to update. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
protected $table; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* Sets the table to update. |
34
|
|
|
* |
35
|
|
|
* @param string $table The table to update. |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
18 |
|
public function table($table) |
41
|
|
|
{ |
42
|
|
|
$this->table = $this->quoter->quoteName($table); |
|
|
|
|
43
|
18 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* Builds this query object into a string. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
16 |
|
protected function build() |
54
|
|
|
{ |
55
|
16 |
|
return 'UPDATE' |
56
|
|
|
. $this->buildFlags() |
57
|
|
|
. $this->buildTable() |
58
|
|
|
. $this->buildValuesForUpdate() |
59
|
|
|
. $this->buildWhere() |
60
|
|
|
. $this->buildOrderBy() |
61
|
|
|
. $this->buildLimit() |
62
|
|
|
. $this->buildReturning(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* Builds the table clause. |
68
|
|
|
* |
69
|
|
|
* @return null |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
16 |
|
protected function buildTable() |
73
|
|
|
{ |
74
|
16 |
|
return " {$this->table}"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* Adds a WHERE condition to the query by AND. If the condition has |
80
|
|
|
* ?-placeholders, additional arguments to the method will be bound to |
81
|
|
|
* those placeholders sequentially. |
82
|
|
|
* |
83
|
|
|
* @param string $cond The WHERE condition. |
84
|
|
|
* @param mixed ...$bind arguments to bind to placeholders |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
1 |
|
public function where($cond) |
90
|
|
|
{ |
91
|
|
|
$this->addWhere('AND', func_get_args()); |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* Adds a WHERE condition to the query by OR. If the condition has |
98
|
|
|
* ?-placeholders, additional arguments to the method will be bound to |
99
|
|
|
* those placeholders sequentially. |
100
|
|
|
* |
101
|
|
|
* @param string $cond The WHERE condition. |
102
|
|
|
* @param mixed ...$bind arguments to bind to placeholders |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
* |
106
|
|
|
* @see where() |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
14 |
|
public function orWhere($cond) |
110
|
|
|
{ |
111
|
|
|
$this->addWhere('OR', func_get_args()); |
112
|
14 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* Sets one column value placeholder; if an optional second parameter is |
118
|
|
|
* passed, that value is bound to the placeholder. |
119
|
|
|
* |
120
|
|
|
* @param string $col The column name. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
|
public function col($col) |
126
|
|
|
{ |
127
|
|
|
return call_user_func_array(array($this, 'addCol'), func_get_args()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* Sets multiple column value placeholders. If an element is a key-value |
133
|
|
|
* pair, the key is treated as the column name and the value is bound to |
134
|
|
|
* that column. |
135
|
|
|
* |
136
|
|
|
* @param array $cols A list of column names, optionally as key-value |
137
|
|
|
* pairs where the key is a column name and the value is a bind value for |
138
|
|
|
* that column. |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
* |
142
|
|
|
*/ |
143
|
15 |
|
public function cols(array $cols) |
144
|
|
|
{ |
145
|
|
|
return $this->addCols($cols); |
146
|
15 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
* Sets a column value directly; the value will not be escaped, although |
151
|
|
|
* fully-qualified identifiers in the value will be quoted. |
152
|
|
|
* |
153
|
|
|
* @param string $col The column name. |
154
|
|
|
* |
155
|
|
|
* @param string $value The column value expression. |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
* |
159
|
|
|
*/ |
160
|
|
|
public function set($col, $value) |
161
|
|
|
{ |
162
|
|
|
return $this->setCol($col, $value); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* |
167
|
|
|
* Builds the updated columns and values of the statement. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
16 |
|
protected function buildValuesForUpdate() |
173
|
|
|
{ |
174
|
16 |
|
$values = array(); |
175
|
16 |
|
foreach ($this->col_values as $col => $value) { |
176
|
2 |
|
$values[] = "{$col} = {$value}"; |
177
|
|
|
} |
178
|
|
|
return PHP_EOL . 'SET' . $this->indentCsv($values); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.