1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
<?php
namespace ORM;
use Database;
abstract class Repository {
protected $Database;
protected $entities = [];
abstract public static function getTableName(): string;
abstract public static function getClassName(): string;
public function __construct(Database $Database) {
$this->Database = $Database;
}
#===============================================================================
# Adds an entity to the runtime cache
#===============================================================================
protected function storeInstance(int $identifier, EntityInterface $Entity) {
return $this->entities[$identifier] = $Entity;
}
#===============================================================================
# Adds an array of entities to the runtime cache
#===============================================================================
protected function storeMultipleInstances(array $entities) {
foreach($entities as $Entity) {
$this->storeInstance($Entity->getID(), $Entity);
}
return $entities;
}
#===============================================================================
# Gets an entity from the runtime cache
#===============================================================================
protected function fetchInstance(int $identifier) {
return $this->entities[$identifier] ?? FALSE;
}
#===============================================================================
# Removes an entity from the runtime cache
#===============================================================================
protected function removeInstance(int $identifier) {
if(isset($this->cache[$identifier])) {
unset($this->cache[$identifier]);
}
}
#===========================================================================
# Insert entity
#===========================================================================
public function insert(EntityInterface $Entity): bool {
$attributes = $Entity->getFilteredAttributes();
foreach($attributes as $field => $value) {
$fields[] = $field;
$values[] = '?';
}
$fields = implode(', ', $fields ?? []);
$values = implode(', ', $values ?? []);
$query = 'INSERT INTO %s (%s) VALUES(%s)';
$query = sprintf($query, static::getTableName(), $fields, $values);
$Statement = $this->Database->prepare($query);
return $Statement->execute(array_values($attributes));
}
#===========================================================================
# Update entity
#===========================================================================
public function update(EntityInterface $Entity): bool {
$attributes = $Entity->getFilteredAttributes();
foreach($attributes as $field => $value) {
$params[] = "$field = ?";
}
$params = implode(', ', $params ?? []);
$query = 'UPDATE %s SET %s WHERE id = '.intval($Entity->getID());
$query = sprintf($query, static::getTableName(), $params);
$Statement = $this->Database->prepare($query);
return $Statement->execute(array_values($attributes));
}
#===========================================================================
# Delete entity
#===========================================================================
public function delete(EntityInterface $Entity): bool {
$query = 'DELETE FROM %s WHERE id = ?';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->prepare($query);
return $Statement->execute([$Entity->getID()]);
}
#===========================================================================
# Find entity based on primary key
#===========================================================================
public function find(int $id): ?EntityInterface {
if($Entity = $this->fetchInstance($id)) {
return $Entity;
}
return $this->findBy('id', $id);
}
#===============================================================================
# Find entity based on specific field comparison
#===============================================================================
public function findBy(string $field, $value): ?EntityInterface {
$query = 'SELECT * FROM %s WHERE %s = ?';
$query = sprintf($query, static::getTableName(), $field);
$Statement = $this->Database->prepare($query);
$Statement->execute([$value]);
if($Entity = $Statement->fetchObject(static::getClassName())) {
$this->storeInstance($Entity->getID(), $Entity);
return $Entity;
}
return NULL;
}
#===============================================================================
# Find previous entity
#===============================================================================
public function findPrev(EntityInterface $Entity): ?EntityInterface {
$query = 'SELECT * FROM %s WHERE time_insert < ? ORDER BY time_insert DESC LIMIT 1';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->prepare($query);
$Statement->execute([$Entity->get('time_insert')]);
if($Entity = $Statement->fetchObject(static::getClassName())) {
$this->storeInstance($Entity->getID(), $Entity);
return $Entity;
}
return NULL;
}
#===============================================================================
# Find next entity
#===============================================================================
public function findNext(EntityInterface $Entity): ?EntityInterface {
$query = 'SELECT * FROM %s WHERE time_insert > ? ORDER BY time_insert ASC LIMIT 1';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->prepare($query);
$Statement->execute([$Entity->get('time_insert')]);
if($Entity = $Statement->fetchObject(static::getClassName())) {
$this->storeInstance($Entity->getID(), $Entity);
return $Entity;
}
return NULL;
}
#===========================================================================
# Find last (which means the newest) entity
#===========================================================================
public function getLast(): ?EntityInterface {
$query = 'SELECT * FROM %s ORDER BY time_insert DESC LIMIT 1';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->query($query);
if($Entity = $Statement->fetchObject(static::getClassName())) {
$this->storeInstance($Entity->getID(), $Entity);
return $Entity;
}
return NULL;
}
#===========================================================================
# Get entity count
#===========================================================================
public function getCount(): int {
$query = 'SELECT COUNT(id) FROM %s';
$query = sprintf($query, static::getTableName());
return $this->Database->query($query)->fetchColumn();
}
#===========================================================================
# Get paginated entity list
#===========================================================================
public function getPaginated(string $order, int $limit, int $offset = 0): array {
return $this->getAll([], $order, "$offset,$limit");
}
#===========================================================================
# Get all entities
#===========================================================================
public function getAll(array $filter = [], string $order = null, string $limit = null): array {
$select = 'SELECT * FROM '.static::getTableName();
$wheres = [];
$params = [];
if(!empty($filter)) {
foreach($filter as $column => $value) {
$wheres[] = "$column = ?";
$params[] = $value;
}
$where = 'WHERE '.implode(' AND ', $wheres);
}
if($order) {
$order = "ORDER BY $order";
}
if($limit) {
$limit = "LIMIT $limit";
}
$query = "$select %s %s %s";
$query = sprintf($query, $where ?? '', $order ?? '', $limit ?? '');
$Statement = $this->Database->prepare($query);
$Statement->execute($params);
if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
$this->storeMultipleInstances($entities);
return $entities;
}
return [];
}
#===============================================================================
# Get entities based on search query
#===============================================================================
public function search(string $search, array $filter = []): array {
if($search === '*') {
return $this->getAll([], NULL, 20);
}
if(strlen($filter['year'] ?? '') !== 0) {
$extend[] = 'YEAR(time_insert) = ? AND';
$params[] = $filter['year'];
}
if(strlen($filter['month'] ?? '') !== 0) {
$extend[] = 'MONTH(time_insert) = ? AND';
$params[] = $filter['month'];
}
if(strlen($filter['day'] ?? '') !== 0) {
$extend[] = 'DAY(time_insert) = ? AND';
$params[] = $filter['day'];
}
$dateparts = implode(' ', $extend ?? []);
$query = 'SELECT * FROM %s WHERE %s MATCH(name, body)
AGAINST(? IN BOOLEAN MODE) LIMIT 20';
$query = sprintf($query, static::getTableName(), $dateparts);
$Statement = $this->Database->prepare($query);
$Statement->execute(array_merge($params ?? [], [$search]));
if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
$this->storeMultipleInstances($entities);
return $entities;
}
return [];
}
#===============================================================================
# Get a list of distinct days
#===============================================================================
public function getDistinctDays(): array {
$query = 'SELECT DISTINCT DAY(time_insert) AS d FROM %s ORDER BY d';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->query($query);
if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
return $result;
}
return [];
}
#===============================================================================
# Get a list of distinct months
#===============================================================================
public function getDistinctMonths(): array {
$query = 'SELECT DISTINCT MONTH(time_insert) AS m FROM %s ORDER BY m';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->query($query);
if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
return $result;
}
return [];
}
#===============================================================================
# Get a list of distinct years
#===============================================================================
public function getDistinctYears(): array {
$query = 'SELECT DISTINCT YEAR(time_insert) AS y FROM %s ORDER BY y';
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->query($query);
if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) {
return $result;
}
return [];
}
}
|