-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatement.php
134 lines (119 loc) · 3.19 KB
/
Statement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace ModernPDO;
/**
* Wrapper over PDOStatement.
*
* Use ModerPDO objects to get this class object.
*/
class Statement
{
/**
* Statement constructor.
*/
public function __construct(
private ?\PDOStatement $statement,
) {
}
/**
* Returns statement status.
*/
public function status(): bool
{
return $this->statement !== null;
}
/**
* Returns the number of rows affected by the last SQL statement.
*
* @see https://www.php.net/manual/en/pdostatement.rowcount.php
*/
public function rowCount(): int
{
try {
$count = $this->statement?->rowCount() ?? 0;
} catch (\Throwable $th) {
$count = 0;
}
return $count;
}
/**
* Returns the number of columns in the result set.
*
* @see https://www.php.net/manual/ru/pdostatement.columncount.php
*/
public function columnCount(): int
{
try {
$count = $this->statement?->columnCount() ?? 0;
} catch (\Throwable $th) {
$count = 0;
}
return $count;
}
/**
* Returns a single column from the next row of a result set.
*
* @return mixed If successful, a value, otherwise false
*
* @see https://www.php.net/manual/en/pdostatement.fetchall.php
*/
public function fetchColumn(int $column = 0): mixed
{
try {
/** @var mixed Comment for PSalm and CSFixer */
$result = $this->statement?->fetchColumn($column) ?? false;
} catch (\Throwable $th) {
$result = false;
}
return $result;
}
/**
* Fetches the next row and returns it as an object.
*
* @return object|false If successful, an object, otherwise false
*
* @see https://www.php.net/manual/en/pdostatement.fetchobject.php
*/
public function fetchObject(): object|false
{
try {
$object = $this->statement?->fetchObject() ?? false;
} catch (\Throwable $th) {
$object = false;
}
return $object;
}
/**
* Fetches the next row from a result set.
*
* @return array<string, mixed> If successful, an array of data, otherwise an empty array
*
* @see https://www.php.net/manual/en/pdostatement.fetch.php
*/
public function fetch(): array
{
try {
/** @var array<string, mixed>|false */
$row = $this->statement?->fetch(\PDO::FETCH_ASSOC) ?? false;
} catch (\Throwable $th) {
$row = false;
}
return $row !== false ? $row : [];
}
/**
* Fetches the remaining rows from a result set.
*
* @return list<array<string, mixed>> If successful, an array of arrays, otherwise an empty array
*
* @see https://www.php.net/manual/en/pdostatement.fetchall.php
*/
public function fetchAll(): array
{
try {
/** @var list<array<string, mixed>>|false */
$rows = $this->statement?->fetchAll(\PDO::FETCH_ASSOC) ?? false;
} catch (\Throwable $th) {
$rows = false;
}
return $rows !== false ? $rows : [];
}
}