Completed
Pull Request — 2.10.x (#3984)
by Craig
61:21 queued 57:56
created

Statement   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Test Coverage

Coverage 85.92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 27
eloc 64
c 2
b 0
f 0
dl 0
loc 271
ccs 61
cts 71
cp 0.8592
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A errorInfo() 0 3 1
A __construct() 0 6 1
A bindValue() 0 19 4
A columnCount() 0 3 1
A closeCursor() 0 3 1
A bindParam() 0 6 1
A errorCode() 0 3 1
A setFetchMode() 0 11 3
A fetch() 0 3 1
A getIterator() 0 3 1
B execute() 0 32 6
A fetchAll() 0 11 3
A getWrappedStatement() 0 3 1
A rowCount() 0 3 1
A fetchColumn() 0 3 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 1124
    public function __construct($sql, Connection $conn)
69
    {
70 1124
        $this->sql      = $sql;
71 1124
        $this->stmt     = $conn->getWrappedConnection()->prepare($sql);
72 1123
        $this->conn     = $conn;
73 1123
        $this->platform = $conn->getDatabasePlatform();
74 1123
    }
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 987
    public function bindValue($name, $value, $type = ParameterType::STRING)
91
    {
92 987
        $this->params[$name] = $value;
93 987
        $this->types[$name]  = $type;
94 987
        if ($type !== null) {
95 987
            if (is_string($type)) {
96 813
                $type = Type::getType($type);
97
            }
98 987
            if ($type instanceof Type) {
99 814
                $value       = $type->convertToDatabaseValue($value, $this->platform);
100 814
                $bindingType = $type->getBindingType();
101
            } else {
102 984
                $bindingType = $type;
103
            }
104
105 987
            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 1010
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
125
    {
126 1010
        $this->params[$name] = $var;
127 1010
        $this->types[$name]  = $type;
128
129 1010
        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 1113
    public function execute($params = null)
142
    {
143 1113
        if (is_array($params)) {
144 845
            $this->params = $params;
145
        }
146
147 1113
        $logger = $this->conn->getConfiguration()->getSQLLogger();
148 1113
        if ($logger) {
149 1113
            $logger->startQuery($this->sql, $this->params, $this->types);
150
        }
151
152
        try {
153 1113
            $stmt = $this->stmt->execute($params);
154 47
        } catch (Throwable $ex) {
155 47
            if ($logger) {
156 47
                $logger->stopQuery();
157
            }
158 47
            throw DBALException::driverExceptionDuringQuery(
159 47
                $this->conn->getDriver(),
160
                $ex,
161 47
                $this->sql,
162 47
                $this->conn->resolveParams($this->params, $this->types)
163
            );
164
        }
165
166 1112
        if ($logger) {
167 1112
            $logger->stopQuery();
168
        }
169 1112
        $this->params = [];
170 1112
        $this->types  = [];
171
172 1112
        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 688
    public function closeCursor()
181
    {
182 688
        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 1118
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
217
    {
218 1118
        if ($arg2 === null) {
219 1118
            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 881
    public function getIterator()
235
    {
236 881
        return $this->stmt;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242 996
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
243
    {
244 996
        return $this->stmt->fetch($fetchMode);
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 941
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
251
    {
252 941
        if ($ctorArgs !== null) {
253 24
            return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
254
        }
255
256 940
        if ($fetchArgument !== null) {
257 573
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
258
        }
259
260 940
        return $this->stmt->fetchAll($fetchMode);
261
    }
262
263
    /**
264
     * {@inheritDoc}
265
     */
266 916
    public function fetchColumn($columnIndex = 0)
267
    {
268 916
        return $this->stmt->fetchColumn($columnIndex);
269
    }
270
271
    /**
272
     * Returns the number of rows affected by the last execution of this statement.
273
     *
274
     * @return int The number of affected rows.
275
     */
276 211
    public function rowCount()
277
    {
278 211
        return $this->stmt->rowCount();
279
    }
280
281
    /**
282
     * Gets the wrapped driver statement.
283
     *
284
     * @return \Doctrine\DBAL\Driver\Statement
285
     */
286 32
    public function getWrappedStatement()
287
    {
288 32
        return $this->stmt;
289
    }
290
}
291