Passed
Push — master ( 3829ec...650305 )
by Aleksandar
03:24 queued 15s
created

PdoResultReader::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
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 $fetchMode = \PDO::FETCH_ASSOC;
38
39
    /**
40
     * Whether statement was executed.
41
     * @var bool
42
     */
43
    protected $isExecuted = false;
44
45
    /**
46
     * Constructor for PDO Result reader
47
     * @param \PDOStatement $statement Statement to be read from.
48
     */
49
    public function __construct(\PDOStatement $statement)
50
    {
51
        $this->statement = $statement;
52
    }
53
54
55
    /**
56
     * Create a reader from PDO statement.
57
     *
58
     * @param \PDOStatement $statement
59
     * @return static
60
     */
61
    public static function create(\PDOStatement $statement)
62
    {
63
        return new static($statement);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function getAllRows(): array
70
    {
71
        $results = [];
72
73
        while (($row = $this->getNextRow()) !== false) {
74
            $results[] = $row;
75
        }
76
77
        return $results;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function getNextRow()
84
    {
85
        return $this->statement->fetch($this->fetchMode);
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function getAllColumns($columnIndex = 0): array
92
    {
93
        $results = [];
94
95
        while (($column = $this->getNextColumn($columnIndex)) !== false) {
96
            $results[] = $column;
97
        }
98
99
        return $results;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function getNextColumn($columnIndex = 0)
106
    {
107
        return $this->statement->fetchColumn($columnIndex);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function reset(): void
114
    {
115
        if ($this->isExecuted) {
116
            $this->finalize();
117
        }
118
119
        $this->statement->execute();
120
        $this->isExecuted = true;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function finalize(): void
127
    {
128
        $this->statement->closeCursor();
129
        $this->isExecuted = false;
130
    }
131
}