Statement.php 8.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL;

Benjamin Morel's avatar
Benjamin Morel committed
22 23
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
24 25
use function is_array;
use function is_string;
26 27 28 29

/**
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
 * for logging, DBAL mapping types, etc.
30
 *
31 32 33
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
34
class Statement implements \IteratorAggregate, DriverStatement
35 36
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
37 38 39
     * The SQL statement.
     *
     * @var string
40
     */
41
    protected $sql;
Benjamin Morel's avatar
Benjamin Morel committed
42

43
    /**
Benjamin Morel's avatar
Benjamin Morel committed
44 45 46
     * The bound parameters.
     *
     * @var array
47
     */
48
    protected $params = [];
Benjamin Morel's avatar
Benjamin Morel committed
49

50
    /**
Benjamin Morel's avatar
Benjamin Morel committed
51 52 53
     * The parameter types.
     *
     * @var array
54
     */
55
    protected $types = [];
Benjamin Morel's avatar
Benjamin Morel committed
56

57
    /**
Benjamin Morel's avatar
Benjamin Morel committed
58 59 60
     * The underlying driver statement.
     *
     * @var \Doctrine\DBAL\Driver\Statement
61
     */
62
    protected $stmt;
Benjamin Morel's avatar
Benjamin Morel committed
63

64
    /**
Benjamin Morel's avatar
Benjamin Morel committed
65 66 67
     * The underlying database platform.
     *
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
68
     */
69
    protected $platform;
Benjamin Morel's avatar
Benjamin Morel committed
70

71
    /**
Benjamin Morel's avatar
Benjamin Morel committed
72 73 74
     * The connection this statement is bound to and executed on.
     *
     * @var \Doctrine\DBAL\Connection
75
     */
76
    protected $conn;
77 78 79 80

    /**
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
     *
Benjamin Morel's avatar
Benjamin Morel committed
81 82
     * @param string                    $sql  The SQL of the statement.
     * @param \Doctrine\DBAL\Connection $conn The connection on which the statement should be executed.
83 84 85
     */
    public function __construct($sql, Connection $conn)
    {
86 87 88 89
        $this->sql = $sql;
        $this->stmt = $conn->getWrappedConnection()->prepare($sql);
        $this->conn = $conn;
        $this->platform = $conn->getDatabasePlatform();
90 91 92 93
    }

    /**
     * Binds a parameter value to the statement.
94
     *
95 96 97 98
     * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
     * If bound with a DBAL mapping type, the binding type is derived from the mapping
     * type and the value undergoes the conversion routines of the mapping type before
     * being bound.
99
     *
Benjamin Morel's avatar
Benjamin Morel committed
100 101 102 103
     * @param string $name  The name or position of the parameter.
     * @param mixed  $value The value of the parameter.
     * @param mixed  $type  Either a PDO binding type or a DBAL mapping type name or instance.
     *
104
     * @return bool TRUE on success, FALSE on failure.
105
     */
106
    public function bindValue($name, $value, $type = ParameterType::STRING)
107
    {
108
        $this->params[$name] = $value;
109
        $this->types[$name] = $type;
110 111 112 113 114
        if ($type !== null) {
            if (is_string($type)) {
                $type = Type::getType($type);
            }
            if ($type instanceof Type) {
115
                $value = $type->convertToDatabaseValue($value, $this->platform);
116 117
                $bindingType = $type->getBindingType();
            } else {
118
                $bindingType = $type;
119
            }
Benjamin Morel's avatar
Benjamin Morel committed
120

121
            return $this->stmt->bindValue($name, $value, $bindingType);
122
        }
Gabriel Caruso's avatar
Gabriel Caruso committed
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 133 134 135 136
     * @param string   $name   The name or position of the parameter.
     * @param mixed    $var    The reference to the variable to bind.
     * @param int      $type   The PDO binding type.
     * @param int|null $length Must be specified when using an OUT bind
     *                         so that PHP allocates enough memory to hold the returned value.
Benjamin Morel's avatar
Benjamin Morel committed
137
     *
138
     * @return bool TRUE on success, FALSE on failure.
139
     */
140
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
141
    {
142 143
        $this->params[$name] = $var;
        $this->types[$name] = $type;
144

Benjamin Morel's avatar
Benjamin Morel committed
145
        return $this->stmt->bindParam($name, $var, $type, $length);
146 147 148 149
    }

    /**
     * Executes the statement with the currently bound parameters.
150
     *
Benjamin Morel's avatar
Benjamin Morel committed
151 152
     * @param array|null $params
     *
153
     * @return bool TRUE on success, FALSE on failure.
Benjamin Morel's avatar
Benjamin Morel committed
154 155
     *
     * @throws \Doctrine\DBAL\DBALException
156 157 158
     */
    public function execute($params = null)
    {
159
        if (is_array($params)) {
160 161
            $this->params = $params;
        }
Benjamin Morel's avatar
Benjamin Morel committed
162

163 164
        $logger = $this->conn->getConfiguration()->getSQLLogger();
        if ($logger) {
165
            $logger->startQuery($this->sql, $this->params, $this->types);
166 167
        }

168 169 170
        try {
            $stmt = $this->stmt->execute($params);
        } catch (\Exception $ex) {
171 172 173
            if ($logger) {
                $logger->stopQuery();
            }
174 175 176 177 178 179
            throw DBALException::driverExceptionDuringQuery(
                $this->conn->getDriver(),
                $ex,
                $this->sql,
                $this->conn->resolveParams($this->params, $this->types)
            );
180
        }
181

182 183
        if ($logger) {
            $logger->stopQuery();
184
        }
185 186
        $this->params = [];
        $this->types = [];
Benjamin Morel's avatar
Benjamin Morel committed
187

188
        return $stmt;
189 190 191
    }

    /**
192 193
     * Closes the cursor, freeing the database resources used by this statement.
     *
194
     * @return bool TRUE on success, FALSE on failure.
195 196 197
     */
    public function closeCursor()
    {
198
        return $this->stmt->closeCursor();
199 200 201 202
    }

    /**
     * Returns the number of columns in the result set.
203
     *
204
     * @return int
205 206 207
     */
    public function columnCount()
    {
208
        return $this->stmt->columnCount();
209 210 211 212
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
213
     *
214 215 216 217
     * @return string
     */
    public function errorCode()
    {
218
        return $this->stmt->errorCode();
219 220 221 222
    }

    /**
     * Fetches extended error information associated with the last operation on the statement.
223
     *
224 225 226 227
     * @return array
     */
    public function errorInfo()
    {
228
        return $this->stmt->errorInfo();
229 230
    }

Benjamin Morel's avatar
Benjamin Morel committed
231 232 233
    /**
     * {@inheritdoc}
     */
234
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
235
    {
236 237
        if ($arg2 === null) {
            return $this->stmt->setFetchMode($fetchMode);
Steve Müller's avatar
Steve Müller committed
238
        } elseif ($arg3 === null) {
239 240 241
            return $this->stmt->setFetchMode($fetchMode, $arg2);
        }

242
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
243 244
    }

Benjamin Morel's avatar
Benjamin Morel committed
245 246 247 248 249
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
250 251
    public function getIterator()
    {
252
        return $this->stmt;
253 254
    }

255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
259
    {
260
        return $this->stmt->fetch($fetchMode);
261 262 263
    }

    /**
264
     * {@inheritdoc}
265
     */
266
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
267
    {
268
        if ($fetchArgument) {
269
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
270
        }
Benjamin Morel's avatar
Benjamin Morel committed
271

272
        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
Benjamin Morel's avatar
Benjamin Morel committed
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 283
     */
    public function fetchColumn($columnIndex = 0)
    {
284
        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 293
     */
    public function rowCount()
    {
294
        return $this->stmt->rowCount();
295 296 297 298
    }

    /**
     * Gets the wrapped driver statement.
299
     *
300
     * @return \Doctrine\DBAL\Driver\Statement
301 302 303
     */
    public function getWrappedStatement()
    {
304
        return $this->stmt;
305
    }
306
}