-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathDB.php
More file actions
588 lines (512 loc) · 16.8 KB
/
Copy pathDB.php
File metadata and controls
588 lines (512 loc) · 16.8 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/**
* Creates a singleton connection to the database with credentials from the config file.
*
* @package NamelessMC\Database
* @author Samerton
* @version 2.0.0-pr13
* @license MIT
*/
class DB
{
private static ?DB $_instance = null;
private string $_prefix;
private ?string $_force_charset;
private ?string $_force_collation;
protected PDO $_pdo;
private PDOStatement $_statement;
private bool $_error = false;
private array $_results;
private int $_count = 0;
protected QueryRecorder $_query_recorder;
private function __construct(
string $host,
string $database,
string $username,
string $password,
int $port,
?string $force_charset,
?string $force_collation,
string $prefix
) {
$this->_force_charset = $force_charset;
$this->_force_collation = $force_collation;
$this->_prefix = $prefix;
$connection_string = 'mysql:host=' . $host . ';port=' . $port . ';dbname=' . $database;
if ($force_charset) {
$connection_string .= ';charset=' . $force_charset;
}
$this->_pdo = new PDO(
$connection_string,
$username,
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
);
$this->_query_recorder = QueryRecorder::getInstance();
}
public static function getCustomInstance(
string $host,
string $database,
string $username,
string $password,
int $port = 3306,
?string $force_charset = null,
?string $force_collation = null,
string $prefix = 'nl2_'
): DB {
return new DB(
$host,
$database,
$username,
$password,
$port,
$force_charset,
$force_collation,
$prefix
);
}
public static function getInstance(): DB
{
if (self::$_instance) {
return self::$_instance;
}
if (Config::get('mysql.initialise_charset')) {
$force_charset = Config::get('mysql.charset') ?: 'utf8mb4';
} else {
$force_charset = null;
}
if (Config::get('mysql.initialise_collation')) {
$force_collation = Config::get('mysql.collation') ?: 'utf8mb4_unicode_ci';
} else {
$force_collation = null;
}
return self::$_instance = self::getCustomInstance(
Config::get('mysql.host'),
Config::get('mysql.db'),
Config::get('mysql.username'),
Config::get('mysql.password'),
Config::get('mysql.port'),
$force_charset,
$force_collation
);
}
/**
* Get the underlying PDO instance.
*
* @return PDO The PDO instance.
*/
public function getPDO(): PDO
{
return $this->_pdo;
}
/**
* Begin a MySQL transaction.
*/
public function beginTransaction(): void
{
$this->_pdo->beginTransaction();
}
/**
* Commit a MySQL transaction.
*/
public function commitTransaction(): void
{
if ($this->_pdo->inTransaction()) {
$this->_pdo->commit();
}
}
/**
* Roll back a MySQL transaction.
*/
public function rollBackTransaction(): void
{
if ($this->_pdo->inTransaction()) {
$this->_pdo->rollBack();
}
}
/**
* Execute a database query within a MySQL transaction, and get the results of the query, if any.
*
* @param Closure(DB): mixed $closure The closure to pass this instance to and execute within a transaction context.
* @return mixed The results of the query, null if none.
*/
public function transaction(Closure $closure)
{
$result = null;
try {
$this->beginTransaction();
$result = $closure($this);
$this->commitTransaction();
} catch (Exception $exception) {
$this->rollBackTransaction();
}
return $result;
}
/**
* Get the first row of the result.
*
* @return object|null The result object, or null if no result was returned.
*/
public function first(): ?object
{
return $this->results()[0] ?? null;
}
/**
* Get all the results of the query.
*
* @return array The results of the query.
*/
public function results(): array
{
return $this->_results;
}
/**
* Get the number of rows affected by the last query.
*
* @return int The number of rows.
*/
public function count(): int
{
return $this->_count;
}
/**
* Get whether any results exist.
*
* @return bool Whether any results exist.
*/
public function exists(): bool
{
return $this->_count > 0;
}
/**
* Get the last inserted ID.
*
* @return string|false ID of the last inserted row or false on failure
*/
public function lastId()
{
return $this->_pdo->lastInsertId();
}
/**
* Whether there was an error during the last query.
*
* @return bool Whether there was an error.
*/
public function error(): bool
{
return $this->_error;
}
/**
* Perform a SELECT query on the database.
*
* @param string $table The table to select from.
* @param mixed $where The where clause. If not an array, it will be used for "id" column lookup.
* @return static|false This instance if successful, false otherwise.
*/
public function get(string $table, $where = [])
{
if (!is_array($where)) {
$where = ['id', '=', $where];
}
return $this->action('SELECT *', $table, $where);
}
/**
* Perform a DELETE query on the database.
*
* @param string $table The table to delete from.
* @param mixed $where The where clause. If not an array, it will be used for "id" column lookup.
* @return static|false This instance if successful, false otherwise.
*/
public function delete(string $table, $where)
{
if (!is_array($where)) {
$where = ['id', '=', $where];
}
return $this->action('DELETE', $table, $where);
}
/**
* Perform a raw SQL query on the database.
*
* @param string $sql The SQL query string to execute.
* @param array $params The parameters to bind to the query.
* @param bool $isSelect Whether the statement is a select, defaults to null
* @return static This DB instance.
*/
public function query(string $sql, array $params = [], ?bool $isSelect = null)
{
$this->_error = false;
if ($this->_statement = $this->_pdo->prepare($sql)) {
$x = 1;
foreach ($params as $param) {
// Convert "true" and "false" to 1 and 0 so that query params can be more fluent
if (is_bool($param)) {
$param = $param ? 1 : 0;
}
$this->_statement->bindValue(
$x,
$param,
is_int($param)
? PDO::PARAM_INT
: PDO::PARAM_STR
);
$x++;
}
$this->_query_recorder->pushQuery($sql, $params);
if ($this->_statement->execute()) {
// Only fetch the results if this is a SELECT query.
if ($isSelect || str_starts_with(strtoupper(ltrim($sql)), 'SELECT')) {
$this->_results = $this->_statement->fetchAll(PDO::FETCH_OBJ);
}
$this->_count = $this->_statement->rowCount();
} else {
print_r($this->_pdo->errorInfo());
$this->_error = true;
}
} else {
$this->_results = [];
}
return $this;
}
/**
* Execute some SQL action (which uses a where clause) on the database.
*
* @param string $action The action to perform (SELECT, DELETE).
* @param string $table The table to perform the action on.
* @param array $where The where clause.
* @return static|false This instance if successful, false otherwise.
*/
private function action(string $action, string $table, array $where = [])
{
[$where, $where_params] = self::makeWhere($where);
$table = $this->_prefix . $table;
$sql = "{$action} FROM {$table} {$where}";
if (!$this->query($sql, $where_params)->error()) {
return $this;
}
return false;
}
/**
* Insert a new row into a table within the database.
*
* @param string $table The table to insert into.
* @param array $fields Array of data in "column => value" format to insert.
* @return bool Whether an error occurred or not.
*/
public function insert(string $table, array $fields = []): bool
{
$keys = array_keys($fields);
$fieldCount = count($fields);
$values = '';
$x = 1;
for ($i = 0; $i < $fieldCount; $i++) {
$values .= '?';
if ($x < $fieldCount) {
$values .= ', ';
}
$x++;
}
$table = $this->_prefix . $table;
$sql = "INSERT INTO {$table} (`" . implode('`,`', $keys) . "`) VALUES ({$values})";
return !$this->query($sql, $fields)->error();
}
/**
* Perform an UPDATE query on a table.
*
* @param string $table The table to update.
* @param array|int $where The where clause. If not an array, it will be used for "id" column lookup.
* @param array $fields Array of data in "column => value" format to update.
* @return bool Whether an error occurred or not.
*/
public function update(string $table, array|int $where, array $fields): bool
{
$set = '';
$x = 1;
foreach (array_keys($fields) as $column) {
$set .= "`{$column}` = ?";
if ($x < count($fields)) {
$set .= ', ';
}
$x++;
}
if (!is_array($where)) {
$where = ['id', '=', $where];
}
[$where, $where_params] = self::makeWhere($where);
$table = $this->_prefix . $table;
$sql = "UPDATE {$table} SET {$set} $where";
return !$this->query($sql, array_merge($fields, $where_params))->error();
}
/**
* Increment a numeric column value by 1.
*
* @param string $table The table to use.
* @param int $id The id of the row to increment a column in.
* @param string $field The field to increment.
* @return bool Whether an error occurred or not.
*/
public function increment(string $table, int $id, string $field): bool
{
$table = $this->_prefix . $table;
return !$this->query("UPDATE {$table} SET {$field} = {$field} + 1 WHERE id = ?", [$id])->error();
}
/**
* Decrement a numeric column value by 1.
*
* @param string $table The table to use.
* @param int $id The id of the row to decrement a column in.
* @param string $field The field to increment.
* @return bool Whether an error occurred or not.
*/
public function decrement(string $table, int $id, string $field): bool
{
$table = $this->_prefix . $table;
return !$this->query("UPDATE {$table} SET {$field} = {$field} - 1 WHERE id = ?", [$id])->error();
}
/**
* Select rows from the database, ordering by a specific column and sort type.
*
* @param string $table The table to use.
* @param string $order The column to order by.
* @param string $sort ASC or DESC
* @return static|false This instance if successful, false otherwise.
*/
public function orderAll(string $table, string $order, string $sort)
{
$table = $this->_prefix . $table;
$sql = "SELECT * FROM {$table} ORDER BY {$order} {$sort}";
if (!$this->query($sql)->error()) {
return $this;
}
return false;
}
/**
* Select rows from the database with a where clause, ordering by a specific column and sort type.
*
* @param string $table The table to use.
* @param string $order The column to order by.
* @param string $sort ASC or DESC
* @return static|false This instance if successful, false otherwise.
*/
public function orderWhere(string $table, string $where, string $order, string $sort)
{
$table = $this->_prefix . $table;
$sql = "SELECT * FROM {$table} WHERE {$where} ORDER BY {$order} {$sort}";
if (!$this->query($sql)->error()) {
return $this;
}
return false;
}
/**
* Create a new table in the database.
*
* @param string $name The name of the table.
* @param string $table_schema The table SQL schema.
* @return bool Whether an error occurred or not.
*/
public function createTable(string $name, string $table_schema): bool
{
$name = $this->_prefix . $name;
$sql = "CREATE TABLE `{$name}` ({$table_schema}) ENGINE=InnoDB";
if ($this->_force_charset) {
$sql .= ' DEFAULT CHARSET=' . $this->_force_charset;
}
if ($this->_force_collation) {
$sql .= ' COLLATE=' . $this->_force_collation;
}
return !$this->query($sql)->error();
}
/**
* Perform a SHOW TABLES LIKE query.
*
* @param string $table Name of table to try and lookup.
* @return int|false The number of rows affected, or false on failure.
*/
public function showTables(string $table)
{
$table = $this->_prefix . $table;
$sql = "SHOW TABLES LIKE '{$table}'";
if (!$this->query($sql)->error()) {
return $this->_statement->rowCount();
}
return false;
}
/**
* Add a new column to a table.
*
* @param string $table Name of table to alter.
* @param string $column The column to add.
* @param string $attributes The attributes of the column.
* @return bool Whether an error occurred or not.
*/
public function addColumn(string $table, string $column, string $attributes): bool
{
$table = $this->_prefix . $table;
$sql = "ALTER TABLE {$table} ADD {$column} {$attributes}";
return !$this->query($sql)->error();
}
/**
* Convert an array of where clause data into a MySQL WHERE clause and params.
*
* @param array $clauses An array, or nested array, of
* column, operator (default =), value, and glue (default AND).
* @return array The where clause string, and parameters to bind.
*/
public static function makeWhere(array $clauses): array
{
if (count($clauses) === count($clauses, COUNT_RECURSIVE)) {
return self::makeWhere([$clauses]);
}
$where_clauses = [];
foreach ($clauses as $clause) {
if (!is_array($clause)) {
throw new InvalidArgumentException('Where clause must be an array');
}
if (count($clause) !== count($clause, COUNT_RECURSIVE)) {
self::makeWhere(...$clause);
continue;
}
$column = null;
$operator = '=';
$value = null;
$glue = 'AND';
switch (count($clause)) {
case 4:
[$column, $operator, $value, $glue] = $clause;
break;
case 3:
[$column, $operator, $value] = $clause;
break;
case 2:
[$column, $value] = $clause;
break;
default:
throw new InvalidArgumentException('Invalid where clause');
}
if (!in_array($operator, ['=', '<>', '<', '>', '<=', '>=', 'LIKE', 'NOT LIKE'])) {
throw new InvalidArgumentException("Invalid operator: {$operator}");
}
$where_clauses[] = [
'column' => $column,
'operator' => $operator,
'value' => $value,
'glue' => $glue,
];
}
$first = true;
$where = '';
$params = [];
foreach ($where_clauses as $clause) {
if ($first) {
$where .= 'WHERE ';
$first = false;
} else {
$where .= " {$clause['glue']} ";
}
$where .= "`{$clause['column']}` {$clause['operator']} ?";
$params[] = $clause['value'];
}
return [$where, $params];
}
}