/
DB.php
336 lines (296 loc) · 9.52 KB
/
DB.php
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* MySQL management.
*/
class DB
{
/**
* Connection to mysql.
* @var mysqli
*/
protected $connection;
/**
* @param string $host
* @param string $user
* @param string $pass
* @param string $db
* @throws Exception
*/
public function __construct(
string $host,
string $user,
string $pass,
string $db
) {
$this->connection = mysqli_connect(
$host,
$user,
$pass,
$db
);
if (!$this->connection) {
throw new Exception(mysqli_connect_error());
}
}
/**
* Closes the connection when the object is removed from memory.
*/
public function __destruct()
{
if ($this->connection) {
mysqli_close($this->connection);
}
}
/**
* Parses a value to be used in a SQL statement.
* @param string $value The value to be parsed.
* @return string The parsed value.
*/
public function Parse(string $value)
{
return mysqli_real_escape_string($this->connection, (string) $value);
}
/**
* Executes the statement and returns the result.
* @param string $stm Statement to be executed.
* @return mysqli_result|bool Returns the result of the statement or false if there was an error.
*/
public function Statement(string $stm): mysqli_result|bool
{
$tmp = mysqli_query($this->connection, $stm);
return $tmp ? $tmp : false;
}
/**
* Executes the query. Internally it uses sprintf, so the values starting % will be replaced.
* @param string $query Query to be executed.
* @param mixed $arguments Arguments to be replaced in the query
* @return bool|array If the query is SELECT, it returns an array of objects. Oterwhise, it returns a boolean.
*/
public function Query(string $query, ...$arguments): bool|array
{
$values = [];
$result = false;
foreach ($arguments as $a) {
$values[] = $this->Parse($a);
}
$query = (string) call_user_func_array('vsprintf', [$query, $values]);
if ('' !== $query) {
$tmp = $this->Statement($query);
if ($tmp instanceof mysqli_result) {
$result = [];
while ($fetch = mysqli_fetch_object($tmp)) {
$result[] = $fetch;
}
} else {
$result = $tmp;
}
}
return $result;
}
/**
* Simple SELECT builder.
* @param string $table
* @param array $fields
* @param array $where
* @param string $orderBy
* @param string $orderType
* @param int $limit
* @return array
*/
public function Select(
string $table,
array $fields = ['*'],
array $where = [],
string|null $orderBy = null,
string $orderType = 'DESC',
int $limit = -1
): array {
$query = 'SELECT ' . implode(',', $fields) . ' FROM ' . $table;
if (!empty($where)) {
$query .= ' WHERE ';
$parsedValues = [];
foreach ($where as $key => $value) {
if (is_null($value)) {
$parsedValues[] = $key . " IS NULL";
} else {
$parsedValues[] = $key . "='" . $this->Parse($value) . "'";
}
}
$query .= implode(' AND ', $parsedValues);
}
if (!is_null($orderBy)) {
$query .= ' ORDER BY ' . $orderBy . ' ' . $orderType;
}
if ($limit !== -1) {
$query .= ' LIMIT ' . $limit;
}
$tmp = $this->Statement($query);
if ($tmp) {
$result = [];
while ($fetch = mysqli_fetch_object($tmp)) {
$result[] = $fetch;
}
} else {
$result = false;
}
return $result;
}
/**
* Shortcut for Select(table, [*], where)
* @param string $table
* @param array $conditions
* @return array
*/
public function Where(string $table, array $conditions = []): array
{
return $this->Select($table, ['*'], $conditions);
}
/**
* Shortcut for Select(table, ['*'], where), but only returns one object.
* @param string $table
* @param array $conditions
* @return object|null
*/
public function Find(string $table = '', array $conditions = []): object|null
{
$tmp = $this->Select($table, ['*'], $conditions, null, 'DESC', 1);
return !empty($tmp) ? $tmp[0] : null;
}
/**
* Inserts data into the database.
* @param string $table Name of the table where the data will be inserted.
* @param array $data Associative array with the fields of the table as keys and the
* values to be inserted as the values.
* @return bool True if the data is correctly inserted.
*/
public function Insert(string $table, array $data = []): bool
{
$values = array_values($data);
$fields = array_keys($data);
$correctValues = [];
foreach ($values as $v) {
if (is_null($v)) {
$correctValues[] = "NULL";
} else {
$correctValues[] = "'" . $this->Parse($v) . "'";
}
}
$insert = 'INSERT INTO ' . $table . ' (' . implode(',', $fields) . ') VALUES (' . implode(',', $correctValues) . ')';
return (bool)$this->Statement($insert);
}
/**
* Makes a bulk insert of data into a table.
* @param string $table Name of the table.
* @param array $fields Array of string containing the fields that will be inserted.
* @param array $data Array of arrays containing the multiple inserts.
* @return bool True if the data is correctly inserted.
*/
public function Bulk(string $table, array $fields = [], array $data = [[]]): bool
{
$bulkValues = [];
foreach ($data as $d) {
$correctValues = [];
foreach ($d as $v) {
if (is_null($v)) {
$correctValues[] = "NULL";
} else {
$correctValues[] = "'" . $this->Parse($v) . "'";
}
}
$bulkValues[] = '(' . implode(',', $correctValues) . ')';
}
$insert = 'INSERT INTO ' . $table . ' (' . implode(',', $fields) . ') VALUES ' . implode(',', $bulkValues);
return (bool)$this->Statement($insert);
}
/**
* Updates the data of the database.
* @param string $table Name of the table to be updated.
* @param array $data Associative array with the fields of the table as keys and the
* values to be inserted as the values of the array.
* @param array $conditions Associative array withe the fields of the table as keys, and the
* values of the filters as the values of the array.
* @return boolean True if the data is correctly updated.
*/
public function Update(string $table, array $data = [], array $conditions = []): bool
{
$update = 'UPDATE ' . $table . ' SET ';
$parsedValues = [];
foreach ($data as $key => $value) {
if (is_null($value)) {
$parsedValues[] = $key . "=" . "NULL";
} else {
$parsedValues[] = $key . "='" . $this->Parse($value) . "'";
}
}
$update .= implode(',', $parsedValues);
if (!empty($conditions)) {
$update .= ' WHERE ';
$parsedFilters = [];
foreach ($conditions as $key => $value) {
$parsedFilters[] = $key . "='" . $this->Parse($value) . "'";
}
$update .= implode(' AND ', $parsedFilters);
}
return (bool)$this->Statement($update);
}
/**
* Deletes data from the database.
* @param string $table Affected table.
* @param array $filters Associative array with the keys of the array
* as the fields and the values of the arrays as the values used in the filter.
* @return boolean True if the data is correctly removed.
*/
public function Delete(string $table, array $filters = []): bool
{
$del = 'DELETE FROM ' . $table;
if (!empty($filters)) {
$del .= ' WHERE ';
$parsedFilters = [];
foreach ($filters as $key => $value) {
$parsedFilters[] = $key . "='" . $this->Parse($value) . "'";
}
$del .= implode(' AND ', $parsedFilters);
}
return (bool)$this->Statement($del);
}
/**
* Begins a transaction.
* @return bool
*/
public function Begin(): bool
{
return mysqli_begin_transaction($this->connection);
}
/**
* Commits the transaction.
* @return bool
*/
public function Commit(): bool
{
return mysqli_commit($this->connection);
}
/**
* Reverts the transaction.
* @return bool
*/
public function Rollback(): bool
{
return mysqli_rollback($this->connection);
}
/**
* Returns the Id of the last inserted entity.
* @return int|string
*/
public function LastId(): int
{
return (int)mysqli_insert_id($this->connection);
}
/**
* Returns the last error message.
* @return string
*/
public function GetError(): string
{
return mysqli_error($this->connection);
}
}