1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntityUpdater\BulkSimpleEntityCreatorHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\MysqliConnectionFactory; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\UuidFunctionPolyfill; |
12
|
|
|
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType; |
13
|
|
|
use Ramsey\Uuid\UuidInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
17
|
|
|
*/ |
18
|
|
|
class BulkSimpleEntityCreator extends AbstractBulkProcess |
19
|
|
|
{ |
20
|
|
|
public const INSERT_MODE_INSERT = 'INSERT '; |
21
|
|
|
public const INSERT_MODE_IGNORE = 'INSERT IGNORE '; |
22
|
|
|
public const INSERT_MODE_DEFAULT = self::INSERT_MODE_INSERT; |
23
|
|
|
public const INSERT_MODES = [ |
24
|
|
|
self::INSERT_MODE_INSERT, |
25
|
|
|
self::INSERT_MODE_IGNORE, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BulkSimpleEntityCreatorHelper |
30
|
|
|
*/ |
31
|
|
|
private $helper; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $tableName; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $entityFqn; |
40
|
|
|
/** |
41
|
|
|
* Is the UUID binary |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $isBinaryUuid = true; |
46
|
|
|
/** |
47
|
|
|
* @var ClassMetadataInfo |
48
|
|
|
*/ |
49
|
|
|
private $meta; |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $primaryKeyCol; |
54
|
|
|
/** |
55
|
|
|
* @var \mysqli |
56
|
|
|
*/ |
57
|
|
|
private $mysqli; |
58
|
|
|
/** |
59
|
|
|
* @var UuidFunctionPolyfill |
60
|
|
|
*/ |
61
|
|
|
private $uuidFunctionPolyfill; |
62
|
|
|
/** |
63
|
|
|
* @var UuidFactory |
64
|
|
|
*/ |
65
|
|
|
private $uuidFactory; |
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $query; |
70
|
|
|
/** |
71
|
|
|
* For creation this should always be 100%, so 1 |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
private $requireAffectedRatio = 1; |
76
|
|
|
/** |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
|
|
private $totalAffectedRows = 0; |
80
|
|
|
|
81
|
|
|
private $insertMode = self::INSERT_MODE_DEFAULT; |
82
|
|
|
|
83
|
1 |
|
public function __construct( |
84
|
|
|
EntityManagerInterface $entityManager, |
85
|
|
|
MysqliConnectionFactory $mysqliConnectionFactory, |
86
|
|
|
UuidFunctionPolyfill $uuidFunctionPolyfill, |
87
|
|
|
UuidFactory $uuidFactory |
88
|
|
|
) { |
89
|
1 |
|
parent::__construct($entityManager); |
90
|
1 |
|
$this->mysqli = $mysqliConnectionFactory->createFromEntityManager($entityManager); |
91
|
1 |
|
$this->uuidFunctionPolyfill = $uuidFunctionPolyfill; |
92
|
1 |
|
$this->uuidFactory = $uuidFactory; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
public function endBulkProcess(): void |
96
|
|
|
{ |
97
|
1 |
|
parent::endBulkProcess(); |
98
|
|
|
// Reset the insert mode to default to prevent state bleeding across batch runs |
99
|
1 |
|
$this->setInsertMode(self::INSERT_MODE_DEFAULT); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $insertMode |
104
|
|
|
* |
105
|
|
|
* @return BulkSimpleEntityCreator |
106
|
|
|
*/ |
107
|
1 |
|
public function setInsertMode(string $insertMode): BulkSimpleEntityCreator |
108
|
|
|
{ |
109
|
1 |
|
if (false === \in_array($insertMode, self::INSERT_MODES, true)) { |
110
|
|
|
throw new \InvalidArgumentException('Invalid insert mode'); |
111
|
|
|
} |
112
|
1 |
|
$this->insertMode = $insertMode; |
113
|
1 |
|
if ($this->insertMode === self::INSERT_MODE_IGNORE) { |
114
|
|
|
$this->requireAffectedRatio = 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function addEntityToSave(EntityInterface $entity): void |
121
|
|
|
{ |
122
|
|
|
throw new \RuntimeException('You should not try to save Entities with this saver'); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function addEntitiesToSave(array $entities): void |
126
|
|
|
{ |
127
|
1 |
|
foreach ($entities as $entityData) { |
128
|
1 |
|
if (\is_array($entityData)) { |
129
|
1 |
|
$this->addEntityCreationData($entityData); |
130
|
1 |
|
continue; |
131
|
|
|
} |
132
|
|
|
throw new \InvalidArgumentException('You should only pass in simple arrays of scalar entity data'); |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
public function addEntityCreationData(array $entityData): void |
137
|
|
|
{ |
138
|
1 |
|
$this->entitiesToSave[] = $entityData; |
139
|
1 |
|
$this->bulkSaveIfChunkBigEnough(); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
public function setHelper(BulkSimpleEntityCreatorHelper $helper): void |
143
|
|
|
{ |
144
|
1 |
|
$this->helper = $helper; |
145
|
1 |
|
$this->tableName = $helper->getTableName(); |
146
|
1 |
|
$this->entityFqn = $helper->getEntityFqn(); |
147
|
1 |
|
$this->meta = $this->entityManager->getClassMetadata($this->entityFqn); |
148
|
1 |
|
$this->primaryKeyCol = $this->meta->getSingleIdentifierFieldName(); |
149
|
1 |
|
$this->isBinaryUuid = $this->isBinaryUuid(); |
150
|
1 |
|
$this->runPolyfillIfRequired(); |
151
|
1 |
|
} |
152
|
|
|
|
153
|
1 |
|
private function isBinaryUuid(): bool |
154
|
|
|
{ |
155
|
1 |
|
$idMapping = $this->meta->getFieldMapping($this->meta->getSingleIdentifierFieldName()); |
156
|
|
|
|
157
|
1 |
|
return $idMapping['type'] === UuidBinaryOrderedTimeType::NAME; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
private function runPolyfillIfRequired(): void |
161
|
|
|
{ |
162
|
1 |
|
if (false === $this->isBinaryUuid) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
1 |
|
$this->uuidFunctionPolyfill->run(); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* As these are not actually entities, lets empty them out before |
170
|
|
|
* parent::freeResources tries to detach from the entity manager |
171
|
|
|
*/ |
172
|
1 |
|
protected function freeResources() |
173
|
|
|
{ |
174
|
1 |
|
$this->entitiesToSave = []; |
175
|
1 |
|
parent::freeResources(); |
176
|
1 |
|
} |
177
|
|
|
|
178
|
1 |
|
protected function doSave(): void |
179
|
|
|
{ |
180
|
1 |
|
foreach ($this->entitiesToSave as $entityData) { |
181
|
1 |
|
$this->appendToQuery($this->buildSql($entityData)); |
182
|
|
|
} |
183
|
1 |
|
$this->runQuery(); |
184
|
1 |
|
$this->reset(); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
1 |
|
private function appendToQuery(string $sql) |
188
|
|
|
{ |
189
|
1 |
|
$this->query .= "\n$sql"; |
190
|
1 |
|
} |
191
|
|
|
|
192
|
1 |
|
private function buildSql(array $entityData): string |
193
|
|
|
{ |
194
|
1 |
|
$sql = $this->insertMode . " into {$this->tableName} set "; |
195
|
|
|
$sqls = [ |
196
|
1 |
|
$this->primaryKeyCol . ' = ' . $this->generateId(), |
197
|
|
|
]; |
198
|
1 |
|
foreach ($entityData as $key => $value) { |
199
|
1 |
|
if ($key === $this->primaryKeyCol) { |
200
|
|
|
throw new \InvalidArgumentException( |
201
|
|
|
'You should not pass in IDs, they will be auto generated' |
202
|
|
|
); |
203
|
|
|
} |
204
|
1 |
|
if ($value instanceof UuidInterface) { |
205
|
|
|
$sqls[] = "`$key` = " . $this->getUuidSql($value); |
206
|
|
|
continue; |
207
|
|
|
} |
208
|
1 |
|
$value = $this->mysqli->escape_string((string)$value); |
209
|
1 |
|
$sqls[] = "`$key` = '$value'"; |
210
|
|
|
} |
211
|
1 |
|
$sql .= implode(', ', $sqls) . ';'; |
212
|
|
|
|
213
|
1 |
|
return $sql; |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
private function generateId() |
217
|
|
|
{ |
218
|
1 |
|
if ($this->isBinaryUuid) { |
219
|
1 |
|
return $this->getUuidSql($this->uuidFactory->getOrderedTimeUuid()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->getUuidSql($this->uuidFactory->getUuid()); |
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
private function getUuidSql(UuidInterface $uuid) |
226
|
|
|
{ |
227
|
1 |
|
if ($this->isBinaryUuid) { |
228
|
1 |
|
$uuidString = (string)$uuid; |
229
|
1 |
|
return "UUID_TO_BIN('$uuidString', true)"; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
throw new \RuntimeException('This is not currently suppported - should be easy enough though'); |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
private function runQuery(): void |
236
|
|
|
{ |
237
|
1 |
|
if ('' === $this->query) { |
238
|
|
|
return; |
239
|
|
|
} |
240
|
1 |
|
$this->mysqli->ping(); |
241
|
1 |
|
$this->query = " |
242
|
|
|
START TRANSACTION; |
243
|
|
|
SET FOREIGN_KEY_CHECKS = 0; |
244
|
1 |
|
{$this->query} |
245
|
|
|
SET FOREIGN_KEY_CHECKS = 1; |
246
|
|
|
COMMIT;"; |
247
|
1 |
|
$result = $this->mysqli->multi_query($this->query); |
248
|
1 |
|
if (true !== $result) { |
249
|
|
|
throw new \RuntimeException( |
250
|
|
|
'Multi Query returned false which means the first statement failed: ' . |
251
|
|
|
$this->mysqli->error |
252
|
|
|
); |
253
|
|
|
} |
254
|
1 |
|
$affectedRows = 0; |
255
|
1 |
|
$queryCount = 0; |
256
|
|
|
do { |
257
|
1 |
|
$queryCount++; |
258
|
1 |
|
$errorNo = (int)$this->mysqli->errno; |
259
|
1 |
|
if (0 !== $errorNo) { |
260
|
|
|
$errorMessage = 'Query #' . $queryCount . |
261
|
|
|
' got MySQL Error #' . $errorNo . |
262
|
|
|
': ' . $this->mysqli->error |
263
|
|
|
. "\nQuery: " . $this->getQueryLine($queryCount) . "'\n"; |
264
|
|
|
throw new \RuntimeException($errorMessage); |
265
|
|
|
} |
266
|
1 |
|
$affectedRows += max($this->mysqli->affected_rows, 0); |
267
|
1 |
|
if (false === $this->mysqli->more_results()) { |
268
|
1 |
|
break; |
269
|
|
|
} |
270
|
1 |
|
$this->mysqli->next_result(); |
271
|
1 |
|
} while (true); |
272
|
1 |
|
if ($affectedRows < count($this->entitiesToSave) * $this->requireAffectedRatio) { |
273
|
|
|
throw new \RuntimeException( |
274
|
|
|
'Affected rows count of ' . $affectedRows . |
275
|
|
|
' does match the expected count of entitiesToSave ' . count($this->entitiesToSave) |
276
|
|
|
); |
277
|
|
|
} |
278
|
1 |
|
$this->totalAffectedRows += $affectedRows; |
279
|
1 |
|
$this->mysqli->commit(); |
280
|
1 |
|
} |
281
|
|
|
|
282
|
|
|
private function getQueryLine(int $line): string |
283
|
|
|
{ |
284
|
|
|
$lines = explode(";\n", $this->query); |
285
|
|
|
|
286
|
|
|
return $lines[$line + 1]; |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
private function reset(): void |
290
|
|
|
{ |
291
|
1 |
|
$this->entitiesToSave = []; |
292
|
1 |
|
$this->query = ''; |
293
|
1 |
|
} |
294
|
|
|
} |
295
|
|
|
|