1 | <?php |
||
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 | $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 | 16 | return 'UPDATE' |
|
58 | . $this->buildFlags() |
||
59 | . $this->buildTable() |
||
60 | . $this->buildValuesForUpdate() |
||
61 | . $this->buildWhere() |
||
62 | . $this->buildOrderBy() |
||
63 | . $this->buildLimit() |
||
64 | . $this->buildReturning(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * Builds the table clause. |
||
70 | * |
||
71 | * @return null |
||
72 | * |
||
73 | */ |
||
74 | 16 | protected function buildTable() |
|
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 | public function col($col, ...$value) |
||
91 | { |
||
92 | 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 | 20 | public function cols(array $cols) |
|
109 | { |
||
110 | return $this->addCols($cols); |
||
111 | 20 | } |
|
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 | public function set($col, $value) |
||
126 | { |
||
127 | 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 | 2 | $values[] = "{$col} = {$value}"; |
|
142 | } |
||
143 | return PHP_EOL . 'SET' . $this->indentCsv($values); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * |
||
148 | * Template method overridden for queries that allow LIMIT and OFFSET. |
||
149 | * |
||
150 | * Builds the `LIMIT ... OFFSET` clause of the statement. |
||
151 | * |
||
152 | * Note that this will allow OFFSET values with a LIMIT. |
||
153 | * |
||
154 | * @return string |
||
155 | * |
||
156 | */ |
||
157 | 4 | protected function buildLimit() |
|
161 | } |
||
162 |