|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Sharding\Query; |
|
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\Preprocessor\DefaultPreprocessor; |
|
10
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
|
11
|
|
|
use Bdf\Prime\Query\Contract\Cachable; |
|
12
|
|
|
use Bdf\Prime\Query\Contract\Query\InsertQueryInterface; |
|
13
|
|
|
use Bdf\Prime\Query\Custom\BulkInsert\BulkInsertQuery; |
|
14
|
|
|
use Bdf\Prime\Query\Extension\CachableTrait; |
|
15
|
|
|
use Bdf\Prime\Sharding\Extension\ShardPicker; |
|
16
|
|
|
use Bdf\Prime\Sharding\ShardingConnection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handle INSERT operations on Sharding connection set |
|
20
|
|
|
* The shard will be choosed using inserted data value, and the operation will be delegated to the shard connection |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
class ShardingInsertQuery extends CompilableClause implements InsertQueryInterface, CommandInterface, Cachable |
|
23
|
|
|
{ |
|
24
|
|
|
use CachableTrait; |
|
25
|
|
|
use ShardPicker; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The DBAL Connection. |
|
29
|
|
|
* |
|
30
|
|
|
* @var ShardingConnection |
|
31
|
|
|
*/ |
|
32
|
|
|
private $connection; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $table; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string[] |
|
41
|
|
|
*/ |
|
42
|
|
|
private $columns = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $mode = self::MODE_INSERT; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $values = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Queries indexed by shard id |
|
56
|
|
|
* |
|
57
|
|
|
* @var BulkInsertQuery[] |
|
58
|
|
|
*/ |
|
59
|
|
|
private $queries = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var BulkInsertQuery |
|
63
|
|
|
*/ |
|
64
|
|
|
private $currentQuery; |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* ShardingInsertQuery constructor. |
|
69
|
|
|
* |
|
70
|
|
|
* @param ShardingConnection $connection |
|
71
|
|
|
* @param PreprocessorInterface $preprocessor |
|
72
|
|
|
*/ |
|
73
|
31 |
|
public function __construct(ShardingConnection $connection, PreprocessorInterface $preprocessor = null) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
31 |
|
parent::__construct($preprocessor ?: new DefaultPreprocessor()); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
31 |
|
$this->connection = $connection; |
|
78
|
31 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function compiler() { } |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
|
|
|
|
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setCompiler(CompilerInterface $compiler) { } |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function connection() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->connection; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
|
|
|
|
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function on(ConnectionInterface $connection) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->connection = $connection; |
|
|
|
|
|
|
104
|
|
|
$this->queries = []; |
|
105
|
|
|
$this->currentQuery = null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
|
|
|
|
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
19 |
|
public function from($from, $alias = null) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
19 |
|
return $this->into($from); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
|
|
|
|
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
|
|
|
|
|
119
|
|
|
public function bulk($flag = true) |
|
120
|
|
|
{ |
|
121
|
|
|
throw new \BadMethodCallException('Bulk insert is not (yet ?) supported by sharding connection'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
|
|
|
|
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
30 |
|
public function into($table) |
|
128
|
|
|
{ |
|
129
|
30 |
|
$this->table = $table; |
|
130
|
|
|
|
|
131
|
30 |
|
foreach ($this->queries as $query) { |
|
132
|
1 |
|
$query->into($table); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
30 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
|
|
|
|
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
20 |
|
public function columns(array $columns) |
|
142
|
|
|
{ |
|
143
|
20 |
|
$this->columns = $columns; |
|
144
|
|
|
|
|
145
|
20 |
|
foreach ($this->queries as $query) { |
|
146
|
1 |
|
$query->columns($columns); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
20 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
|
|
|
|
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
*/ |
|
155
|
29 |
|
public function values(array $data, $replace = false) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
29 |
|
$this->values = $data; |
|
158
|
29 |
|
$this->currentQuery = null; |
|
159
|
|
|
|
|
160
|
29 |
|
$this->currentQuery()->values($data); |
|
161
|
|
|
|
|
162
|
29 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
|
|
|
|
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
21 |
|
public function mode($mode) |
|
169
|
|
|
{ |
|
170
|
21 |
|
$this->mode = $mode; |
|
171
|
|
|
|
|
172
|
21 |
|
foreach ($this->queries as $query) { |
|
173
|
11 |
|
$query->mode($mode); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
21 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
|
|
|
|
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
20 |
|
public function ignore($flag = true) |
|
183
|
|
|
{ |
|
184
|
20 |
|
return $this->mode($flag ? self::MODE_IGNORE : self::MODE_INSERT); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
|
|
|
|
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public function replace($flag = true) |
|
191
|
|
|
{ |
|
192
|
1 |
|
return $this->mode($flag ? self::MODE_REPLACE : self::MODE_INSERT); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
|
|
|
|
|
196
|
|
|
* {@inheritdoc} |
|
197
|
|
|
*/ |
|
198
|
30 |
|
public function execute($columns = null) |
|
199
|
|
|
{ |
|
200
|
30 |
|
$count = $this->currentQuery()->execute($columns); |
|
201
|
|
|
|
|
202
|
29 |
|
if ($count > 0) { |
|
203
|
29 |
|
$this->clearCacheOnWrite(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
29 |
|
return $count; |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Get the query associated to the matching sharding |
|
211
|
|
|
* |
|
212
|
|
|
* @return BulkInsertQuery |
|
213
|
|
|
*/ |
|
214
|
30 |
|
private function currentQuery() |
|
|
|
|
|
|
215
|
|
|
{ |
|
216
|
30 |
|
if ($this->currentQuery !== null) { |
|
217
|
29 |
|
return $this->currentQuery; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
30 |
|
$shardId = $this->getShardId(); |
|
221
|
|
|
|
|
222
|
29 |
|
if (isset($this->queries[$shardId])) { |
|
223
|
11 |
|
return $this->queries[$shardId]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** @var BulkInsertQuery $query */ |
|
|
|
|
|
|
227
|
29 |
|
$query = $this->connection->getConnection($shardId)->make(BulkInsertQuery::class, $this->preprocessor()); |
|
228
|
|
|
|
|
229
|
|
|
$query |
|
230
|
29 |
|
->setCache($this->cache) |
|
|
|
|
|
|
231
|
29 |
|
->into($this->table) |
|
|
|
|
|
|
232
|
29 |
|
->columns($this->columns) |
|
|
|
|
|
|
233
|
29 |
|
->mode($this->mode) |
|
|
|
|
|
|
234
|
|
|
; |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
29 |
|
return $this->currentQuery = $this->queries[$shardId] = $query; |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Get the targeted shard ID |
|
241
|
|
|
* |
|
242
|
|
|
* @return string|null |
|
243
|
|
|
*/ |
|
|
|
|
|
|
244
|
30 |
|
private function getShardId() |
|
|
|
|
|
|
245
|
|
|
{ |
|
246
|
30 |
|
if ($this->shardId !== null) { |
|
247
|
2 |
|
return $this->shardId; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
28 |
|
$distributionKey = $this->connection->getDistributionKey(); |
|
251
|
|
|
|
|
252
|
28 |
|
if (!isset($this->values[$distributionKey])) { |
|
253
|
1 |
|
throw new \LogicException('The value "'.$distributionKey.'" must be provided for selecting the sharding'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
27 |
|
return $this->connection->getShardChoser()->pick($this->values[$distributionKey], $this->connection); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* {@inheritdoc} |
|
261
|
|
|
*/ |
|
262
|
1 |
|
protected function cacheNamespace() |
|
263
|
|
|
{ |
|
264
|
1 |
|
return $this->connection->getName().':'.$this->table; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|