Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 LogicException;
11
use Traversable;
12
 
13
use function array_shift;
14
use function func_num_args;
15
 
16
class Result
17
{
18
    private DriverResult $result;
19
    private Connection $connection;
20
 
21
    /** @internal The result can be only instantiated by {@see Connection} or {@see Statement}. */
22
    public function __construct(DriverResult $result, Connection $connection)
23
    {
24
        $this->result     = $result;
25
        $this->connection = $connection;
26
    }
27
 
28
    /**
29
     * Returns the next row of the result as a numeric array or FALSE if there are no more rows.
30
     *
31
     * @return list<mixed>|false
32
     *
33
     * @throws Exception
34
     */
35
    public function fetchNumeric()
36
    {
37
        try {
38
            return $this->result->fetchNumeric();
39
        } catch (DriverException $e) {
40
            throw $this->connection->convertException($e);
41
        }
42
    }
43
 
44
    /**
45
     * Returns the next row of the result as an associative array or FALSE if there are no more rows.
46
     *
47
     * @return array<string,mixed>|false
48
     *
49
     * @throws Exception
50
     */
51
    public function fetchAssociative()
52
    {
53
        try {
54
            return $this->result->fetchAssociative();
55
        } catch (DriverException $e) {
56
            throw $this->connection->convertException($e);
57
        }
58
    }
59
 
60
    /**
61
     * Returns the first value of the next row of the result or FALSE if there are no more rows.
62
     *
63
     * @return mixed|false
64
     *
65
     * @throws Exception
66
     */
67
    public function fetchOne()
68
    {
69
        try {
70
            return $this->result->fetchOne();
71
        } catch (DriverException $e) {
72
            throw $this->connection->convertException($e);
73
        }
74
    }
75
 
76
    /**
77
     * Returns an array containing all of the result rows represented as numeric arrays.
78
     *
79
     * @return list<list<mixed>>
80
     *
81
     * @throws Exception
82
     */
83
    public function fetchAllNumeric(): array
84
    {
85
        try {
86
            return $this->result->fetchAllNumeric();
87
        } catch (DriverException $e) {
88
            throw $this->connection->convertException($e);
89
        }
90
    }
91
 
92
    /**
93
     * Returns an array containing all of the result rows represented as associative arrays.
94
     *
95
     * @return list<array<string,mixed>>
96
     *
97
     * @throws Exception
98
     */
99
    public function fetchAllAssociative(): array
100
    {
101
        try {
102
            return $this->result->fetchAllAssociative();
103
        } catch (DriverException $e) {
104
            throw $this->connection->convertException($e);
105
        }
106
    }
107
 
108
    /**
109
     * Returns an array containing the values of the first column of the result.
110
     *
111
     * @return array<mixed,mixed>
112
     *
113
     * @throws Exception
114
     */
115
    public function fetchAllKeyValue(): array
116
    {
117
        $this->ensureHasKeyValue();
118
 
119
        $data = [];
120
 
121
        foreach ($this->fetchAllNumeric() as [$key, $value]) {
122
            $data[$key] = $value;
123
        }
124
 
125
        return $data;
126
    }
127
 
128
    /**
129
     * Returns an associative array with the keys mapped to the first column and the values being
130
     * an associative array representing the rest of the columns and their values.
131
     *
132
     * @return array<mixed,array<string,mixed>>
133
     *
134
     * @throws Exception
135
     */
136
    public function fetchAllAssociativeIndexed(): array
137
    {
138
        $data = [];
139
 
140
        foreach ($this->fetchAllAssociative() as $row) {
141
            $data[array_shift($row)] = $row;
142
        }
143
 
144
        return $data;
145
    }
146
 
147
    /**
148
     * @return list<mixed>
149
     *
150
     * @throws Exception
151
     */
152
    public function fetchFirstColumn(): array
153
    {
154
        try {
155
            return $this->result->fetchFirstColumn();
156
        } catch (DriverException $e) {
157
            throw $this->connection->convertException($e);
158
        }
159
    }
160
 
161
    /**
162
     * @return Traversable<int,list<mixed>>
163
     *
164
     * @throws Exception
165
     */
166
    public function iterateNumeric(): Traversable
167
    {
168
        while (($row = $this->fetchNumeric()) !== false) {
169
            yield $row;
170
        }
171
    }
172
 
173
    /**
174
     * @return Traversable<int,array<string,mixed>>
175
     *
176
     * @throws Exception
177
     */
178
    public function iterateAssociative(): Traversable
179
    {
180
        while (($row = $this->fetchAssociative()) !== false) {
181
            yield $row;
182
        }
183
    }
184
 
185
    /**
186
     * {@inheritDoc}
187
     *
188
     * @throws Exception
189
     */
190
    public function iterateKeyValue(): Traversable
191
    {
192
        $this->ensureHasKeyValue();
193
 
194
        foreach ($this->iterateNumeric() as [$key, $value]) {
195
            yield $key => $value;
196
        }
197
    }
198
 
199
    /**
200
     * Returns an iterator over the result set with the keys mapped to the first column and the values being
201
     * an associative array representing the rest of the columns and their values.
202
     *
203
     * @return Traversable<mixed,array<string,mixed>>
204
     *
205
     * @throws Exception
206
     */
207
    public function iterateAssociativeIndexed(): Traversable
208
    {
209
        foreach ($this->iterateAssociative() as $row) {
210
            yield array_shift($row) => $row;
211
        }
212
    }
213
 
214
    /**
215
     * @return Traversable<int,mixed>
216
     *
217
     * @throws Exception
218
     */
219
    public function iterateColumn(): Traversable
220
    {
221
        while (($value = $this->fetchOne()) !== false) {
222
            yield $value;
223
        }
224
    }
225
 
226
    /** @throws Exception */
227
    public function rowCount(): int
228
    {
229
        try {
230
            return $this->result->rowCount();
231
        } catch (DriverException $e) {
232
            throw $this->connection->convertException($e);
233
        }
234
    }
235
 
236
    /** @throws Exception */
237
    public function columnCount(): int
238
    {
239
        try {
240
            return $this->result->columnCount();
241
        } catch (DriverException $e) {
242
            throw $this->connection->convertException($e);
243
        }
244
    }
245
 
246
    public function free(): void
247
    {
248
        $this->result->free();
249
    }
250
 
251
    /** @throws Exception */
252
    private function ensureHasKeyValue(): void
253
    {
254
        $columnCount = $this->columnCount();
255
 
256
        if ($columnCount < 2) {
257
            throw NoKeyValue::fromColumnCount($columnCount);
258
        }
259
    }
260
 
261
    /**
262
     * BC layer for a wide-spread use-case of old DBAL APIs
263
     *
264
     * @deprecated This API is deprecated and will be removed after 2022
265
     *
266
     * @return mixed
267
     *
268
     * @throws Exception
269
     */
270
    public function fetch(int $mode = FetchMode::ASSOCIATIVE)
271
    {
272
        if (func_num_args() > 1) {
273
            throw new LogicException('Only invocations with one argument are still supported by this legacy API.');
274
        }
275
 
276
        if ($mode === FetchMode::ASSOCIATIVE) {
277
            return $this->fetchAssociative();
278
        }
279
 
280
        if ($mode === FetchMode::NUMERIC) {
281
            return $this->fetchNumeric();
282
        }
283
 
284
        if ($mode === FetchMode::COLUMN) {
285
            return $this->fetchOne();
286
        }
287
 
288
        throw new LogicException('Only fetch modes declared on Doctrine\DBAL\FetchMode are supported by legacy API.');
289
    }
290
 
291
    /**
292
     * BC layer for a wide-spread use-case of old DBAL APIs
293
     *
294
     * @deprecated This API is deprecated and will be removed after 2022
295
     *
296
     * @return list<mixed>
297
     *
298
     * @throws Exception
299
     */
300
    public function fetchAll(int $mode = FetchMode::ASSOCIATIVE): array
301
    {
302
        if (func_num_args() > 1) {
303
            throw new LogicException('Only invocations with one argument are still supported by this legacy API.');
304
        }
305
 
306
        if ($mode === FetchMode::ASSOCIATIVE) {
307
            return $this->fetchAllAssociative();
308
        }
309
 
310
        if ($mode === FetchMode::NUMERIC) {
311
            return $this->fetchAllNumeric();
312
        }
313
 
314
        if ($mode === FetchMode::COLUMN) {
315
            return $this->fetchFirstColumn();
316
        }
317
 
318
        throw new LogicException('Only fetch modes declared on Doctrine\DBAL\FetchMode are supported by legacy API.');
319
    }
320
}