Statement.php 7.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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
Benjamin Eberlei's avatar
Benjamin Eberlei committed
18
 * and is licensed under the MIT license. For more information, see
19 20 21 22 23 24 25 26 27 28 29 30
 * <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 48
    /**
     * @var array The parameter types
     */
    protected $types = array();
49
    /**
50
     * @var \Doctrine\DBAL\Driver\Statement The underlying driver statement.
51
     */
52
    protected $stmt;
53
    /**
54
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
55
     */
56
    protected $platform;
57
    /**
58
     * @var \Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
59
     */
60
    protected $conn;
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.
66
     * @param \Doctrine\DBAL\Connection The connection on which the statement should be executed.
67 68 69
     */
    public function __construct($sql, Connection $conn)
    {
70 71 72 73
        $this->sql = $sql;
        $this->stmt = $conn->getWrappedConnection()->prepare($sql);
        $this->conn = $conn;
        $this->platform = $conn->getDatabasePlatform();
74 75 76 77
    }

    /**
     * Binds a parameter value to the statement.
78
     *
79 80 81 82
     * 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.
83
     *
84 85
     * @param string $name The name or position of the parameter.
     * @param mixed $value The value of the parameter.
86 87 88 89 90
     * @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)
    {
91
        $this->params[$name] = $value;
92
        $this->types[$name] = $type;
93 94 95 96 97
        if ($type !== null) {
            if (is_string($type)) {
                $type = Type::getType($type);
            }
            if ($type instanceof Type) {
98
                $value = $type->convertToDatabaseValue($value, $this->platform);
99 100 101 102
                $bindingType = $type->getBindingType();
            } else {
                $bindingType = $type; // PDO::PARAM_* constants
            }
103
            return $this->stmt->bindValue($name, $value, $bindingType);
104
        } else {
105
            return $this->stmt->bindValue($name, $value);
106 107 108 109 110
        }
    }

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

    /**
     * Executes the statement with the currently bound parameters.
126
     *
127
     * @param array $params
128 129 130 131
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function execute($params = null)
    {
132 133
        $logger = $this->conn->getConfiguration()->getSQLLogger();
        if ($logger) {
134
            $logger->startQuery($this->sql, $this->params, $this->types);
135 136
        }

137
        $stmt = $this->stmt->execute($params);
138

139 140
        if ($logger) {
            $logger->stopQuery();
141
        }
142
        $this->params = array();
143
        $this->types = array();
144
        return $stmt;
145 146 147
    }

    /**
148 149
     * Closes the cursor, freeing the database resources used by this statement.
     *
150 151 152 153
     * @return boolean TRUE on success, FALSE on failure.
     */
    public function closeCursor()
    {
154
        return $this->stmt->closeCursor();
155 156 157 158
    }

    /**
     * Returns the number of columns in the result set.
159
     *
160 161 162 163
     * @return integer
     */
    public function columnCount()
    {
164
        return $this->stmt->columnCount();
165 166 167 168
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
169
     *
170 171 172 173
     * @return string
     */
    public function errorCode()
    {
174
        return $this->stmt->errorCode();
175 176 177 178
    }

    /**
     * Fetches extended error information associated with the last operation on the statement.
179
     *
180 181 182 183
     * @return array
     */
    public function errorInfo()
    {
184
        return $this->stmt->errorInfo();
185 186
    }

187
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
188
    {
189
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
190 191 192 193
    }

    public function getIterator()
    {
194
        return $this->stmt;
195 196
    }

197 198
    /**
     * Fetches the next row from a result set.
199
     *
200
     * @param integer $fetchMode
201 202 203
     * @return mixed The return value of this function on success depends on the fetch type.
     *               In all cases, FALSE is returned on failure.
     */
204
    public function fetch($fetchMode = null)
205
    {
206
        return $this->stmt->fetch($fetchMode);
207 208 209 210
    }

    /**
     * Returns an array containing all of the result set rows.
211
     *
212
     * @param integer $fetchMode
Fabio B. Silva's avatar
Fabio B. Silva committed
213
     * @param mixed $fetchArgument
214 215
     * @return array An array containing all of the remaining rows in the result set.
     */
216
    public function fetchAll($fetchMode = null, $fetchArgument = 0)
217
    {
218
        if ($fetchArgument !== 0) {
219
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
220
        }
221
        return $this->stmt->fetchAll($fetchMode);
222 223 224 225
    }

    /**
     * Returns a single column from the next row of a result set.
226
     *
227
     * @param integer $columnIndex
228
     * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
229 230 231
     */
    public function fetchColumn($columnIndex = 0)
    {
232
        return $this->stmt->fetchColumn($columnIndex);
233 234 235 236
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
237
     *
238 239 240 241
     * @return integer The number of affected rows.
     */
    public function rowCount()
    {
242
        return $this->stmt->rowCount();
243 244 245 246
    }

    /**
     * Gets the wrapped driver statement.
247
     *
248
     * @return \Doctrine\DBAL\Driver\Statement
249 250 251
     */
    public function getWrappedStatement()
    {
252
        return $this->stmt;
253
    }
254
}