summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/PgSQL/Result.php
blob: 954758cc0fc6a3184bc9744161b9d139b9f0a9c1 (plain)
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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PgSQL;

use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\PgSQL\Exception\UnexpectedValue;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use PgSql\Result as PgSqlResult;

use function array_keys;
use function array_map;
use function assert;
use function hex2bin;
use function pg_affected_rows;
use function pg_fetch_all;
use function pg_fetch_all_columns;
use function pg_fetch_assoc;
use function pg_fetch_row;
use function pg_field_name;
use function pg_field_type;
use function pg_free_result;
use function pg_num_fields;
use function substr;

use const PGSQL_ASSOC;
use const PGSQL_NUM;
use const PHP_INT_SIZE;

final class Result implements ResultInterface
{
    private ?PgSqlResult $result;

    public function __construct(PgSqlResult $result)
    {
        $this->result = $result;
    }

    public function __destruct()
    {
        if (! isset($this->result)) {
            return;
        }

        $this->free();
    }

    /** {@inheritDoc} */
    public function fetchNumeric(): array|false
    {
        if ($this->result === null) {
            return false;
        }

        $row = pg_fetch_row($this->result);
        if ($row === false) {
            return false;
        }

        return $this->mapNumericRow($row, $this->fetchNumericColumnTypes());
    }

    /** {@inheritDoc} */
    public function fetchAssociative(): array|false
    {
        if ($this->result === null) {
            return false;
        }

        $row = pg_fetch_assoc($this->result);
        if ($row === false) {
            return false;
        }

        return $this->mapAssociativeRow($row, $this->fetchAssociativeColumnTypes());
    }

    /** {@inheritDoc} */
    public function fetchOne(): mixed
    {
        return FetchUtils::fetchOne($this);
    }

    /** {@inheritDoc} */
    public function fetchAllNumeric(): array
    {
        if ($this->result === null) {
            return [];
        }

        $types = $this->fetchNumericColumnTypes();

        return array_map(
            fn (array $row) => $this->mapNumericRow($row, $types),
            pg_fetch_all($this->result, PGSQL_NUM),
        );
    }

    /** {@inheritDoc} */
    public function fetchAllAssociative(): array
    {
        if ($this->result === null) {
            return [];
        }

        $types = $this->fetchAssociativeColumnTypes();

        return array_map(
            fn (array $row) => $this->mapAssociativeRow($row, $types),
            pg_fetch_all($this->result, PGSQL_ASSOC),
        );
    }

    /** {@inheritDoc} */
    public function fetchFirstColumn(): array
    {
        if ($this->result === null) {
            return [];
        }

        $postgresType = pg_field_type($this->result, 0);

        return array_map(
            fn ($value) => $this->mapType($postgresType, $value),
            pg_fetch_all_columns($this->result),
        );
    }

    public function rowCount(): int
    {
        if ($this->result === null) {
            return 0;
        }

        return pg_affected_rows($this->result);
    }

    public function columnCount(): int
    {
        if ($this->result === null) {
            return 0;
        }

        return pg_num_fields($this->result);
    }

    public function free(): void
    {
        if ($this->result === null) {
            return;
        }

        pg_free_result($this->result);
        $this->result = null;
    }

    /** @return array<int, string> */
    private function fetchNumericColumnTypes(): array
    {
        assert($this->result !== null);

        $types     = [];
        $numFields = pg_num_fields($this->result);
        for ($i = 0; $i < $numFields; ++$i) {
            $types[$i] = pg_field_type($this->result, $i);
        }

        return $types;
    }

    /** @return array<string, string> */
    private function fetchAssociativeColumnTypes(): array
    {
        assert($this->result !== null);

        $types     = [];
        $numFields = pg_num_fields($this->result);
        for ($i = 0; $i < $numFields; ++$i) {
            $types[pg_field_name($this->result, $i)] = pg_field_type($this->result, $i);
        }

        return $types;
    }

    /**
     * @param list<string|null>  $row
     * @param array<int, string> $types
     *
     * @return list<mixed>
     */
    private function mapNumericRow(array $row, array $types): array
    {
        assert($this->result !== null);

        return array_map(
            fn ($value, $field) => $this->mapType($types[$field], $value),
            $row,
            array_keys($row),
        );
    }

    /**
     * @param array<string, string|null> $row
     * @param array<string, string>      $types
     *
     * @return array<string, mixed>
     */
    private function mapAssociativeRow(array $row, array $types): array
    {
        assert($this->result !== null);

        $mappedRow = [];
        foreach ($row as $field => $value) {
            $mappedRow[$field] = $this->mapType($types[$field], $value);
        }

        return $mappedRow;
    }

    private function mapType(string $postgresType, ?string $value): string|int|float|bool|null
    {
        if ($value === null) {
            return null;
        }

        return match ($postgresType) {
            'bool' => match ($value) {
                't' => true,
                'f' => false,
                default => throw UnexpectedValue::new($value, $postgresType),
            },
            'bytea' => hex2bin(substr($value, 2)),
            'float4', 'float8' => (float) $value,
            'int2', 'int4' => (int) $value,
            'int8' => PHP_INT_SIZE >= 8 ? (int) $value : $value,
            default => $value,
        };
    }
}