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
|
|
|
use WhereTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* The table to update. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
protected $table; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* Sets the table to update. |
36
|
|
|
* |
37
|
|
|
* @param string $table The table to update. |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
18 |
|
public function table($table) |
43
|
|
|
{ |
44
|
18 |
|
$this->table = $this->quoter->quoteName($table); |
45
|
18 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* Builds this query object into a string. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
16 |
|
protected function build() |
56
|
|
|
{ |
57
|
|
|
return 'UPDATE' |
58
|
16 |
|
. $this->buildFlags() |
59
|
16 |
|
. $this->buildTable() |
60
|
16 |
|
. $this->buildValuesForUpdate() |
61
|
16 |
|
. $this->buildWhere() |
62
|
16 |
|
. $this->buildOrderBy() |
63
|
16 |
|
. $this->buildLimit() |
64
|
16 |
|
. $this->buildReturning(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* Builds the table clause. |
70
|
|
|
* |
71
|
|
|
* @return null |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
16 |
|
protected function buildTable() |
75
|
|
|
{ |
76
|
16 |
|
return " {$this->table}"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* Sets one column value placeholder; if an optional second parameter is |
82
|
|
|
* passed, that value is bound to the placeholder. |
83
|
|
|
* |
84
|
|
|
* @param string $col The column name. |
85
|
|
|
* |
86
|
|
|
* @param array $value |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
6 |
|
public function col($col, ...$value) |
91
|
|
|
{ |
92
|
6 |
|
return $this->addCol($col, ...$value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* Sets multiple column value placeholders. If an element is a key-value |
98
|
|
|
* pair, the key is treated as the column name and the value is bound to |
99
|
|
|
* that column. |
100
|
|
|
* |
101
|
|
|
* @param array $cols A list of column names, optionally as key-value |
102
|
|
|
* pairs where the key is a column name and the value is a bind value for |
103
|
|
|
* that column. |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
15 |
|
public function cols(array $cols) |
109
|
|
|
{ |
110
|
15 |
|
return $this->addCols($cols); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* Sets a column value directly; the value will not be escaped, although |
116
|
|
|
* fully-qualified identifiers in the value will be quoted. |
117
|
|
|
* |
118
|
|
|
* @param string $col The column name. |
119
|
|
|
* |
120
|
|
|
* @param string $value The column value expression. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
14 |
|
public function set($col, $value) |
126
|
|
|
{ |
127
|
14 |
|
return $this->setCol($col, $value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* Builds the updated columns and values of the statement. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
16 |
|
protected function buildValuesForUpdate() |
138
|
|
|
{ |
139
|
16 |
|
$values = array(); |
140
|
16 |
|
foreach ($this->col_values as $col => $value) { |
141
|
16 |
|
$values[] = "{$col} = {$value}"; |
142
|
16 |
|
} |
143
|
16 |
|
return PHP_EOL . 'SET' . $this->indentCsv($values); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|