Statement.php 6.65 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL;

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

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

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

36
    /**
Benjamin Morel's avatar
Benjamin Morel committed
37 38
     * The parameter types.
     *
39
     * @var int[]|string[]|Type[]
40
     */
41
    protected $types = [];
Benjamin Morel's avatar
Benjamin Morel committed
42

43
    /**
Benjamin Morel's avatar
Benjamin Morel committed
44 45
     * The underlying driver statement.
     *
46
     * @var DriverStatement
47
     */
48
    protected $stmt;
Benjamin Morel's avatar
Benjamin Morel committed
49

50
    /**
Benjamin Morel's avatar
Benjamin Morel committed
51 52
     * The underlying database platform.
     *
53
     * @var AbstractPlatform
54
     */
55
    protected $platform;
Benjamin Morel's avatar
Benjamin Morel committed
56

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

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

    /**
     * Binds a parameter value to the statement.
80
     *
81 82 83 84
     * 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.
85
     *
86 87 88 89 90 91 92
     * @param string|int      $param Parameter identifier. For a prepared statement using named placeholders,
     *                               this will be a parameter name of the form :name. For a prepared statement
     *                               using question mark placeholders, this will be the 1-indexed position
     *                               of the parameter.
     * @param mixed           $value The value to bind to the parameter.
     * @param string|int|Type $type  Either one of the constants defined in {@link \Doctrine\DBAL\ParameterType}
     *                               or a DBAL mapping type name or instance.
Benjamin Morel's avatar
Benjamin Morel committed
93
     *
94 95
     * @throws DBALException
     * @throws DriverException
96
     */
97
    public function bindValue($param, $value, $type = ParameterType::STRING) : void
98
    {
99 100
        $this->params[$param] = $value;
        $this->types[$param]  = $type;
101 102 103 104 105 106 107 108 109 110

        if (is_string($type)) {
            $type = Type::getType($type);
        }

        if ($type instanceof Type) {
            $value       = $type->convertToDatabaseValue($value, $this->platform);
            $bindingType = $type->getBindingType();
        } else {
            $bindingType = $type;
111
        }
Gabriel Caruso's avatar
Gabriel Caruso committed
112

113
        $this->stmt->bindValue($param, $value, $bindingType);
114 115 116 117
    }

    /**
     * Binds a parameter to a value by reference.
118
     *
119
     * Binding a parameter by reference does not support DBAL mapping types.
120
     *
121 122 123 124 125 126 127 128
     * @param string|int $param    Parameter identifier. For a prepared statement using named placeholders,
     *                             this will be a parameter name of the form :name. For a prepared statement
     *                             using question mark placeholders, this will be the 1-indexed position
     *                             of the parameter.
     * @param mixed      $variable The variable to bind to the parameter.
     * @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
129
     *
130
     * @throws DriverException
131
     */
132
    public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void
133
    {
134 135
        $this->params[$param] = $variable;
        $this->types[$param]  = $type;
136

137
        $this->stmt->bindParam($param, $variable, $type, $length);
138 139 140
    }

    /**
141
     * {@inheritDoc}
Benjamin Morel's avatar
Benjamin Morel committed
142
     *
143
     * @throws DBALException
144
     */
145
    public function execute(?array $params = null) : void
146
    {
147
        if (is_array($params)) {
148 149
            $this->params = $params;
        }
Benjamin Morel's avatar
Benjamin Morel committed
150

151
        $logger = $this->conn->getConfiguration()->getSQLLogger();
152
        $logger->startQuery($this->sql, $this->params, $this->types);
153

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

167
        $this->params = [];
168
        $this->types  = [];
169 170
    }

171
    public function closeCursor() : void
172
    {
173
        $this->stmt->closeCursor();
174 175 176 177 178
    }

    /**
     * Returns the number of columns in the result set.
     */
179
    public function columnCount() : int
180
    {
181
        return $this->stmt->columnCount();
182 183
    }

Benjamin Morel's avatar
Benjamin Morel committed
184 185 186
    /**
     * {@inheritdoc}
     */
187
    public function setFetchMode(int $fetchMode, ...$args) : void
188
    {
189
        $this->stmt->setFetchMode($fetchMode, ...$args);
190 191
    }

Benjamin Morel's avatar
Benjamin Morel committed
192 193 194 195 196
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
197 198
    public function getIterator()
    {
199
        return $this->stmt;
200 201
    }

202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function fetch(?int $fetchMode = null, ...$args)
206
    {
207
        return $this->stmt->fetch($fetchMode, ...$args);
208 209 210
    }

    /**
211
     * {@inheritdoc}
212
     */
213
    public function fetchAll(?int $fetchMode = null, ...$args) : array
214
    {
215
        return $this->stmt->fetchAll($fetchMode, ...$args);
216 217 218
    }

    /**
219
     * {@inheritDoc}
220
     */
221
    public function fetchColumn(int $columnIndex = 0)
222
    {
223
        return $this->stmt->fetchColumn($columnIndex);
224 225 226 227
    }

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

    /**
     * Gets the wrapped driver statement.
     */
239
    public function getWrappedStatement() : DriverStatement
240
    {
241
        return $this->stmt;
242
    }
243
}