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