Passed
Push — main ( eb2cf4...a37793 )
by BRUNO
01:44
created

Crud::getLogSQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        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->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...
53
    }
54
55
    /**
56
     * @param array $params
57
     * @return bool
58
     */
59
    public function insertArray(array $params): bool
60
    {
61
        if (!empty($params)) {
62
            $query = "INSERT INTO $this->tableName";
63
            $values = [];
64
            $dataColumns = array_keys($params);
65
            if (isset ($dataColumns[0]))
66
                $query .= ' (`' . implode('`, `', $dataColumns) . '`) ';
67
            $query .= ' VALUES (';
68
69
            foreach ($dataColumns as $index => $column) {
70
                $values[] = $params[$column];
71
                $query .= "?, ";
72
            }
73
            $query = rtrim($query, ', ');
74
            $query .= ')';
75
76
            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...
77
        } else {
78
            return false;
79
        }
80
    }
81
82
    /**
83
     * @param string $fields
84
     * @param array|null $values
85
     * @param string|null $where
86
     * @param bool $debug
87
     * @return bool|void
88
     */
89
    public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
90
    {
91
        $fields_T = "";
92
        $atributos = explode(",", $fields);
93
94
        foreach ($atributos as $item) { $fields_T .= ", {$item} = ?"; }
95
        $fields_T = substr($fields_T, 2);
96
        $sql = "UPDATE {$this->tableName} SET {$fields_T}";
97
        if (isset($where)) { $sql .= " WHERE $where"; }
98
        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...
99
        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...
100
    }
101
102
    /**
103
     * @param array|null $values
104
     * @param string|null $where
105
     * @param bool $debug
106
     * @return bool|void
107
     */
108
    public function delete(array $values = null, string $where = null, bool $debug = false)
109
    {
110
        $sql = "DELETE FROM {$this->tableName}";
111
        if (isset($where)) { $sql .= " WHERE $where"; }
112
        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...
113
        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...
114
    }
115
116
117
118
    /**
119
     * @return false|string
120
     */
121
    public function lastInsertId()
122
    {
123
        return $this->lastId();
124
    }
125
126
    /**
127
     * @return string|null
128
     */
129
    public function getLogSQL():?string
130
    {
131
        return $this->logSQL;
132
    }
133
134
}