Failed Conditions
Pull Request — master (#2958)
by Sergei
38:14
created

Statement::fetchAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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