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
|
|
|
protected $instance = null; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $fields; |
26
|
|
|
|
27
|
|
|
/** @var PDOStatement|null */ |
28
|
|
|
protected $prepare = null; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $database = CONFIG_DATA_LAYER["dbname"]; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $classModel; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $tableName; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
private $tableAlias; |
41
|
|
|
|
42
|
|
|
/** @var array */ |
43
|
|
|
protected $resultArray = array(); |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
private $logSQL; |
47
|
|
|
|
48
|
|
|
/** @var PDOException */ |
49
|
|
|
private $error; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $query = ""; |
53
|
|
|
|
54
|
|
|
/** @var array */ |
55
|
|
|
private $params = []; |
56
|
|
|
|
57
|
|
|
private QueryBuilder $queryBuild; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** @return PDO|false */ |
61
|
|
|
private function getInstance() |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
65
|
|
|
$this->database .= ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (empty($this->instance)) { |
69
|
|
|
$this->instance = new PDO( |
70
|
|
|
CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->database . ';port=' . CONFIG_DATA_LAYER['port'], |
71
|
|
|
CONFIG_DATA_LAYER['username'], |
72
|
|
|
CONFIG_DATA_LAYER['passwd'], |
73
|
|
|
CONFIG_DATA_LAYER['options'] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->instance; |
78
|
|
|
} catch (PDOException $e) { |
79
|
|
|
$this->setError($e); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param PDO $pdo |
86
|
|
|
* @return Crud |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
protected function setInstance(PDO $pdo) |
90
|
|
|
{ |
91
|
|
|
$this->instance = $pdo; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $database |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
protected function setDatabase(string $database) |
100
|
|
|
{ |
101
|
|
|
$this->database = $database; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getDatabase(): string |
109
|
|
|
{ |
110
|
|
|
return $this->database; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $tableName |
115
|
|
|
* @return Crud |
116
|
|
|
*/ |
117
|
|
|
protected function setFields(string $fields) |
118
|
|
|
{ |
119
|
|
|
$this->fields = $fields; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
protected function getFields():string |
127
|
|
|
{ |
128
|
|
|
return $this->fields; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $tableName |
133
|
|
|
* @return Crud |
134
|
|
|
*/ |
135
|
|
|
protected function setTable(string $tableName, string $tableAlias = ""): self |
136
|
|
|
{ |
137
|
|
|
if (!empty($tableAlias)) |
138
|
|
|
$this->tableAlias = $tableAlias; |
139
|
|
|
$this->tableName = $tableName; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function getTable(): string |
147
|
|
|
{ |
148
|
|
|
return $this->tableName; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getQueryBuilder(): QueryBuilder |
152
|
|
|
{ |
153
|
|
|
if (!empty($this->queryBuild)) |
154
|
|
|
return $this->queryBuild; |
155
|
|
|
return new QueryBuilder($this->getInstance()); |
156
|
|
|
} |
157
|
|
|
public function getTableAlias(): string |
158
|
|
|
{ |
159
|
|
|
return $this->tableAlias; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $classModel |
164
|
|
|
* @return Crud |
165
|
|
|
*/ |
166
|
|
|
protected function setClassModel(string $classModel) |
167
|
|
|
{ |
168
|
|
|
$this->classModel = $classModel; |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param String $query |
174
|
|
|
* @param array|null $params |
175
|
|
|
* @return false|mixed|\PDOStatement|null |
176
|
|
|
*/ |
177
|
|
|
protected function executeSQL(string $query, ?array $params = null) |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
$this->prepare = $this->getInstance()->prepare($query); |
181
|
|
|
$this->setLogSQL($query, $params); |
182
|
|
|
$this->prepare->execute($params); |
183
|
|
|
return $this->prepare; |
184
|
|
|
} catch (PDOException $e) { |
185
|
|
|
$this->setError($e); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param $prepare |
191
|
|
|
* @return int|false |
192
|
|
|
*/ |
193
|
|
|
protected function count(PDOStatement $prepare = null): ?int |
194
|
|
|
{ |
195
|
|
|
try { |
196
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
197
|
|
|
return $prepare->rowCount(); |
|
|
|
|
198
|
|
|
} catch (PDOException $e) { |
199
|
|
|
$this->setError($e);} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param $prepare |
204
|
|
|
* @return array|false |
205
|
|
|
*/ |
206
|
|
|
protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
207
|
|
|
{ |
208
|
|
|
try { |
209
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
210
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
211
|
|
|
$this->resultArray = $dados; |
212
|
|
|
return $dados; |
213
|
|
|
} catch (PDOException $e) { |
214
|
|
|
$this->setError($e); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param $prepare |
220
|
|
|
* @return array|false |
221
|
|
|
*/ |
222
|
|
|
protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
223
|
|
|
{ |
224
|
|
|
try { |
225
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
226
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_OBJ); |
227
|
|
|
$this->resultArray = $dados; |
228
|
|
|
return $dados; |
229
|
|
|
} catch (PDOException $e) { |
230
|
|
|
$this->setError($e); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param $prepare |
236
|
|
|
* @param String|null $class |
237
|
|
|
* @return array|false |
238
|
|
|
*/ |
239
|
|
|
protected function fetchArrayClass(PDOStatement $prepare = null, string $class = null): ?array |
240
|
|
|
{ |
241
|
|
|
try { |
242
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
243
|
|
|
$class = empty($class) ? $this->classModel : $class; |
244
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
245
|
|
|
$this->resultArray = $dados; |
246
|
|
|
return $dados; |
247
|
|
|
} catch (PDOException $e) { |
248
|
|
|
$this->setError($e); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param $prepare |
254
|
|
|
* @return array|false |
255
|
|
|
*/ |
256
|
|
|
protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
257
|
|
|
{ |
258
|
|
|
try { |
259
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
260
|
|
|
$dados = $prepare->fetch(PDO::FETCH_ASSOC); |
261
|
|
|
return $dados; |
262
|
|
|
} catch (PDOException $e) { |
263
|
|
|
$this->setError($e); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param $prepare |
269
|
|
|
* @return stdClass|false |
270
|
|
|
*/ |
271
|
|
|
protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
272
|
|
|
{ |
273
|
|
|
try { |
274
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
275
|
|
|
$dados = $prepare->fetch(PDO::FETCH_OBJ); |
276
|
|
|
return $dados; |
277
|
|
|
} catch (PDOException $e) { |
278
|
|
|
$this->setError($e); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param $prepare |
284
|
|
|
* @param String|null $class |
285
|
|
|
* @return array|false |
286
|
|
|
*/ |
287
|
|
|
protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
288
|
|
|
{ |
289
|
|
|
try { |
290
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
291
|
|
|
$class = empty($class) ? $this->classModel : $class; |
292
|
|
|
$dados = $prepare->fetchObject(CONFIG_DATA_LAYER["directory_models"] . $class); |
293
|
|
|
return $dados; |
294
|
|
|
} catch (PDOException $e) { |
295
|
|
|
$this->setError($e); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
protected function beginTrasaction(): ?bool |
303
|
|
|
{ |
304
|
|
|
try { |
305
|
|
|
$this->getInstance()->beginTransaction(); |
306
|
|
|
return true; |
307
|
|
|
} catch (PDOException $e) { |
308
|
|
|
$this->setError($e); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return bool|null |
315
|
|
|
*/ |
316
|
|
|
protected function commitTransaction(): ?bool |
317
|
|
|
{ |
318
|
|
|
try { |
319
|
|
|
$this->getInstance()->commit(); |
320
|
|
|
return true; |
321
|
|
|
} catch (PDOException $e) { |
322
|
|
|
$this->setError($e); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return bool|null |
328
|
|
|
* |
329
|
|
|
*/ |
330
|
|
|
protected function rollBackTransaction(): ?bool |
331
|
|
|
{ |
332
|
|
|
|
333
|
|
|
try { |
334
|
|
|
$this->getInstance()->rollBack(); |
335
|
|
|
return true; |
336
|
|
|
} catch (PDOException $e) { |
337
|
|
|
$this->setError($e); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return string|null |
343
|
|
|
* */ |
344
|
|
|
private function lastId(): ?string |
345
|
|
|
{ |
346
|
|
|
try { |
347
|
|
|
$ultimo = $this->getInstance()->lastInsertId(); |
348
|
|
|
return $ultimo; |
349
|
|
|
} catch (PDOException $e) { |
350
|
|
|
$this->setError($e); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @param $sql_string |
356
|
|
|
* @param array|null $params |
357
|
|
|
* @return void |
358
|
|
|
*/ |
359
|
|
|
private function setLogSQL($sql_string, ?array $params = null) |
360
|
|
|
{ |
361
|
|
|
if (!empty($params)) { |
362
|
|
|
$indexed = $params == array_values($params); |
363
|
|
|
foreach ($params as $k => $v) { |
364
|
|
|
if (is_object($v)) { |
365
|
|
|
if ($v instanceof \DateTime) { |
366
|
|
|
$v = $v->format('Y-m-d H:i:s'); |
367
|
|
|
} else { |
368
|
|
|
continue; |
369
|
|
|
} |
370
|
|
|
} elseif (is_string($v)) { |
371
|
|
|
$v = "'$v'"; |
372
|
|
|
} elseif ($v === null) { |
373
|
|
|
$v = 'NULL'; |
374
|
|
|
} elseif (is_array($v)) { |
375
|
|
|
$v = implode(',', $v); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if ($indexed) { |
379
|
|
|
$sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
380
|
|
|
} else { |
381
|
|
|
if ($k[0] != ':') { |
382
|
|
|
$k = ':' . $k; |
383
|
|
|
} //add leading colon if it was left out |
384
|
|
|
$sql_string = str_replace($k, $v, $sql_string); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
$this->logSQL = $sql_string; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param PDOException $e |
393
|
|
|
* @param string $sql |
394
|
|
|
* @return void |
395
|
|
|
*/ |
396
|
|
|
private function setError(PDOException $e) |
397
|
|
|
{ |
398
|
|
|
$this->error = $e; |
399
|
|
|
throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getLogSQL()}"); |
|
|
|
|
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.