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\Mysql; |
10
|
|
|
|
11
|
|
|
use Aura\SqlQuery\Common; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* An object for MySQL INSERT queries. |
16
|
|
|
* |
17
|
|
|
* @package Aura.SqlQuery |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class Insert extends Common\Insert |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* Column values for ON DUPLICATE KEY UPDATE section of query; the key is |
25
|
|
|
* the column name and the value is the column value. |
26
|
|
|
* |
27
|
|
|
* @param array |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
protected $col_on_update_values; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* Adds or removes HIGH_PRIORITY flag. |
35
|
|
|
* |
36
|
|
|
* @param bool $enable Set or unset flag (default true). |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
1 |
|
public function highPriority($enable = true) |
42
|
|
|
{ |
43
|
1 |
|
$this->setFlag('HIGH_PRIORITY', $enable); |
44
|
1 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* Adds or removes LOW_PRIORITY flag. |
50
|
|
|
* |
51
|
|
|
* @param bool $enable Set or unset flag (default true). |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
1 |
|
public function lowPriority($enable = true) |
57
|
|
|
{ |
58
|
1 |
|
$this->setFlag('LOW_PRIORITY', $enable); |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* Adds or removes IGNORE flag. |
65
|
|
|
* |
66
|
|
|
* @param bool $enable Set or unset flag (default true). |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
1 |
|
public function ignore($enable = true) |
72
|
|
|
{ |
73
|
1 |
|
$this->setFlag('IGNORE', $enable); |
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* Adds or removes DELAYED flag. |
80
|
|
|
* |
81
|
|
|
* @param bool $enable Set or unset flag (default true). |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
1 |
|
public function delayed($enable = true) |
87
|
|
|
{ |
88
|
1 |
|
$this->setFlag('DELAYED', $enable); |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* Sets one column value placeholder in ON DUPLICATE KEY UPDATE section; |
95
|
|
|
* if an optional second parameter is passed, that value is bound to the |
96
|
|
|
* placeholder. |
97
|
|
|
* |
98
|
|
|
* @param string $col The column name. |
99
|
|
|
* |
100
|
|
|
* @param mixed,... $val Optional: a value to bind to the placeholder. |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
1 |
|
public function onDuplicateKeyUpdateCol($col) |
106
|
|
|
{ |
107
|
1 |
|
$key = $this->quoter->quoteName($col); |
108
|
1 |
|
$bind = $col . '__on_duplicate_key'; |
109
|
1 |
|
$this->col_on_update_values[$key] = ":$bind"; |
110
|
1 |
|
$args = func_get_args(); |
111
|
1 |
|
if (count($args) > 1) { |
112
|
1 |
|
$this->bindValue($bind, $args[1]); |
113
|
|
|
} |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* |
119
|
|
|
* Sets multiple column value placeholders in ON DUPLICATE KEY UPDATE |
120
|
|
|
* section. If an element is a key-value pair, the key is treated as the |
121
|
|
|
* column name and the value is bound to that column. |
122
|
|
|
* |
123
|
|
|
* @param array $cols A list of column names, optionally as key-value |
124
|
|
|
* pairs where the key is a column name and the value is a bind value for |
125
|
|
|
* that column. |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
* |
129
|
|
|
*/ |
130
|
1 |
|
public function onDuplicateKeyUpdateCols(array $cols) |
131
|
|
|
{ |
132
|
1 |
|
foreach ($cols as $key => $val) { |
133
|
1 |
|
if (is_int($key)) { |
134
|
|
|
// integer key means the value is the column name |
135
|
1 |
|
$this->onDuplicateKeyUpdateCol($val); |
136
|
|
|
} else { |
137
|
|
|
// the key is the column name and the value is a value to |
138
|
|
|
// be bound to that column |
139
|
1 |
|
$this->onDuplicateKeyUpdateCol($key, $val); |
140
|
|
|
} |
141
|
|
|
} |
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* |
147
|
|
|
* Sets a column value directly in ON DUPLICATE KEY UPDATE section; the |
148
|
|
|
* value will not be escaped, although fully-qualified identifiers in the |
149
|
|
|
* value will be quoted. |
150
|
|
|
* |
151
|
|
|
* @param string $col The column name. |
152
|
|
|
* |
153
|
|
|
* @param string $value The column value expression. |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
* |
157
|
|
|
*/ |
158
|
1 |
|
public function onDuplicateKeyUpdate($col, $value) |
159
|
|
|
{ |
160
|
1 |
|
if ($value === null) { |
161
|
1 |
|
$value = 'NULL'; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$key = $this->quoter->quoteName($col); |
165
|
1 |
|
$value = $this->quoter->quoteNamesIn($value); |
166
|
1 |
|
$this->col_on_update_values[$key] = $value; |
167
|
1 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
* Builds this query object into a string. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
* |
176
|
|
|
*/ |
177
|
11 |
|
protected function build() |
178
|
|
|
{ |
179
|
|
|
return 'INSERT' |
180
|
11 |
|
. $this->buildFlags() |
181
|
11 |
|
. $this->buildInto() |
182
|
11 |
|
. $this->buildValuesForInsert() |
183
|
11 |
|
. $this->buildValuesForUpdateOnDuplicateKey() |
184
|
11 |
|
. $this->buildReturning(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
* Builds the UPDATE ON DUPLICATE KEY part of the statement. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
* |
193
|
|
|
*/ |
194
|
11 |
|
protected function buildValuesForUpdateOnDuplicateKey() |
195
|
|
|
{ |
196
|
11 |
|
if (! $this->col_on_update_values) { |
|
|
|
|
197
|
10 |
|
return ''; // not applicable |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
$values = array(); |
201
|
1 |
|
foreach ($this->col_on_update_values as $key => $row) { |
202
|
1 |
|
$values[] = $this->indent(array($key . ' = ' . $row)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return ' ON DUPLICATE KEY UPDATE' |
206
|
1 |
|
. implode (',', $values); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.