1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Repository; |
4
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Connection\ConnectionInterface; |
6
|
|
|
use Janisbiz\LightOrm\Entity\EntityInterface; |
7
|
|
|
use Janisbiz\LightOrm\ConnectionPool; |
8
|
|
|
use Janisbiz\LightOrm\Generator\Writer\WriterInterface; |
9
|
|
|
use Janisbiz\LightOrm\Paginator\Paginator; |
10
|
|
|
use Janisbiz\LightOrm\Paginator\PaginatorInterface; |
11
|
|
|
use Janisbiz\LightOrm\QueryBuilder\QueryBuilderInterface; |
12
|
|
|
use Psr\Log\LoggerAwareInterface; |
13
|
|
|
use Psr\Log\LoggerAwareTrait; |
14
|
|
|
use Psr\Log\LogLevel; |
15
|
|
|
|
16
|
|
|
abstract class AbstractRepository implements RepositoryInterface, LoggerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use LoggerAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ConnectionPool |
22
|
|
|
*/ |
23
|
|
|
protected $connectionPool; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var null|bool |
27
|
|
|
*/ |
28
|
|
|
protected $commit; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param null|double|int|string $value |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
* @throws RepositoryException |
35
|
|
|
*/ |
36
|
|
|
protected function quote($value) |
37
|
|
|
{ |
38
|
|
|
switch ($phpParamType = \mb_strtolower(\gettype($value))) { |
39
|
|
|
case 'null': |
40
|
|
|
$pdoParamType = \PDO::PARAM_NULL; |
41
|
|
|
|
42
|
|
|
break; |
43
|
|
|
|
44
|
|
|
case 'integer': |
45
|
|
|
$pdoParamType = \PDO::PARAM_INT; |
46
|
|
|
|
47
|
|
|
break; |
48
|
|
|
|
49
|
|
|
case 'double': |
50
|
|
|
case 'string': |
51
|
|
|
$pdoParamType = \PDO::PARAM_STR; |
52
|
|
|
|
53
|
|
|
break; |
54
|
|
|
|
55
|
|
|
case 'boolean': |
56
|
|
|
$pdoParamType = \PDO::PARAM_BOOL; |
57
|
|
|
|
58
|
|
|
break; |
59
|
|
|
|
60
|
|
|
default: |
61
|
|
|
throw new RepositoryException(\sprintf( |
62
|
|
|
'Parameter type "%s" could not be quoted for SQL execution!', |
63
|
|
|
$phpParamType |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->getConnection()->quote($value, $pdoParamType); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param QueryBuilderInterface $queryBuilder |
72
|
|
|
* @param int $pageSize |
73
|
|
|
* @param int $currentPage |
74
|
|
|
* @param array $options |
75
|
|
|
* |
76
|
|
|
* @return PaginatorInterface |
77
|
|
|
*/ |
78
|
|
|
protected function paginator( |
79
|
|
|
QueryBuilderInterface $queryBuilder, |
80
|
|
|
int $pageSize, |
81
|
|
|
int $currentPage = 1, |
82
|
|
|
array $options = [] |
83
|
|
|
): PaginatorInterface { |
84
|
|
|
if ($pageSize < 1) { |
85
|
|
|
$pageSize = 1; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($currentPage < 1) { |
89
|
|
|
$currentPage = 1; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new Paginator( |
93
|
|
|
$queryBuilder, |
94
|
|
|
function (QueryBuilderInterface $queryBuilder, $pageSize, $currentPage) { |
95
|
|
|
$this->addPaginateQuery($queryBuilder, $pageSize, $currentPage); |
96
|
|
|
}, |
97
|
|
|
function (QueryBuilderInterface $queryBuilder, $toString) { |
98
|
|
|
return $this->getPaginateResult($queryBuilder, $toString); |
99
|
|
|
}, |
100
|
|
|
$pageSize, |
101
|
|
|
$currentPage, |
102
|
|
|
$options |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $level |
108
|
|
|
* @param string $message |
109
|
|
|
* @param array $context |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
protected function log(string $level, string $message, array $context = []): AbstractRepository |
114
|
|
|
{ |
115
|
|
|
if (null === $this->logger) { |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->logger->log($level, $message, $context); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ConnectionInterface|null $connection |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
protected function beginTransaction(ConnectionInterface $connection = null): AbstractRepository |
130
|
|
|
{ |
131
|
|
|
if (null === $connection) { |
132
|
|
|
$connection = $this->getConnection(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (true === ($this->commit = !$connection->inTransaction())) { |
|
|
|
|
136
|
|
|
$connection->beginTransaction(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param ConnectionInterface|null $connection |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
protected function commit(ConnectionInterface $connection = null): bool |
148
|
|
|
{ |
149
|
|
|
if (null === $connection) { |
150
|
|
|
$connection = $this->getConnection(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (true === $this->commit) { |
154
|
|
|
return $connection->commit(); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param ConnectionInterface|null $connection |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
protected function rollback(ConnectionInterface $connection = null): AbstractRepository |
166
|
|
|
{ |
167
|
|
|
if (null === $connection) { |
168
|
|
|
$connection = $this->getConnection(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($connection->inTransaction()) { |
172
|
|
|
$connection->rollBack(); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param QueryBuilderInterface $queryBuilder |
180
|
|
|
* @param array $bindData |
181
|
|
|
* @param ConnectionInterface|null $connection |
182
|
|
|
* |
183
|
|
|
* @return \PDOStatement |
184
|
|
|
*/ |
185
|
|
|
protected function prepareAndExecute( |
186
|
|
|
QueryBuilderInterface $queryBuilder, |
187
|
|
|
array $bindData, |
188
|
|
|
ConnectionInterface $connection = null |
189
|
|
|
): \PDOStatement { |
190
|
|
|
if (null === $connection) { |
191
|
|
|
$connection = $this->getConnection(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$statement = $connection->prepare($queryBuilder->buildQuery()); |
|
|
|
|
195
|
|
|
$statement->execute($bindData); |
196
|
|
|
|
197
|
|
|
$this->log( |
198
|
|
|
LogLevel::DEBUG, |
199
|
|
|
'Execute query "{query}" with parameters "{parameters}".', |
200
|
|
|
[ |
201
|
|
|
'query' => $queryBuilder->buildQuery(), |
202
|
|
|
'parameters' => $bindData, |
203
|
|
|
] |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
return $statement; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return ConnectionInterface |
211
|
|
|
*/ |
212
|
|
|
protected function getConnection(): ConnectionInterface |
213
|
|
|
{ |
214
|
|
|
if (null === $this->connectionPool) { |
215
|
|
|
$this->connectionPool = new ConnectionPool(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this->connectionPool->getConnection( |
219
|
|
|
$this->getEntityClassConstant(WriterInterface::CLASS_CONSTANT_DATABASE_NAME) |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $constant |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
protected function getEntityClassConstant($constant): string |
229
|
|
|
{ |
230
|
|
|
return \constant(\sprintf('%s::%s', $this->getEntityClass(), $constant)); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return \Closure |
235
|
|
|
*/ |
236
|
|
|
protected function createRepositoryCallClosure(): \Closure |
237
|
|
|
{ |
238
|
|
|
return function ($methodName, QueryBuilderInterface $queryBuilder, $toString) { |
239
|
|
|
return $this->$methodName($queryBuilder, $toString); |
240
|
|
|
}; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param EntityInterface|null $entity |
245
|
|
|
* |
246
|
|
|
* @return QueryBuilderInterface |
247
|
|
|
*/ |
248
|
|
|
abstract protected function createQueryBuilder(EntityInterface $entity = null): QueryBuilderInterface; |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
abstract protected function getEntityClass(): string; |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param QueryBuilderInterface $queryBuilder |
257
|
|
|
* @param int $pageSize |
258
|
|
|
* @param int $currentPage |
259
|
|
|
* |
260
|
|
|
* @return $this |
261
|
|
|
*/ |
262
|
|
|
abstract protected function addPaginateQuery( |
263
|
|
|
QueryBuilderInterface $queryBuilder, |
264
|
|
|
int $pageSize, |
265
|
|
|
int $currentPage |
266
|
|
|
): AbstractRepository; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param QueryBuilderInterface $queryBuilder |
270
|
|
|
* @param bool $toString |
271
|
|
|
* |
272
|
|
|
* @return EntityInterface[] |
273
|
|
|
*/ |
274
|
|
|
abstract protected function getPaginateResult(QueryBuilderInterface $queryBuilder, bool $toString = false): array; |
275
|
|
|
} |
276
|
|
|
|