1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Repository; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\Profiler; |
6
|
|
|
use Aura\SqlQuery\AbstractQuery; |
7
|
|
|
use Aura\SqlQuery\QueryFactory; |
8
|
|
|
use Ps2alerts\Api\Contract\ConfigAwareInterface; |
9
|
|
|
use Ps2alerts\Api\Contract\ConfigAwareTrait; |
10
|
|
|
use Ps2alerts\Api\Contract\DatabaseAwareInterface; |
11
|
|
|
use Ps2alerts\Api\Contract\DatabaseAwareTrait; |
12
|
|
|
use Ps2alerts\Api\Contract\RedisAwareInterface; |
13
|
|
|
use Ps2alerts\Api\Contract\RedisAwareTrait; |
14
|
|
|
use Ps2alerts\Api\Contract\UuidAwareInterface; |
15
|
|
|
use Ps2alerts\Api\Contract\UuidAwareTrait; |
16
|
|
|
|
17
|
|
|
abstract class AbstractEndpointRepository implements |
18
|
|
|
ConfigAwareInterface, |
19
|
|
|
DatabaseAwareInterface, |
20
|
|
|
RedisAwareInterface, |
21
|
|
|
UuidAwareInterface |
22
|
|
|
{ |
23
|
|
|
use ConfigAwareTrait; |
24
|
|
|
use DatabaseAwareTrait; |
25
|
|
|
use RedisAwareTrait; |
26
|
|
|
use UuidAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Determines the table that the DB is interfacing with |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
abstract public function getTable(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Determines the primary key of the table |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract public function getPrimaryKey(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determines the Result key of the table |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
abstract public function getResultKey(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Allows the ability to overload and swap the DB driver if required |
51
|
|
|
* |
52
|
|
|
* @return \Aura\Sql\ExtendedPdo |
53
|
|
|
*/ |
54
|
|
|
protected function getDbDriver() |
55
|
|
|
{ |
56
|
|
|
return $this->getDatabaseDriver(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Allows the ability to overload and swap the DB driver if required |
61
|
|
|
* |
62
|
|
|
* @return \Aura\Sql\ExtendedPdo |
63
|
|
|
*/ |
64
|
|
|
protected function getDbArchiveDriver() |
65
|
|
|
{ |
66
|
|
|
return $this->getDatabaseArchiveDriver(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Builds a new query factory ready for use with the QueryObjects |
71
|
|
|
* |
72
|
|
|
* @param string $type Type of query to use, such as Select, Update etc. |
73
|
|
|
* @param bool $newOnly Whether we're getting a new query or using a raw one |
74
|
|
|
* |
75
|
|
|
* @return \Aura\SqlQuery\QueryInterface |
76
|
|
|
*/ |
77
|
|
|
public function newQuery($type = 'single', $newOnly = false) |
78
|
|
|
{ |
79
|
|
|
$factory = new QueryFactory('mysql'); |
80
|
|
|
|
81
|
|
|
if ($type === 'single') { |
82
|
|
|
$query = $factory->newSelect(); |
83
|
|
|
|
84
|
|
|
if (!$newOnly) { |
85
|
|
|
$query->from($this->getTable()); |
86
|
|
|
} |
87
|
|
|
} elseif ($type === 'update') { |
88
|
|
|
$query = $factory->newUpdate(); |
89
|
|
|
} elseif ($type === 'delete') { |
90
|
|
|
$query = $factory->newDelete(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $query; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Executes the statement to the DB and returns the results |
98
|
|
|
* |
99
|
|
|
* @param \Aura\SqlQuery\AbstractQuery $query Query object to execute |
100
|
|
|
* @param boolean $single Return a sinlge record or not |
101
|
|
|
* @param boolean $object Return an object or not |
102
|
|
|
* @param boolean $archive Flag to force to use the archive database |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function fireStatementAndReturn( |
107
|
|
|
$query, |
108
|
|
|
$single = false, |
109
|
|
|
$object = false, |
110
|
|
|
$archive = false |
111
|
|
|
) { |
112
|
|
|
$pdo = $this->getDbDriver(); |
113
|
|
|
if ($archive === true) { |
114
|
|
|
// If we've been bounced back to search the archive, load that driver |
115
|
|
|
$pdo = $this->getDbArchiveDriver(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($this->getConfigItem('db_query_debug') === true) { |
119
|
|
|
$pdo->setProfiler(new Profiler); |
120
|
|
|
$pdo->getProfiler()->setActive(true); |
121
|
|
|
var_dump($pdo); |
|
|
|
|
122
|
|
|
var_dump($query->getStatement()); |
123
|
|
|
var_dump($query->getBindValues()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($single === false) { |
127
|
|
View Code Duplication |
if ($object === false) { |
|
|
|
|
128
|
|
|
$return = $pdo->fetchAll($query->getStatement(), $query->getBindValues()); |
129
|
|
|
} else { |
130
|
|
|
$return = $pdo->fetchObjects($query->getStatement(), $query->getBindValues()); |
131
|
|
|
} |
132
|
|
View Code Duplication |
} else { |
|
|
|
|
133
|
|
|
if ($object === false) { |
134
|
|
|
$return = $pdo->fetchOne($query->getStatement(), $query->getBindValues()); |
135
|
|
|
} else { |
136
|
|
|
$return = $pdo->fetchObject($query->getStatement(), $query->getBindValues()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($this->getConfigItem('db_query_debug') === true) { |
141
|
|
|
var_dump($pdo->getProfiler()->getProfiles()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// If no data, recall this function with archive flag |
145
|
|
|
if (empty($return) && $archive === false) { |
146
|
|
|
return $this->fireStatementAndReturn( |
147
|
|
|
$query, |
148
|
|
|
$single, |
149
|
|
|
$object, |
150
|
|
|
true |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $return; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Allows for Raw SQL firing without the query builder |
159
|
|
|
* |
160
|
|
|
* @param string $sql |
161
|
|
|
* @param boolean $single |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function readRaw($sql, $single = false) |
166
|
|
|
{ |
167
|
|
|
$pdo = $this->getDbDriver(); |
168
|
|
|
|
169
|
|
|
if ($single === false) { |
170
|
|
|
return $pdo->fetchAll($sql); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $pdo->fetchOne($sql); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Reads a single record from the database |
178
|
|
|
* |
179
|
|
|
* @param string $id |
180
|
|
|
* @param string $keyType |
181
|
|
|
* @param boolean $object |
182
|
|
|
* @param boolean $archive |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function readSinglebyId($id, $keyType = 'primary', $object = false, $archive = false) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$query = $this->newQuery(); |
189
|
|
|
$key = $this->returnKeyType($keyType); |
190
|
|
|
|
191
|
|
|
$query->cols(['*']) |
192
|
|
|
->where("{$key} = ?", $id); |
193
|
|
|
|
194
|
|
|
return $this->fireStatementAndReturn($query, true, $object, $archive); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Allows reading of elements by ID including those from the archive |
199
|
|
|
* |
200
|
|
|
* @param string $id |
201
|
|
|
* @param string $keyType |
202
|
|
|
* @param boolean $object |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function readSingleByIdWithArchive($id, $keyType = 'primary', $object = false) |
207
|
|
|
{ |
208
|
|
|
return array_merge( |
209
|
|
|
$this->readSingleById($id, $keyType, $object), |
210
|
|
|
$this->readSingleById($id, $keyType, $object, true) |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Reads all related records from the database |
216
|
|
|
* |
217
|
|
|
* @param string $id |
218
|
|
|
* @param string $keyType Field to search on |
219
|
|
|
* @param boolean $archive |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
public function readAllById($id, $keyType = 'primary', $archive = false) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$query = $this->newQuery(); |
226
|
|
|
$key = $this->returnKeyType($keyType); |
227
|
|
|
|
228
|
|
|
$query->cols(['*']) |
229
|
|
|
->where("{$key} = ?", $id); |
230
|
|
|
|
231
|
|
|
return $this->fireStatementAndReturn($query, false, false, $archive); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Grab all data from the database including from the archive |
236
|
|
|
* |
237
|
|
|
* @param string $id |
238
|
|
|
* @param string $keyType |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
public function readAllByIdWithArchive($id, $keyType = 'primary') |
243
|
|
|
{ |
244
|
|
|
return array_merge( |
245
|
|
|
$this->readAllById($id, $keyType), |
246
|
|
|
$this->readAllById($id, $keyType, true) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Reads all records based off a simple where statement |
252
|
|
|
* |
253
|
|
|
* @param array $fields |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
public function readAllByFields($fields) |
258
|
|
|
{ |
259
|
|
|
$query = $this->newQuery(); |
260
|
|
|
$query->cols(['*']); |
261
|
|
|
|
262
|
|
|
foreach ($fields as $field => $value) { |
263
|
|
|
$query->where("{$field} = ?", $value); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this->fireStatementAndReturn($query); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns all records with no filtering |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
public function readAll() |
275
|
|
|
{ |
276
|
|
|
$query = $this->newQuery(); |
277
|
|
|
$query->cols(['*']); |
278
|
|
|
|
279
|
|
|
return $this->fireStatementAndReturn($query); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Reads the count of records based off a where statement |
284
|
|
|
* |
285
|
|
|
* @param array $fields |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function readCountByFields($fields) |
290
|
|
|
{ |
291
|
|
|
$query = $this->newQuery(); |
292
|
|
|
$key = $this->returnKeyType('primary'); |
293
|
|
|
|
294
|
|
|
$query->cols(["COUNT({$key}) as COUNT"]); |
295
|
|
|
|
296
|
|
|
foreach ($fields as $field => $value) { |
297
|
|
|
$query->where("{$field} = ?", $value); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$result = $this->fireStatementAndReturn($query); |
301
|
|
|
|
302
|
|
|
// Done this to prevent the need for clients to also do this. Returns a single number this way. |
303
|
|
|
return $result[0]["COUNT"]; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Sets the proper key to search on based off a string |
308
|
|
|
* |
309
|
|
|
* @param string $key |
310
|
|
|
* |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
|
|
public function returnKeyType($key) |
314
|
|
|
{ |
315
|
|
|
switch ($key) { |
316
|
|
|
case 'result': |
317
|
|
|
return $this->getResultKey(); |
318
|
|
|
case 'primary': |
319
|
|
|
return $this->getPrimaryKey(); |
320
|
|
|
default: |
321
|
|
|
return $key; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Generates a quoted string which is appropiate for WHERE IN statements |
327
|
|
|
* |
328
|
|
|
* @param array $array |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
public function generateWhereInString(array $array) |
333
|
|
|
{ |
334
|
|
|
$pdo = $this->getDbDriver(); |
335
|
|
|
$string = "({$pdo->quote($array)})"; |
336
|
|
|
return $string; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: