Passed
Push — main ( 0bc77b...15d6bc )
by BRUNO
07:44
created

QueryBuilder::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
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS ABSTRACT QUERY
7
 * Basic class to make connection between the database and the application
8
 *
9
 * @author Bruno Morais <[email protected]>
10
 * @copyright MIT, bmorais.com
11
 * @package bmorais\database
12
 * @subpackage class
13
 * @access private
14
 */
15
16
class QueryBuilder
17
{
18
    use DatalayerTrait;
19
20
    public function __construct(\PDO $instance)
21
    {
22
        $this->setInstance($instance);
23
    }
24
25
    public function select(string $fields = "*", array $paramns = []):self
26
    {
27
        $query = "SELECT {$fields} FROM {$this->getTable()} ";
28
        if (!empty($this->getTableAlias()))
29
            $query .= "AS {$this->getTableAlias()}";
30
        $this->add($query, $paramns);
31
        return $this;
32
    }
33
34
    public function insert(string $fields, array $paramns):self
35
    {
36
        $numparams = '';
37
        foreach ($paramns as $item) {
38
            $numparams .= ',?';
39
        }
40
        $numparams = substr($numparams, 1);
41
        $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
42
        $this->add($query, $paramns);
43
        return $this;
44
    }
45
46
    public function update(string $fields, array $paramns):self
47
    {
48
        $fields_T = '';
49
        $atributos = explode(',', $fields);
50
51
        foreach ($atributos as $item) {
52
            $fields_T .= ", {$item} = ?";
53
        }
54
        $fields_T = substr($fields_T, 2);
55
        $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
56
        $this->add($query, $paramns);
57
        return $this;
58
    }
59
60
    public function delete(string $fields, array $paramns):self
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

60
    public function delete(/** @scrutinizer ignore-unused */ string $fields, array $paramns):self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        $query = "DELETE FROM {$this->getTable()}";
63
        $this->add($query, $paramns);
64
        return $this;
65
    }
66
67
    public function query(string $texto, array $paramns): self
68
    {
69
        $this->add($texto, $paramns);
70
        return $this;
71
    }
72
73
    public function where(string $texto, array $paramns): self
74
    {
75
        $query = "WHERE {$texto} ";
76
        $this->add($query, $paramns);
77
        return $this;
78
    }
79
80
    public function andWhere(string $condition, array $paramns): self
81
    {
82
        $query = "AND {$condition} ";
83
        $this->add($query, $paramns);
84
        return $this;
85
    }
86
87
    public function orWhere(string $texto, array $paramns): self
88
    {
89
        $query = "OR {$texto} ";
90
        $this->add($query, $paramns);
91
        return $this;
92
    }
93
94
    public function orderBy(string $texto): self
95
    {
96
        $query = "ORDER BY {$texto} ";
97
        $this->add($query);
98
        return $this;
99
    }
100
101
    public function limit(string $texto): self
102
    {
103
        $query = "LIMIT {$texto} ";
104
        $this->add($query);
105
        return $this;
106
    }
107
108
    public function offset(string $texto): self
109
    {
110
        $query = "OFFSET {$texto} ";
111
        $this->add($query);
112
        return $this;
113
    }
114
115
    public function groupBy(string  $texto): self{
116
        $query = "GROUP BY {$texto} ";
117
        $this->add($query);
118
        return $this;
119
    }
120
121
    public function having(string $texto): self
122
    {
123
        $query = "HAVING {$texto} ";
124
        $this->add($query);
125
        return $this;
126
    }
127
128
    public function innerJoin(string $table, string $alias, string $codition): self
129
    {
130
        $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
131
        $this->add($query);
132
        return $this;
133
    }
134
135
    public function leftJoin(string $table, string $alias, string $codition): self
136
    {
137
        $query = "LEFT JOIN {$table} AS {$alias} ON $codition ";
138
        $this->add($query);
139
        return $this;
140
    }
141
142
    public function rightJoin(string $table, string $alias, string $codition): self
143
    {
144
        $query = "RIGHT JOIN {$table} AS {$alias} ON $codition ";
145
        $this->add($query);
146
        return $this;
147
    }
148
149
    public function execute(): self
150
    {
151
        $this->executeSQL($this->query, $this->params);
152
        return $this;
153
    }
154
155
    public function debug()
156
    {
157
        return  $this->query.'<pre>'.print_r($this->params).'</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->params) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

157
        return  $this->query.'<pre>'./** @scrutinizer ignore-type */ print_r($this->params).'</pre>';
Loading history...
158
    }
159
160
    /**
161
     * @return false|string
162
     */
163
    public function lastInsertId(): ?string
164
    {
165
        return $this->lastId();
166
    }
167
168
    /**
169
     * @return string|null
170
     */
171
    public function getLogSQL(): ?string
172
    {
173
        return $this->logSQL ?? "";
174
    }
175
176
    private function add(string $text, array $params = [])
177
    {
178
        if (!empty($params))
179
            $this->params[] = $params;
180
        $this->query .= $text;
181
    }
182
183
}
184