db

Форк
0
/
ResultSet.php 
159 строк · 3.5 Кб
1
<?php
2

3
namespace Upside\Db;
4

5
class ResultSet
6
{
7
    /** The PDOStatement associated with this result set. */
8
    protected \PDOStatement $statement;
9

10
    /**
11
     * @param \PDOStatement $statement The PDOStatement associated with this result set.
12
     */
13
    public function __construct(\PDOStatement $statement)
14
    {
15
        $this->statement = $statement;
16
    }
17

18
    public function __destruct()
19
    {
20
        $this->statement->closeCursor();
21
    }
22

23
    /**
24
     * Count affected rows
25
     */
26
    public function count(): int
27
    {
28
        return $this->statement->rowCount();
29
    }
30

31
    /**
32
     * Fetch all results
33
     */
34
    public function all(?callable $callable = null, int $fetchStyle = 0): array
35
    {
36
        if ($callable === null) {
37
            return $this->statement->fetchAll($fetchStyle);
38
        }
39

40
        return $this->statement->fetchAll($fetchStyle | \PDO::FETCH_FUNC, $callable);
41
    }
42

43
    public function all_group(?bool $uniq = false, ?callable $callable = null): array
44
    {
45
        $fetchStyle = \PDO::FETCH_GROUP | ($uniq ? \PDO::FETCH_UNIQUE : 0);
46
        if ($callable === null) {
47
            return $this->statement->fetchAll($fetchStyle);
48
        }
49

50
        return $this->statement->fetchAll($fetchStyle | \PDO::FETCH_FUNC, $callable);
51
    }
52

53
    /**
54
     * Fetch first result
55
     */
56
    public function first(?callable $callable = null): mixed
57
    {
58
        if ($callable !== null) {
59
            $result = $this->statement->fetch(\PDO::FETCH_ASSOC);
60
            $this->statement->closeCursor();
61
            if (\is_array($result)) {
62
                $result = \call_user_func_array($callable, $result);
63
            }
64
        } else {
65
            $result = $this->statement->fetch();
66
            $this->statement->closeCursor();
67
        }
68

69
        return $result;
70
    }
71

72
    /**
73
     * Fetch next result
74
     */
75
    public function next(): mixed
76
    {
77
        return $this->statement->fetch();
78
    }
79

80
    /**
81
     * Close current cursor
82
     */
83
    public function flush(): bool
84
    {
85
        return $this->statement->closeCursor();
86
    }
87

88
    /**
89
     * Return a column
90
     *
91
     * @param int $col 0-indexed number of the column you wish to retrieve
92
     */
93
    public function column(int $col = 0): mixed
94
    {
95
        return $this->statement->fetchColumn($col);
96
    }
97

98
    /**
99
     * Fetch each result as an associative array
100
     */
101
    public function fetch_assoc(): static
102
    {
103
        $this->statement->setFetchMode(\PDO::FETCH_ASSOC);
104

105
        return $this;
106
    }
107

108
    /**
109
     * Fetch each result as an stdClass object
110
     */
111
    public function fetch_object(): static
112
    {
113
        $this->statement->setFetchMode(\PDO::FETCH_OBJ);
114

115
        return $this;
116
    }
117

118
    public function fetch_named(): static
119
    {
120
        $this->statement->setFetchMode(\PDO::FETCH_NAMED);
121

122
        return $this;
123
    }
124

125
    public function fetch_num(): static
126
    {
127
        $this->statement->setFetchMode(\PDO::FETCH_NUM);
128

129
        return $this;
130
    }
131

132
    public function fetch_both(): static
133
    {
134
        $this->statement->setFetchMode(\PDO::FETCH_BOTH);
135

136
        return $this;
137
    }
138

139
    public function fetch_key_pair(): static
140
    {
141
        $this->statement->setFetchMode(\PDO::FETCH_KEY_PAIR);
142

143
        return $this;
144
    }
145

146
    public function fetch_class(string $class, array $ctorargs = []): static
147
    {
148
        $this->statement->setFetchMode(\PDO::FETCH_CLASS, $class, $ctorargs);
149

150
        return $this;
151
    }
152

153
    public function fetch_custom(\Closure $func): static
154
    {
155
        $func($this->statement);
156

157
        return $this;
158
    }
159
}
160

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.