Passed
Push — main ( 87ff77...29cbd1 )
by Peter
09:57
created

PDOWrapper::fetchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Extra;
6
7
use PDO;
8
use PDOStatement;
9
use QB\Generic\IQueryPart;
10
11
class PDOWrapper
12
{
13
    protected PDO $pdo;
14
15
    /**
16
     * Wrapper constructor.
17
     *
18
     * @param \PDO $pdo
19
     */
20 6
    public function __construct(PDO $pdo)
21
    {
22 6
        $this->pdo = $pdo;
23 6
    }
24
25
    /**
26
     * @return \PDO
27
     */
28
    public function getPdo(): PDO
29
    {
30
        return $this->pdo;
31
    }
32
33
    /**
34
     * @suppress PhanUndeclaredMethod
35
     *
36
     * @param IQueryPart $query
37
     *
38
     * @return PDOStatement
39
     */
40 6
    public function prepare(IQueryPart $query): PDOStatement
41
    {
42 6
        $sql    = (string)$query;
43 6
        $params = $query->getParams();
44
45 6
        $statement = $this->pdo->prepare($sql);
46
47 6
        foreach ($params as $k => $v) {
48 6
            $k2 = is_numeric($k) ? $k + 1 : $k;
49 6
            $statement->bindParam($k2, $v[0], $v[1]);
50
        }
51
52 6
        return $statement;
53
    }
54
55
    /**
56
     * @param IQueryPart $query
57
     *
58
     * @return bool
59
     */
60 1
    public function execute(IQueryPart $query): bool
61
    {
62 1
        $statement = $this->prepare($query);
63
64 1
        return $statement->execute();
65
    }
66
67
    /**
68
     * @param IQueryPart $query
69
     * @param int        $mode
70
     * @param mixed      ...$args
71
     *
72
     * @return array
73
     */
74 1
    public function fetchAll(IQueryPart $query, int $mode = PDO::FETCH_BOTH, ...$args): array
75
    {
76 1
        $statement = $this->prepare($query);
77
78 1
        $statement->execute();
79
80 1
        return $statement->fetchAll($mode, ...$args);
81
    }
82
83
    /**
84
     * @param IQueryPart $query
85
     * @param int        $column
86
     *
87
     * @return array|int|bool|float|null
88
     */
89 1
    public function fetchColumn(IQueryPart $query, int $column = 0)
90
    {
91 1
        $statement = $this->prepare($query);
92
93 1
        $statement->execute();
94
95 1
        return $statement->fetchColumn($column);
96
    }
97
98
    /**
99
     * @param IQueryPart $query
100
     * @param int        $column
101
     * @param int        $orientation
102
     * @param int        $offset
103
     *
104
     * @return array
105
     */
106 1
    public function fetch(
107
        IQueryPart $query,
108
        int $column = 0,
109
        int $orientation = PDO::FETCH_ORI_NEXT,
110
        int $offset = 0
111
    ): array {
112 1
        $statement = $this->prepare($query);
113
114 1
        $statement->execute();
115
116 1
        return $statement->fetch($column, $orientation, $offset);
117
    }
118
}
119