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
|
23 |
|
public function table($table) |
43
|
|
|
{ |
44
|
23 |
|
$this->table = $this->quoter->quoteName($table); |
45
|
23 |
|
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->builder->buildFlags($this->flags) |
|
|
|
|
59
|
16 |
|
. $this->builder->buildTable($this->table) |
60
|
16 |
|
. $this->builder->buildValuesForUpdate($this->col_values) |
61
|
16 |
|
. $this->builder->buildWhere($this->where) |
62
|
16 |
|
. $this->builder->buildOrderBy($this->order_by); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* Sets one column value placeholder; if an optional second parameter is |
68
|
|
|
* passed, that value is bound to the placeholder. |
69
|
|
|
* |
70
|
|
|
* @param string $col The column name. |
71
|
|
|
* |
72
|
|
|
* @param array $value |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
6 |
|
public function col($col, ...$value) |
77
|
|
|
{ |
78
|
6 |
|
return $this->addCol($col, ...$value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* Sets multiple column value placeholders. If an element is a key-value |
84
|
|
|
* pair, the key is treated as the column name and the value is bound to |
85
|
|
|
* that column. |
86
|
|
|
* |
87
|
|
|
* @param array $cols A list of column names, optionally as key-value |
88
|
|
|
* pairs where the key is a column name and the value is a bind value for |
89
|
|
|
* that column. |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
* |
93
|
|
|
*/ |
94
|
20 |
|
public function cols(array $cols) |
95
|
|
|
{ |
96
|
20 |
|
return $this->addCols($cols); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* Sets a column value directly; the value will not be escaped, although |
102
|
|
|
* fully-qualified identifiers in the value will be quoted. |
103
|
|
|
* |
104
|
|
|
* @param string $col The column name. |
105
|
|
|
* |
106
|
|
|
* @param string $value The column value expression. |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
* |
110
|
|
|
*/ |
111
|
14 |
|
public function set($col, $value) |
112
|
|
|
{ |
113
|
14 |
|
return $this->setCol($col, $value); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: