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