1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Dms\MySQL\Repository; |
4
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Connection\ConnectionInterface; |
6
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Enum\CommandEnum; |
7
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilder; |
8
|
|
|
use Janisbiz\LightOrm\Entity\EntityInterface; |
9
|
|
|
use Janisbiz\LightOrm\Generator\Writer\WriterInterface; |
10
|
|
|
use Janisbiz\LightOrm\QueryBuilder\QueryBuilderInterface as BaseQueryBuilderInterface; |
11
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderInterface; |
12
|
|
|
use Janisbiz\LightOrm\Repository\AbstractRepository as BaseAbstractRepository; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @method ConnectionInterface getConnection() |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractRepository extends BaseAbstractRepository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param QueryBuilderInterface $queryBuilder |
21
|
|
|
* @param bool $toString |
22
|
|
|
* |
23
|
|
|
* @return string|EntityInterface |
24
|
|
|
* @throws RepositoryException|\Exception |
25
|
|
|
*/ |
26
|
|
|
protected function insert(QueryBuilderInterface $queryBuilder, bool $toString = false) |
27
|
|
|
{ |
28
|
|
|
if (!($entity = $queryBuilder->getEntity())) { |
29
|
|
|
throw new RepositoryException( |
30
|
|
|
'Cannot perform insert on query without entity! Please create query builder with entity.' |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->addEntityInsertQuery($queryBuilder, $entity); |
35
|
|
|
|
36
|
|
|
if (true === $toString) { |
37
|
|
|
return $queryBuilder->toString(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->beginTransaction($connection = $this->getConnection()); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$this->prepareAndExecute($queryBuilder, $queryBuilder->bindValueData(), $connection); |
45
|
|
|
} catch (\Exception $e) { |
46
|
|
|
$this->rollBack($connection); |
47
|
|
|
|
48
|
|
|
throw $e; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!empty($primaryKeysAutoIncrement = $entity->primaryKeysAutoIncrement())) { |
52
|
|
|
$entityData = &$entity->data(); |
53
|
|
|
|
54
|
|
|
foreach ($primaryKeysAutoIncrement as $primaryKeyAutoIncrement) { |
55
|
|
|
$entityData[$primaryKeyAutoIncrement] = (int) $connection->lastInsertId($entity::TABLE_NAME); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->commit($connection); |
60
|
|
|
|
61
|
|
|
return $entity; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param QueryBuilderInterface $queryBuilder |
66
|
|
|
* @param bool $toString |
67
|
|
|
* |
68
|
|
|
* @return string|EntityInterface |
69
|
|
|
*/ |
70
|
|
|
protected function insertIgnore(QueryBuilderInterface $queryBuilder, bool $toString = false) |
71
|
|
|
{ |
72
|
|
|
return $this->insert($queryBuilder, $toString); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param QueryBuilderInterface $queryBuilder |
77
|
|
|
* @param bool $toString |
78
|
|
|
* |
79
|
|
|
* @return string|EntityInterface |
80
|
|
|
*/ |
81
|
|
|
protected function replace(QueryBuilderInterface $queryBuilder, bool $toString = false) |
82
|
|
|
{ |
83
|
|
|
return $this->insert($queryBuilder, $toString); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param QueryBuilderInterface $queryBuilder |
88
|
|
|
* @param bool $toString |
89
|
|
|
* |
90
|
|
|
* @return null|string |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
protected function findOne(QueryBuilderInterface $queryBuilder, bool $toString = false) |
94
|
|
|
{ |
95
|
|
|
if (true === $toString) { |
96
|
|
|
return $queryBuilder->toString(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$statement = $this->prepareAndExecute($queryBuilder, $queryBuilder->bindData()); |
101
|
|
|
$statement->setFetchMode( |
102
|
|
|
\PDO::FETCH_CLASS, |
103
|
|
|
($entity = $queryBuilder->getEntity()) ? \get_class($entity) : $this->getEntityClass(), |
104
|
|
|
[ |
105
|
|
|
false, |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
} catch (\Exception $e) { |
109
|
|
|
$this->rollback(); |
110
|
|
|
|
111
|
|
|
throw $e; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $statement->fetch() ?: null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param QueryBuilderInterface $queryBuilder |
119
|
|
|
* @param bool $toString |
120
|
|
|
* |
121
|
|
|
* @return array|string |
122
|
|
|
* @throws \Exception |
123
|
|
|
*/ |
124
|
|
|
protected function find(QueryBuilderInterface $queryBuilder, bool $toString = false) |
125
|
|
|
{ |
126
|
|
|
if (true === $toString) { |
127
|
|
|
return $queryBuilder->toString(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$statement = $this->prepareAndExecute($queryBuilder, $queryBuilder->bindData()); |
132
|
|
|
$statement->setFetchMode( |
133
|
|
|
\PDO::FETCH_CLASS, |
134
|
|
|
($entity = $queryBuilder->getEntity()) ? \get_class($entity) : $this->getEntityClass(), |
135
|
|
|
[ |
136
|
|
|
false, |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
} catch (\Exception $e) { |
140
|
|
|
$this->rollback(); |
141
|
|
|
|
142
|
|
|
throw $e; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $statement->fetchAll() ?: []; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param QueryBuilderInterface $queryBuilder |
150
|
|
|
* @param bool $toString |
151
|
|
|
* |
152
|
|
|
* @return null|bool|EntityInterface|string |
153
|
|
|
* @throws \Exception |
154
|
|
|
*/ |
155
|
|
|
protected function update(QueryBuilderInterface $queryBuilder, bool $toString = false) |
156
|
|
|
{ |
157
|
|
|
if (($entity = $queryBuilder->getEntity()) && !$this->addEntityUpdateQuery($queryBuilder, $entity, $toString)) { |
158
|
|
|
return $entity; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (true === $toString) { |
162
|
|
|
return $queryBuilder->toString(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->beginTransaction($connection = $this->getConnection()); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
$connection->setSqlSafeUpdates(); |
169
|
|
|
|
170
|
|
|
$this->prepareAndExecute($queryBuilder, $queryBuilder->bindData(), $connection); |
171
|
|
|
|
172
|
|
|
$connection->unsetSqlSafeUpdates(); |
173
|
|
|
} catch (\Exception $e) { |
174
|
|
|
$this->rollBack($connection); |
175
|
|
|
$connection->unsetSqlSafeUpdates(); |
176
|
|
|
|
177
|
|
|
throw $e; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->commit($connection) ? ($entity ?: true) : false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param QueryBuilderInterface $queryBuilder |
185
|
|
|
* @param bool $toString |
186
|
|
|
* |
187
|
|
|
* @return null|string|bool|EntityInterface |
188
|
|
|
*/ |
189
|
|
|
protected function updateIgnore(QueryBuilderInterface $queryBuilder, bool $toString = false) |
190
|
|
|
{ |
191
|
|
|
return $this->update($queryBuilder, $toString); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param QueryBuilderInterface $queryBuilder |
196
|
|
|
* @param bool $toString |
197
|
|
|
* |
198
|
|
|
* @return bool|string |
199
|
|
|
* @throws \Exception |
200
|
|
|
*/ |
201
|
|
|
protected function delete(QueryBuilderInterface $queryBuilder, bool $toString = false) |
202
|
|
|
{ |
203
|
|
|
if (($entity = $queryBuilder->getEntity()) && !$this->addEntityDeleteQuery($queryBuilder, $entity)) { |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (true === $toString) { |
208
|
|
|
return $queryBuilder->toString(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->beginTransaction($connection = $this->getConnection()); |
212
|
|
|
|
213
|
|
|
try { |
214
|
|
|
$connection->setSqlSafeUpdates(); |
215
|
|
|
|
216
|
|
|
$this->prepareAndExecute($queryBuilder, $queryBuilder->bindData(), $connection); |
217
|
|
|
|
218
|
|
|
$connection->unsetSqlSafeUpdates(); |
219
|
|
|
} catch (\Exception $e) { |
220
|
|
|
$this->rollBack($connection); |
221
|
|
|
$connection->unsetSqlSafeUpdates(); |
222
|
|
|
|
223
|
|
|
throw $e; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this->commit($connection); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param QueryBuilderInterface $queryBuilder |
231
|
|
|
* @param bool $toString |
232
|
|
|
* |
233
|
|
|
* @return int |
234
|
|
|
* @throws RepositoryException|\Exception |
235
|
|
|
*/ |
236
|
|
|
protected function count(BaseQueryBuilderInterface $queryBuilder, bool $toString = false) |
237
|
|
|
{ |
238
|
|
|
if (CommandEnum::SELECT !== $queryBuilder->commandData()) { |
|
|
|
|
239
|
|
|
throw new RepositoryException(\sprintf( |
240
|
|
|
'Command "%s" is not a valid command for count query! Use "%s" command to execute count query.', |
241
|
|
|
$queryBuilder->commandData(), |
242
|
|
|
CommandEnum::SELECT |
243
|
|
|
)); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** Flush all columns and use "true" as a value to avoid column conflicts and get actual result count */ |
247
|
|
|
$queryBuilder->column(true, true); |
|
|
|
|
248
|
|
|
|
249
|
|
|
$queryBuilderCount = $this |
250
|
|
|
->createQueryBuilder() |
251
|
|
|
->command($queryBuilder->commandData()) |
252
|
|
|
->column('COUNT(*)', true) |
253
|
|
|
->table(\sprintf('(%s) AS total_count', $queryBuilder->buildQuery()), true) |
254
|
|
|
->bind($queryBuilder->bindData()) |
|
|
|
|
255
|
|
|
; |
256
|
|
|
|
257
|
|
|
if ($toString === true) { |
258
|
|
|
return $queryBuilderCount->toString(); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$connection = $this->getConnection(); |
262
|
|
|
|
263
|
|
|
try { |
264
|
|
|
$statement = $this->prepareAndExecute($queryBuilderCount, $queryBuilderCount->bindData(), $connection); |
265
|
|
|
} catch (\Exception $e) { |
266
|
|
|
$this->rollBack($connection); |
267
|
|
|
|
268
|
|
|
throw $e; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $statement->fetchColumn(0); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param EntityInterface|null $entity |
276
|
|
|
* |
277
|
|
|
* @return QueryBuilderInterface |
278
|
|
|
*/ |
279
|
|
|
protected function createQueryBuilder(?EntityInterface $entity = null): BaseQueryBuilderInterface |
280
|
|
|
{ |
281
|
|
|
return (new QueryBuilder($this->createRepositoryCallClosure(), $entity)) |
282
|
|
|
->column(\sprintf('%s.*', $this->getEntityClassConstant(WriterInterface::CLASS_CONSTANT_TABLE_NAME))) |
283
|
|
|
->table($this->getEntityClassConstant(WriterInterface::CLASS_CONSTANT_TABLE_NAME)) |
284
|
|
|
; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param QueryBuilderInterface $queryBuilder |
289
|
|
|
* @param EntityInterface $entity |
290
|
|
|
* |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
protected function addEntityInsertQuery(QueryBuilderInterface $queryBuilder, EntityInterface $entity) |
294
|
|
|
{ |
295
|
|
|
$entityColumns = $entity->columns(); |
296
|
|
|
$entityData = &$entity->data(); |
297
|
|
|
|
298
|
|
|
foreach ($entityColumns as $column) { |
299
|
|
|
if (\array_key_exists($column, $entityData)) { |
|
|
|
|
300
|
|
|
$queryBuilder->value($column, $entityData[$column]); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param QueryBuilderInterface $queryBuilder |
309
|
|
|
* @param EntityInterface $entity |
310
|
|
|
* @param bool $toString |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
|
|
protected function addEntityUpdateQuery( |
315
|
|
|
QueryBuilderInterface $queryBuilder, |
316
|
|
|
EntityInterface $entity, |
317
|
|
|
bool $toString |
318
|
|
|
) { |
319
|
|
|
$performUpdate = false; |
320
|
|
|
$entityData = &$entity->data(); |
321
|
|
|
$entityDataOriginal = &$entity->dataOriginal(); |
322
|
|
|
|
323
|
|
|
if (false === $entity->isNew() || true === $entity->isSaved()) { |
324
|
|
|
foreach ($entity->primaryKeys() as $primaryKey) { |
325
|
|
|
if (isset($entityDataOriginal[$primaryKey])) { |
326
|
|
|
$queryBuilder->where( |
327
|
|
|
\sprintf('%s.%s = :%s_WhereUpdate', $entity::TABLE_NAME, $primaryKey, $primaryKey), |
328
|
|
|
[ |
329
|
|
|
\sprintf('%s_WhereUpdate', $primaryKey) => $entityDataOriginal[$primaryKey], |
330
|
|
|
] |
331
|
|
|
); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
} else { |
335
|
|
|
return $performUpdate; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
foreach ($entity->columns() as $column) { |
339
|
|
|
/** Updating only what is needed to update, for faster queries, skipping equal values */ |
340
|
|
|
if (!\array_key_exists($column, $entityDataOriginal) |
|
|
|
|
341
|
|
|
|| (isset($entityDataOriginal[$column]) && $entityDataOriginal[$column] == $entityData[$column]) |
342
|
|
|
) { |
343
|
|
|
continue; |
344
|
|
|
} elseif (false === $performUpdate) { |
345
|
|
|
$performUpdate = true; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$queryBuilder->set(\sprintf('%s.%s', $entity::TABLE_NAME, $column), $entityData[$column]); |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* If we want to get query as astring, we don't perform actions on original data as update won't be |
352
|
|
|
* performed! |
353
|
|
|
*/ |
354
|
|
|
if (false === $toString) { |
355
|
|
|
$entityDataOriginal[$column] = $entityData[$column]; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return $performUpdate; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param QueryBuilderInterface $queryBuilder |
364
|
|
|
* @param EntityInterface $entity |
365
|
|
|
* |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
|
|
protected function addEntityDeleteQuery(QueryBuilderInterface $queryBuilder, EntityInterface $entity) |
369
|
|
|
{ |
370
|
|
|
$performDelete = false; |
371
|
|
|
$entityData = $entity->data(); |
372
|
|
|
|
373
|
|
|
foreach ($entity->primaryKeys() as $primaryKey) { |
374
|
|
|
if (isset($entityData[$primaryKey]) && !\is_null($entityData[$primaryKey])) { |
375
|
|
|
if (false === $performDelete) { |
376
|
|
|
$performDelete = true; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$primaryKeyNormalised = \sprintf( |
380
|
|
|
'%s_Delete', |
381
|
|
|
\implode( |
382
|
|
|
'', |
383
|
|
|
\array_map( |
384
|
|
|
function ($columnPart) { |
385
|
|
|
return \mb_convert_case($columnPart, MB_CASE_TITLE); |
386
|
|
|
}, |
387
|
|
|
\array_merge( |
388
|
|
|
\explode('.', $entity::TABLE_NAME), |
389
|
|
|
[ |
390
|
|
|
$primaryKey, |
391
|
|
|
] |
392
|
|
|
) |
393
|
|
|
) |
394
|
|
|
) |
395
|
|
|
); |
396
|
|
|
|
397
|
|
|
$queryBuilder->where( |
398
|
|
|
\sprintf('%s.%s = :%s', $entity::TABLE_NAME, $primaryKey, $primaryKeyNormalised), |
399
|
|
|
[ |
400
|
|
|
$primaryKeyNormalised => $entityData[$primaryKey], |
401
|
|
|
] |
402
|
|
|
); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
return $performDelete; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @param QueryBuilderInterface $queryBuilder |
411
|
|
|
* @param int $pageSize |
412
|
|
|
* @param int $currentPage |
413
|
|
|
* |
414
|
|
|
* @return $this |
415
|
|
|
*/ |
416
|
|
|
protected function addPaginateQuery( |
417
|
|
|
BaseQueryBuilderInterface $queryBuilder, |
418
|
|
|
int $pageSize, |
419
|
|
|
int $currentPage |
420
|
|
|
): BaseAbstractRepository { |
421
|
|
|
$queryBuilder->limitWithOffset($pageSize, $pageSize * $currentPage - $pageSize); |
|
|
|
|
422
|
|
|
|
423
|
|
|
return $this; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @param QueryBuilderInterface $queryBuilder |
428
|
|
|
* @param bool $toString |
429
|
|
|
* |
430
|
|
|
* @return EntityInterface[] |
431
|
|
|
*/ |
432
|
|
|
protected function getPaginateResult(BaseQueryBuilderInterface $queryBuilder, bool $toString = false): array |
433
|
|
|
{ |
434
|
|
|
return $queryBuilder->find($toString); |
|
|
|
|
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|