Failed Conditions
Push — type-registry ( 0931f1...33c798 )
by Michael
23:21
created

Statement::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Types\Type;
8
use IteratorAggregate;
9
use PDO;
10
use Throwable;
11
use function is_array;
12
use function is_string;
13
14
/**
15
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
16
 * for logging, DBAL mapping types, etc.
17
 */
18
class Statement implements IteratorAggregate, DriverStatement
19
{
20
    /**
21
     * The SQL statement.
22
     *
23
     * @var string
24
     */
25
    protected $sql;
26
27
    /**
28
     * The bound parameters.
29
     *
30
     * @var mixed[]
31
     */
32
    protected $params = [];
33
34
    /**
35
     * The parameter types.
36
     *
37
     * @var int[]|string[]
38
     */
39
    protected $types = [];
40
41
    /**
42
     * The underlying driver statement.
43
     *
44
     * @var \Doctrine\DBAL\Driver\Statement
45
     */
46
    protected $stmt;
47
48
    /**
49
     * The underlying database platform.
50
     *
51
     * @var AbstractPlatform
52
     */
53
    protected $platform;
54
55
    /**
56
     * The connection this statement is bound to and executed on.
57
     *
58
     * @var Connection
59
     */
60
    protected $conn;
61
62
    /**
63
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
64
     *
65
     * @param string     $sql  The SQL of the statement.
66
     * @param Connection $conn The connection on which the statement should be executed.
67
     */
68 1091
    public function __construct($sql, Connection $conn)
69
    {
70 1091
        $this->sql      = $sql;
71 1091
        $this->stmt     = $conn->getWrappedConnection()->prepare($sql);
72 1067
        $this->conn     = $conn;
73 1067
        $this->platform = $conn->getDatabasePlatform();
74 1067
    }
75
76
    /**
77
     * Binds a parameter value to the statement.
78
     *
79
     * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
80
     * If bound with a DBAL mapping type, the binding type is derived from the mapping
81
     * type and the value undergoes the conversion routines of the mapping type before
82
     * being bound.
83
     *
84
     * @param string|int $name  The name or position of the parameter.
85
     * @param mixed      $value The value of the parameter.
86
     * @param mixed      $type  Either a PDO binding type or a DBAL mapping type name or instance.
87
     *
88
     * @return bool TRUE on success, FALSE on failure.
89
     */
90 1019
    public function bindValue($name, $value, $type = ParameterType::STRING)
91
    {
92 1019
        $this->params[$name] = $value;
93 1019
        $this->types[$name]  = $type;
94 1019
        if ($type !== null) {
95 1019
            if (is_string($type)) {
96 844
                $type = Type::getType($type);
97
            }
98 1019
            if ($type instanceof Type) {
99 844
                $value       = $type->convertToDatabaseValue($value, $this->platform);
100 844
                $bindingType = $type->getBindingType();
101
            } else {
102 1019
                $bindingType = $type;
103
            }
104
105 1019
            return $this->stmt->bindValue($name, $value, $bindingType);
106
        }
107
108
        return $this->stmt->bindValue($name, $value);
109
    }
110
111
    /**
112
     * Binds a parameter to a value by reference.
113
     *
114
     * Binding a parameter by reference does not support DBAL mapping types.
115
     *
116
     * @param string|int $name   The name or position of the parameter.
117
     * @param mixed      $var    The reference to the variable to bind.
118
     * @param int        $type   The PDO binding type.
119
     * @param int|null   $length Must be specified when using an OUT bind
120
     *                           so that PHP allocates enough memory to hold the returned value.
121
     *
122
     * @return bool TRUE on success, FALSE on failure.
123
     */
124 1043
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
125
    {
126 1043
        $this->params[$name] = $var;
127 1043
        $this->types[$name]  = $type;
128
129 1043
        return $this->stmt->bindParam($name, $var, $type, $length);
130
    }
131
132
    /**
133
     * Executes the statement with the currently bound parameters.
134
     *
135
     * @param mixed[]|null $params
136
     *
137
     * @return bool TRUE on success, FALSE on failure.
138
     *
139
     * @throws DBALException
140
     */
141 1043
    public function execute($params = null)
142
    {
143 1043
        if (is_array($params)) {
144 868
            $this->params = $params;
145
        }
146
147 1043
        $logger = $this->conn->getConfiguration()->getSQLLogger();
148 1043
        if ($logger) {
149 1043
            $logger->startQuery($this->sql, $this->params, $this->types);
150
        }
151
152
        try {
153 1043
            $stmt = $this->stmt->execute($params);
154 24
        } catch (Throwable $ex) {
155 24
            if ($logger) {
156 24
                $logger->stopQuery();
157
            }
158 24
            throw DBALException::driverExceptionDuringQuery(
159 24
                $this->conn->getDriver(),
160 24
                $ex,
161 24
                $this->sql,
162 24
                $this->conn->resolveParams($this->params, $this->types)
163
            );
164
        }
165
166 1043
        if ($logger) {
167 1043
            $logger->stopQuery();
168
        }
169 1043
        $this->params = [];
170 1043
        $this->types  = [];
171
172 1043
        return $stmt;
173
    }
174
175
    /**
176
     * Closes the cursor, freeing the database resources used by this statement.
177
     *
178
     * @return bool TRUE on success, FALSE on failure.
179
     */
180 683
    public function closeCursor()
181
    {
182 683
        return $this->stmt->closeCursor();
183
    }
184
185
    /**
186
     * Returns the number of columns in the result set.
187
     *
188
     * @return int
189
     */
190
    public function columnCount()
191
    {
192
        return $this->stmt->columnCount();
193
    }
194
195
    /**
196
     * Fetches the SQLSTATE associated with the last operation on the statement.
197
     *
198
     * @return string|int|bool
199
     */
200
    public function errorCode()
201
    {
202
        return $this->stmt->errorCode();
203
    }
204
205
    /**
206
     * {@inheritDoc}
207
     */
208
    public function errorInfo()
209
    {
210
        return $this->stmt->errorInfo();
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 1067
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
217
    {
218 1067
        if ($arg2 === null) {
219 1067
            return $this->stmt->setFetchMode($fetchMode);
220
        }
221
222
        if ($arg3 === null) {
223
            return $this->stmt->setFetchMode($fetchMode, $arg2);
224
        }
225
226
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
227
    }
228
229
    /**
230
     * Required by interface IteratorAggregate.
231
     *
232
     * {@inheritdoc}
233
     */
234 916
    public function getIterator()
235
    {
236 916
        return $this->stmt;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242 1019
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
243
    {
244 1019
        return $this->stmt->fetch($fetchMode);
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 971
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
251
    {
252 971
        if ($fetchArgument) {
253 572
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
254
        }
255
256 971
        return $this->stmt->fetchAll($fetchMode);
257
    }
258
259
    /**
260
     * {@inheritDoc}
261
     */
262 940
    public function fetchColumn($columnIndex = 0)
263
    {
264 940
        return $this->stmt->fetchColumn($columnIndex);
265
    }
266
267
    /**
268
     * Returns the number of rows affected by the last execution of this statement.
269
     *
270
     * @return int The number of affected rows.
271
     */
272 192
    public function rowCount()
273
    {
274 192
        return $this->stmt->rowCount();
275
    }
276
277
    /**
278
     * Gets the wrapped driver statement.
279
     *
280
     * @return \Doctrine\DBAL\Driver\Statement
281
     */
282
    public function getWrappedStatement()
283
    {
284
        return $this->stmt;
285
    }
286
}
287