PdoResultReader::getColumnRows()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\Pdo;
19
20
use ArekX\PQL\Contracts\ResultReader;
21
22
/**
23
 * PDO Reader for reading data from Statements.
24
 */
25
class PdoResultReader implements ResultReader
26
{
27
    /**
28
     * Current statement in use.
29
     * @var \PDOStatement
30
     */
31
    public \PDOStatement $statement;
32
33
    /**
34
     * Current fetch mode.
35
     * @var int
36
     */
37
    public int $fetchMode = \PDO::FETCH_ASSOC;
38
39
    /**
40
     * Whether statement was executed.
41
     * @var bool
42
     */
43
    protected bool $isExecuted = false;
44
45
    /**
46
     * Constructor for PDO Result reader
47
     * @param \PDOStatement $statement Statement to be read from.
48
     */
49 11
    public function __construct(\PDOStatement $statement)
50
    {
51 11
        $this->statement = $statement;
52
    }
53
54
55
    /**
56
     * Create a reader from PDO statement.
57
     *
58
     * @param \PDOStatement $statement
59
     * @return static
60
     */
61 11
    public static function create(\PDOStatement $statement): static
62
    {
63 11
        return new static($statement);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 2
    public function getAllRows(): array
70
    {
71 2
        $results = [];
72
73 2
        while (($row = $this->getNextRow()) !== false) {
74 2
            $results[] = $row;
75
        }
76
77 2
        return $results;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 3
    public function getNextRow(): mixed
84
    {
85 3
        return $this->statement->fetch($this->fetchMode);
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 1
    public function getColumnRows($columnIndex = 0): array
92
    {
93 1
        $results = [];
94
95 1
        while (($column = $this->getNextColumn($columnIndex)) !== false) {
96 1
            $results[] = $column;
97
        }
98
99 1
        return $results;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 8
    public function getNextColumn($columnIndex = 0): mixed
106
    {
107 8
        return $this->statement->fetchColumn($columnIndex);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113 1
    public function reset(): void
114
    {
115 1
        if ($this->isExecuted) {
116 1
            $this->finalize();
117
        }
118
119 1
        $this->statement->execute();
120 1
        $this->isExecuted = true;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126 9
    public function finalize(): void
127
    {
128 9
        $this->statement->closeCursor();
129 9
        $this->isExecuted = false;
130
    }
131
}
132