Statement.php 6.79 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 147
        $logger = $this->conn->getConfiguration()->getSQLLogger();
        if ($logger) {
148
            $logger->startQuery($this->sql, $this->params, $this->types);
149 150
        }

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

165 166
        if ($logger) {
            $logger->stopQuery();
167
        }
168
        $this->params = [];
169
        $this->types  = [];
Benjamin Morel's avatar
Benjamin Morel committed
170

171
        return $stmt;
172 173 174
    }

    /**
175 176
     * Closes the cursor, freeing the database resources used by this statement.
     *
177
     * @return bool TRUE on success, FALSE on failure.
178 179 180
     */
    public function closeCursor()
    {
181
        return $this->stmt->closeCursor();
182 183 184 185
    }

    /**
     * Returns the number of columns in the result set.
186
     *
187
     * @return int
188 189 190
     */
    public function columnCount()
    {
191
        return $this->stmt->columnCount();
192 193 194 195
    }

    /**
     * Fetches the SQLSTATE associated with the last operation on the statement.
196
     *
197
     * @return string|int|bool
198 199 200
     */
    public function errorCode()
    {
201
        return $this->stmt->errorCode();
202 203 204
    }

    /**
205
     * {@inheritDoc}
206 207 208
     */
    public function errorInfo()
    {
209
        return $this->stmt->errorInfo();
210 211
    }

Benjamin Morel's avatar
Benjamin Morel committed
212 213 214
    /**
     * {@inheritdoc}
     */
215
    public function setFetchMode($fetchMode, ...$args)
216
    {
217
        return $this->stmt->setFetchMode($fetchMode, ...$args);
218 219
    }

Benjamin Morel's avatar
Benjamin Morel committed
220 221 222 223 224
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
225 226
    public function getIterator()
    {
227
        return $this->stmt;
228 229
    }

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

    /**
239
     * {@inheritdoc}
240
     */
241
    public function fetchAll($fetchMode = null, ...$args)
242
    {
243
        return $this->stmt->fetchAll($fetchMode, ...$args);
244 245 246
    }

    /**
247
     * {@inheritDoc}
248 249 250
     */
    public function fetchColumn($columnIndex = 0)
    {
251
        return $this->stmt->fetchColumn($columnIndex);
252 253 254 255
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
256
     *
257
     * @return int The number of affected rows.
258 259 260
     */
    public function rowCount()
    {
261
        return $this->stmt->rowCount();
262 263 264 265
    }

    /**
     * Gets the wrapped driver statement.
266
     *
267
     * @return \Doctrine\DBAL\Driver\Statement
268 269 270
     */
    public function getWrappedStatement()
    {
271
        return $this->stmt;
272
    }
273
}