Statement.php 8.6 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 167 168
        try {
            $stmt = $this->stmt->execute($params);
        } catch (\Exception $ex) {
            throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
        }
169

170 171
        if ($logger) {
            $logger->stopQuery();
172
        }
173
        $this->params = array();
174
        $this->types = array();
Benjamin Morel's avatar
Benjamin Morel committed
175

176
        return $stmt;
177 178 179
    }

    /**
180 181
     * Closes the cursor, freeing the database resources used by this statement.
     *
182 183 184 185
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function closeCursor()
    {
186
        return $this->stmt->closeCursor();
187 188 189 190
    }

    /**
     * Returns the number of columns in the result set.
191
     *
192 193 194 195
     * @return integer
     */
    public function columnCount()
    {
196
        return $this->stmt->columnCount();
197 198 199 200
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
201
     *
202 203 204 205
     * @return string
     */
    public function errorCode()
    {
206
        return $this->stmt->errorCode();
207 208 209 210
    }

    /**
     * Fetches extended error information associated with the last operation on the statement.
211
     *
212 213 214 215
     * @return array
     */
    public function errorInfo()
    {
216
        return $this->stmt->errorInfo();
217 218
    }

Benjamin Morel's avatar
Benjamin Morel committed
219 220 221
    /**
     * {@inheritdoc}
     */
222
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
223
    {
224 225 226 227 228 229
        if ($arg2 === null) {
            return $this->stmt->setFetchMode($fetchMode);
        } else if ($arg3 === null) {
            return $this->stmt->setFetchMode($fetchMode, $arg2);
        }

230
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
231 232
    }

Benjamin Morel's avatar
Benjamin Morel committed
233 234 235 236 237
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
238 239
    public function getIterator()
    {
240
        return $this->stmt;
241 242
    }

243 244
    /**
     * Fetches the next row from a result set.
245
     *
Benjamin Morel's avatar
Benjamin Morel committed
246 247
     * @param integer|null $fetchMode
     *
248 249 250
     * @return mixed The return value of this function on success depends on the fetch type.
     *               In all cases, FALSE is returned on failure.
     */
251
    public function fetch($fetchMode = null)
252
    {
253
        return $this->stmt->fetch($fetchMode);
254 255 256 257
    }

    /**
     * Returns an array containing all of the result set rows.
258
     *
Benjamin Morel's avatar
Benjamin Morel committed
259 260 261
     * @param integer|null $fetchMode
     * @param mixed        $fetchArgument
     *
262 263
     * @return array An array containing all of the remaining rows in the result set.
     */
264
    public function fetchAll($fetchMode = null, $fetchArgument = 0)
265
    {
266
        if ($fetchArgument !== 0) {
267
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
268
        }
Benjamin Morel's avatar
Benjamin Morel committed
269

270
        return $this->stmt->fetchAll($fetchMode);
271 272 273 274
    }

    /**
     * Returns a single column from the next row of a result set.
275
     *
276
     * @param integer $columnIndex
Benjamin Morel's avatar
Benjamin Morel committed
277
     *
278
     * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
279 280 281
     */
    public function fetchColumn($columnIndex = 0)
    {
282
        return $this->stmt->fetchColumn($columnIndex);
283 284 285 286
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
287
     *
288 289 290 291
     * @return integer The number of affected rows.
     */
    public function rowCount()
    {
292
        return $this->stmt->rowCount();
293 294 295 296
    }

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