1
|
|
|
<?php |
2
|
|
|
namespace BMorais\Database; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* CLASS TRAIT DATALAYER |
6
|
|
|
* This class of execution methods in the database |
7
|
|
|
* |
8
|
|
|
* @author Bruno Morais <[email protected]> |
9
|
|
|
* @copyright MIT, bmorais.com |
10
|
|
|
* @package bmorais\database |
11
|
|
|
* @subpackage class |
12
|
|
|
* @access private |
13
|
|
|
*/ |
14
|
|
|
use PDO; |
15
|
|
|
use PDOException; |
16
|
|
|
use PDOStatement; |
17
|
|
|
use stdClass; |
18
|
|
|
|
19
|
|
|
trait DatalayerTrait |
20
|
|
|
{ |
21
|
|
|
/** @var PDO|null |
22
|
|
|
* @deprecated |
23
|
|
|
* */ |
24
|
|
|
protected $instance = null; |
25
|
|
|
|
26
|
|
|
/** @var string |
27
|
|
|
* @deprecated |
28
|
|
|
*/ |
29
|
|
|
protected string $fields; |
30
|
|
|
|
31
|
|
|
/** @var PDOStatement|null |
32
|
|
|
* @deprecated */ |
33
|
|
|
protected $prepare = null; |
34
|
|
|
|
35
|
|
|
/** @var string |
36
|
|
|
* @deprecated |
37
|
|
|
*/ |
38
|
|
|
protected $database = CONFIG_DATA_LAYER["dbname"]; |
39
|
|
|
|
40
|
|
|
/** @var string |
41
|
|
|
* @deprecated |
42
|
|
|
*/ |
43
|
|
|
protected $classModel; |
44
|
|
|
|
45
|
|
|
/** @var string |
46
|
|
|
* @deprecated |
47
|
|
|
*/ |
48
|
|
|
protected $tableName; |
49
|
|
|
|
50
|
|
|
/** @var string */ |
51
|
|
|
private $tableAlias; |
52
|
|
|
|
53
|
|
|
/** @var array |
54
|
|
|
*/ |
55
|
|
|
private array $resultArray = []; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
private $logSQL; |
59
|
|
|
|
60
|
|
|
/** @var PDOException */ |
61
|
|
|
private $error; |
62
|
|
|
|
63
|
|
|
/** @var string */ |
64
|
|
|
private string $query = ""; |
65
|
|
|
|
66
|
|
|
/** @var array */ |
67
|
|
|
private array $params = []; |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** @return PDO|false */ |
71
|
|
|
private function getConnect() |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
75
|
|
|
$database = $this->getDatabase().ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
76
|
|
|
$this->setDatabase($database); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (empty($this->instance)) { |
|
|
|
|
80
|
|
|
$this->instance = new PDO( |
|
|
|
|
81
|
|
|
CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->getDatabase() . ';port=' . CONFIG_DATA_LAYER['port'], |
82
|
|
|
CONFIG_DATA_LAYER['username'], |
83
|
|
|
CONFIG_DATA_LAYER['passwd'], |
84
|
|
|
CONFIG_DATA_LAYER['options'] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->instance; |
|
|
|
|
89
|
|
|
} catch (PDOException $e) { |
90
|
|
|
$this->setError($e); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param ?PDO $pdo |
97
|
|
|
* @return Crud |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
|
|
protected function setInstance(?PDO $pdo): self |
101
|
|
|
{ |
102
|
|
|
$this->instance = $pdo; |
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getInstance(): PDO |
107
|
|
|
{ |
108
|
|
|
return $this->getConnect(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $database |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
protected function setDatabase(string $database): self |
116
|
|
|
{ |
117
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
118
|
|
|
$database = $database.ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
119
|
|
|
$this->database = $database; |
|
|
|
|
120
|
|
|
} else { |
121
|
|
|
$this->database = $database; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!empty($this->instance)){ |
|
|
|
|
125
|
|
|
$this->executeSQL("USE {$this->getDatabase()}"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
protected function getDatabase(): string |
135
|
|
|
{ |
136
|
|
|
return $this->database ?? ""; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $fields |
141
|
|
|
* @return Crud |
142
|
|
|
*/ |
143
|
|
|
protected function setFields(string $fields): self |
144
|
|
|
{ |
145
|
|
|
$this->fields = $fields; |
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getFields():string |
153
|
|
|
{ |
154
|
|
|
return $this->fields; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $tableName |
159
|
|
|
* @param string $tableAlias |
160
|
|
|
* @return CrudBuilder|Crud|DatalayerTrait |
161
|
|
|
*/ |
162
|
|
|
protected function setTable(string $tableName, string $tableAlias = ""): self |
163
|
|
|
{ |
164
|
|
|
if (!empty($tableAlias)) |
165
|
|
|
$this->tableAlias = $tableAlias; |
166
|
|
|
$this->tableName = $tableName; |
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
protected function getTable(): string |
174
|
|
|
{ |
175
|
|
|
return $this->tableName ?? ""; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function getTableAlias(): string |
179
|
|
|
{ |
180
|
|
|
return $this->tableAlias ?? ""; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $classModel |
185
|
|
|
* @return Crud |
186
|
|
|
*/ |
187
|
|
|
protected function setClassModel(string $classModel): self |
188
|
|
|
{ |
189
|
|
|
$this->classModel = $classModel; |
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function getClassModel(): string |
194
|
|
|
{ |
195
|
|
|
return $this->classModel; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $classModel |
200
|
|
|
* @return Crud |
201
|
|
|
*/ |
202
|
|
|
protected function setPrepare(PDOStatement $prepare): self |
203
|
|
|
{ |
204
|
|
|
$this->prepare = $prepare; |
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function getPrepare(): PDOStatement |
209
|
|
|
{ |
210
|
|
|
return $this->prepare; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $params |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
|
|
protected function setParameter($params): self |
218
|
|
|
{ |
219
|
|
|
$this->params = array_merge($this->params, $params); |
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $params |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
protected function getParameter(): array |
228
|
|
|
{ |
229
|
|
|
return $this->params; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function getResult(): array |
233
|
|
|
{ |
234
|
|
|
return $this->resultArray; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function setResult(array $array): self |
238
|
|
|
{ |
239
|
|
|
$this->resultArray = $array; |
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $query |
245
|
|
|
* @param array|null $params |
246
|
|
|
* @return PDOStatement|void |
247
|
|
|
*/ |
248
|
|
|
protected function executeSQL(string $query, ?array $params = null) |
249
|
|
|
{ |
250
|
|
|
try { |
251
|
|
|
$this->setPrepare($this->getInstance()->prepare($query)); |
252
|
|
|
$this->setSQL($query, $params); |
253
|
|
|
$this->getPrepare()->execute($params); |
254
|
|
|
return $this->getPrepare(); |
255
|
|
|
} catch (PDOException $e) { |
256
|
|
|
$this->setError($e); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param $prepare |
262
|
|
|
* @return int|false |
263
|
|
|
*/ |
264
|
|
|
protected function count(PDOStatement $prepare = null): ?int |
265
|
|
|
{ |
266
|
|
|
try { |
267
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
268
|
|
|
return $prepare->rowCount(); |
269
|
|
|
} catch (PDOException $e) { |
270
|
|
|
$this->setError($e);} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param PDOStatement|null $prepare |
275
|
|
|
* @return array|null |
276
|
|
|
*/ |
277
|
|
|
protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
278
|
|
|
{ |
279
|
|
|
try { |
280
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
281
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
282
|
|
|
$this->setResult($dados); |
283
|
|
|
return $dados; |
284
|
|
|
} catch (PDOException $e) { |
285
|
|
|
$this->setError($e); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param $prepare |
291
|
|
|
* @return array|false |
292
|
|
|
*/ |
293
|
|
|
protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
294
|
|
|
{ |
295
|
|
|
try { |
296
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
297
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_OBJ); |
298
|
|
|
$this->setResult($dados); |
299
|
|
|
return $dados; |
300
|
|
|
} catch (PDOException $e) { |
301
|
|
|
$this->setError($e); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param $prepare |
307
|
|
|
* @param String|null $classModel |
308
|
|
|
* @return array|false |
309
|
|
|
*/ |
310
|
|
|
protected function fetchArrayClass(PDOStatement $prepare = null, string $classModel = null): ?array |
311
|
|
|
{ |
312
|
|
|
try { |
313
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
314
|
|
|
$classModel = empty($classModel) ? $this->getClassModel() : $classModel; |
315
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel); |
316
|
|
|
$this->setResult($dados); |
317
|
|
|
return $dados; |
318
|
|
|
} catch (PDOException $e) { |
319
|
|
|
$this->setError($e); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param $prepare |
325
|
|
|
* @return array|false |
326
|
|
|
*/ |
327
|
|
|
protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
328
|
|
|
{ |
329
|
|
|
try { |
330
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
331
|
|
|
return $prepare->fetch(PDO::FETCH_ASSOC); |
332
|
|
|
} catch (PDOException $e) { |
333
|
|
|
$this->setError($e); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param PDOStatement|null $prepare |
339
|
|
|
* @return stdClass|null |
340
|
|
|
*/ |
341
|
|
|
protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
342
|
|
|
{ |
343
|
|
|
try { |
344
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
345
|
|
|
return $prepare->fetch(PDO::FETCH_OBJ); |
346
|
|
|
} catch (PDOException $e) { |
347
|
|
|
$this->setError($e); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param $prepare |
353
|
|
|
* @param String|null $class |
354
|
|
|
* @return array|false |
355
|
|
|
*/ |
356
|
|
|
protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
357
|
|
|
{ |
358
|
|
|
try { |
359
|
|
|
$prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
360
|
|
|
$class = empty($class) ? $this->getClassModel() : $class; |
361
|
|
|
return $prepare->fetchObject(CONFIG_DATA_LAYER["directory_models"] . $class); |
362
|
|
|
} catch (PDOException $e) { |
363
|
|
|
$this->setError($e); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @return bool |
369
|
|
|
*/ |
370
|
|
|
protected function beginTrasaction(): ?bool |
371
|
|
|
{ |
372
|
|
|
try { |
373
|
|
|
$this->getInstance()->beginTransaction(); |
374
|
|
|
return true; |
375
|
|
|
} catch (PDOException $e) { |
376
|
|
|
$this->setError($e); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @return bool|null |
383
|
|
|
*/ |
384
|
|
|
protected function commitTransaction(): ?bool |
385
|
|
|
{ |
386
|
|
|
try { |
387
|
|
|
$this->getInstance()->commit(); |
388
|
|
|
return true; |
389
|
|
|
} catch (PDOException $e) { |
390
|
|
|
$this->setError($e); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @return bool|null |
396
|
|
|
* |
397
|
|
|
*/ |
398
|
|
|
protected function rollBackTransaction(): ?bool |
399
|
|
|
{ |
400
|
|
|
|
401
|
|
|
try { |
402
|
|
|
$this->getInstance()->rollBack(); |
403
|
|
|
return true; |
404
|
|
|
} catch (PDOException $e) { |
405
|
|
|
$this->setError($e); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @return string|null |
411
|
|
|
* */ |
412
|
|
|
private function lastId(): ?string |
413
|
|
|
{ |
414
|
|
|
try { |
415
|
|
|
return $this->getInstance()->lastInsertId(); |
416
|
|
|
} catch (PDOException $e) { |
417
|
|
|
$this->setError($e); |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param $sql_string |
423
|
|
|
* @param array|null $params |
424
|
|
|
* @return void |
425
|
|
|
*/ |
426
|
|
|
private function setSQL($sql_string, ?array $params = null) |
427
|
|
|
{ |
428
|
|
|
try { |
429
|
|
|
if (!empty($params)) { |
430
|
|
|
$indexed = $params == array_values($params); |
431
|
|
|
foreach ($params as $k => $v) { |
432
|
|
|
if (is_object($v)) { |
433
|
|
|
if ($v instanceof \DateTime) { |
434
|
|
|
$v = $v->format('Y-m-d H:i:s'); |
435
|
|
|
} else { |
436
|
|
|
continue; |
437
|
|
|
} |
438
|
|
|
} elseif (is_string($v)) { |
439
|
|
|
$v = "'$v'"; |
440
|
|
|
} elseif ($v === null) { |
441
|
|
|
$v = 'NULL'; |
442
|
|
|
} elseif (is_array($v)) { |
443
|
|
|
$v = implode(',', $v); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
if ($indexed) { |
447
|
|
|
$sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
448
|
|
|
} else { |
449
|
|
|
if ($k[0] != ':') { |
450
|
|
|
$k = ':' . $k; |
451
|
|
|
} //add leading colon if it was left out |
452
|
|
|
$sql_string = str_replace($k, $v, $sql_string); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
$this->logSQL = $sql_string; |
457
|
|
|
} catch (PDOException $e) { |
458
|
|
|
$this->setError($e); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @return string|null |
464
|
|
|
*/ |
465
|
|
|
protected function getSQL(): ?string |
466
|
|
|
{ |
467
|
|
|
try { |
468
|
|
|
return $this->logSQL ?? ""; |
469
|
|
|
} catch (\PDOException $e) { |
|
|
|
|
470
|
|
|
$this->setError($e); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @param PDOException $e |
476
|
|
|
* @return void |
477
|
|
|
*/ |
478
|
|
|
private function setError(PDOException $e) |
479
|
|
|
{ |
480
|
|
|
$this->error = $e; |
481
|
|
|
throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getSQL()}"); |
482
|
|
|
|
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|