Statement.php 7.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
/*
 *  $Id$
 *
 * 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
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL;

use PDO,
    Doctrine\DBAL\Types\Type,
    Doctrine\DBAL\Driver\Statement as DriverStatement;

/**
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
 * for logging, DBAL mapping types, etc.
31
 *
32 33 34
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
35
class Statement implements \IteratorAggregate, DriverStatement
36 37 38 39
{
    /**
     * @var string The SQL statement.
     */
40
    protected $sql;
41 42 43
    /**
     * @var array The bound parameters.
     */
44
    protected $params = array();
45 46 47
    /**
     * @var Doctrine\DBAL\Driver\Statement The underlying driver statement.
     */
48
    protected $stmt;
49 50 51
    /**
     * @var Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
     */
52
    protected $platform;
53 54 55
    /**
     * @var Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
     */
56
    protected $conn;
57 58 59 60 61 62 63 64 65

    /**
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
     *
     * @param string $sql The SQL of the statement.
     * @param Doctrine\DBAL\Connection The connection on which the statement should be executed.
     */
    public function __construct($sql, Connection $conn)
    {
66 67 68 69
        $this->sql = $sql;
        $this->stmt = $conn->getWrappedConnection()->prepare($sql);
        $this->conn = $conn;
        $this->platform = $conn->getDatabasePlatform();
70 71 72 73
    }

    /**
     * Binds a parameter value to the statement.
74
     *
75 76 77 78
     * 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.
79
     *
80 81 82 83 84 85 86
     * @param $name The name or position of the parameter.
     * @param $value The value of the parameter.
     * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function bindValue($name, $value, $type = null)
    {
87
        $this->params[$name] = $value;
88 89 90 91 92
        if ($type !== null) {
            if (is_string($type)) {
                $type = Type::getType($type);
            }
            if ($type instanceof Type) {
93
                $value = $type->convertToDatabaseValue($value, $this->platform);
94 95 96 97
                $bindingType = $type->getBindingType();
            } else {
                $bindingType = $type; // PDO::PARAM_* constants
            }
98
            return $this->stmt->bindValue($name, $value, $bindingType);
99
        } else {
100
            return $this->stmt->bindValue($name, $value);
101 102 103 104 105
        }
    }

    /**
     * Binds a parameter to a value by reference.
106
     *
107
     * Binding a parameter by reference does not support DBAL mapping types.
108
     *
109 110 111 112 113 114 115
     * @param string $name The name or position of the parameter.
     * @param mixed $value The reference to the variable to bind
     * @param integer $type The PDO binding type.
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function bindParam($name, &$var, $type = PDO::PARAM_STR)
    {
116
        return $this->stmt->bindParam($name, $var, $type);
117 118 119 120
    }

    /**
     * Executes the statement with the currently bound parameters.
121
     *
122 123 124 125
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function execute($params = null)
    {
126
        $hasLogger = $this->conn->getConfiguration()->getSQLLogger();
127
        if ($hasLogger) {
128
            $this->conn->getConfiguration()->getSQLLogger()->startQuery($this->sql, $this->params);
129 130
        }

131
        $stmt = $this->stmt->execute($params);
132 133

        if ($hasLogger) {
134
            $this->conn->getConfiguration()->getSQLLogger()->stopQuery();
135
        }
136
        $this->params = array();
137
        return $stmt;
138 139 140
    }

    /**
141 142
     * Closes the cursor, freeing the database resources used by this statement.
     *
143 144 145 146
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function closeCursor()
    {
147
        return $this->stmt->closeCursor();
148 149 150 151
    }

    /**
     * Returns the number of columns in the result set.
152
     *
153 154 155 156
     * @return integer
     */
    public function columnCount()
    {
157
        return $this->stmt->columnCount();
158 159 160 161
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
162
     *
163 164 165 166
     * @return string
     */
    public function errorCode()
    {
167
        return $this->stmt->errorCode();
168 169 170 171
    }

    /**
     * Fetches extended error information associated with the last operation on the statement.
172
     *
173 174 175 176
     * @return array
     */
    public function errorInfo()
    {
177
        return $this->stmt->errorInfo();
178 179
    }

180 181
    public function setFetchMode($fetchStyle)
    {
182
        return $this->stmt->setFetchMode($fetchStyle);
183 184 185 186
    }

    public function getIterator()
    {
187
        return $this->stmt;
188 189
    }

190 191
    /**
     * Fetches the next row from a result set.
192
     *
193 194 195 196 197 198
     * @param integer $fetchStyle
     * @return mixed The return value of this function on success depends on the fetch type.
     *               In all cases, FALSE is returned on failure.
     */
    public function fetch($fetchStyle = PDO::FETCH_BOTH)
    {
199
        return $this->stmt->fetch($fetchStyle);
200 201 202 203
    }

    /**
     * Returns an array containing all of the result set rows.
204
     *
205 206 207 208 209 210 211
     * @param integer $fetchStyle
     * @param integer $columnIndex
     * @return array An array containing all of the remaining rows in the result set.
     */
    public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
    {
        if ($columnIndex != 0) {
212
            return $this->stmt->fetchAll($fetchStyle, $columnIndex);
213
        }
214
        return $this->stmt->fetchAll($fetchStyle);
215 216 217 218
    }

    /**
     * Returns a single column from the next row of a result set.
219
     *
220
     * @param integer $columnIndex
221
     * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
222 223 224
     */
    public function fetchColumn($columnIndex = 0)
    {
225
        return $this->stmt->fetchColumn($columnIndex);
226 227 228 229
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
230
     *
231 232 233 234
     * @return integer The number of affected rows.
     */
    public function rowCount()
    {
235
        return $this->stmt->rowCount();
236 237 238 239
    }

    /**
     * Gets the wrapped driver statement.
240
     *
241 242 243 244
     * @return Doctrine\DBAL\Driver\Statement
     */
    public function getWrappedStatement()
    {
245
        return $this->stmt;
246 247
    }
}