Statement.php 8.85 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
    {
141 142
        $this->params[$name] = $var;
        $this->types[$name] = $type;
143

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

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

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

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

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

187
        return $stmt;
188 189 190
    }

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

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

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

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

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

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

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

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

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

281
        return $this->stmt->fetchAll($fetchMode);
282 283 284 285
    }

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

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

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