|
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
|
18 |
|
$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
|
|
|
return 'UPDATE' |
|
56
|
16 |
|
. $this->buildFlags() |
|
57
|
16 |
|
. $this->buildTable() |
|
58
|
16 |
|
. $this->buildValuesForUpdate() |
|
59
|
16 |
|
. $this->buildWhere() |
|
60
|
16 |
|
. $this->buildOrderBy() |
|
61
|
16 |
|
. $this->buildLimit() |
|
62
|
16 |
|
. $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
|
15 |
|
public function where($cond) |
|
90
|
|
|
{ |
|
91
|
15 |
|
$this->addWhere('AND', func_get_args()); |
|
92
|
15 |
|
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
|
14 |
|
$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
|
6 |
|
public function col($col) |
|
126
|
|
|
{ |
|
127
|
6 |
|
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
|
15 |
|
return $this->addCols($cols); |
|
146
|
|
|
} |
|
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
|
14 |
|
public function set($col, $value) |
|
161
|
|
|
{ |
|
162
|
14 |
|
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
|
16 |
|
$values[] = "{$col} = {$value}"; |
|
177
|
|
|
} |
|
178
|
16 |
|
return PHP_EOL . 'SET' . $this->indentCsv($values); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|