|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Custom\BulkInsert; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Query\CommandInterface; |
|
7
|
|
|
use Bdf\Prime\Query\CompilableClause; |
|
8
|
|
|
use Bdf\Prime\Query\Compiler\CompilerInterface; |
|
9
|
|
|
use Bdf\Prime\Query\Compiler\CompilerState; |
|
10
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor; |
|
11
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
|
12
|
|
|
use Bdf\Prime\Query\Contract\Cachable; |
|
13
|
|
|
use Bdf\Prime\Query\Contract\Compilable; |
|
14
|
|
|
use Bdf\Prime\Query\Contract\Query\InsertQueryInterface; |
|
15
|
|
|
use Bdf\Prime\Query\Contract\WriteOperation; |
|
16
|
|
|
use Bdf\Prime\Query\Extension\CachableTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handle optimised insert query |
|
20
|
|
|
* |
|
21
|
|
|
* <code> |
|
22
|
|
|
* // Simple insert |
|
23
|
|
|
* $insert |
|
24
|
|
|
* ->into('perforn') |
|
25
|
|
|
* ->values([ |
|
26
|
|
|
* 'first_name' => 'John', |
|
27
|
|
|
* 'last_name' => 'Doe' |
|
28
|
|
|
* ]) |
|
29
|
|
|
* ->execute() |
|
30
|
|
|
* ; |
|
31
|
|
|
* |
|
32
|
|
|
* // Bulk insert |
|
33
|
|
|
* $insert |
|
34
|
|
|
* ->bulk() |
|
35
|
|
|
* ->values([ |
|
36
|
|
|
* 'first_name' => 'Alan', |
|
37
|
|
|
* 'last_name' => 'Smith' |
|
38
|
|
|
* ]) |
|
39
|
|
|
* ->values([ |
|
40
|
|
|
* 'first_name' => 'Mickey', |
|
41
|
|
|
* 'last_name' => 'Mouse' |
|
42
|
|
|
* ]) |
|
43
|
|
|
* ->execute() |
|
44
|
|
|
* ; |
|
45
|
|
|
* </code> |
|
46
|
|
|
*/ |
|
47
|
|
|
class BulkInsertQuery extends CompilableClause implements CommandInterface, Compilable, Cachable, InsertQueryInterface |
|
48
|
|
|
{ |
|
49
|
|
|
use CachableTrait; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The DBAL Connection. |
|
53
|
|
|
* |
|
54
|
|
|
* @var ConnectionInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $connection; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The SQL compiler |
|
60
|
|
|
* |
|
61
|
|
|
* @var CompilerInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $compiler; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* BulkInsertQuery constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param ConnectionInterface $connection |
|
70
|
|
|
* @param PreprocessorInterface|null $preprocessor |
|
71
|
|
|
*/ |
|
72
|
119 |
|
public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
119 |
|
parent::__construct($preprocessor ?: new DefaultPreprocessor(), new CompilerState()); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
119 |
|
$this->on($connection); |
|
77
|
|
|
|
|
78
|
119 |
|
$this->statements = [ |
|
79
|
119 |
|
'table' => null, |
|
80
|
|
|
'columns' => [], |
|
81
|
|
|
'values' => [], |
|
82
|
119 |
|
'mode' => self::MODE_INSERT, |
|
83
|
|
|
'bulk' => false |
|
|
|
|
|
|
84
|
|
|
]; |
|
85
|
119 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function compiler() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->compiler; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
|
|
|
|
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setCompiler(CompilerInterface $compiler) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->compiler = $compiler; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
|
|
|
|
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
119 |
|
public function on(ConnectionInterface $connection) |
|
109
|
|
|
{ |
|
110
|
119 |
|
$this->connection = $connection; |
|
111
|
119 |
|
$this->compiler = $connection->factory()->compiler(static::class); |
|
112
|
|
|
|
|
113
|
119 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function connection() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->connection; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
#[WriteOperation] |
|
|
|
|
|
|
128
|
413 |
|
public function execute($columns = null) |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
413 |
|
$nb = $this->connection->execute($this)->count(); |
|
131
|
|
|
|
|
132
|
411 |
|
if ($nb > 0) { |
|
133
|
411 |
|
$this->clearCacheOnWrite(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
411 |
|
return $nb; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
|
|
|
|
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
118 |
|
public function into($table) |
|
143
|
|
|
{ |
|
144
|
118 |
|
$this->compilerState->invalidate('table'); |
|
145
|
118 |
|
$this->compilerState->invalidate('columns'); |
|
146
|
|
|
|
|
147
|
|
|
// Reset columns when changing table |
|
148
|
118 |
|
$this->statements['columns'] = []; |
|
149
|
118 |
|
$this->statements['table'] = $table; |
|
150
|
|
|
|
|
151
|
118 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
|
|
|
|
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
* |
|
157
|
|
|
* Alias of into for compatibility purpose with interface |
|
|
|
|
|
|
158
|
|
|
* |
|
159
|
|
|
* @see BulkInsertQuery::into() |
|
160
|
|
|
*/ |
|
161
|
68 |
|
public function from($from, $alias = null) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
68 |
|
return $this->into($from); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
|
|
|
|
|
167
|
|
|
* {@inheritdoc} |
|
168
|
|
|
*/ |
|
169
|
118 |
|
public function columns(array $columns) |
|
170
|
|
|
{ |
|
171
|
118 |
|
$this->compilerState->invalidate('columns'); |
|
172
|
|
|
|
|
173
|
118 |
|
$this->statements['columns'] = []; |
|
174
|
|
|
|
|
175
|
118 |
|
foreach ($columns as $name => $type) { |
|
176
|
118 |
|
if (is_int($name)) { |
|
177
|
117 |
|
$this->statements['columns'][] = [ |
|
178
|
117 |
|
'name' => $type, |
|
179
|
|
|
'type' => null |
|
|
|
|
|
|
180
|
|
|
]; |
|
181
|
|
|
} else { |
|
182
|
1 |
|
$this->statements['columns'][] = [ |
|
183
|
1 |
|
'name' => $name, |
|
184
|
118 |
|
'type' => $type |
|
|
|
|
|
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
118 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
|
|
|
|
|
193
|
|
|
* {@inheritdoc} |
|
194
|
|
|
*/ |
|
195
|
427 |
|
public function values(array $data, $replace = false) |
|
196
|
|
|
{ |
|
197
|
427 |
|
if (empty($this->statements['columns'])) { |
|
198
|
27 |
|
$this->columns(array_keys($data)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
427 |
|
if ($this->statements['bulk']) { |
|
202
|
6 |
|
$this->compilerState->invalidate(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
427 |
|
if (!$this->statements['bulk'] || $replace) { |
|
206
|
421 |
|
$this->statements['values'] = [$data]; |
|
207
|
|
|
} else { |
|
208
|
6 |
|
$this->statements['values'][] = $data; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
427 |
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
|
|
|
|
|
215
|
|
|
* {@inheritdoc} |
|
216
|
|
|
*/ |
|
217
|
410 |
|
public function mode($mode) |
|
218
|
|
|
{ |
|
219
|
410 |
|
if ($mode !== $this->statements['mode']) { |
|
220
|
11 |
|
$this->compilerState->invalidate('mode'); |
|
221
|
11 |
|
$this->statements['mode'] = $mode; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
410 |
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
|
|
|
|
|
228
|
|
|
* {@inheritdoc} |
|
229
|
|
|
*/ |
|
230
|
378 |
|
public function ignore($flag = true) |
|
231
|
|
|
{ |
|
232
|
378 |
|
return $this->mode($flag ? self::MODE_IGNORE : self::MODE_INSERT); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
|
|
|
|
|
236
|
|
|
* {@inheritdoc} |
|
237
|
|
|
*/ |
|
238
|
2 |
|
public function replace($flag = true) |
|
239
|
|
|
{ |
|
240
|
2 |
|
return $this->mode($flag ? self::MODE_REPLACE : self::MODE_INSERT); |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
|
|
|
|
|
244
|
|
|
* {@inheritdoc} |
|
245
|
|
|
*/ |
|
246
|
8 |
|
public function bulk($flag = true) |
|
247
|
|
|
{ |
|
248
|
8 |
|
$this->compilerState->invalidate(); |
|
249
|
8 |
|
$this->statements['bulk'] = $flag; |
|
250
|
|
|
|
|
251
|
8 |
|
return $this; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
|
|
|
|
|
255
|
|
|
* {@inheritdoc} |
|
256
|
|
|
*/ |
|
257
|
415 |
|
public function compile($forceRecompile = false) |
|
258
|
|
|
{ |
|
259
|
415 |
|
if (!$forceRecompile && $this->state()->compiled) { |
|
260
|
348 |
|
return $this->state()->compiled; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
399 |
|
return $this->state()->compiled = $this->compiler->compileInsert($this); |
|
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* {@inheritdoc} |
|
268
|
|
|
*/ |
|
269
|
413 |
|
public function getBindings() |
|
270
|
|
|
{ |
|
271
|
413 |
|
return $this->compiler->getBindings($this); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* {@inheritdoc} |
|
276
|
|
|
*/ |
|
277
|
|
|
public function type() |
|
278
|
|
|
{ |
|
279
|
|
|
return self::TYPE_INSERT; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Get cache namespace |
|
284
|
|
|
* |
|
285
|
|
|
* @return string |
|
286
|
|
|
*/ |
|
287
|
2 |
|
protected function cacheNamespace() |
|
288
|
|
|
{ |
|
289
|
2 |
|
return $this->connection->getName().':'.$this->statements['table']; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|