Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\PgSQL;
4
 
5
use Doctrine\DBAL\Driver\PgSQL\Exception\UnknownParameter;
6
use Doctrine\DBAL\Driver\Statement as StatementInterface;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\Deprecations\Deprecation;
9
use PgSql\Connection as PgSqlConnection;
10
use TypeError;
11
 
12
use function assert;
13
use function func_num_args;
14
use function get_class;
15
use function gettype;
16
use function is_int;
17
use function is_object;
18
use function is_resource;
19
use function ksort;
20
use function pg_escape_bytea;
21
use function pg_escape_identifier;
22
use function pg_get_result;
23
use function pg_last_error;
24
use function pg_query;
25
use function pg_result_error;
26
use function pg_send_execute;
27
use function sprintf;
28
 
29
final class Statement implements StatementInterface
30
{
31
    /** @var PgSqlConnection|resource */
32
    private $connection;
33
 
34
    private string $name;
35
 
36
    /** @var array<array-key, int> */
37
    private array $parameterMap;
38
 
39
    /** @var array<int, mixed> */
40
    private array $parameters = [];
41
 
42
    /** @psalm-var array<int, int> */
43
    private array $parameterTypes = [];
44
 
45
    /**
46
     * @param PgSqlConnection|resource $connection
47
     * @param array<array-key, int>    $parameterMap
48
     */
49
    public function __construct($connection, string $name, array $parameterMap)
50
    {
51
        if (! is_resource($connection) && ! $connection instanceof PgSqlConnection) {
52
            throw new TypeError(sprintf(
53
                'Expected connection to be a resource or an instance of %s, got %s.',
54
                PgSqlConnection::class,
55
                is_object($connection) ? get_class($connection) : gettype($connection),
56
            ));
57
        }
58
 
59
        $this->connection   = $connection;
60
        $this->name         = $name;
61
        $this->parameterMap = $parameterMap;
62
    }
63
 
64
    public function __destruct()
65
    {
66
        if (! isset($this->connection)) {
67
            return;
68
        }
69
 
70
        @pg_query(
71
            $this->connection,
72
            'DEALLOCATE ' . pg_escape_identifier($this->connection, $this->name),
73
        );
74
    }
75
 
76
    /** {@inheritdoc} */
77
    public function bindValue($param, $value, $type = ParameterType::STRING): bool
78
    {
79
        if (! isset($this->parameterMap[$param])) {
80
            throw UnknownParameter::new((string) $param);
81
        }
82
 
83
        $this->parameters[$this->parameterMap[$param]]     = $value;
84
        $this->parameterTypes[$this->parameterMap[$param]] = $type;
85
 
86
        return true;
87
    }
88
 
89
    /** {@inheritdoc} */
90
    public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
91
    {
92
        Deprecation::trigger(
93
            'doctrine/dbal',
94
            'https://github.com/doctrine/dbal/pull/5563',
95
            '%s is deprecated. Use bindValue() instead.',
96
            __METHOD__,
97
        );
98
 
99
        if (func_num_args() < 3) {
100
            Deprecation::trigger(
101
                'doctrine/dbal',
102
                'https://github.com/doctrine/dbal/pull/5558',
103
                'Not passing $type to Statement::bindParam() is deprecated.'
104
                . ' Pass the type corresponding to the parameter being bound.',
105
            );
106
        }
107
 
108
        if (func_num_args() > 4) {
109
            Deprecation::triggerIfCalledFromOutside(
110
                'doctrine/dbal',
111
                'https://github.com/doctrine/dbal/issues/4533',
112
                'The $driverOptions argument of Statement::bindParam() is deprecated.',
113
            );
114
        }
115
 
116
        if (! isset($this->parameterMap[$param])) {
117
            throw UnknownParameter::new((string) $param);
118
        }
119
 
120
        $this->parameters[$this->parameterMap[$param]]     = &$variable;
121
        $this->parameterTypes[$this->parameterMap[$param]] = $type;
122
 
123
        return true;
124
    }
125
 
126
    /** {@inheritdoc} */
127
    public function execute($params = null): Result
128
    {
129
        if ($params !== null) {
130
            Deprecation::trigger(
131
                'doctrine/dbal',
132
                'https://github.com/doctrine/dbal/pull/5556',
133
                'Passing $params to Statement::execute() is deprecated. Bind parameters using'
134
                . ' Statement::bindParam() or Statement::bindValue() instead.',
135
            );
136
 
137
            foreach ($params as $param => $value) {
138
                if (is_int($param)) {
139
                    $this->bindValue($param + 1, $value, ParameterType::STRING);
140
                } else {
141
                    $this->bindValue($param, $value, ParameterType::STRING);
142
                }
143
            }
144
        }
145
 
146
        ksort($this->parameters);
147
 
148
        $escapedParameters = [];
149
        foreach ($this->parameters as $parameter => $value) {
150
            switch ($this->parameterTypes[$parameter]) {
151
                case ParameterType::BINARY:
152
                case ParameterType::LARGE_OBJECT:
153
                    $escapedParameters[] = $value === null ? null : pg_escape_bytea($this->connection, $value);
154
                    break;
155
                default:
156
                    $escapedParameters[] = $value;
157
            }
158
        }
159
 
160
        if (@pg_send_execute($this->connection, $this->name, $escapedParameters) !== true) {
161
            throw new Exception(pg_last_error($this->connection));
162
        }
163
 
164
        $result = @pg_get_result($this->connection);
165
        assert($result !== false);
166
 
167
        if ((bool) pg_result_error($result)) {
168
            throw Exception::fromResult($result);
169
        }
170
 
171
        return new Result($result);
172
    }
173
}