Failed Conditions
Push — phpstan ( 752f9f...1acc4a )
by Michael
21:57
created

Statement::errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL;
21
22
use Doctrine\DBAL\Types\Type;
23
use Doctrine\DBAL\Driver\Statement as DriverStatement;
24
use function is_array;
25
use function is_string;
26
27
/**
28
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
29
 * for logging, DBAL mapping types, etc.
30
 *
31
 * @author Roman Borschel <[email protected]>
32
 * @since 2.0
33
 */
34
class Statement implements \IteratorAggregate, DriverStatement
35
{
36
    /**
37
     * The SQL statement.
38
     *
39
     * @var string
40
     */
41
    protected $sql;
42
43
    /**
44
     * The bound parameters.
45
     *
46
     * @var array
47
     */
48
    protected $params = [];
49
50
    /**
51
     * The parameter types.
52
     *
53
     * @var array
54
     */
55
    protected $types = [];
56
57
    /**
58
     * The underlying driver statement.
59
     *
60
     * @var \Doctrine\DBAL\Driver\Statement
61
     */
62
    protected $stmt;
63
64
    /**
65
     * The underlying database platform.
66
     *
67
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
68
     */
69
    protected $platform;
70
71
    /**
72
     * The connection this statement is bound to and executed on.
73
     *
74
     * @var \Doctrine\DBAL\Connection
75
     */
76
    protected $conn;
77
78
    /**
79
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
80
     *
81
     * @param string                    $sql  The SQL of the statement.
82
     * @param \Doctrine\DBAL\Connection $conn The connection on which the statement should be executed.
83
     */
84 796
    public function __construct($sql, Connection $conn)
85
    {
86 796
        $this->sql = $sql;
87 796
        $this->stmt = $conn->getWrappedConnection()->prepare($sql);
88 779
        $this->conn = $conn;
89 779
        $this->platform = $conn->getDatabasePlatform();
90 779
    }
91
92
    /**
93
     * Binds a parameter value to the statement.
94
     *
95
     * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
96
     * If bound with a DBAL mapping type, the binding type is derived from the mapping
97
     * type and the value undergoes the conversion routines of the mapping type before
98
     * being bound.
99
     *
100
     * @param string $name  The name or position of the parameter.
101
     * @param mixed  $value The value of the parameter.
102
     * @param mixed  $type  Either a PDO binding type or a DBAL mapping type name or instance.
103
     *
104
     * @return bool TRUE on success, FALSE on failure.
105
     */
106 178
    public function bindValue($name, $value, $type = ParameterType::STRING)
107
    {
108 178
        $this->params[$name] = $value;
109 178
        $this->types[$name] = $type;
110 178
        if ($type !== null) {
111 178
            if (is_string($type)) {
112 34
                $type = Type::getType($type);
113
            }
114 178
            if ($type instanceof Type) {
115 51
                $value = $type->convertToDatabaseValue($value, $this->platform);
116 51
                $bindingType = $type->getBindingType();
117
            } else {
118 127
                $bindingType = $type;
119
            }
120
121 178
            return $this->stmt->bindValue($name, $value, $bindingType);
122
        }
123
124
        return $this->stmt->bindValue($name, $value);
125
    }
126
127
    /**
128
     * Binds a parameter to a value by reference.
129
     *
130
     * Binding a parameter by reference does not support DBAL mapping types.
131
     *
132
     * @param string   $name   The name or position of the parameter.
133
     * @param mixed    $var    The reference to the variable to bind.
134
     * @param int      $type   The PDO binding type.
135
     * @param int|null $length Must be specified when using an OUT bind
136
     *                         so that PHP allocates enough memory to hold the returned value.
137
     *
138
     * @return bool TRUE on success, FALSE on failure.
139
     */
140 136
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
141
    {
142 136
        $this->params[$name] = $var;
143 136
        $this->types[$name] = $type;
144
145 136
        return $this->stmt->bindParam($name, $var, $type, $length);
146
    }
147
148
    /**
149
     * Executes the statement with the currently bound parameters.
150
     *
151
     * @param array|null $params
152
     *
153
     * @return bool TRUE on success, FALSE on failure.
154
     *
155
     * @throws \Doctrine\DBAL\DBALException
156
     */
157 626
    public function execute($params = null)
158
    {
159 626
        if (is_array($params)) {
160 68
            $this->params = $params;
161
        }
162
163 626
        $logger = $this->conn->getConfiguration()->getSQLLogger();
164 626
        if ($logger) {
165 626
            $logger->startQuery($this->sql, $this->params, $this->types);
166
        }
167
168
        try {
169 626
            $stmt = $this->stmt->execute($params);
170 17
        } catch (\Exception $ex) {
171 17
            if ($logger) {
172 17
                $logger->stopQuery();
173
            }
174 17
            throw DBALException::driverExceptionDuringQuery(
175 17
                $this->conn->getDriver(),
176 17
                $ex,
177 17
                $this->sql,
178 17
                $this->conn->resolveParams($this->params, $this->types)
179
            );
180
        }
181
182 609
        if ($logger) {
183 609
            $logger->stopQuery();
184
        }
185 609
        $this->params = [];
186 609
        $this->types = [];
187
188 609
        return $stmt;
189
    }
190
191
    /**
192
     * Closes the cursor, freeing the database resources used by this statement.
193
     *
194
     * @return bool TRUE on success, FALSE on failure.
195
     */
196 179
    public function closeCursor()
197
    {
198 179
        return $this->stmt->closeCursor();
199
    }
200
201
    /**
202
     * Returns the number of columns in the result set.
203
     *
204
     * @return int
205
     */
206
    public function columnCount()
207
    {
208
        return $this->stmt->columnCount();
209
    }
210
211
    /**
212
     * Fetches the SQLSTATE associated with the last operation on the statement.
213
     *
214
     * @return string|int|bool
215
     */
216
    public function errorCode()
217
    {
218
        return $this->stmt->errorCode();
219
    }
220
221
    /**
222
     * Fetches extended error information associated with the last operation on the statement.
223
     *
224
     * @return array
225
     */
226
    public function errorInfo()
227
    {
228
        return $this->stmt->errorInfo();
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234 711
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
235
    {
236 711
        if ($arg2 === null) {
237 711
            return $this->stmt->setFetchMode($fetchMode);
238
        } elseif ($arg3 === null) {
239
            return $this->stmt->setFetchMode($fetchMode, $arg2);
240
        }
241
242
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
243
    }
244
245
    /**
246
     * Required by interface IteratorAggregate.
247
     *
248
     * {@inheritdoc}
249
     */
250 17
    public function getIterator()
251
    {
252 17
        return $this->stmt;
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258 170
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
259
    {
260 170
        return $this->stmt->fetch($fetchMode);
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266 133
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
267
    {
268 133
        if ($fetchArgument) {
269 14
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
270
        }
271
272 119
        return $this->stmt->fetchAll($fetchMode);
273
    }
274
275
    /**
276
     * Returns a single column from the next row of a result set.
277
     *
278
     * @param int $columnIndex
279
     *
280
     * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
281
     */
282 230
    public function fetchColumn($columnIndex = 0)
283
    {
284 230
        return $this->stmt->fetchColumn($columnIndex);
285
    }
286
287
    /**
288
     * Returns the number of rows affected by the last execution of this statement.
289
     *
290
     * @return int The number of affected rows.
291
     */
292 68
    public function rowCount()
293
    {
294 68
        return $this->stmt->rowCount();
295
    }
296
297
    /**
298
     * Gets the wrapped driver statement.
299
     *
300
     * @return \Doctrine\DBAL\Driver\Statement
301
     */
302
    public function getWrappedStatement()
303
    {
304
        return $this->stmt;
305
    }
306
}
307