Passed
Push — main ( b157b0...77200e )
by Peter
02:25
created

PDOHelper::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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