Failed Conditions
Pull Request — master (#2958)
by Sergei
38:14
created

Statement::columnCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Portability;
21
22
/**
23
 * Portability wrapper for a Statement.
24
 *
25
 * @link   www.doctrine-project.org
26
 * @since  2.0
27
 * @author Benjamin Eberlei <[email protected]>
28
 */
29
class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
30
{
31
    /**
32
     * @var integer
33
     */
34
    private $portability;
35
36
    /**
37
     * @var \Doctrine\DBAL\Driver\Statement
38
     */
39
    private $stmt;
40
41
    /**
42
     * @var integer
43
     */
44
    private $case;
45
46
    /**
47
     * @var integer
48
     */
49
    private $defaultFetchMode = self::FETCH_BOTH;
50
51
    /**
52
     * Wraps <tt>Statement</tt> and applies portability measures.
53
     *
54
     * @param \Doctrine\DBAL\Driver\Statement       $stmt
55
     * @param \Doctrine\DBAL\Portability\Connection $conn
56
     */
57 15
    public function __construct($stmt, Connection $conn)
58
    {
59 15
        $this->stmt = $stmt;
60 15
        $this->portability = $conn->getPortability();
61 15
        $this->case = $conn->getFetchCase();
62 15
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function bindParam($column, &$variable, $type = Statement::PARAM_STR, $length = null)
68
    {
69 1
        return $this->stmt->bindParam($column, $variable, $type, $length);
70
    }
71
    /**
72
     * {@inheritdoc}
73
     */
74
75 1
    public function bindValue($param, $value, $type = self::PARAM_STR)
76
    {
77 1
        return $this->stmt->bindValue($param, $value, $type);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    public function closeCursor()
84
    {
85 1
        return $this->stmt->closeCursor();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function columnCount()
92
    {
93 1
        return $this->stmt->columnCount();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function errorCode()
100
    {
101 1
        return $this->stmt->errorCode();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function errorInfo()
108
    {
109 1
        return $this->stmt->errorInfo();
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 3
    public function execute($params = null)
116
    {
117 3
        return $this->stmt->execute($params);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 6
    public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
124
    {
125 6
        $this->defaultFetchMode = $fetchMode;
126
127 6
        return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 3
    public function getIterator()
134
    {
135 3
        $data = $this->fetchAll();
136
137 3
        return new \ArrayIterator($data);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 2
    public function fetch($fetchMode = null)
144
    {
145 2
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
146
147 2
        $row = $this->stmt->fetch($fetchMode);
148
149 2
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
150 2
        $fixCase    = ! is_null($this->case) && ($fetchMode == self::FETCH_ASSOC || $fetchMode == self::FETCH_BOTH)
151 2
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
152
153 2
        $row = $this->fixRow($row, $iterateRow, $fixCase);
154
155 2
        return $row;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 6
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
162
    {
163 6
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
164
165 6
        if ($fetchArgument) {
166
            $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
167
        } else {
168 6
            $rows = $this->stmt->fetchAll($fetchMode);
169
        }
170
171 6
        $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
172 6
        $fixCase    = ! is_null($this->case) && ($fetchMode === self::FETCH_ASSOC || $fetchMode === self::FETCH_BOTH)
173 6
            && ($this->portability & Connection::PORTABILITY_FIX_CASE);
174
175 6
        if ( ! $iterateRow && !$fixCase) {
176 1
            return $rows;
177
        }
178
179 5
        if ($fetchMode === self::FETCH_COLUMN) {
180 3
            foreach ($rows as $num => $row) {
181 3
                $rows[$num] = [$row];
182
            }
183
        }
184
185 5
        foreach ($rows as $num => $row) {
186 5
            $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase);
187
        }
188
189 5
        if ($fetchMode === self::FETCH_COLUMN) {
190 3
            foreach ($rows as $num => $row) {
191 3
                $rows[$num] = $row[0];
192
            }
193
        }
194
195 5
        return $rows;
196
    }
197
198
    /**
199
     * @param mixed   $row
200
     * @param integer $iterateRow
201
     * @param boolean $fixCase
202
     *
203
     * @return array
204
     */
205 5
    protected function fixRow($row, $iterateRow, $fixCase)
206
    {
207 5
        if ( ! $row) {
208 2
            return $row;
209
        }
210
211 5
        if ($fixCase) {
212
            $row = array_change_key_case($row, $this->case);
213
        }
214
215 5
        if ($iterateRow) {
216 5
            foreach ($row as $k => $v) {
217 5
                if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') {
218 3
                    $row[$k] = null;
219 5
                } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) {
220 5
                    $row[$k] = rtrim($v);
221
                }
222
            }
223
        }
224
225 5
        return $row;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function fetchColumn($columnIndex = 0)
232
    {
233
        $value = $this->stmt->fetchColumn($columnIndex);
234
235
        if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) {
236
            if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') {
237
                $value = null;
238
            } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) {
239
                $value = rtrim($value);
240
            }
241
        }
242
243
        return $value;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 1
    public function rowCount()
250
    {
251 1
        return $this->stmt->rowCount();
252
    }
253
}
254