-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao.class.php
417 lines (377 loc) · 18.1 KB
/
dao.class.php
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* MIT License
*
* Copyright (c) 2022 Luiz Ricardo de Lima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
class DAOException extends Exception
{
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}
class DAO
{
protected $dbh;
protected $entity;
protected $properties;
protected $key;
protected $keyIsAutogenerated;
private $delMethod;
private $deactivateProperty;
private $driver;
protected const DEL_METHOD = 'del-method';
protected const DEL_METHOD_DELETE = 'del-method-delete';
protected const DEL_METHOD_DEACTIVATE = 'del-method-deactivate';
protected const DEACTIVATE_PROPERTY = 'deactivate-property';
// TODO: new constructor is under construction
/**
* Class constructor.
*
* @param PDO $dbh The database connection object
* @param string $entity The name of the table this DAO represents
* @param array $properties An associative array whose keys are the column names and the
* values are PDO::PARAM_* constants according to each data type
* ({@link https://www.php.net/manual/en/pdo.constants.php}), for the correct
* mapping in the internally created queries
* @param array $settings An associative array with dditional settings. Possible entries are
* * 'automaticKey' => (boolean) indicates whether the key is autogenerated or not
* * 'deleteMethod' => 'delete' or 'deactivate'
* * 'deactivateProperty' => property which flags if an entry is deactivated or not
* * 'key' => property that is the primary key for the entity
*
* @link https://www.php.net/manual/en/pdo.constants.php
*/
function ___construct($dbh, $entity, $properties, $settings = null) {
// Validation
if ($dbh instanceof PDO === false)
throw new DAOException('The database connection is not valid.');
if (empty($entity))
throw new DAOException('Invalid name');
// TODO #1 DAOs without keys should be possible
// Setting id property and performing validation
$this->key = isset($settings['key']) ? $settings['key'] : 'id';
if(!isset($properties[$key]))
throw new DAOException('The key property '.$key.' is not defined in $properties array.');
$this->keyIsAutogenerated = isset($settings['automaticKey']) ? (bool) $settings['automaticKey'] : true;
$this->dbh = $dbh;
$this->entity = $entity;
$this->properties = $properties;
$this->driver = $dbh->getAttribute(PDO::ATTR_DRIVER_NAME);
// Loading default settiings
$this->delMethod = self::DEL_METHOD_DELETE;
$this->deactivateProperty = null;
// Loading settings
if (!empty($settings) && is_array($settings))
{
foreach($settings as $settingsKey => $settingsValue) switch ($settingsKey)
{
case 'deleteMethod':
if ($settingsValue == 'delete')
$this->delMethod = self::DEL_METHOD_DELETE;
else if ($settingsValue == 'deactivate')
$this->delMethod = self::DEL_METHOD_DEACTIVATE;
else
throw new DAOException('Invalid deleteMethod specified on $settings.');
break;
case 'deactivateProperty':
if(isset($properties[$settingsValue]))
$this->deactivateProperty = $settingsValue;
else
throw new DAOException('deactivateProperty specified on $settings does not exist in $properties.');
break;
default :
throw new DAOException('Invalid settings key: '.$settingsKey);
break;
}
}
}
function __construct($dbh, $entity, $properties, $key = 'id', $keyIsAutogenerated = true, $configuration = null) {
// Validation - The properties array must have the $key element set
if(!isset($properties[$key]))
throw new DAOException('The key property '.$key.' was not defined on $properties param.');
$this->dbh = $dbh;
$this->entity = $entity;
$this->properties = $properties;
$this->key = $key;
$this->keyIsAutogenerated = $keyIsAutogenerated;
$this->driver = $dbh->getAttribute(PDO::ATTR_DRIVER_NAME);
// Loading default configuration
$this->delMethod = self::DEL_METHOD_DELETE;
$this->deactivateProperty = null;
// Loading custom configuration, if set
if ($configuration !== null && is_array($configuration))
{
foreach($configuration as $configurationKey => $configurationValue) switch ($configurationKey)
{
case self::DEL_METHOD:
if (in_array($configurationValue, [self::DEL_METHOD_DELETE, self::DEL_METHOD_DEACTIVATE]))
$this->delMethod = $configurationValue;
else
throw new DAOException('Invalid DEL_METHOD specified on $configuration.');
break;
case self::DEACTIVATE_PROPERTY:
if(isset($properties[$configurationValue]))
$this->deactivateProperty = $configurationValue;
else
throw new DAOException('DEACTIVATE_PROPERTY specified on $configuration is not set on $properties.');
break;
}
}
}
/**
* @param array|null $filters - The filter for list. If null, no filtering will be performed.
* If not null, needs to be an array of filter objects. The filter object is an associative
* array with 3 keys: 'property', 'operator', 'value'. When $filters are set and the DAO class
* deletes by deactivating, the deactivate property needs to be included in the filter, or all
* the objects, included the deleted ones will be retrieved. If $filters is null, only the
* not deleted ones will be retrieved. Implemented operators: =, <, >, <=, >=, <>, !=, LIKE.
* Example: [['property' => 'id', 'operator'=>'=', 'value' => '42']]
*/
public function count($filters = null)
{
// Discarding array key information and retrieving an ordered array
if(is_array($filters)) $filters = array_values($filters);
$query = 'SELECT count(*) FROM '.$this->esc($this->entity);
$query.= $this->evalFilters($filters);
$stmt = $this->dbh->prepare($query);
if(is_array($filters)) foreach ($filters as $i => $filter) {
$stmt->bindValue($i+1, $filter['value'], $this->properties[$filter['property']]);
}
$stmt->execute();
return $stmt->fetchColumn();
}
public function create($object) {
if (!$this->keyIsAutogenerated && !isset($object[$this->key]))
throw new DAOException('Missing key property '.$this->key.' on create. This property is not autogenerated.');
// Exception: empty columns and values and sqlite
if (empty($object) && $this->driver == 'sqlite') {
$query = 'INSERT INTO '.$this->esc($this->entity).' DEFAULT VALUES';
$stmt = $this->dbh->prepare($query);
$stmt->execute();
return $this->dbh->lastInsertId();
}
$pos = 0;
$names = array();
$types = array();
$values = array();
$params = array();
foreach ($this->properties as $propertyName => $propertyType) {
// If keys are autogenerated, any input values for them will be ignored
if ($propertyName == $this->key && $this->keyIsAutogenerated)
continue;
if (array_key_exists($propertyName, $object)) {
$names[$pos] = $this->esc($propertyName);
$types[$pos] = $propertyType;
$values[$pos] = $object[$propertyName];
$params[$pos] = '?';
$pos++;
}
}
$query = 'INSERT INTO '.$this->esc($this->entity).' (';
$query.= implode(', ', $names);
$query.= ') VALUES (';
$query.= implode(', ', $params);
$query.= ')';
$stmt = $this->dbh->prepare($query);
for($i = 0; $i < $pos; $i++) {
$stmt->bindValue($i+1, $values[$i], $types[$i]);
}
$stmt->execute();
if ($this->keyIsAutogenerated)
return $this->dbh->lastInsertId();
else
return $object[$this->key];
}
public function del($key) {
switch ($this->delMethod) {
case self::DEL_METHOD_DELETE:
$stmt = $this->dbh->prepare(
'DELETE FROM '.$this->esc($this->entity).' WHERE '.$this->esc($this->key).' = ?');
$stmt->bindValue(1, $key, $this->properties[$this->key]);
$stmt->execute();
return true;
break;
case self::DEL_METHOD_DEACTIVATE:
$query = 'UPDATE '.$this->esc($this->entity);
$query.= ' SET '.$this->esc($this->deactivateProperty).' = 0';
$query.= ' WHERE '.$this->esc($this->key).' = ?';
$stmt = $this->dbh->prepare($query);
$stmt->bindValue(1, $key, $this->properties[$this->key]);
$stmt->execute();
return true;
break;
}
}
/**
* Escapes table names and column names in the pattern:
* table => "table"
* column => "column"
* table.column => "table"."column"
* table.* => "table".*
*/
protected function esc($name) {
$parts = explode('.', $name);
foreach ($parts as $i => $value) {
if ($value == '*') continue;
switch ($this->driver) {
case 'mysql':
$parts[$i] = "`$value`";
break;
default:
$parts[$i] = "\"$value\"";
break;
}
}
return implode('.', $parts);
}
public function exists($key) {
$filters = array();
$filters[] = ['property' => $this->key, 'operator'=>'=', 'value' => $key];
if ($this->delMethod == self::DEL_METHOD_DEACTIVATE)
$filters[] = ['property' => $this->deactivateProperty, 'operator'=>'=', 'value' => 1];
return (boolean) $this->count($filters);
}
/** $filters is a numeric-indexed array */
private function evalFilters($filters) {
$filterQuery = array();
if ($filters === null && $this->delMethod == self::DEL_METHOD_DEACTIVATE) {
$true = ($this->properties[$this->deactivateProperty] == PDO::PARAM_STR) ? "'1'" : "1";
return ' WHERE '.$this->esc($this->deactivateProperty).' = '.$true;
}
if (is_array($filters)) foreach ($filters as $key => $filter) {
if (!isset($filter['operator']))
throw new DAOException('Operator not specified for filter in position '.$key.'.');
else if (!in_array($filter['operator'], ['=','<','>','<=','>=','<>','!=','LIKE']))
throw new DAOException('Invalid or unimplemented operator '.$filter['operator'].' for filter in position '.$key.'.');
else if (!isset($filter['property']))
throw new DAOException('Missing \'property\' for filter in position '.$key.'.');
else if(!isset($this->properties[$filter['property']]))
throw new DAOException('The property '.$filter['property'].' for filter in position '.$key.' is not defined in the class.');
else if (!isset($filter['value']))
throw new DAOException('Missing \'value\' for filter in position '.$key.'.');
else { // all good!
$filterQuery[] = $this->esc($filter['property']).' '.$filter['operator'].' ?';
}
}
if (!empty($filterQuery)) return ' WHERE '.implode(' AND ', $filterQuery);
else return '';
}
/** $orderBy is a numeric-indexed array */
private function evalOrderBy($orderBy) {
$orderByQuery = array();
if (is_array($orderBy)) foreach ($orderBy as $key => $ob) {
if (!isset($ob['property']))
throw new DAOException('Missing \'property\' for orderBy in position '.$key.'.');
else if(!isset($this->properties[$ob['property']]))
throw new DAOException('The property '.$ob['property'].' for orderBy in position '.$key.' is not defined in the class.');
else if (isset($ob['direction']) && !in_array(strtolower($ob['direction']), ['asc', 'desc']))
throw new DAOException('Invalid \'direction\' for orderBy in position '.$key.'.');
else { // all good!
$direction = isset($ob['direction']) ? strtoupper($ob['direction']) : 'ASC';
$orderByQuery[] = $this->esc($ob['property']).' '.$direction;
}
}
if (!empty($orderByQuery)) return ' ORDER BY '.implode(', ', $orderByQuery);
else return '';
}
public function get($key) {
$stmt = $this->dbh->prepare(
'SELECT * FROM '.$this->esc($this->entity).' WHERE '.$this->esc($this->key).' = ?');
$stmt->bindValue(1, $key, $this->properties[$this->key]);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result ? $result : null;
}
/**
* @param array|null $filters - The filter for list. If null, no filtering will be performed.
* If not null, needs to be an array of filter objects. The filter object is an associative
* array with 3 keys: 'property', 'operator', 'value'. When $filters are set and the DAO class
* deletes by deactivating, the deactivate property needs to be included in the filter, or all
* the objects, included the deleted ones will be retrieved. If $filters is null, only the
* not deleted ones will be retrieved. Implemented operators: =, <, >, <=, >=, <>, !=, LIKE.
* Example: [['property' => 'id', 'operator'=>'=', 'value' => '42']]
*
* @param array|null $orderBy - The order by information, if any. If null, no ordering will be
* performed. If not null, it needs to be an indexed array (simple array) of orderby objects.
* The orderby object is an associative array with 2 keys: 'property', 'direction'.
* If 'direction' is not set, it will default to 'ASC'. 'direction' is case-insensitive.
* Example: [['property' => 'id', 'direction' => 'desc']]
*/
public function list($elementKeyAsArrayKey = true, $filters = null, $orderBy = null)
{
// Discarding array key information and retrieving an ordered array
if(is_array($filters)) $filters = array_values($filters);
$query = 'SELECT * FROM '.$this->esc($this->entity).' ';
$query.= $this->evalFilters($filters);
$query.= $this->evalOrderBy($orderBy);
$stmt = $this->dbh->prepare($query);
if(is_array($filters)) foreach ($filters as $i => $filter) {
$stmt->bindValue($i+1, $filter['value'], $this->properties[$filter['property']]);
}
$stmt->execute();
$result = array();
while ($object = $stmt->fetch(PDO::FETCH_ASSOC)) {
if($elementKeyAsArrayKey)
$result[$object[$this->key]] = $object;
else
$result[] = $object;
}
return $result;
}
public function update($object) {
if (!isset($object[$this->key]))
throw new DAOException('Missing key property '.$this->key.' on update.');
$pos = 0;
$types = array();
$values = array();
$params = array();
foreach ($this->properties as $propertyName => $propertyType) {
if ($propertyName == $this->key) continue;
if (array_key_exists($propertyName, $object)) {
$types[$pos] = $propertyType;
$values[$pos] = $object[$propertyName];
$params[$pos] = $this->esc($propertyName).' = ?';
$pos++;
}
}
$query = 'UPDATE '.$this->esc($this->entity).' SET ';
$query.= implode(', ', $params);
$query.= ' WHERE '.$this->esc($this->key).' = ?';
$stmt = $this->dbh->prepare($query);
for($i = 0; $i < $pos; $i++) {
$stmt->bindValue($i+1, $values[$i], $types[$i]);
}
$stmt->bindValue($pos+1, $object[$this->key], $this->properties[$this->key]);
$stmt->execute();
return true;
}
public function updateKey($oldkey, $newkey) {
$query = 'UPDATE '.$this->esc($this->entity);
$query.= ' SET '.$this->esc($this->key).' = ?';
$query.= ' WHERE '.$this->esc($this->key).' = ?';
$stmt = $this->dbh->prepare($query);
$stmt->bindValue(1, $newkey, $this->properties[$this->key]);
$stmt->bindValue(2, $oldkey, $this->properties[$this->key]);
$stmt->execute();
return true;
}
}