Failed Conditions
Push — 3.0.x ( 66f057...14f5f1 )
by Sergei
36s queued 17s
created

Statement::errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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