summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php
blob: b48ab5d8397397487eff1006002b239504d41713 (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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PgSQL;

use Doctrine\DBAL\Driver\PgSQL\Exception\UnknownParameter;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use PgSql\Connection as PgSqlConnection;

use function assert;
use function is_resource;
use function ksort;
use function pg_escape_bytea;
use function pg_escape_identifier;
use function pg_get_result;
use function pg_last_error;
use function pg_query;
use function pg_result_error;
use function pg_send_execute;
use function stream_get_contents;

final class Statement implements StatementInterface
{
    /** @var array<int, mixed> */
    private array $parameters = [];

    /** @psalm-var array<int, ParameterType> */
    private array $parameterTypes = [];

    /** @param array<array-key, int> $parameterMap */
    public function __construct(
        private readonly PgSqlConnection $connection,
        private readonly string $name,
        private readonly array $parameterMap,
    ) {
    }

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

        @pg_query(
            $this->connection,
            'DEALLOCATE ' . pg_escape_identifier($this->connection, $this->name),
        );
    }

    /** {@inheritDoc} */
    public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void
    {
        if (! isset($this->parameterMap[$param])) {
            throw UnknownParameter::new((string) $param);
        }

        $this->parameters[$this->parameterMap[$param]]     = $value;
        $this->parameterTypes[$this->parameterMap[$param]] = $type;
    }

    /** {@inheritDoc} */
    public function execute(): Result
    {
        ksort($this->parameters);

        $escapedParameters = [];
        foreach ($this->parameters as $parameter => $value) {
            $escapedParameters[] = match ($this->parameterTypes[$parameter]) {
                ParameterType::BINARY, ParameterType::LARGE_OBJECT => $value === null
                    ? null
                    : pg_escape_bytea($this->connection, is_resource($value) ? stream_get_contents($value) : $value),
                default => $value,
            };
        }

        if (@pg_send_execute($this->connection, $this->name, $escapedParameters) !== true) {
            throw new Exception(pg_last_error($this->connection));
        }

        $result = @pg_get_result($this->connection);
        assert($result !== false);

        if ((bool) pg_result_error($result)) {
            throw Exception::fromResult($result);
        }

        return new Result($result);
    }
}