Statement.php 8.78 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 24
use PDO;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
25 26 27 28

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

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

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

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

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

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

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

    /**
     * Binds a parameter value to the statement.
93
     *
94 95 96 97
     * 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.
98
     *
Benjamin Morel's avatar
Benjamin Morel committed
99 100 101 102
     * @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.
     *
103 104 105 106
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function bindValue($name, $value, $type = null)
    {
107
        $this->params[$name] = $value;
108
        $this->types[$name] = $type;
109 110 111 112 113
        if ($type !== null) {
            if (is_string($type)) {
                $type = Type::getType($type);
            }
            if ($type instanceof Type) {
114
                $value = $type->convertToDatabaseValue($value, $this->platform);
115 116 117 118
                $bindingType = $type->getBindingType();
            } else {
                $bindingType = $type; // PDO::PARAM_* constants
            }
Benjamin Morel's avatar
Benjamin Morel committed
119

120
            return $this->stmt->bindValue($name, $value, $bindingType);
121
        } else {
122
            return $this->stmt->bindValue($name, $value);
123 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
     *
Benjamin Morel's avatar
Benjamin Morel committed
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 integer      $type   The PDO binding type.
     * @param integer|null $length Must be specified when using an OUT bind
     *                             so that PHP allocates enough memory to hold the returned value.
     *
137 138
     * @return boolean TRUE on success, FALSE on failure.
     */
139
    public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null)
140
    {
Benjamin Morel's avatar
Benjamin Morel committed
141
        return $this->stmt->bindParam($name, $var, $type, $length);
142 143 144 145
    }

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

159 160
        $logger = $this->conn->getConfiguration()->getSQLLogger();
        if ($logger) {
161
            $logger->startQuery($this->sql, $this->params, $this->types);
162 163
        }

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

178 179
        if ($logger) {
            $logger->stopQuery();
180
        }
181
        $this->params = array();
182
        $this->types = array();
Benjamin Morel's avatar
Benjamin Morel committed
183

184
        return $stmt;
185 186 187
    }

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

    /**
     * Returns the number of columns in the result set.
199
     *
200 201 202 203
     * @return integer
     */
    public function columnCount()
    {
204
        return $this->stmt->columnCount();
205 206 207 208
    }

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

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

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

238
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
239 240
    }

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

251 252
    /**
     * Fetches the next row from a result set.
253
     *
Benjamin Morel's avatar
Benjamin Morel committed
254 255
     * @param integer|null $fetchMode
     *
256 257 258
     * @return mixed The return value of this function on success depends on the fetch type.
     *               In all cases, FALSE is returned on failure.
     */
259
    public function fetch($fetchMode = null)
260
    {
261
        return $this->stmt->fetch($fetchMode);
262 263 264 265
    }

    /**
     * Returns an array containing all of the result set rows.
266
     *
Benjamin Morel's avatar
Benjamin Morel committed
267 268 269
     * @param integer|null $fetchMode
     * @param mixed        $fetchArgument
     *
270 271
     * @return array An array containing all of the remaining rows in the result set.
     */
272
    public function fetchAll($fetchMode = null, $fetchArgument = 0)
273
    {
274
        if ($fetchArgument !== 0) {
275
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
276
        }
Benjamin Morel's avatar
Benjamin Morel committed
277

278
        return $this->stmt->fetchAll($fetchMode);
279 280 281 282
    }

    /**
     * Returns a single column from the next row of a result set.
283
     *
284
     * @param integer $columnIndex
Benjamin Morel's avatar
Benjamin Morel committed
285
     *
286
     * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
287 288 289
     */
    public function fetchColumn($columnIndex = 0)
    {
290
        return $this->stmt->fetchColumn($columnIndex);
291 292 293 294
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
295
     *
296 297 298 299
     * @return integer The number of affected rows.
     */
    public function rowCount()
    {
300
        return $this->stmt->rowCount();
301 302 303 304
    }

    /**
     * Gets the wrapped driver statement.
305
     *
306
     * @return \Doctrine\DBAL\Driver\Statement
307 308 309
     */
    public function getWrappedStatement()
    {
310
        return $this->stmt;
311
    }
312
}