Statement.php 7.19 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 10
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use IteratorAggregate;
use PDO;
use Throwable;
11 12
use function is_array;
use function is_string;
13 14 15 16 17

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

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

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

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

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

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

    /**
     * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
     *
65 66
     * @param string     $sql  The SQL of the statement.
     * @param Connection $conn The connection on which the statement should be executed.
67 68 69
     */
    public function __construct($sql, Connection $conn)
    {
70 71 72
        $this->sql      = $sql;
        $this->stmt     = $conn->getWrappedConnection()->prepare($sql);
        $this->conn     = $conn;
73
        $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 86
     * @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
87
     *
88
     * @return bool TRUE on success, FALSE on failure.
89
     */
90
    public function bindValue($name, $value, $type = ParameterType::STRING)
91
    {
92
        $this->params[$name] = $value;
93
        $this->types[$name]  = $type;
94 95 96 97 98
        if ($type !== null) {
            if (is_string($type)) {
                $type = Type::getType($type);
            }
            if ($type instanceof Type) {
99
                $value       = $type->convertToDatabaseValue($value, $this->platform);
100 101
                $bindingType = $type->getBindingType();
            } else {
102
                $bindingType = $type;
103
            }
Benjamin Morel's avatar
Benjamin Morel committed
104

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

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

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

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

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

147 148
        $logger = $this->conn->getConfiguration()->getSQLLogger();
        if ($logger) {
149
            $logger->startQuery($this->sql, $this->params, $this->types);
150 151
        }

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

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

172
        return $stmt;
173 174 175
    }

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
213 214 215
    /**
     * {@inheritdoc}
     */
216
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
217
    {
218 219
        if ($arg2 === null) {
            return $this->stmt->setFetchMode($fetchMode);
Steve Müller's avatar
Steve Müller committed
220
        } elseif ($arg3 === null) {
221 222 223
            return $this->stmt->setFetchMode($fetchMode, $arg2);
        }

224
        return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
225 226
    }

Benjamin Morel's avatar
Benjamin Morel committed
227 228 229 230 231
    /**
     * Required by interface IteratorAggregate.
     *
     * {@inheritdoc}
     */
232 233
    public function getIterator()
    {
234
        return $this->stmt;
235 236
    }

237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
241
    {
242
        return $this->stmt->fetch($fetchMode);
243 244 245
    }

    /**
246
     * {@inheritdoc}
247
     */
248
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
249
    {
250
        if ($fetchArgument) {
251
            return $this->stmt->fetchAll($fetchMode, $fetchArgument);
252
        }
Benjamin Morel's avatar
Benjamin Morel committed
253

254
        return $this->stmt->fetchAll($fetchMode);
255 256 257
    }

    /**
258
     * {@inheritDoc}
259 260 261
     */
    public function fetchColumn($columnIndex = 0)
    {
262
        return $this->stmt->fetchColumn($columnIndex);
263 264 265 266
    }

    /**
     * Returns the number of rows affected by the last execution of this statement.
267
     *
268
     * @return int The number of affected rows.
269 270 271
     */
    public function rowCount()
    {
272
        return $this->stmt->rowCount();
273 274 275 276
    }

    /**
     * Gets the wrapped driver statement.
277
     *
278
     * @return \Doctrine\DBAL\Driver\Statement
279 280 281
     */
    public function getWrappedStatement()
    {
282
        return $this->stmt;
283
    }
284
}