Completed
Pull Request — master (#3174)
by José Carlos
16:47
created

Statement::execute()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

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