Completed
Pull Request — develop (#3515)
by Sergei
20:18
created

Statement::rowCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Portability;
6
7
use Doctrine\DBAL\Driver\ResultStatement;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use Doctrine\DBAL\Driver\StatementIterator;
10
use Doctrine\DBAL\FetchMode;
11
use Doctrine\DBAL\ParameterType;
12
use IteratorAggregate;
13
use function array_change_key_case;
14
use function assert;
15
use function is_string;
16
use function rtrim;
17
18
/**
19
 * Portability wrapper for a Statement.
20
 */
21
class Statement implements IteratorAggregate, DriverStatement
22
{
23
    /** @var int */
24
    private $portability;
25
26
    /** @var DriverStatement|ResultStatement */
27
    private $stmt;
28
29
    /** @var int */
30
    private $case;
31
32
    /** @var int */
33
    private $defaultFetchMode = FetchMode::MIXED;
34
35
    /**
36
     * Wraps <tt>Statement</tt> and applies portability measures.
37
     *
38
     * @param DriverStatement|ResultStatement $stmt
39
     */
40 289
    public function __construct($stmt, Connection $conn)
41
    {
42 289
        $this->stmt        = $stmt;
43 289
        $this->portability = $conn->getPortability();
44 289
        $this->case        = $conn->getFetchCase();
45 289
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 169
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
51
    {
52 169
        assert($this->stmt instanceof DriverStatement);
53
54 169
        $this->stmt->bindParam($column, $variable, $type, $length);
55 169
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 145
    public function bindValue($param, $value, $type = ParameterType::STRING) : void
61
    {
62 145
        assert($this->stmt instanceof DriverStatement);
63
64 145
        $this->stmt->bindValue($param, $value, $type);
65 145
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 121
    public function closeCursor() : void
71
    {
72 121
        $this->stmt->closeCursor();
73 121
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 97
    public function columnCount()
79
    {
80 97
        return $this->stmt->columnCount();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 279
    public function execute($params = null) : void
87
    {
88 279
        assert($this->stmt instanceof DriverStatement);
89
90 279
        $this->stmt->execute($params);
91 279
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 282
    public function setFetchMode($fetchMode, ...$args) : void
97
    {
98 282
        $this->defaultFetchMode = $fetchMode;
99
100 282
        $this->stmt->setFetchMode($fetchMode, ...$args);
101 282
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 329
    public function getIterator()
107
    {
108 329
        return new StatementIterator($this);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 279
    public function fetch($fetchMode = null, ...$args)
115
    {
116 279
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
117
118 279
        $row = $this->stmt->fetch($fetchMode, ...$args);
119
120 279
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
121 279
        $fixCase    = $this->case !== null
122 279
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
123 279
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
124
125 279
        $row = $this->fixRow($row, $iterateRow, $fixCase);
126
127 279
        return $row;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 281
    public function fetchAll($fetchMode = null, ...$args)
134
    {
135 281
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
136
137 281
        $rows = $this->stmt->fetchAll($fetchMode, ...$args);
138
139 281
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
140 281
        $fixCase    = $this->case !== null
141 281
            && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED)
142 281
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
143
144 281
        if (! $iterateRow && ! $fixCase) {
145
            return $rows;
146
        }
147
148 281
        if ($fetchMode === FetchMode::COLUMN) {
149 231
            foreach ($rows as $num => $row) {
150 231
                $rows[$num] = [$row];
151
            }
152
        }
153
154 281
        foreach ($rows as $num => $row) {
155 281
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
156
        }
157
158 281
        if ($fetchMode === FetchMode::COLUMN) {
159 231
            foreach ($rows as $num => $row) {
160 231
                $rows[$num] = $row[0];
161
            }
162
        }
163
164 281
        return $rows;
165
    }
166
167
    /**
168
     * @param mixed $row
169
     * @param int   $iterateRow
170
     * @param bool  $fixCase
171
     *
172
     * @return mixed
173
     */
174 282
    protected function fixRow($row, $iterateRow, $fixCase)
175
    {
176 282
        if (! $row) {
177 279
            return $row;
178
        }
179
180 282
        if ($fixCase) {
181 103
            $row = array_change_key_case($row, $this->case);
182
        }
183
184 282
        if ($iterateRow) {
185 281
            foreach ($row as $k => $v) {
186 281
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
187 276
                    $row[$k] = null;
188 281
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
189 123
                    $row[$k] = rtrim($v);
190
                }
191
            }
192
        }
193
194 282
        return $row;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function fetchColumn($columnIndex = 0)
201
    {
202
        $value = $this->stmt->fetchColumn($columnIndex);
203
204
        if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
205
            if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
206
                $value = null;
207
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
208
                $value = rtrim($value);
209
            }
210
        }
211
212
        return $value;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 25
    public function rowCount() : int
219
    {
220 25
        assert($this->stmt instanceof DriverStatement);
221
222 25
        return $this->stmt->rowCount();
223
    }
224
}
225