Statement.php 6.68 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL;

Benjamin Morel's avatar
Benjamin Morel committed
5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6 7 8 9
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use IteratorAggregate;
use Throwable;
10 11
use function is_array;
use function is_string;
12 13 14 15 16

/**
 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
 * for logging, DBAL mapping types, etc.
 */
17
class Statement implements IteratorAggregate, DriverStatement
18 19
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
20 21 22
     * The SQL statement.
     *
     * @var string
23
     */
24
    protected $sql;
Benjamin Morel's avatar
Benjamin Morel committed
25

26
    /**
Benjamin Morel's avatar
Benjamin Morel committed
27 28
     * The bound parameters.
     *
29
     * @var mixed[]
30
     */
31
    protected $params = [];
Benjamin Morel's avatar
Benjamin Morel committed
32

33
    /**
Benjamin Morel's avatar
Benjamin Morel committed
34 35
     * The parameter types.
     *
36
     * @var int[]|string[]
37
     */
38
    protected $types = [];
Benjamin Morel's avatar
Benjamin Morel committed
39

40
    /**
Benjamin Morel's avatar
Benjamin Morel committed
41 42 43
     * The underlying driver statement.
     *
     * @var \Doctrine\DBAL\Driver\Statement
44
     */
45
    protected $stmt;
Benjamin Morel's avatar
Benjamin Morel committed
46

47
    /**
Benjamin Morel's avatar
Benjamin Morel committed
48 49
     * The underlying database platform.
     *
50
     * @var AbstractPlatform
51
     */
52
    protected $platform;
Benjamin Morel's avatar
Benjamin Morel committed
53

54
    /**
Benjamin Morel's avatar
Benjamin Morel committed
55 56
     * The connection this statement is bound to and executed on.
     *
57
     * @var Connection
58
     */
59
    protected $conn;
60 61 62 63

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

    /**
     * Binds a parameter value to the statement.
77
     *
78 79 80 81
     * 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.
82
     *
83 84 85
     * @param string|int $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.
Benjamin Morel's avatar
Benjamin Morel committed
86
     *
87
     * @return bool TRUE on success, FALSE on failure.
88
     */
89
    public function bindValue($name, $value, $type = ParameterType::STRING)
90
    {
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
                $bindingType = $type->getBindingType();
            } else {
101
                $bindingType = $type;
102
            }
Benjamin Morel's avatar
Benjamin Morel committed
103

104
            return $this->stmt->bindValue($name, $value, $bindingType);
105
        }
Gabriel Caruso's avatar
Gabriel Caruso committed
106 107

        return $this->stmt->bindValue($name, $value);
108 109 110 111
    }

    /**
     * Binds a parameter to a value by reference.
112
     *
113
     * Binding a parameter by reference does not support DBAL mapping types.
114
     *
115 116 117 118 119
     * @param string|int $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
120
     *
121
     * @return bool TRUE on success, FALSE on failure.
122
     */
123
    public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
124
    {
125
        $this->params[$name] = $var;
126
        $this->types[$name]  = $type;
127

Benjamin Morel's avatar
Benjamin Morel committed
128
        return $this->stmt->bindParam($name, $var, $type, $length);
129 130 131 132
    }

    /**
     * Executes the statement with the currently bound parameters.
133
     *
134
     * @param mixed[]|null $params
Benjamin Morel's avatar
Benjamin Morel committed
135
     *
136
     * @return bool TRUE on success, FALSE on failure.
Benjamin Morel's avatar
Benjamin Morel committed
137
     *
138
     * @throws DBALException
139 140 141
     */
    public function execute($params = null)
    {
142
        if (is_array($params)) {
143 144
            $this->params = $params;
        }
Benjamin Morel's avatar
Benjamin Morel committed
145

146
        $logger = $this->conn->getConfiguration()->getSQLLogger();
147
        $logger->startQuery($this->sql, $this->params, $this->types);
148

149 150
        try {
            $stmt = $this->stmt->execute($params);
151
        } catch (Throwable $ex) {
152
            $logger->stopQuery();
153 154 155 156 157 158
            throw DBALException::driverExceptionDuringQuery(
                $this->conn->getDriver(),
                $ex,
                $this->sql,
                $this->conn->resolveParams($this->params, $this->types)
            );
159
        }
160

161
        $logger->stopQuery();
162
        $this->params = [];
163
        $this->types  = [];
Benjamin Morel's avatar
Benjamin Morel committed
164

165
        return $stmt;
166 167 168
    }

    /**
169 170
     * Closes the cursor, freeing the database resources used by this statement.
     *
171
     * @return bool TRUE on success, FALSE on failure.
172 173 174
     */
    public function closeCursor()
    {
175
        return $this->stmt->closeCursor();
176 177 178 179
    }

    /**
     * Returns the number of columns in the result set.
180
     *
181
     * @return int
182 183 184
     */
    public function columnCount()
    {
185
        return $this->stmt->columnCount();
186 187 188 189
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
190
     *
191
     * @return string|int|bool
192 193 194
     */
    public function errorCode()
    {
195
        return $this->stmt->errorCode();
196 197 198
    }

    /**
199
     * {@inheritDoc}
200 201 202
     */
    public function errorInfo()
    {
203
        return $this->stmt->errorInfo();
204 205
    }

Benjamin Morel's avatar
Benjamin Morel committed
206 207 208
    /**
     * {@inheritdoc}
     */
209
    public function setFetchMode($fetchMode, ...$args)
210
    {
211
        return $this->stmt->setFetchMode($fetchMode, ...$args);
212 213
    }

Benjamin Morel's avatar
Benjamin Morel committed
214 215 216 217 218
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
219 220
    public function getIterator()
    {
221
        return $this->stmt;
222 223
    }

224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function fetch($fetchMode = null, ...$args)
228
    {
229
        return $this->stmt->fetch($fetchMode, ...$args);
230 231 232
    }

    /**
233
     * {@inheritdoc}
234
     */
235
    public function fetchAll($fetchMode = null, ...$args)
236
    {
237
        return $this->stmt->fetchAll($fetchMode, ...$args);
238 239 240
    }

    /**
241
     * {@inheritDoc}
242 243 244
     */
    public function fetchColumn($columnIndex = 0)
    {
245
        return $this->stmt->fetchColumn($columnIndex);
246 247 248 249
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
250
     *
251
     * @return int The number of affected rows.
252 253 254
     */
    public function rowCount()
    {
255
        return $this->stmt->rowCount();
256 257 258 259
    }

    /**
     * Gets the wrapped driver statement.
260
     *
261
     * @return \Doctrine\DBAL\Driver\Statement
262 263 264
     */
    public function getWrappedStatement()
    {
265
        return $this->stmt;
266
    }
267
}