|
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
|
|
|
* if true, use a REPLACE sql command instead of INSERT |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
private $use_replace = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* Column values for ON DUPLICATE KEY UPDATE section of query; the key is |
|
34
|
|
|
* the column name and the value is the column value. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $col_on_update_values; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* Use a REPLACE statement. |
|
44
|
|
|
* Matches similar orReplace() function for Sqlite |
|
45
|
|
|
* |
|
46
|
|
|
* @param bool $enable Set or unset flag (default true). |
|
47
|
|
|
* |
|
48
|
|
|
* @return $this |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function orReplace($enable = true) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->use_replace = $enable; |
|
54
|
1 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* |
|
59
|
|
|
* Adds or removes HIGH_PRIORITY flag. |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool $enable Set or unset flag (default true). |
|
62
|
|
|
* |
|
63
|
|
|
* @return $this |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function highPriority($enable = true) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->setFlag('HIGH_PRIORITY', $enable); |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* Adds or removes LOW_PRIORITY flag. |
|
75
|
|
|
* |
|
76
|
|
|
* @param bool $enable Set or unset flag (default true). |
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
79
|
|
|
* |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function lowPriority($enable = true) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$this->setFlag('LOW_PRIORITY', $enable); |
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* Adds or removes IGNORE flag. |
|
90
|
|
|
* |
|
91
|
|
|
* @param bool $enable Set or unset flag (default true). |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
* |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function ignore($enable = true) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->setFlag('IGNORE', $enable); |
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* |
|
104
|
|
|
* Adds or removes DELAYED flag. |
|
105
|
|
|
* |
|
106
|
|
|
* @param bool $enable Set or unset flag (default true). |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
* |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function delayed($enable = true) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$this->setFlag('DELAYED', $enable); |
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* Sets one column value placeholder in ON DUPLICATE KEY UPDATE section; |
|
120
|
|
|
* if an optional second parameter is passed, that value is bound to the |
|
121
|
|
|
* placeholder. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $col The column name. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $value Optional: a value to bind to the placeholder. |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
* |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function onDuplicateKeyUpdateCol($col, ...$value) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$key = $this->quoter->quoteName($col); |
|
133
|
1 |
|
$bind = $col . '__on_duplicate_key'; |
|
134
|
1 |
|
$this->col_on_update_values[$key] = ":$bind"; |
|
135
|
1 |
|
if (count($value) > 0) { |
|
136
|
1 |
|
$this->bindValue($bind, $value[0]); |
|
137
|
1 |
|
} |
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* |
|
143
|
|
|
* Sets multiple column value placeholders in ON DUPLICATE KEY UPDATE |
|
144
|
|
|
* section. If an element is a key-value pair, the key is treated as the |
|
145
|
|
|
* column name and the value is bound to that column. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $cols A list of column names, optionally as key-value |
|
148
|
|
|
* pairs where the key is a column name and the value is a bind value for |
|
149
|
|
|
* that column. |
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
* |
|
153
|
|
|
*/ |
|
154
|
1 |
|
public function onDuplicateKeyUpdateCols(array $cols) |
|
155
|
|
|
{ |
|
156
|
1 |
|
foreach ($cols as $key => $val) { |
|
157
|
1 |
|
if (is_int($key)) { |
|
158
|
|
|
// integer key means the value is the column name |
|
159
|
1 |
|
$this->onDuplicateKeyUpdateCol($val); |
|
160
|
1 |
|
} else { |
|
161
|
|
|
// the key is the column name and the value is a value to |
|
162
|
|
|
// be bound to that column |
|
163
|
1 |
|
$this->onDuplicateKeyUpdateCol($key, $val); |
|
164
|
|
|
} |
|
165
|
1 |
|
} |
|
166
|
1 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* |
|
171
|
|
|
* Sets a column value directly in ON DUPLICATE KEY UPDATE section; the |
|
172
|
|
|
* value will not be escaped, although fully-qualified identifiers in the |
|
173
|
|
|
* value will be quoted. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $col The column name. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $value The column value expression. |
|
178
|
|
|
* |
|
179
|
|
|
* @return $this |
|
180
|
|
|
* |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function onDuplicateKeyUpdate($col, $value) |
|
183
|
|
|
{ |
|
184
|
1 |
|
if ($value === null) { |
|
185
|
1 |
|
$value = 'NULL'; |
|
186
|
1 |
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
$key = $this->quoter->quoteName($col); |
|
189
|
1 |
|
$value = $this->quoter->quoteNamesIn($value); |
|
190
|
1 |
|
$this->col_on_update_values[$key] = $value; |
|
191
|
1 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* |
|
196
|
|
|
* Builds this query object into a string. |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
* |
|
200
|
|
|
*/ |
|
201
|
12 |
|
protected function build() |
|
202
|
|
|
{ |
|
203
|
12 |
|
return ($this->use_replace ? 'REPLACE' : 'INSERT') |
|
204
|
12 |
|
. $this->buildFlags() |
|
205
|
12 |
|
. $this->buildInto() |
|
206
|
12 |
|
. $this->buildValuesForInsert() |
|
207
|
12 |
|
. $this->buildValuesForUpdateOnDuplicateKey() |
|
208
|
12 |
|
. $this->buildReturning(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* |
|
213
|
|
|
* Builds the UPDATE ON DUPLICATE KEY part of the statement. |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
* |
|
217
|
|
|
*/ |
|
218
|
12 |
|
protected function buildValuesForUpdateOnDuplicateKey() |
|
219
|
|
|
{ |
|
220
|
12 |
|
if (empty($this->col_on_update_values)) { |
|
221
|
11 |
|
return ''; // not applicable |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
$values = array(); |
|
225
|
1 |
|
foreach ($this->col_on_update_values as $key => $row) { |
|
226
|
1 |
|
$values[] = $this->indent(array($key . ' = ' . $row)); |
|
227
|
1 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
return ' ON DUPLICATE KEY UPDATE' |
|
230
|
1 |
|
. implode (',', $values); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|