-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.php
executable file
·119 lines (105 loc) · 3.33 KB
/
Query.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
<?php
declare(strict_types=1);
namespace MaplePHP\Query;
use MaplePHP\Query\Exceptions\ConnectException;
use MaplePHP\Query\Interfaces\DBInterface;
class Query
{
private $sql;
private ?string $pluck = null;
private ?Connect $connection = null;
public function __construct(string|DBInterface $sql, $connection = null)
{
$this->sql = $sql;
if ($sql instanceof DBInterface) {
$this->sql = $sql->sql();
}
$this->connection = is_null($connection) ? Connect::getInstance() : $connection;
}
public function setPluck(?string $pluck): void
{
$this->pluck = $pluck;
}
/**
* Execute query result
* @return object|array|bool
* @throws ConnectException
*/
public function execute(): object|array|bool
{
if ($result = $this->connection->query($this->sql)) {
return $result;
} else {
throw new ConnectException($this->connection->DB()->error, 1);
}
}
/**
* Execute query result And fetch as object
* @return bool|object|string
* @throws ConnectException
*/
public function get(): bool|object|string
{
return $this->obj();
}
/**
* SAME AS @get(): Execute query result And fetch as obejct
* @return bool|object|string (Mysql result)
* @throws ConnectException
*/
final public function obj(string $class = "stdClass", array $constructor_args = []): bool|object|string
{
$result = $this->execute();
if (is_object($result) && $result->num_rows > 0) {
$obj = $result->fetch_object($class, $constructor_args);
if(!is_null($this->pluck)) {
$obj = $obj->{$this->pluck};
}
return $obj;
}
return false;
}
/**
* Execute SELECT and fetch as array with nested objects
* @param callable|null $callback callaback, make changes in query and if return then change key
* @return array
* @throws ConnectException
*/
final public function fetch(?callable $callback = null, string $class = "stdClass", array $constructor_args = []): array
{
$key = 0;
$select = null;
$arr = array();
$result = $this->execute();
if (is_object($result) && $result->num_rows > 0) {
while ($row = $result->fetch_object($class, $constructor_args)) {
if(!is_null($this->pluck)) {
$row = $row->{$this->pluck};
}
if ($callback) {
$select = $callback($row, $key);
}
$data = ((!is_null($select)) ? $select : $key);
if (is_array($data)) {
if (!is_array($select)) {
throw new \InvalidArgumentException("The return value of the callable needs to be an array!", 1);
}
$arr = array_replace_recursive($arr, $select);
} else {
$arr[$data] = $row;
}
$key++;
}
}
return $arr;
}
/**
* Get insert AI ID from prev inserted result
* @return string|int
* @throws ConnectException
*/
public function insertID(): string|int
{
return $this->connection->DB()->insert_id;
}
}