FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PHP-MySQL-Class/DBPDO.php at master · a1phanumeric/PHP-MySQL-Class · GitHub
a1phanumeric
/
PHP-MySQL-Class
Public
Notifications
You must be signed in to change notification settings
Fork
248
Star
360
Code
Issues
16
Pull requests
4
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
PHP-MySQL-Class
/
DBPDO.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
384 lines (307 loc) · 9.3 KB
Breadcrumbs
PHP-MySQL-Class
/
DBPDO.php
Copy path
File metadata and controls
384 lines (307 loc) · 9.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace
A1phanumeric
;
use
Closure
;
use
PDO
;
use
PDOException
;
use
PDOStatement
;
use
RuntimeException
;
class
DBPDO
{
private
static
array
$
instances
= [];
public
?
PDO
$
pdo
=
null
;
private
?
string
$
error
=
null
;
private
string
$
dbname
;
private
string
$
dbhost
;
private
string
$
dbuser
;
private
string
$
dbpass
;
private
bool
$
sqlserver
;
private
array
$
options
;
/** @var callable|null */
private
$
queryLogger
=
null
;
public
static
function
getInstance
(
string
$
dbhost
,
string
$
dbname
,
string
$
dbuser
,
string
$
dbpass
,
bool
$
sqlserver
=
false
,
array
$
options
= []
):
self
{
$
instanceKey
=
self
::
buildInstanceKey
(
$
dbhost
,
$
dbname
,
$
dbuser
,
$
sqlserver
,
$
options
);
if
(!
isset
(
self
::
$
instances
[
$
instanceKey
])) {
self
::
$
instances
[
$
instanceKey
] =
new
self
(
$
dbhost
,
$
dbname
,
$
dbuser
,
$
dbpass
,
$
sqlserver
,
$
options
);
}
return
self
::
$
instances
[
$
instanceKey
];
}
public
function
__construct
(
string
$
dbhost
=
''
,
string
$
dbname
=
''
,
string
$
dbuser
=
''
,
string
$
dbpass
=
''
,
bool
$
sqlserver
=
false
,
array
$
options
= []
) {
$
this
->
dbhost
=
$
dbhost
;
$
this
->
dbname
=
$
dbname
;
$
this
->
dbuser
=
$
dbuser
;
$
this
->
dbpass
=
$
dbpass
;
$
this
->
sqlserver
=
$
sqlserver
;
$
this
->
options
=
$
options
;
$
this
->
connect
();
}
private
static
function
buildInstanceKey
(
string
$
dbhost
,
string
$
dbname
,
string
$
dbuser
,
bool
$
sqlserver
,
array
$
options
):
string
{
$
stableOptions
=
$
options
;
ksort
(
$
stableOptions
);
return
hash
(
'
sha256
'
,
implode
(
'
|
'
, [
$
dbhost
,
$
dbname
,
$
dbuser
,
$
sqlserver
?
'
sqlsrv
'
:
'
mysql
'
,
json_encode
(
$
stableOptions
),
]));
}
// Disallow cloning and unserializing
private
function
__clone
()
{
}
public
function
__wakeup
():
void
{
throw
new
RuntimeException
(
'
Unserializing DBPDO is not allowed.
'
);
}
public
function
prep_query
(
string
$
query
):
PDOStatement
{
if
(
$
this
->
pdo
===
null
&& !
$
this
->
connect
()) {
throw
new
RuntimeException
(
'
Database connection is not available.
'
);
}
return
$
this
->
pdo
->
prepare
(
$
query
);
}
public
function
connect
():
bool
{
if
(
$
this
->
pdo
instanceof
PDO
) {
return
true
;
}
$
dsn
=
$
this
->
buildDsn
();
$
user
=
$
this
->
dbuser
;
$
password
=
$
this
->
dbpass
;
try
{
$
this
->
pdo
=
new
PDO
(
$
dsn
,
$
user
,
$
password
,
$
this
->
buildPdoAttributes
());
$
this
->
error
=
null
;
return
true
;
}
catch
(
PDOException
$
e
) {
$
this
->
error
=
$
e
->
getMessage
();
return
false
;
}
}
public
function
table_exists
(
string
$
table_name
):
bool
{
if
(
$
this
->
sqlserver
) {
$
stmt
=
$
this
->
execute
(
'
SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ? AND TABLE_CATALOG = ?
'
,
[
$
table_name
,
$
this
->
dbname
]
);
if
(
$
stmt
===
false
) {
return
false
;
}
return
(
bool
)
$
stmt
->
fetchColumn
();
}
$
stmt
=
$
this
->
prep_query
(
'
SHOW TABLES LIKE ?
'
);
$
stmt
->
execute
([
$
table_name
]);
return
$
stmt
->
rowCount
() >
0
;
}
/**
* @param mixed $values
*/
public
function
execute
(
string
$
query
,
$
values
=
null
,
bool
$
debug
=
false
)
{
$
params
=
$
this
->
normalizeValues
(
$
values
);
if
(
$
debug
) {
trigger_error
(
'
DBPDO::execute($debug=true) is deprecated. Use setQueryLogger() for diagnostics.
'
,
E_USER_DEPRECATED
);
echo
$
query
;
print_r
(
$
params
);
return
false
;
}
$
start
=
microtime
(
true
);
try
{
$
stmt
=
$
this
->
prep_query
(
$
query
);
$
stmt
->
execute
(
$
params
);
$
durationMs
= (
microtime
(
true
) -
$
start
) *
1000
;
if
(
is_callable
(
$
this
->
queryLogger
)) {
(
$
this
->
queryLogger
)(
$
query
,
$
params
,
$
durationMs
,
null
);
}
return
$
stmt
;
}
catch
(
PDOException
$
e
) {
$
this
->
error
=
$
e
->
getMessage
();
if
(
is_callable
(
$
this
->
queryLogger
)) {
(
$
this
->
queryLogger
)(
$
query
,
$
params
,
null
,
$
this
->
error
);
}
return
false
;
}
}
/**
* @param mixed $values
*/
public
function
fetch
(
string
$
query
,
$
values
=
null
): ?
array
{
$
stmt
=
$
this
->
execute
(
$
query
,
$
values
);
if
(
$
stmt
===
false
) {
return
null
;
}
$
result
=
$
stmt
->
fetch
(
PDO
::
FETCH_ASSOC
);
return
$
result
===
false
?
null
:
$
result
;
}
/**
* @param mixed $values
*/
public
function
fetchAll
(
string
$
query
,
$
values
=
null
, ?
string
$
key
=
null
):
array
{
$
stmt
=
$
this
->
execute
(
$
query
,
$
values
);
if
(
$
stmt
===
false
) {
return
[];
}
$
results
=
$
stmt
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
if
(
$
key
!==
null
&& !
empty
(
$
results
)) {
$
keyed_results
= [];
foreach
(
$
results
as
$
result
) {
if
(
array_key_exists
(
$
key
,
$
result
)) {
$
keyed_results
[
$
result
[
$
key
]] =
$
result
;
}
}
return
$
keyed_results
;
}
return
$
results
;
}
public
function
beginTransaction
():
bool
{
if
(
$
this
->
pdo
===
null
&& !
$
this
->
connect
()) {
return
false
;
}
return
$
this
->
pdo
->
beginTransaction
();
}
public
function
commit
():
bool
{
if
(
$
this
->
pdo
===
null
) {
return
false
;
}
return
$
this
->
pdo
->
commit
();
}
public
function
rollBack
():
bool
{
if
(
$
this
->
pdo
===
null
|| !
$
this
->
pdo
->
inTransaction
()) {
return
false
;
}
return
$
this
->
pdo
->
rollBack
();
}
public
function
inTransaction
():
bool
{
if
(
$
this
->
pdo
===
null
) {
return
false
;
}
return
$
this
->
pdo
->
inTransaction
();
}
/**
* @param Closure $callback function(DBPDO $db): mixed
* @return mixed
*/
public
function
transaction
(
Closure
$
callback
)
{
if
(!
$
this
->
beginTransaction
()) {
throw
new
RuntimeException
(
'
Unable to start database transaction.
'
);
}
try
{
$
result
=
$
callback
(
$
this
);
$
this
->
commit
();
return
$
result
;
}
catch
(
\
Throwable
$
e
) {
if
(
$
this
->
inTransaction
()) {
$
this
->
rollBack
();
}
throw
$
e
;
}
}
public
function
lastInsertId
():
string
{
if
(
$
this
->
pdo
===
null
&& !
$
this
->
connect
()) {
return
'
0
'
;
}
return
$
this
->
pdo
->
lastInsertId
();
}
public
function
getPdo
(): ?
PDO
{
return
$
this
->
pdo
;
}
public
function
getLastError
(): ?
string
{
return
$
this
->
error
;
}
/**
* @param callable $logger function(string $query, array $params, ?float $durationMs, ?string $error): void
*/
public
function
setQueryLogger
(
callable
$
logger
):
self
{
$
this
->
queryLogger
=
$
logger
;
return
$
this
;
}
private
function
buildDsn
():
string
{
if
(
$
this
->
sqlserver
) {
$
encrypt
=
$
this
->
options
[
'
encrypt
'
] ??
false
;
$
trustServerCertificate
=
$
this
->
options
[
'
trust_server_certificate
'
] ??
false
;
$
port
=
$
this
->
options
[
'
port
'
] ??
null
;
$
server
=
$
this
->
dbhost
;
if
(
$
port
!==
null
) {
$
server
.=
'
,
'
.
$
port
;
}
return
sprintf
(
'
sqlsrv:Server=%s;Database=%s;Encrypt=%s;TrustServerCertificate=%s
'
,
$
server
,
$
this
->
dbname
,
$
encrypt
?
'
yes
'
:
'
no
'
,
$
trustServerCertificate
?
'
yes
'
:
'
no
'
);
}
$
charset
=
$
this
->
options
[
'
charset
'
] ??
'
utf8mb4
'
;
$
port
=
$
this
->
options
[
'
port
'
] ??
null
;
$
dsn
=
sprintf
(
'
mysql:dbname=%s;host=%s;charset=%s
'
,
$
this
->
dbname
,
$
this
->
dbhost
,
$
charset
);
if
(
$
port
!==
null
) {
$
dsn
.=
'
;port=
'
. (
int
)
$
port
;
}
return
$
dsn
;
}
private
function
buildPdoAttributes
():
array
{
$
persistent
=
$
this
->
options
[
'
persistent
'
] ??
false
;
$
attributes
= [
PDO
::
ATTR_ERRMODE
=>
PDO
::
ERRMODE_EXCEPTION
,
PDO
::
ATTR_DEFAULT_FETCH_MODE
=>
PDO
::
FETCH_ASSOC
,
PDO
::
ATTR_PERSISTENT
=> (
bool
)
$
persistent
,
];
if
(!
$
this
->
sqlserver
) {
$
attributes
[
PDO
::
ATTR_EMULATE_PREPARES
] =
false
;
if
(
isset
(
$
this
->
options
[
'
timeout
'
])) {
$
attributes
[
PDO
::
ATTR_TIMEOUT
] = (
int
)
$
this
->
options
[
'
timeout
'
];
}
}
if
(
isset
(
$
this
->
options
[
'
attributes
'
]) &&
is_array
(
$
this
->
options
[
'
attributes
'
])) {
$
attributes
=
$
this
->
options
[
'
attributes
'
] +
$
attributes
;
}
return
$
attributes
;
}
/**
* @param mixed $values
*/
private
function
normalizeValues
(
$
values
):
array
{
if
(
$
values
===
null
) {
return
[];
}
if
(
is_array
(
$
values
)) {
return
$
values
;
}
return
[
$
values
];
}
}
Back
|
FazBrowse Home
|
New Git URL