Passed
Push — main ( 0cf401...c49a8e )
by BRUNO
01:47
created

Crud   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 142
rs 10
c 0
b 0
f 0
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 11 4
A delete() 0 6 3
A getLogSQL() 0 3 1
A insert() 0 8 3
A updateArray() 0 18 4
A insertArray() 0 20 4
A lastInsertId() 0 3 1
A select() 0 10 4
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;
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
        $this->executeSQL($sql, $values);
33
        if ($returnModel) {
34
            return $this->fetchArrayClass($this->prepare);
35
        } else {
36
            return $this->fetchArrayObj($this->prepare);
37
        }
38
    }
39
40
    /**
41
     * @param string $fields
42
     * @param array|null $values
43
     * @param $debug
44
     * @return bool|void
45
     */
46
    public function insert(string $fields, array $values = null, $debug = false)
47
    {
48
        $numparams = "";
49
        foreach ($values as $item) { $numparams .= ",?"; }
50
        $numparams = substr($numparams, 1);
51
        $sql = "INSERT INTO {$this->tableName} ({$fields}) VALUES ({$numparams})";
52
        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...
53
        return $this->executeSQL($sql, $values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeSQL($sql, $values) returns the type PDOStatement which is incompatible with the documented return type boolean.
Loading history...
54
    }
55
56
    /**
57
     * @param array $params
58
     * @return bool
59
     */
60
    public function insertArray(array $params): bool
61
    {
62
        if (!empty($params)) {
63
            $query = "INSERT INTO $this->tableName";
64
            $values = [];
65
            $dataColumns = array_keys($params);
66
            if (isset ($dataColumns[0]))
67
                $query .= ' (`' . implode('`, `', $dataColumns) . '`) ';
68
            $query .= ' VALUES (';
69
70
            foreach ($dataColumns as $index => $column) {
71
                $values[] = $params[$column];
72
                $query .= "?, ";
73
            }
74
            $query = rtrim($query, ', ');
75
            $query .= ')';
76
77
            return $this->executeSQL($query, $values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeSQL($query, $values) returns the type PDOStatement which is incompatible with the type-hinted return boolean.
Loading history...
78
        } else {
79
            return false;
80
        }
81
    }
82
83
    /**
84
     * @param string $fields
85
     * @param array|null $values
86
     * @param string|null $where
87
     * @param bool $debug
88
     * @return bool|void
89
     */
90
    public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
91
    {
92
        $fields_T = "";
93
        $atributos = explode(",", $fields);
94
95
        foreach ($atributos as $item) { $fields_T .= ", {$item} = ?"; }
96
        $fields_T = substr($fields_T, 2);
97
        $sql = "UPDATE {$this->tableName} SET {$fields_T}";
98
        if (isset($where)) { $sql .= " WHERE $where"; }
99
        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...
100
        return $this->executeSQL($sql, $values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeSQL($sql, $values) returns the type PDOStatement which is incompatible with the documented return type boolean.
Loading history...
101
    }
102
103
    /**
104
     * @param array $params
105
     * @param string $where
106
     * @return bool
107
     */
108
    public function updateArray(array $params, string $where = ""): bool
109
    {
110
        if (!empty($params)) {
111
            $query = "UPDATE {$this->tableName} SET";
112
            $values = [];
113
114
            foreach ($params as $index => $column) {
115
                $query .= " {$index} = ?, ";
116
                $values[] = $params[$index];
117
            }
118
            $query = rtrim($query, ", ");
119
120
            if (!empty($where))
121
                $query .= " WHERE {$where}";
122
123
            return $this->executeSQL($query, $values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeSQL($query, $values) returns the type PDOStatement which is incompatible with the type-hinted return boolean.
Loading history...
124
        } else {
125
            return false;
126
        }
127
    }
128
129
    /**
130
     * @param array|null $values
131
     * @param string|null $where
132
     * @param bool $debug
133
     * @return bool|void
134
     */
135
    public function delete(array $values = null, string $where = null, bool $debug = false)
136
    {
137
        $sql = "DELETE FROM {$this->tableName}";
138
        if (isset($where)) { $sql .= " WHERE $where"; }
139
        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...
140
        return $this->executeSQL($sql, $values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executeSQL($sql, $values) returns the type PDOStatement which is incompatible with the documented return type boolean.
Loading history...
141
    }
142
143
    /**
144
     * @return false|string
145
     */
146
    public function lastInsertId()
147
    {
148
        return $this->lastId();
149
    }
150
151
    /**
152
     * @return string|null
153
     */
154
    public function getLogSQL():?string
155
    {
156
        return $this->logSQL;
157
    }
158
159
}