diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Result.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Result.php | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Result.php b/vendor/doctrine/dbal/src/Result.php new file mode 100644 index 0000000..6bc7c6d --- /dev/null +++ b/vendor/doctrine/dbal/src/Result.php | |||
@@ -0,0 +1,270 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\Exception as DriverException; | ||
8 | use Doctrine\DBAL\Driver\Result as DriverResult; | ||
9 | use Doctrine\DBAL\Exception\NoKeyValue; | ||
10 | use Traversable; | ||
11 | |||
12 | use function array_shift; | ||
13 | use function assert; | ||
14 | use function count; | ||
15 | |||
16 | class Result | ||
17 | { | ||
18 | /** @internal The result can be only instantiated by {@see Connection} or {@see Statement}. */ | ||
19 | public function __construct(private readonly DriverResult $result, private readonly Connection $connection) | ||
20 | { | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * Returns the next row of the result as a numeric array or FALSE if there are no more rows. | ||
25 | * | ||
26 | * @return list<mixed>|false | ||
27 | * | ||
28 | * @throws Exception | ||
29 | */ | ||
30 | public function fetchNumeric(): array|false | ||
31 | { | ||
32 | try { | ||
33 | return $this->result->fetchNumeric(); | ||
34 | } catch (DriverException $e) { | ||
35 | throw $this->connection->convertException($e); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Returns the next row of the result as an associative array or FALSE if there are no more rows. | ||
41 | * | ||
42 | * @return array<string,mixed>|false | ||
43 | * | ||
44 | * @throws Exception | ||
45 | */ | ||
46 | public function fetchAssociative(): array|false | ||
47 | { | ||
48 | try { | ||
49 | return $this->result->fetchAssociative(); | ||
50 | } catch (DriverException $e) { | ||
51 | throw $this->connection->convertException($e); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Returns the first value of the next row of the result or FALSE if there are no more rows. | ||
57 | * | ||
58 | * @throws Exception | ||
59 | */ | ||
60 | public function fetchOne(): mixed | ||
61 | { | ||
62 | try { | ||
63 | return $this->result->fetchOne(); | ||
64 | } catch (DriverException $e) { | ||
65 | throw $this->connection->convertException($e); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Returns an array containing all of the result rows represented as numeric arrays. | ||
71 | * | ||
72 | * @return list<list<mixed>> | ||
73 | * | ||
74 | * @throws Exception | ||
75 | */ | ||
76 | public function fetchAllNumeric(): array | ||
77 | { | ||
78 | try { | ||
79 | return $this->result->fetchAllNumeric(); | ||
80 | } catch (DriverException $e) { | ||
81 | throw $this->connection->convertException($e); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Returns an array containing all of the result rows represented as associative arrays. | ||
87 | * | ||
88 | * @return list<array<string,mixed>> | ||
89 | * | ||
90 | * @throws Exception | ||
91 | */ | ||
92 | public function fetchAllAssociative(): array | ||
93 | { | ||
94 | try { | ||
95 | return $this->result->fetchAllAssociative(); | ||
96 | } catch (DriverException $e) { | ||
97 | throw $this->connection->convertException($e); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Returns an array containing the values of the first column of the result. | ||
103 | * | ||
104 | * @return array<mixed,mixed> | ||
105 | * | ||
106 | * @throws Exception | ||
107 | */ | ||
108 | public function fetchAllKeyValue(): array | ||
109 | { | ||
110 | $this->ensureHasKeyValue(); | ||
111 | |||
112 | $data = []; | ||
113 | |||
114 | foreach ($this->fetchAllNumeric() as $row) { | ||
115 | assert(count($row) >= 2); | ||
116 | [$key, $value] = $row; | ||
117 | $data[$key] = $value; | ||
118 | } | ||
119 | |||
120 | return $data; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Returns an associative array with the keys mapped to the first column and the values being | ||
125 | * an associative array representing the rest of the columns and their values. | ||
126 | * | ||
127 | * @return array<mixed,array<string,mixed>> | ||
128 | * | ||
129 | * @throws Exception | ||
130 | */ | ||
131 | public function fetchAllAssociativeIndexed(): array | ||
132 | { | ||
133 | $data = []; | ||
134 | |||
135 | foreach ($this->fetchAllAssociative() as $row) { | ||
136 | $data[array_shift($row)] = $row; | ||
137 | } | ||
138 | |||
139 | return $data; | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * @return list<mixed> | ||
144 | * | ||
145 | * @throws Exception | ||
146 | */ | ||
147 | public function fetchFirstColumn(): array | ||
148 | { | ||
149 | try { | ||
150 | return $this->result->fetchFirstColumn(); | ||
151 | } catch (DriverException $e) { | ||
152 | throw $this->connection->convertException($e); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * @return Traversable<int,list<mixed>> | ||
158 | * | ||
159 | * @throws Exception | ||
160 | */ | ||
161 | public function iterateNumeric(): Traversable | ||
162 | { | ||
163 | while (($row = $this->fetchNumeric()) !== false) { | ||
164 | yield $row; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * @return Traversable<int,array<string,mixed>> | ||
170 | * | ||
171 | * @throws Exception | ||
172 | */ | ||
173 | public function iterateAssociative(): Traversable | ||
174 | { | ||
175 | while (($row = $this->fetchAssociative()) !== false) { | ||
176 | yield $row; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * @return Traversable<mixed, mixed> | ||
182 | * | ||
183 | * @throws Exception | ||
184 | */ | ||
185 | public function iterateKeyValue(): Traversable | ||
186 | { | ||
187 | $this->ensureHasKeyValue(); | ||
188 | |||
189 | foreach ($this->iterateNumeric() as $row) { | ||
190 | assert(count($row) >= 2); | ||
191 | [$key, $value] = $row; | ||
192 | |||
193 | yield $key => $value; | ||
194 | } | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * Returns an iterator over the result set with the keys mapped to the first column and the values being | ||
199 | * an associative array representing the rest of the columns and their values. | ||
200 | * | ||
201 | * @return Traversable<mixed,array<string,mixed>> | ||
202 | * | ||
203 | * @throws Exception | ||
204 | */ | ||
205 | public function iterateAssociativeIndexed(): Traversable | ||
206 | { | ||
207 | foreach ($this->iterateAssociative() as $row) { | ||
208 | yield array_shift($row) => $row; | ||
209 | } | ||
210 | } | ||
211 | |||
212 | /** | ||
213 | * @return Traversable<int,mixed> | ||
214 | * | ||
215 | * @throws Exception | ||
216 | */ | ||
217 | public function iterateColumn(): Traversable | ||
218 | { | ||
219 | while (($value = $this->fetchOne()) !== false) { | ||
220 | yield $value; | ||
221 | } | ||
222 | } | ||
223 | |||
224 | /** | ||
225 | * Returns the number of rows affected by the DELETE, INSERT, or UPDATE statement that produced the result. | ||
226 | * | ||
227 | * If the statement executed a SELECT query or a similar platform-specific SQL (e.g. DESCRIBE, SHOW, etc.), | ||
228 | * some database drivers may return the number of rows returned by that query. However, this behaviour | ||
229 | * is not guaranteed for all drivers and should not be relied on in portable applications. | ||
230 | * | ||
231 | * If the number of rows exceeds {@see PHP_INT_MAX}, it might be returned as string if the driver supports it. | ||
232 | * | ||
233 | * @return int|numeric-string | ||
234 | * | ||
235 | * @throws Exception | ||
236 | */ | ||
237 | public function rowCount(): int|string | ||
238 | { | ||
239 | try { | ||
240 | return $this->result->rowCount(); | ||
241 | } catch (DriverException $e) { | ||
242 | throw $this->connection->convertException($e); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | /** @throws Exception */ | ||
247 | public function columnCount(): int | ||
248 | { | ||
249 | try { | ||
250 | return $this->result->columnCount(); | ||
251 | } catch (DriverException $e) { | ||
252 | throw $this->connection->convertException($e); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | public function free(): void | ||
257 | { | ||
258 | $this->result->free(); | ||
259 | } | ||
260 | |||
261 | /** @throws Exception */ | ||
262 | private function ensureHasKeyValue(): void | ||
263 | { | ||
264 | $columnCount = $this->columnCount(); | ||
265 | |||
266 | if ($columnCount < 2) { | ||
267 | throw NoKeyValue::fromColumnCount($columnCount); | ||
268 | } | ||
269 | } | ||
270 | } | ||