Failed Conditions
Pull Request — master (#3319)
by Massimiliano
11:33
created

Statement   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Test Coverage

Coverage 82.86%

Importance

Changes 0
Metric Value
wmc 26
eloc 62
dl 0
loc 265
ccs 58
cts 70
cp 0.8286
rs 10
c 0
b 0
f 0

15 Methods

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