1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeekLab\GLPDO2; |
4
|
|
|
|
5
|
|
|
// Make EA inspection stop complaining. |
6
|
|
|
use PDO; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class GLPDO2 |
10
|
|
|
{ |
11
|
|
|
/** @var PDO $PDO */ |
12
|
|
|
private $PDO; |
13
|
|
|
|
14
|
1 |
|
public function __construct(PDO $pdo) |
15
|
|
|
{ |
16
|
1 |
|
$this->PDO = $pdo; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Begin transaction. |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
3 |
|
public function beginTransaction(): bool |
25
|
|
|
{ |
26
|
3 |
|
return $this->PDO->beginTransaction(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Is the operation in the middle of the transaction? |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
2 |
|
public function inTransaction(): bool |
35
|
|
|
{ |
36
|
2 |
|
return $this->PDO->inTransaction(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Commit transaction. |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
1 |
|
public function commit(): bool |
45
|
|
|
{ |
46
|
1 |
|
return $this->PDO->commit(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Rollback transaction. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
1 |
|
public function rollback(): bool |
55
|
|
|
{ |
56
|
1 |
|
return $this->PDO->rollBack(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Perform UPDATE or DELETE query and return the number of affected rows. |
61
|
|
|
* |
62
|
|
|
* @param Statement $SQL |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
50 |
|
private function queryAffectedRows(Statement $SQL): int |
68
|
|
|
{ |
69
|
|
|
// Execute statement |
70
|
50 |
|
$sth = $SQL->execute($this->PDO); |
71
|
|
|
|
72
|
|
|
// Return number of rows affected |
73
|
50 |
|
return $sth->rowCount(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Perform DELETE query. |
78
|
|
|
* |
79
|
|
|
* @param Statement $SQL |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
50 |
|
public function queryDelete(Statement $SQL): int |
85
|
|
|
{ |
86
|
50 |
|
return $this->queryAffectedRows($SQL); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Perform UPDATE query |
91
|
|
|
* |
92
|
|
|
* @param Statement $SQL |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
1 |
|
public function queryUpdate(Statement $SQL): int |
98
|
|
|
{ |
99
|
1 |
|
return $this->queryAffectedRows($SQL); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Perform INSERT query |
104
|
|
|
* |
105
|
|
|
* @param Statement $SQL |
106
|
|
|
* |
107
|
|
|
* @return bool|string |
108
|
|
|
* @throws Exception |
109
|
|
|
*/ |
110
|
50 |
|
public function queryInsert(Statement $SQL) |
111
|
|
|
{ |
112
|
|
|
// Execute the statement |
113
|
50 |
|
$sth = $SQL->execute($this->PDO); |
114
|
|
|
|
115
|
50 |
|
if (!$sth->rowCount()) { |
116
|
|
|
// Insert failed |
117
|
50 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Return the ID |
121
|
50 |
|
return $this->PDO->lastInsertId(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Execute statement and returns first row of results as an associative array. |
126
|
|
|
* |
127
|
|
|
* @param Statement $SQL |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
* @throws Exception |
131
|
|
|
*/ |
132
|
5 |
|
public function selectRow(Statement $SQL): mixed |
133
|
|
|
{ |
134
|
|
|
// Execute the statement |
135
|
5 |
|
$sth = $SQL->execute($this->PDO); |
136
|
|
|
|
137
|
|
|
// Return the first row fetched |
138
|
5 |
|
return $sth->fetch(PDO::FETCH_ASSOC); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return multiple rows result as an array |
143
|
|
|
* |
144
|
|
|
* @param Statement $SQL |
145
|
|
|
* |
146
|
|
|
* @return array{} | false |
147
|
|
|
* @throws Exception |
148
|
|
|
*/ |
149
|
15 |
|
public function selectRows(Statement $SQL): bool | array |
150
|
|
|
{ |
151
|
|
|
// Execute the statement |
152
|
15 |
|
$sth = $SQL->execute($this->PDO); |
153
|
|
|
|
154
|
15 |
|
return $sth->fetchAll(PDO::FETCH_ASSOC); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Executes statement and return a specific column from the first row of results. |
159
|
|
|
* |
160
|
|
|
* @param Statement $SQL |
161
|
|
|
* @param string $column |
162
|
|
|
* @param bool $caseSensitive |
163
|
|
|
* @param mixed | null $default |
164
|
|
|
* |
165
|
|
|
* @return string | null |
166
|
|
|
* @throws Exception |
167
|
|
|
*/ |
168
|
2 |
|
public function selectValue( |
169
|
|
|
Statement $SQL, |
170
|
|
|
string $column, |
171
|
|
|
bool $caseSensitive = false, |
172
|
|
|
mixed $default = null |
173
|
|
|
): ?string { |
174
|
2 |
|
$row = $this->selectRow($SQL); |
175
|
|
|
|
176
|
2 |
|
if (!$caseSensitive) { |
177
|
1 |
|
$row = array_change_key_case($row); |
178
|
1 |
|
$column = strtolower($column); |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
return $row[$column] ?? $default; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|