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

namespace Doctrine\DBAL\Portability;

Sergei Morozov's avatar
Sergei Morozov committed
5
use Doctrine\DBAL\Driver\ResultStatement;
6
use Doctrine\DBAL\Driver\Statement as DriverStatement;
7
use Doctrine\DBAL\Driver\StatementIterator;
8 9
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
10 11
use IteratorAggregate;
use PDO;
12
use function array_change_key_case;
Sergei Morozov's avatar
Sergei Morozov committed
13
use function assert;
14 15
use function is_string;
use function rtrim;
16 17

/**
Benjamin Morel's avatar
Benjamin Morel committed
18
 * Portability wrapper for a Statement.
19
 */
20
class Statement implements IteratorAggregate, DriverStatement
21
{
22
    /** @var int */
23
    private $portability;
24

Sergei Morozov's avatar
Sergei Morozov committed
25
    /** @var DriverStatement|ResultStatement */
26
    private $stmt;
27

28
    /** @var int */
29 30
    private $case;

31
    /** @var int */
32
    private $defaultFetchMode = FetchMode::MIXED;
33

34
    /**
Benjamin Morel's avatar
Benjamin Morel committed
35
     * Wraps <tt>Statement</tt> and applies portability measures.
36
     *
Sergei Morozov's avatar
Sergei Morozov committed
37
     * @param DriverStatement|ResultStatement $stmt
38 39 40
     */
    public function __construct($stmt, Connection $conn)
    {
41
        $this->stmt        = $stmt;
42
        $this->portability = $conn->getPortability();
43
        $this->case        = $conn->getFetchCase();
44 45
    }

Benjamin Morel's avatar
Benjamin Morel committed
46 47 48
    /**
     * {@inheritdoc}
     */
49
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
50
    {
Sergei Morozov's avatar
Sergei Morozov committed
51 52
        assert($this->stmt instanceof DriverStatement);

53
        return $this->stmt->bindParam($column, $variable, $type, $length);
54
    }
55

Benjamin Morel's avatar
Benjamin Morel committed
56 57 58
    /**
     * {@inheritdoc}
     */
59
    public function bindValue($param, $value, $type = ParameterType::STRING)
60
    {
Sergei Morozov's avatar
Sergei Morozov committed
61 62
        assert($this->stmt instanceof DriverStatement);

63 64 65
        return $this->stmt->bindValue($param, $value, $type);
    }

Benjamin Morel's avatar
Benjamin Morel committed
66 67 68
    /**
     * {@inheritdoc}
     */
69 70 71 72 73
    public function closeCursor()
    {
        return $this->stmt->closeCursor();
    }

Benjamin Morel's avatar
Benjamin Morel committed
74 75 76
    /**
     * {@inheritdoc}
     */
77 78 79 80 81
    public function columnCount()
    {
        return $this->stmt->columnCount();
    }

Benjamin Morel's avatar
Benjamin Morel committed
82 83 84
    /**
     * {@inheritdoc}
     */
85 86
    public function errorCode()
    {
Sergei Morozov's avatar
Sergei Morozov committed
87 88
        assert($this->stmt instanceof DriverStatement);

89 90 91
        return $this->stmt->errorCode();
    }

Benjamin Morel's avatar
Benjamin Morel committed
92 93 94
    /**
     * {@inheritdoc}
     */
95 96
    public function errorInfo()
    {
Sergei Morozov's avatar
Sergei Morozov committed
97 98
        assert($this->stmt instanceof DriverStatement);

99 100 101
        return $this->stmt->errorInfo();
    }

Benjamin Morel's avatar
Benjamin Morel committed
102 103 104
    /**
     * {@inheritdoc}
     */
105 106
    public function execute($params = null)
    {
Sergei Morozov's avatar
Sergei Morozov committed
107 108
        assert($this->stmt instanceof DriverStatement);

109 110 111
        return $this->stmt->execute($params);
    }

Benjamin Morel's avatar
Benjamin Morel committed
112 113 114
    /**
     * {@inheritdoc}
     */
115
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
116
    {
117
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
118 119

        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
120 121
    }

Benjamin Morel's avatar
Benjamin Morel committed
122 123 124
    /**
     * {@inheritdoc}
     */
125 126
    public function getIterator()
    {
127
        return new StatementIterator($this);
128 129
    }

Benjamin Morel's avatar
Benjamin Morel committed
130 131 132
    /**
     * {@inheritdoc}
     */
133
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
134
    {
135
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
136

137
        $row = $this->stmt->fetch($fetchMode);
138

139
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
140
        $fixCase    = $this->case !== null
141
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
142 143 144
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

        $row = $this->fixRow($row, $iterateRow, $fixCase);
145

146 147 148
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
149 150 151
    /**
     * {@inheritdoc}
     */
152
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
153
    {
154
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
155

156 157
        if ($fetchArgument) {
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
158
        } else {
159
            $rows = $this->stmt->fetchAll($fetchMode);
160
        }
161

162
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
163
        $fixCase    = $this->case !== null
164 165 166
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);

167
        if (! $iterateRow && ! $fixCase) {
168 169 170
            return $rows;
        }

171
        if ($fetchMode === FetchMode::COLUMN) {
172
            foreach ($rows as $num => $row) {
173
                $rows[$num] = [$row];
174 175 176
            }
        }

177
        foreach ($rows as $num => $row) {
178 179
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
        }
180

181
        if ($fetchMode === FetchMode::COLUMN) {
182 183 184 185 186
            foreach ($rows as $num => $row) {
                $rows[$num] = $row[0];
            }
        }

187 188
        return $rows;
    }
189

Benjamin Morel's avatar
Benjamin Morel committed
190
    /**
191 192 193
     * @param mixed $row
     * @param int   $iterateRow
     * @param bool  $fixCase
Benjamin Morel's avatar
Benjamin Morel committed
194
     *
195
     * @return mixed
Benjamin Morel's avatar
Benjamin Morel committed
196
     */
197 198
    protected function fixRow($row, $iterateRow, $fixCase)
    {
199
        if (! $row) {
200 201
            return $row;
        }
202

203 204 205 206 207
        if ($fixCase) {
            $row = array_change_key_case($row, $this->case);
        }

        if ($iterateRow) {
208
            foreach ($row as $k => $v) {
209 210
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
                    $row[$k] = null;
Steve Müller's avatar
Steve Müller committed
211
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
212 213 214 215
                    $row[$k] = rtrim($v);
                }
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
216

217 218 219
        return $row;
    }

Benjamin Morel's avatar
Benjamin Morel committed
220 221 222
    /**
     * {@inheritdoc}
     */
223 224 225
    public function fetchColumn($columnIndex = 0)
    {
        $value = $this->stmt->fetchColumn($columnIndex);
226

227 228 229
        if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
            if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
                $value = null;
Steve Müller's avatar
Steve Müller committed
230
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
231 232 233
                $value = rtrim($value);
            }
        }
234

235 236 237
        return $value;
    }

Benjamin Morel's avatar
Benjamin Morel committed
238 239 240
    /**
     * {@inheritdoc}
     */
241 242
    public function rowCount()
    {
Sergei Morozov's avatar
Sergei Morozov committed
243 244
        assert($this->stmt instanceof DriverStatement);

245 246
        return $this->stmt->rowCount();
    }
247
}