-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_builder.php
More file actions
251 lines (218 loc) · 7.78 KB
/
query_builder.php
File metadata and controls
251 lines (218 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// ORIGINAL https://phpzm.rocks/php-like-a-boss-2-crie-seu-querybuilder-af430b4125f9
// REFLECTION https://catchmetech.com/en/post/94/how-to-create-an-orm-framework-in-pure-php-orm-creation-tutorial
// DYNAMIC OBJ https://stackoverflow.com/questions/14395631/php-create-object-without-class
require 'query_db_abstraction.php';
/**
* Class QueryBuilder
*
* Provides to the code a clean way to builds SQL querys.
* The code was based on:
* https://phpzm.rocks/php-like-a-boss-2-crie-seu-querybuilder-af430b4125f9
*
* @author Fabio Andreozzi Godoy <fabio.godoy@oldpocket.com>
* @copyright 2019-2020 Fabio Godoy
* @link https://github.com/oldpocket/ARAIS
* @license https://github.com/oldpocket/ARAIS/blob/master/LICENSE
* @version 0.5.0
* @package ARAIS
*
* @method QueryBuilder table (string $table)
* @method QueryBuilder join (string $join)
* @method QueryBuilder fields (array $fields)
* @method QueryBuilder where (array $where)
* @method QueryBuilder order (array $order)
* @method QueryBuilder group (array $group)
* @method QueryBuilder having (array $having)
* @method QueryBuilder limit (array $join)
*/
class QueryBuilder extends QueryDBAbstraction
{
/** @var array The list of clausules to be used in the query */
private $clausules = [];
/**
* @param $name
* @param $arguments
* @return $this
*/
public function __call($name, $arguments)
{
$clausule = $arguments[0];
if (count($arguments) > 1) {
$clausule = $arguments;
}
$this->clausules[strtolower($name)] = $clausule;
return $this;
}
/**
* @param array $values
* @return string
*/
public function insert($values)
{
// recupera o nome da tabela
// ou deixa uma marcação para mostrar que faltou informar esse campo
$table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>';
// recupera o array dos campos
// ou deixa uma marcação para mostrar que faltou informar esse campo
$_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '<fields>';
$fields = implode(', ', $_fields);
// cria uma lista de rótulos para usar "prepared statement"
$_placeholders = array_map(function($v) {
return '?';
}, $_fields);
$placeholders = implode(', ', $_placeholders);
$command = [];
$command[] = 'INSERT INTO';
$command[] = $table;
$command[] = '(' . $fields . ')';
$command[] = 'VALUES';
$command[] = '(' . $placeholders . ')';
// INSERT INTO {table} ({fields}) VALUES ({values});
// junta o comando
$sql = implode(' ', $command);
return $this->executeInsert($sql, $values);
}
/**
* @param $values
* @return string
*/
public function select($values = [])
{
// recupera o nome da tabela
// ou deixa uma marcação para mostrar que faltou informar esse campo
$table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>';
// recupera o array dos campos
// ou deixa uma marcação para mostrar que faltou informar esse campo
$_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '*';
$fields = implode(', ', $_fields);
$join = isset($this->clausules['join']) ? $this->clausules['join'] : '';
$command = [];
$command[] = 'SELECT';
$command[] = $fields;
$command[] = 'FROM';
$command[] = $table;
if ($join) {
$command[] = $join;
}
$clausules = [
'where' => [
'instruction' => 'WHERE',
'separator' => ' ',
],
'group' => [
'instruction' => 'GROUP BY',
'separator' => ', ',
],
'order' => [
'instruction' => 'ORDER BY',
'separator' => ', ',
],
'having' => [
'instruction' => 'HAVING',
'separator' => ' AND ',
],
'limit' => [
'instruction' => 'LIMIT',
'separator' => ',',
],
];
foreach($clausules as $key => $clausule) {
if (isset($this->clausules[$key])) {
$value = $this->clausules[$key];
if (is_array($value)) {
$value = implode($clausule['separator'], $this->clausules[$key]);
}
$command[] = $clausule['instruction'] . ' ' . $value;
}
}
// SELECT {fields} FROM <JOIN> {table} <WHERE> <GROUP> <ORDER> <HAVING> <LIMIT>;
// junta o comando
$sql = implode(' ', $command);
return $this->executeSelect($sql, $values);
}
/**
* @param $values
* @param $filters
* @return int
*/
public function update($values, $filters = [])
{
// recupera o nome da tabela
// ou deixa uma marcação para mostrar que faltou informar esse campo
$table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>';
$join = isset($this->clausules['join']) ? $this->clausules['join'] : '';
// recupera o array dos campos
// ou deixa uma marcação para mostrar que faltou informar esse campo
$_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '<fields>';
$sets = $_fields;
if (is_array($_fields)) {
$sets = implode(', ', array_map(function($value) {
return $value . ' = ?';
}, $_fields));
}
$command = [];
$command[] = 'UPDATE';
$command[] = $table;
if ($join) {
$command[] = $join;
}
$command[] = 'SET';
$command[] = $sets;
$clausules = [
'where' => [
'instruction' => 'WHERE',
'separator' => ' ',
]
];
foreach($clausules as $key => $clausule) {
if (isset($this->clausules[$key])) {
$value = $this->clausules[$key];
if (is_array($value)) {
$value = implode($clausule['separator'], $this->clausules[$key]);
}
$command[] = $clausule['instruction'] . ' ' . $value;
}
}
// UPDATE {table} SET {set} <WHERE>
// junta o comando
$sql = implode(' ', $command);
return $this->executeUpdate($sql, array_merge($values, $filters));
}
/**
* @param $filters
* @return int
*/
public function delete($filters)
{
// recupera o nome da tabela
// ou deixa uma marcação para mostrar que faltou informar esse campo
$table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>';
$join = isset($this->clausules['join']) ? $this->clausules['join'] : '';
$command = [];
$command[] = 'DELETE FROM';
$command[] = $table;
if ($join) {
$command[] = $join;
}
$clausules = [
'where' => [
'instruction' => 'WHERE',
'separator' => ' ',
]
];
foreach($clausules as $key => $clausule) {
if (isset($this->clausules[$key])) {
$value = $this->clausules[$key];
if (is_array($value)) {
$value = implode($clausule['separator'], $this->clausules[$key]);
}
$command[] = $clausule['instruction'] . ' ' . $value;
}
}
// DELETE FROM {table} <JOIN> <USING> <WHERE>
// merge the parts of the command
$sql = implode(' ', $command);
return $this->executeDelete($sql, $filters);
}
}