Passed
Push — main ( 94e8c9...80da6c )
by BRUNO
07:56
created

Crud   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 11 4
A delete() 0 6 3
A insert() 0 8 3
A lastInsertId() 0 3 1
A select() 0 9 4
A getLogSQL() 0 2 1
1
<?php
2
namespace BMorais\Database;
3
4
/**
5
 * CLASSE CRUD
6
 * Classe abastrada para fazer ligação entre o banco e aplicação
7
 *
8
 * @author Bruno Morais <[email protected]>
9
 * @copyright GPL © 2022, bmorais.com
10
 * @package bmorais\database
11
 * @subpackage class
12
 * @access private
13
 */
14
15
abstract class Crud {
16
17
    use DatalayerTrait;
0 ignored issues
show
Bug introduced by
The trait BMorais\Database\DatalayerTrait requires the property $queryString which is not provided by BMorais\Database\Crud.
Loading history...
18
19
    /**
20
     * @param string $fields
21
     * @param string $add
22
     * @param array|null $values
23
     * @param bool $returnModel
24
     * @param bool $debug
25
     * @return array|false|void
26
     */
27
    public function select(string $fields = "*", string $add = "", array $values = null, bool $returnModel = false, bool $debug = false)
28
    {
29
        if (strlen($add)>0) { $add = " " . $add; }
30
        $sql = "SELECT {$fields} FROM {$this->tableName}{$add}";
31
        if ($debug) { echo $sql; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
32
        if ($returnModel) {
33
            return $this->selectDB($sql, $values, $this->classModel);
34
        } else {
35
            return $this->selectDB($sql, $values, null);
36
        }
37
    }
38
39
    /**
40
     * @param string $fields
41
     * @param array|null $values
42
     * @param $debug
43
     * @return bool|void
44
     */
45
    public function insert(string $fields, array $values = null, $debug = false)
46
    {
47
        $numparams = "";
48
        foreach ($values as $item) { $numparams .= ",?"; }
49
        $numparams = substr($numparams, 1);
50
        $sql = "INSERT INTO {$this->tableName} ({$fields}) VALUES ({$numparams})";
51
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
52
        return $this->insertDB($sql, $values);
53
    }
54
55
    /**
56
     * @param string $fields
57
     * @param array|null $values
58
     * @param string|null $where
59
     * @param bool $debug
60
     * @return bool|void
61
     */
62
    public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
63
    {
64
        $fields_T = "";
65
        $atributos = explode(",", $fields);
66
67
        foreach ($atributos as $item) { $fields_T .= ", {$item} = ?"; }
68
        $fields_T = substr($fields_T, 2);
69
        $sql = "UPDATE {$this->tableName} SET {$fields_T}";
70
        if (isset($where)) { $sql .= " WHERE $where"; }
71
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
        return $this->updateDB($sql, $values);
73
    }
74
75
    /**
76
     * @param array|null $values
77
     * @param string|null $where
78
     * @param bool $debug
79
     * @return bool|void
80
     */
81
    public function delete(array $values = null, string $where = null, bool $debug = false)
82
    {
83
        $sql = "DELETE FROM {$this->tableName}";
84
        if (isset($where)) { $sql .= " WHERE $where"; }
85
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
86
        return $this->deleteDB($sql, $values);
87
    }
88
89
    /**
90
     * @return false|string
91
     */
92
    public function lastInsertId()
93
    {
94
        return $this->lastId();
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getLogSQL(){
101
        return $this->logSQL;
102
    }
103
104
}