-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathResult.php
238 lines (220 loc) · 5.89 KB
/
Result.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
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework Database Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\Database;
use Framework\Database\Result\Field;
use LogicException;
use mysqli_result;
use OutOfBoundsException;
use OutOfRangeException;
/**
* Class Result.
*
* @package database
*/
class Result
{
/**
* @var mysqli_result<int,array|false|null>
*/
protected mysqli_result $result;
protected bool $buffered;
protected bool $free = false;
protected string $fetchClass = \stdClass::class;
/**
* @var array<mixed>
*/
protected array $fetchConstructor = [];
/**
* Result constructor.
*
* @param mysqli_result<int,array|false|null> $result
* @param bool $buffered
*/
public function __construct(mysqli_result $result, bool $buffered)
{
$this->result = $result;
$this->buffered = $buffered;
}
public function __destruct()
{
if (!$this->isFree()) {
$this->free();
}
}
/**
* Frees the memory associated with a result.
*/
public function free() : void
{
$this->checkIsFree();
$this->free = true;
$this->result->free();
}
public function isFree() : bool
{
return $this->free;
}
protected function checkIsFree() : void
{
if ($this->isFree()) {
throw new LogicException('Result is already free');
}
}
public function isBuffered() : bool
{
return $this->buffered;
}
/**
* Adjusts the result pointer to an arbitrary row in the result.
*
* @param int $offset The field offset. Must be between zero and the total
* number of rows minus one
*
* @throws LogicException if is an unbuffered result
* @throws OutOfBoundsException for invalid cursor offset
*
* @return bool
*/
public function moveCursor(int $offset) : bool
{
$this->checkIsFree();
if (!$this->isBuffered()) {
throw new LogicException('Cursor cannot be moved on unbuffered results');
}
if ($offset < 0 || ($offset !== 0 && $offset >= $this->result->num_rows)) {
throw new OutOfRangeException(
"Invalid cursor offset: {$offset}"
);
}
return $this->result->data_seek($offset);
}
/**
* @param string $class
* @param mixed ...$constructor
*
* @return static
*/
public function setFetchClass(string $class, mixed ...$constructor) : static
{
$this->fetchClass = $class;
$this->fetchConstructor = $constructor;
return $this;
}
/**
* Fetches the current row as object and move the cursor to the next.
*
* @param string|null $class
* @param mixed ...$constructor
*
* @return object|null
*/
public function fetch(?string $class = null, mixed ...$constructor) : ?object
{
$this->checkIsFree();
$class ??= $this->fetchClass;
$constructor = $constructor ?: $this->fetchConstructor;
if ($constructor) {
// @phpstan-ignore-next-line
return $this->result->fetch_object($class, $constructor);
}
// @phpstan-ignore-next-line
return $this->result->fetch_object($class);
}
/**
* Fetches all rows as objects.
*
* @param string|null $class
* @param mixed ...$constructor
*
* @return array<int,object>
*/
public function fetchAll(?string $class = null, mixed ...$constructor) : array
{
$this->checkIsFree();
$all = [];
while ($row = $this->fetch($class, ...$constructor)) {
$all[] = $row;
}
return $all;
}
/**
* Fetches a specific row as object and move the cursor to the next.
*
* @param int $offset
* @param string|null $class
* @param mixed ...$constructor
*
* @return object
*/
public function fetchRow(int $offset, ?string $class = null, mixed ...$constructor) : object
{
$this->checkIsFree();
$this->moveCursor($offset);
return $this->fetch($class, ...$constructor);
}
/**
* Fetches the current row as array and move the cursor to the next.
*
* @return array<string, float|int|string|null>|null
*/
public function fetchArray() : ?array
{
$this->checkIsFree();
return $this->result->fetch_assoc(); // @phpstan-ignore-line
}
/**
* Fetches all rows as arrays.
*
* @return array<int,array<mixed>>
*/
public function fetchArrayAll() : array
{
$this->checkIsFree();
return $this->result->fetch_all(\MYSQLI_ASSOC);
}
/**
* Fetches a specific row as array and move the cursor to the next.
*
* @param int $offset
*
* @return array<string, float|int|string|null>
*/
public function fetchArrayRow(int $offset) : array
{
$this->checkIsFree();
$this->moveCursor($offset);
return $this->result->fetch_assoc(); // @phpstan-ignore-line
}
/**
* Gets the number of rows in the result set.
*
* @return int|string
*/
public function numRows() : int | string
{
$this->checkIsFree();
return $this->result->num_rows;
}
/**
* Returns an array of objects representing the fields in a result set.
*
* @return array<int,Field> an array of objects which contains field
* definition information
*/
public function fetchFields() : array
{
$this->checkIsFree();
$fields = [];
foreach ($this->result->fetch_fields() as $field) {
$fields[] = new Field($field);
}
return $fields;
}
}