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

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

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

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

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

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

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

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

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

119
            return $this->stmt->bindValue($name, $value, $bindingType);
120
        }
Gabriel Caruso's avatar
Gabriel Caruso committed
121 122

        return $this->stmt->bindValue($name, $value);
123 124 125 126
    }

    /**
     * Binds a parameter to a value by reference.
127
     *
128
     * Binding a parameter by reference does not support DBAL mapping types.
129
     *
130 131 132 133 134
     * @param string   $name   The name or position of the parameter.
     * @param mixed    $var    The reference to the variable to bind.
     * @param int      $type   The PDO binding type.
     * @param int|null $length Must be specified when using an OUT bind
     *                         so that PHP allocates enough memory to hold the returned value.
Benjamin Morel's avatar
Benjamin Morel committed
135
     *
136
     * @return bool TRUE on success, FALSE on failure.
137
     */
138
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
139
    {
140 141
        $this->params[$name] = $var;
        $this->types[$name] = $type;
142

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

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

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

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

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

186
        return $stmt;
187 188 189
    }

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

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

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

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

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

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

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

253
    /**
254
     * {@inheritdoc}
255
     */
256
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
257
    {
258
        return $this->stmt->fetch($fetchMode);
259 260 261
    }

    /**
262
     * {@inheritdoc}
263
     */
264
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
265
    {
266
        if ($fetchArgument) {
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 int $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
     * @return int The number of affected rows.
289 290 291
     */
    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
}