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
|
|
|
|
15
|
|
|
use PDO; |
16
|
|
|
use PDOException; |
17
|
|
|
use PDOStatement; |
18
|
|
|
use stdClass; |
19
|
|
|
|
20
|
|
|
trait DatalayerTrait |
21
|
|
|
{ |
22
|
|
|
/** @var PDO|null */ |
23
|
|
|
protected $instance = null; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $fields; |
27
|
|
|
|
28
|
|
|
/** @var PDOStatement|false */ |
29
|
|
|
protected $prepare = null; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $database = CONFIG_DATA_LAYER["dbname"]; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $classModel; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $tableName; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
protected $resultArray = array(); |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $logSQL; |
45
|
|
|
|
46
|
|
|
/** @var PDOException */ |
47
|
|
|
private $error; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return PDO|false |
51
|
|
|
*/ |
52
|
|
|
private function getInstance() |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
56
|
|
|
$this->database .= ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (empty($this->instance)) { |
60
|
|
|
$this->instance = new PDO( |
61
|
|
|
CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->database . ';port=' . CONFIG_DATA_LAYER['port'], |
62
|
|
|
CONFIG_DATA_LAYER['username'], |
63
|
|
|
CONFIG_DATA_LAYER['passwd'], |
64
|
|
|
CONFIG_DATA_LAYER['options'] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->instance; |
69
|
|
|
} catch (PDOException $e) { |
70
|
|
|
$this->setError($e); |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param String $query |
78
|
|
|
* @param array|null $params |
79
|
|
|
* @return false|mixed|\PDOStatement|null |
80
|
|
|
*/ |
81
|
|
|
protected function executeSQL(string $query, ?array $params = null) |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
$this->prepare = $this->getInstance()->prepare($query); |
85
|
|
|
$this->setLogSQL($query, $params); |
86
|
|
|
$this->prepare->execute($params); |
87
|
|
|
return $this->prepare; |
88
|
|
|
} catch (PDOException $e) { |
89
|
|
|
$this->setError($e, $query); |
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $prepare |
96
|
|
|
* @return int|false |
97
|
|
|
*/ |
98
|
|
|
protected function count($prepare = null): ?int |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
102
|
|
|
$qtd = $prepare->rowCount(); |
103
|
|
|
return $qtd; |
104
|
|
|
} catch (PDOException $e) { |
105
|
|
|
$this->setError($e); |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $prepare |
114
|
|
|
* @return array|false |
115
|
|
|
*/ |
116
|
|
|
protected function fetchArrayAssoc($prepare = null): ?array |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
120
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
121
|
|
|
$this->resultArray = $dados; |
122
|
|
|
return $dados; |
123
|
|
|
} catch (PDOException $e) { |
124
|
|
|
$this->setError($e); |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $prepare |
131
|
|
|
* @return array|false |
132
|
|
|
*/ |
133
|
|
|
protected function fetchArrayObj($prepare = null): ?array |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
137
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_OBJ); |
138
|
|
|
$this->resultArray = $dados; |
139
|
|
|
return $dados; |
140
|
|
|
} catch (PDOException $e) { |
141
|
|
|
$this->setError($e); |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $prepare |
148
|
|
|
* @param String|null $class |
149
|
|
|
* @return array|false |
150
|
|
|
*/ |
151
|
|
|
protected function fetchArrayClass($prepare = null, string $class = null): ?array |
152
|
|
|
{ |
153
|
|
|
try { |
154
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
155
|
|
|
$class = empty($class) ? $this->classModel : $class; |
156
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
157
|
|
|
$this->resultArray = $dados; |
158
|
|
|
return $dados; |
159
|
|
|
} catch (PDOException $e) { |
160
|
|
|
$this->setError($e); |
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $prepare |
167
|
|
|
* @return array|false |
168
|
|
|
*/ |
169
|
|
|
protected function fetchOneAssoc($prepare = null): ?array |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
173
|
|
|
$dados = $prepare->fetch(PDO::FETCH_ASSOC); |
174
|
|
|
return $dados; |
175
|
|
|
} catch (PDOException $e) { |
176
|
|
|
$this->setError($e); |
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param $prepare |
183
|
|
|
* @return stdClass|false |
184
|
|
|
*/ |
185
|
|
|
protected function fetchOneObj($prepare = null): ?stdClass |
186
|
|
|
{ |
187
|
|
|
try { |
188
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
189
|
|
|
$dados = $prepare->fetch(PDO::FETCH_OBJ); |
190
|
|
|
return $dados; |
191
|
|
|
} catch (PDOException $e) { |
192
|
|
|
$this->setError($e); |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param $prepare |
199
|
|
|
* @param String|null $class |
200
|
|
|
* @return array|false |
201
|
|
|
*/ |
202
|
|
|
protected function fetchOneClass($prepare = null, string $class = null): ?object |
203
|
|
|
{ |
204
|
|
|
try { |
205
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare; |
206
|
|
|
$class = empty($class) ? $this->classModel : $class; |
207
|
|
|
$dados = $prepare->fetchObject(CONFIG_DATA_LAYER["directory_models"] . $class); |
208
|
|
|
return $dados; |
209
|
|
|
} catch (PDOException $e) { |
210
|
|
|
$this->setError($e); |
211
|
|
|
return null; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
protected function beginTrasaction(): ?bool |
219
|
|
|
{ |
220
|
|
|
try { |
221
|
|
|
$this->getInstance()->beginTransaction(); |
222
|
|
|
return true; |
223
|
|
|
} catch (PDOException $e) { |
224
|
|
|
$this->setError($e); |
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
protected function commitTransaction(): ?bool |
234
|
|
|
{ |
235
|
|
|
try { |
236
|
|
|
$this->getInstance()->commit(); |
237
|
|
|
return true; |
238
|
|
|
} catch (PDOException $e) { |
239
|
|
|
$this->setError($e); |
240
|
|
|
return null; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
|
|
protected function rollBackTransaction(): ?bool |
248
|
|
|
{ |
249
|
|
|
|
250
|
|
|
try { |
251
|
|
|
$this->getInstance()->rollBack(); |
252
|
|
|
return true; |
253
|
|
|
} catch (PDOException $e) { |
254
|
|
|
$this->setError($e); |
255
|
|
|
return null; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return string|null |
261
|
|
|
*/ |
262
|
|
|
private function lastId(): ?string |
263
|
|
|
{ |
264
|
|
|
try { |
265
|
|
|
$ultimo = $this->getInstance()->lastInsertId(); |
266
|
|
|
return $ultimo; |
267
|
|
|
} catch (PDOException $e) { |
268
|
|
|
$this->setError($e); |
269
|
|
|
return null; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $sql_string |
275
|
|
|
* @param array|null $params |
276
|
|
|
* @return void |
277
|
|
|
*/ |
278
|
|
|
private function setLogSQL($sql_string, array $params = null) |
279
|
|
|
{ |
280
|
|
|
if (!empty($params)) { |
281
|
|
|
$indexed = $params == array_values($params); |
282
|
|
|
foreach ($params as $k => $v) { |
283
|
|
|
if (is_object($v)) { |
284
|
|
|
if ($v instanceof \DateTime) { |
285
|
|
|
$v = $v->format('Y-m-d H:i:s'); |
286
|
|
|
} else { |
287
|
|
|
continue; |
288
|
|
|
} |
289
|
|
|
} elseif (is_string($v)) { |
290
|
|
|
$v = "'$v'"; |
291
|
|
|
} elseif ($v === null) { |
292
|
|
|
$v = 'NULL'; |
293
|
|
|
} elseif (is_array($v)) { |
294
|
|
|
$v = implode(',', $v); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if ($indexed) { |
298
|
|
|
$sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
299
|
|
|
} else { |
300
|
|
|
if ($k[0] != ':') { |
301
|
|
|
$k = ':' . $k; |
302
|
|
|
} //add leading colon if it was left out |
303
|
|
|
$sql_string = str_replace($k, $v, $sql_string); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
$this->logSQL = $sql_string; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param PDOException $e |
312
|
|
|
* @param string $sql |
313
|
|
|
* @return void |
314
|
|
|
*/ |
315
|
|
|
private function setError(PDOException $e) |
316
|
|
|
{ |
317
|
|
|
$this->error = $e; |
318
|
|
|
throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getLogSQL()}"); |
|
|
|
|
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.