Completed
Push — develop ( 361a2b...a96e4b )
by Marco
20s queued 14s
created

Statement::errorInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL;
6
7
use Doctrine\DBAL\Driver\DriverException;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Types\Type;
11
use IteratorAggregate;
12
use Throwable;
13
use function is_array;
14
use function is_string;
15
16
/**
17
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
18
 * for logging, DBAL mapping types, etc.
19
 */
20
class Statement implements IteratorAggregate, DriverStatement
21
{
22
    /**
23
     * The SQL statement.
24
     *
25
     * @var string
26
     */
27
    protected $sql;
28
29
    /**
30
     * The bound parameters.
31
     *
32
     * @var mixed[]
33
     */
34
    protected $params = [];
35
36
    /**
37
     * The parameter types.
38
     *
39
     * @var int[]|string[]
40
     */
41
    protected $types = [];
42
43
    /**
44
     * The underlying driver statement.
45
     *
46
     * @var \Doctrine\DBAL\Driver\Statement
47
     */
48
    protected $stmt;
49
50
    /**
51
     * The underlying database platform.
52
     *
53
     * @var AbstractPlatform
54
     */
55
    protected $platform;
56
57
    /**
58
     * The connection this statement is bound to and executed on.
59
     *
60
     * @var Connection
61
     */
62
    protected $conn;
63
64
    /**
65
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
66
     *
67
     * @param string     $sql  The SQL of the statement.
68 1110
     * @param Connection $conn The connection on which the statement should be executed.
69
     */
70 1110
    public function __construct($sql, Connection $conn)
71 1110
    {
72 1085
        $this->sql      = $sql;
73 1085
        $this->stmt     = $conn->getWrappedConnection()->prepare($sql);
74 1085
        $this->conn     = $conn;
75
        $this->platform = $conn->getDatabasePlatform();
76
    }
77
78
    /**
79
     * Binds a parameter value to the statement.
80
     *
81
     * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
82
     * If bound with a DBAL mapping type, the binding type is derived from the mapping
83
     * type and the value undergoes the conversion routines of the mapping type before
84
     * being bound.
85
     *
86
     * @param string|int $name  The name or position of the parameter.
87
     * @param mixed      $value The value of the parameter.
88
     * @param mixed      $type  Either a PDO binding type or a DBAL mapping type name or instance.
89
     *
90
     * @throws DBALException
91 1002
     * @throws DriverException
92
     */
93 1002
    public function bindValue($name, $value, $type = ParameterType::STRING) : void
94 1002
    {
95
        $this->params[$name] = $value;
96 1002
        $this->types[$name]  = $type;
97 820
98
        if (is_string($type)) {
99
            $type = Type::getType($type);
100 1002
        }
101 821
102 821
        if ($type instanceof Type) {
103
            $value       = $type->convertToDatabaseValue($value, $this->platform);
104 999
            $bindingType = $type->getBindingType();
105
        } else {
106
            $bindingType = $type;
107 1002
        }
108 1002
109
        $this->stmt->bindValue($name, $value, $bindingType);
0 ignored issues
show
Bug introduced by
It seems like $bindingType can also be of type Doctrine\DBAL\Types\Type; however, parameter $type of Doctrine\DBAL\Driver\Statement::bindValue() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        $this->stmt->bindValue($name, $value, /** @scrutinizer ignore-type */ $bindingType);
Loading history...
110
    }
111
112
    /**
113
     * Binds a parameter to a value by reference.
114
     *
115
     * Binding a parameter by reference does not support DBAL mapping types.
116
     *
117
     * @param string|int $name   The name or position of the parameter.
118
     * @param mixed      $var    The reference to the variable to bind.
119
     * @param int        $type   The PDO binding type.
120
     * @param int|null   $length Must be specified when using an OUT bind
121
     *                           so that PHP allocates enough memory to hold the returned value.
122
     *
123 1026
     * @throws DriverException
124
     */
125 1026
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) : void
126 1026
    {
127
        $this->params[$name] = $var;
128 1026
        $this->types[$name]  = $type;
129 1026
130
        $this->stmt->bindParam($name, $var, $type, $length);
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     *
136 1053
     * @throws DBALException
137
     */
138 1053
    public function execute($params = null) : void
139 847
    {
140
        if (is_array($params)) {
141
            $this->params = $params;
142 1053
        }
143 1053
144
        $logger = $this->conn->getConfiguration()->getSQLLogger();
145
        $logger->startQuery($this->sql, $this->params, $this->types);
146 1053
147 180
        try {
148 180
            $this->stmt->execute($params);
149 180
        } catch (Throwable $ex) {
150 180
            throw DBALException::driverExceptionDuringQuery(
151 180
                $this->conn->getDriver(),
152 180
                $ex,
153
                $this->sql,
154 1051
                $this->conn->resolveParams($this->params, $this->types)
155 1053
            );
156
        } finally {
157
            $logger->stopQuery();
158 1051
        }
159 1051
160 1051
        $this->params = [];
161
        $this->types  = [];
162
    }
163
164
    /**
165 664
     * {@inheritDoc}
166
     */
167 664
    public function closeCursor() : void
168 664
    {
169
        $this->stmt->closeCursor();
170
    }
171
172
    /**
173
     * Returns the number of columns in the result set.
174
     *
175
     * @return int
176
     */
177
    public function columnCount()
178
    {
179
        return $this->stmt->columnCount();
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function setFetchMode($fetchMode, ...$args) : void
186
    {
187
        $this->stmt->setFetchMode($fetchMode, ...$args);
188
    }
189
190
    /**
191
     * Required by interface IteratorAggregate.
192
     *
193
     * {@inheritdoc}
194
     */
195
    public function getIterator()
196
    {
197
        return $this->stmt;
198
    }
199
200
    /**
201 1081
     * {@inheritdoc}
202
     */
203 1081
    public function fetch($fetchMode = null, ...$args)
204 1081
    {
205
        return $this->stmt->fetch($fetchMode, ...$args);
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 891
    public function fetchAll($fetchMode = null, ...$args)
212
    {
213 891
        return $this->stmt->fetchAll($fetchMode, ...$args);
214
    }
215
216
    /**
217
     * {@inheritDoc}
218
     */
219 1003
    public function fetchColumn($columnIndex = 0)
220
    {
221 1003
        return $this->stmt->fetchColumn($columnIndex);
222
    }
223
224
    /**
225
     * Returns the number of rows affected by the last execution of this statement.
226
     *
227 953
     * @return int The number of affected rows.
228
     */
229 953
    public function rowCount() : int
230
    {
231
        return $this->stmt->rowCount();
232
    }
233
234
    /**
235 927
     * Gets the wrapped driver statement.
236
     *
237 927
     * @return \Doctrine\DBAL\Driver\Statement
238
     */
239
    public function getWrappedStatement()
240
    {
241
        return $this->stmt;
242
    }
243
}
244