Passed
Push — main ( 80da6c...fca83b )
by BRUNO
02:03
created

DatalayerTrait::setLogSQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
     * @param $sql
264
     * @param $params
265
     * @return bool
266
     */
267
    protected function insertDB($sql, $params = null): bool
268
    {
269
        try {
270
            $this->getInstance();
271
            $this->prepare = $this->instance->prepare($sql);
272
            $rs = $this->prepare->execute($params);
273
            $this->logSQL = $this->prepare->queryString;
274
        } catch (PDOException $e) {
275
            Connect::setError($e,$sql);
276
            return false;
277
        }
278
        return $rs;
279
    }
280
281
    /**
282
     * @param $sql
283
     * @param $params
284
     * @return bool
285
     */
286
    protected function updateDB($sql, $params = null): bool
287
    {
288
        try {
289
            $this->getInstance();
290
            $query = $this->instance->prepare($sql);
291
            $rs = $query->execute($params);
292
            $this->setLogSQL($sql, $params);
293
        } catch (PDOException $e) {
294
            Connect::setError($e,$sql);
295
            return false;
296
        }
297
        return $rs;
298
    }
299
300
    /**
301
     * @param $sql
302
     * @param $params
303
     * @return bool
304
     */
305
    protected function deleteDB($sql, $params = null): bool
306
    {
307
        try {
308
            $this->getInstance();;
309
            $this->prepare = $this->instance->prepare($sql);
310
            $rs = $this->prepare->execute($params);
311
            $this->setLogSQL($sql, $params);
312
        } catch (PDOException $e) {
313
            Connect::setError($e,$sql);
314
            return false;
315
        }
316
        return $rs;
317
    }
318
319
    /**
320
    * RETORNAR O ULTIMO ID INSERIDO
321
    */
322
    private function lastId()
323
    {
324
        $this->getInstance();
325
        $ultimo = $this->instance->lastInsertId();
326
        return $ultimo;
327
328
    }
329
330
    /**
331
     * @return array
332
     */
333
    protected function printErrorInfo(): array
334
    {
335
        return $this->getInstance($this->database)->errorInfo();
0 ignored issues
show
Unused Code introduced by
The call to BMorais\Database\DatalayerTrait::getInstance() has too many arguments starting with $this->database. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

335
        return $this->/** @scrutinizer ignore-call */ getInstance($this->database)->errorInfo();

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.

Loading history...
336
    }
337
338
    private function setLogSQL($sql, $placeholders){
339
        foreach($placeholders as $k => $v){
340
            $sql = preg_replace('/:'.$k.'/',"'".$v."'",$sql);
341
        }
342
        $this->logSQL = $sql;
343
    }
344
}
345