Subversion-Projekte lars-tiefland.laravel_shop

Revision

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