1
|
|
|
<?php
|
2
|
|
|
namespace BMorais\Database;
|
3
|
|
|
/**
|
4
|
|
|
* CLASSE TRAIT DO DATABASE
|
5
|
|
|
* Esta classe de métodos de execução no banco
|
6
|
|
|
*
|
7
|
|
|
* @author Bruno Morais <[email protected]>
|
8
|
|
|
* @copyright GPL © 2022, bmorais.com
|
9
|
|
|
* @package bmorais\database
|
10
|
|
|
* @subpackage class
|
11
|
|
|
* @access private
|
12
|
|
|
*/
|
13
|
|
|
|
14
|
|
|
use PDO;
|
15
|
|
|
use PDOException;
|
16
|
|
|
use stdClass;
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
trait DatalayerTrait
|
20
|
|
|
{
|
21
|
|
|
/** @var PDO */
|
22
|
|
|
protected $instance;
|
23
|
|
|
protected $params;
|
24
|
|
|
protected $prepare = null;
|
25
|
|
|
protected $database = CONFIG_DATA_LAYER["dbname"];
|
26
|
|
|
protected $classModel;
|
27
|
|
|
protected $tableName;
|
28
|
|
|
protected $resultArray = array();
|
29
|
|
|
|
30
|
|
|
private $logSQL;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* @return PDO|null
|
34
|
|
|
*/
|
35
|
|
|
private function getInstance(): ?PDO
|
36
|
|
|
{
|
37
|
|
|
if (strpos($_SERVER['SERVER_NAME'], "homologacao") && !strpos($this->database, "Homologacao") )
|
38
|
|
|
$this->database .= "Homologacao";
|
39
|
|
|
|
40
|
|
|
if (!isset($this->instance)) {
|
41
|
|
|
$this->instance = Connect::getInstance($this->database);
|
42
|
|
|
return $this->instance;
|
43
|
|
|
} else {
|
44
|
|
|
return $this->instance;
|
45
|
|
|
}
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @param String $query
|
50
|
|
|
* @param array|null $params
|
51
|
|
|
* @return false|mixed|\PDOStatement|null
|
52
|
|
|
*/
|
53
|
|
|
protected function executeSQL(String $query, ?array $params = null)
|
54
|
|
|
{
|
55
|
|
|
try {
|
56
|
|
|
$this->getInstance();
|
57
|
|
|
$this->prepare = $this->instance->prepare($query);
|
58
|
|
|
$this->prepare->execute($params);
|
59
|
|
|
$this->setLogSQL($query, $params);
|
60
|
|
|
} catch (PDOException $e) {
|
61
|
|
|
Connect::setError($e,$query);
|
62
|
|
|
return false;
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
return $this->prepare;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* @param $prepare
|
70
|
|
|
* @return int
|
71
|
|
|
*/
|
72
|
|
|
protected function count($prepare=null): int
|
73
|
|
|
{
|
74
|
|
|
try {
|
75
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
76
|
|
|
$qtd = $prepare->rowCount();
|
77
|
|
|
return $qtd;
|
78
|
|
|
} catch (PDOException $e) {
|
79
|
|
|
Connect::setError($e);
|
80
|
|
|
return false;
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
/**
|
87
|
|
|
* @param $prepare
|
88
|
|
|
* @return array|false
|
89
|
|
|
*/
|
90
|
|
|
protected function fetchArrayAssoc($prepare=null): array
|
91
|
|
|
{
|
92
|
|
|
try {
|
93
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
94
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC);
|
95
|
|
|
$this->resultArray = $dados;
|
96
|
|
|
return $dados;
|
97
|
|
|
} catch (PDOException $e) {
|
98
|
|
|
Connect::setError($e);
|
99
|
|
|
return false;
|
100
|
|
|
}
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* @param $prepare
|
105
|
|
|
* @return array|false
|
106
|
|
|
*/
|
107
|
|
|
protected function fetchArrayObj($prepare=null): array
|
108
|
|
|
{
|
109
|
|
|
try {
|
110
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
111
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_OBJ);
|
112
|
|
|
$this->resultArray = $dados;
|
113
|
|
|
return $dados;
|
114
|
|
|
} catch (PDOException $e) {
|
115
|
|
|
Connect::setError($e);
|
116
|
|
|
return false;
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* @param $prepare
|
122
|
|
|
* @param String|null $class
|
123
|
|
|
* @return array|false
|
124
|
|
|
*/
|
125
|
|
|
protected function fetchArrayClass($prepare=null, String $class=null): array
|
126
|
|
|
{
|
127
|
|
|
try {
|
128
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
129
|
|
|
$class = empty($class) ? $this->classModel : $class;
|
130
|
|
|
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class);
|
131
|
|
|
$this->resultArray = $dados;
|
132
|
|
|
return $dados;
|
133
|
|
|
} catch (PDOException $e) {
|
134
|
|
|
Connect::setError($e);
|
135
|
|
|
return false;
|
136
|
|
|
}
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
/**
|
140
|
|
|
* @param $prepare
|
141
|
|
|
* @return array|false
|
142
|
|
|
*/
|
143
|
|
|
protected function fetchOneAssoc($prepare=null): array
|
144
|
|
|
{
|
145
|
|
|
try {
|
146
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
147
|
|
|
$dados = $prepare->fetch(PDO::FETCH_ASSOC);
|
148
|
|
|
return $dados;
|
149
|
|
|
} catch (PDOException $e) {
|
150
|
|
|
Connect::setError($e);
|
151
|
|
|
return false;
|
152
|
|
|
}
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
/**
|
156
|
|
|
* @param $prepare
|
157
|
|
|
* @return stdClass|false
|
158
|
|
|
*/
|
159
|
|
|
protected function fetchOneObj($prepare=null): stdClass
|
160
|
|
|
{
|
161
|
|
|
try {
|
162
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
163
|
|
|
$dados = $prepare->fetch(PDO::FETCH_OBJ);
|
164
|
|
|
return $dados;
|
165
|
|
|
} catch (PDOException $e) {
|
166
|
|
|
Connect::setError($e);
|
167
|
|
|
return false;
|
168
|
|
|
}
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
/**
|
172
|
|
|
* @param $prepare
|
173
|
|
|
* @param String|null $class
|
174
|
|
|
* @return array|false
|
175
|
|
|
*/
|
176
|
|
|
protected function fetchOneClass($prepare=null, String $class=null): object
|
177
|
|
|
{
|
178
|
|
|
try {
|
179
|
|
|
$prepare = empty($prepare) ? $this->prepare : $prepare;
|
180
|
|
|
$class = empty($class) ? $this->classModel : $class;
|
181
|
|
|
$dados = $prepare->fetchObject(CONFIG_DATA_LAYER["directory_models"] . $class);
|
182
|
|
|
return $dados;
|
183
|
|
|
} catch (PDOException $e) {
|
184
|
|
|
Connect::setError($e);
|
185
|
|
|
return false;
|
186
|
|
|
}
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
/**
|
190
|
|
|
* @return bool
|
191
|
|
|
*/
|
192
|
|
|
protected function beginTrasaction(): bool
|
193
|
|
|
{
|
194
|
|
|
try {
|
195
|
|
|
$this->getInstance();
|
196
|
|
|
$this->instance->beginTransaction();
|
197
|
|
|
return true;
|
198
|
|
|
} catch (PDOException $e) {
|
199
|
|
|
Connect::setError($e);
|
200
|
|
|
return false;
|
201
|
|
|
}
|
202
|
|
|
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
/**
|
206
|
|
|
* @return bool
|
207
|
|
|
*/
|
208
|
|
|
protected function commitTransaction(): bool
|
209
|
|
|
{
|
210
|
|
|
try {
|
211
|
|
|
$this->getInstance();
|
212
|
|
|
$this->instance->commit();
|
213
|
|
|
return true;
|
214
|
|
|
} catch (PDOException $e) {
|
215
|
|
|
Connect::setError($e);
|
216
|
|
|
return false;
|
217
|
|
|
}
|
218
|
|
|
}
|
219
|
|
|
|
220
|
|
|
/**
|
221
|
|
|
* @return bool
|
222
|
|
|
*/
|
223
|
|
|
protected function rollBackTransaction(): bool
|
224
|
|
|
{
|
225
|
|
|
|
226
|
|
|
try {
|
227
|
|
|
$this->getInstance();
|
228
|
|
|
$this->instance->rollBack();
|
229
|
|
|
return true;
|
230
|
|
|
} catch (PDOException $e) {
|
231
|
|
|
Connect::setError($e);
|
232
|
|
|
return false;
|
233
|
|
|
}
|
234
|
|
|
}
|
235
|
|
|
|
236
|
|
|
/**
|
237
|
|
|
* @param $sql
|
238
|
|
|
* @param $params
|
239
|
|
|
* @param $class
|
240
|
|
|
* @return array|false
|
241
|
|
|
*/
|
242
|
|
|
protected function selectDB($sql, $params = null, $class = null)
|
243
|
|
|
{
|
244
|
|
|
try {
|
245
|
|
|
$this->getInstance();
|
246
|
|
|
$this->prepare = $this->instance->prepare($sql);
|
247
|
|
|
$this->prepare->execute($params);
|
248
|
|
|
$this->setLogSQL($sql, $params);
|
249
|
|
|
|
250
|
|
|
if (!empty($class)) {
|
251
|
|
|
$rs = $this->fetchArrayClass($this->prepare,$class);
|
252
|
|
|
} else {
|
253
|
|
|
$rs = $this->fetchArrayObj($this->prepare);
|
254
|
|
|
}
|
255
|
|
|
} catch (PDOException $e) {
|
256
|
|
|
Connect::setError($e,$sql);
|
257
|
|
|
return false;
|
258
|
|
|
}
|
259
|
|
|
return $rs;
|
260
|
|
|
}
|
261
|
|
|
|
262
|
|
|
/**
|
263
|
|
|
* RETORNAR O ULTIMO ID INSERIDO
|
264
|
|
|
*/
|
265
|
|
|
private function lastId()
|
266
|
|
|
{
|
267
|
|
|
$this->getInstance();
|
268
|
|
|
$ultimo = $this->instance->lastInsertId();
|
269
|
|
|
return $ultimo;
|
270
|
|
|
|
271
|
|
|
}
|
272
|
|
|
|
273
|
|
|
/**
|
274
|
|
|
* @return array
|
275
|
|
|
*/
|
276
|
|
|
protected function printErrorInfo(): array
|
277
|
|
|
{
|
278
|
|
|
return $this->getInstance($this->database)->errorInfo();
|
|
|
|
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
function setLogSQL($sql_string, array $params = null) {
|
|
|
|
|
282
|
|
|
if (!empty($params)) {
|
283
|
|
|
$indexed = $params == array_values($params);
|
284
|
|
|
foreach($params as $k=>$v) {
|
285
|
|
|
if (is_object($v)) {
|
286
|
|
|
if ($v instanceof \DateTime) $v = $v->format('Y-m-d H:i:s');
|
287
|
|
|
else continue;
|
288
|
|
|
}
|
289
|
|
|
elseif (is_string($v)) $v="'$v'";
|
290
|
|
|
elseif ($v === null) $v='NULL';
|
291
|
|
|
elseif (is_array($v)) $v = implode(',', $v);
|
292
|
|
|
|
293
|
|
|
if ($indexed) {
|
294
|
|
|
$sql_string = preg_replace('/\?/', $v, $sql_string, 1);
|
295
|
|
|
}
|
296
|
|
|
else {
|
297
|
|
|
if ($k[0] != ':') $k = ':'.$k; //add leading colon if it was left out
|
298
|
|
|
$sql_string = str_replace($k,$v,$sql_string);
|
299
|
|
|
}
|
300
|
|
|
}
|
301
|
|
|
}
|
302
|
|
|
$this->logSQL = $sql_string;
|
303
|
|
|
}
|
304
|
|
|
}
|
305
|
|
|
|
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.