-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhancedPDO.class.php
909 lines (817 loc) · 30.8 KB
/
enhancedPDO.class.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php
/**
* This file provides a class that adds extra functionalty to PHP's
* PDO classes. It doesn't extend PDO but instead is a wrapper for
* PDO with some methods for doing very common DB stuff
*
* PHP version 7.4
*
* @category EnancedPDO
* @package EnancedPDO
* @author Evan Wills <evan.i.wills@gmail.com>
* @license GPL2 <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
* @link https://github.com/evanwills/php-utils
*/
if (!function_exists('debug')) { function debug() {} } // phpcs:ignore
/**
* This class provides helper functions that build on top of PHP's
* PDO classes. It doesn't extend PDO but instead is a wrapper for
* PDO with some methods for doing very common DB stuff
*
* @category EnancedPDO
* @package EnancedPDO
* @author Evan Wills <evan.i.wills@gmail.com>
* @license GPL2 <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
* @link https://github.com/evanwills/php-utils
*/
class EnhancedPDO
{
/**
* PDO database connection object
*
* @var ?PDO
*/
private $_db = null;
/**
* Host name/IP address of server the database is on
*
* @var string
*/
private $_dbHost = '';
/**
* Name of the database the PDO object was instantiated with
*
* @var string
*/
private $_dbName = '';
/**
* Type of the database being connected to
*
* @var string
*/
private $_dbType = '';
/**
* List of SQL Server response codes that indicate a query was
* successfully executed
*
* > NOTE: Different DB engines may have different error/success
* > codes. As I find them, they'll be added here
*
* @var string[]
*/
private $_successCodes = ['00000'];
/**
* Constructor for DB base class
*
* @param string $dsn The Data Source Name, or DSN,
* contains the information required to
* connect to the database.
* @param ?string $username The user name for the DSN string.
* This parameter is optional for some
* PDO drivers.
* @param ?string $password The password for the DSN string.
* This parameter is optional for some
* PDO drivers.
* @param ?array $options A key => value array of driver
* specific connection options.
*/
public function __construct(
string $dsn,
?string $username = null,
?string $password = null,
?array $options = null
) {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::__construct'); } // phpcs:ignore
try {
$this->_db = new PDO(
$dsn,
(is_string($username)) ? $username : null,
(is_string($password)) ? $password : null,
(is_array($options)) ? $options : null
);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (preg_match('/;host=(.*?)(?=;|$)/i', $dsn, $matches)) {
$this->_dbHost = $matches[1];
}
if (preg_match('/;dbname=(.*?)(?=;|$)/i', $dsn, $matches)) {
$this->_dbName = $matches[1];
}
if (preg_match('/^([^:]+):.*$/i', $dsn, $matches)) {
$this->_dbType = $matches[1];
}
}
/**
* Get the primary database connection object
*
* @return PDO
*/
public function getDB() : PDO
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::getDB'); } // phpcs:ignore
return $this->_db;
}
/**
* Get the host name/IP address of server the database is on
*
* @return string
*/
public function getDbHost() : string
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::getDbHost'); } // phpcs:ignore
return $this->_dbHost;
}
/**
* Get the name of the database specified in the Database
* connection string.
*
* @return string
*/
public function getDbName() : string
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::getDbName'); } // phpcs:ignore
return $this->_dbName;
}
/**
* Get the type of the database PDO is connected to
*
* @return string
*/
public function getDbType() : string
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::getDbType'); } // phpcs:ignore
return $this->_dbType;
}
/**
* Get a prepared PDO statement
*
* @param string $sql SQL statement
* (Must be non-empty string)
* @param PDO|EnhancedPDO|null $db Override DB connection
*
* @return PDOStatement|false
*/
public function prepare(string $sql, $db = null)
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prepare'); } // phpcs:ignore
if (!is_string($sql) || trim($sql) === '') {
throw new Exception(
'EnhancedPDO::prepare() expects first parameter '.
'$sql to be a non-empty string.'
);
}
if ($db !== null) {
if (is_object($db)) {
if (is_a($db, 'PDO')) {
$_db = $db;
} elseif (is_a($db, 'EnhancedPDO')) {
$_db = $db->getDB();
} else {
throw new Exception(
'EnhancedPDO::prepare() expects second '.
'parameter $db to either be NULL or to be '.
'an PDO object. '.get_class($db).' given'
);
}
} else {
throw new Exception(
'EnhancedPDO::prepare() expects second parameter $db '.
'to either be NULL or to be an PDO object. '.
gettype($db).' given'
);
}
} else {
// Just use the default DB connection
$_db = $this->_db;
}
try {
return $_db->prepare($sql);
} catch (Exception $e) {
debug(
'level=-1',
'Error when preparing SQL statement',
// $_db->errorCode(), Code also returned by errorInfo()
$_db->errorInfo(),
$e->getMessage()
);
return false;
}
}
/**
* Get a prepared PDO statement
*
* NOTE: This is just an alias of EnhancedPDO::prepare();
*
* @param string $sql SQL statement
* @param PDO|EnhancedPDO|null $db Override DB connection
*
* @return PDOStatement|false
*/
public function prep($sql, $db = null)
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prep'); } // phpcs:ignore
try {
return $this->prepare($sql, $db);
} catch(Exception $e) {
// throw new Exception($this->_rethrow($e, 'prep'));
throw $e;
}
}
/**
* Execute supplied PDO statement then check if there were any
* errors. If so, throw an exception with the error text as the
* Exception message
*
* @param PDOStatement $stmt PDO Statement to be executed and
* checked
* @param integer $up Debug level (Makes the debug
* metadata show where this this
* method was called from rather
* than this method itself)
*
* @return PDOStatement
* @throws Exception if there is an error with the query
*/
public function execute(PDOStatement $stmt, int $up = 1) : PDOStatement
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::execute'); } // phpcs:ignore
try {
$stmt->execute();
} catch(Exception $e) {
// Capture the contents of PDOStatement::debugDumpParams()
// so it can be used when throwing an exception;
$bits = $this->debug($stmt, true, $up);
if (trim($bits['msg']) === '') {
throw $e;
} else {
throw new Exception("{$bits['msg']}\n\n{$bits['sent']}");
}
}
if (!in_array($stmt->errorCode(), $this->_successCodes)) {
// Capture the contents of PDOStatement::debugDumpParams()
// so it can be used when throwing an exception;
$bits = $this->debug($stmt, true, $up);
throw new Exception($bits['msg']."\n\n".$bits['sent']."\n");
}
return $stmt;
}
/**
* Execute supplied PDO statement then check if there were any
* errors. If so, throw an exception with the error text as the
* Exception message
*
* NOTE: This is just an alias of EnhancedPDO::execute();
*
* @param PDOStatement $stmt PDO Statement to be executed and
* checked
* @param integer $up Debug level (Makes the debug
* metadata show where this this
* method was called from rather
* than this method itself)
*
* @return PDOStatement
* @throws Exception if there is an error with the query
*/
public function exec(PDOStatement $stmt, int $up = 1) : PDOStatement
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::exec'); } // phpcs:ignore
try {
return $this->execute($stmt, $up + 1);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'exec'));
throw $e;
}
}
/**
* Capture the contents of PDOStatement::debugDumpParams() so
* it can be used elsewhere
*
* @param PDOStatement $stmt Statement to be debuged
* @param boolean $debug Whether or not to pass return value
* into debug() before returning it.
* @param integer $up How many levels up the stack should
* the debug message appear to come
* from
*
* @return array with 5 keys:
* `raw` - Raw SQL passed to prepare();
* `sent` - The SQL with parameters bound in
* NOTE: `sent` will be empty if
* statement has not yet been
* executed;
* `params` - List of all the parameters the query
* expects;
* `code` - SQL Error code (if query has been
* executed)
* `msg` - SQL Error message (if query has been
* executed)
*/
public function debug(
PDOStatement $stmt, bool $debug = false, int $up = 1
) : array {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::debug'); } // phpcs:ignore
// Capture the contents of PDOStatement::debugDumpParams()
// so it can be used elsewhere
ob_start();
$stmt->debugDumpParams();
$data = ob_get_clean();
$output = [
'raw' => '',
'sent' => '',
'params' => [],
'code' => $stmt->errorCode(),
'msg' => ''
];
$regex1 = '/^.*?SQL:(?: *\[[0-9]+\])?(?<raw>.*?)[\r\n]+'.
'(?:Sent SQL:(?: *\[[0-9]+\])(?<sent>.*?)[\r\n]+)?'.
'Params: *[0-9]+(?<params>.*)$/is';
$regex2 = '/Key: Name: \[[0-9]+\] (?<name1>:[a-z0-9_]+)'.
'.*?paramno=(?<num>-?[0-9]+).*?'.
'name=\[[0-9]+\] "(?<name2>[^"]+)".*?'.
'is_param=(?<isParam>[0-9]+).*?'.
'param_type=(?<type>[0-9]+)/is';
if (preg_match($regex1, $data, $bits)) {
$output['raw'] = trim($bits['raw']);
$output['sent'] = trim($bits['sent']);
$bits = trim($bits['params']);
if (preg_match_all($regex2, $bits, $params, PREG_SET_ORDER)) {
for ($a = 0; $a < count($params); $a += 1) {
$output['params'][] = [
'name' => ($params[$a]['name2'] !== '')
? $params[$a]['name2']
: $params[$a]['name1'],
'number' => $params[$a]['num'],
'type' => $params[$a]['type'],
'isParam' => ($params[$a]['isParam'] == 1)
];
}
}
}
if (!in_array($output['code'], $this->_successCodes)) {
// Query has been set to server, get whatever error info
// is available and update the output
$tmp = $stmt->errorInfo();
$output['code'] = $tmp[0];
$output['msg'] = $tmp[2];
}
if ($output['raw'] === '') {
// Looks like query may not have been sent to the server.
// Just get the original query string
$output['raw'] = $stmt->queryString;
}
$up += 1;
if ($debug === true) {
debug($output, 'meta-level='.$up);
}
return $output;
}
/**
* Prepare a PDO statement with :ID parameter, bind the supplied
* ID value to the statement then return the statement for
* further binding and execution
*
* NOTE: When binding ID this assumes that the statement's SQL
* contains an :ID parameter, however, you can override
* this by passing a different string as the third parameter
*
* @param string $sql SQL statement string to be
* prepared
* @param integer $id ID of thing to be bound in
* (If ID is false bind step
* is skipped)
* @param string $param Parameter name to bind to
* @param PDO|EnhancedPDO|null $db Override database
* connection object
*
* @return PDOStatement Query with supplied single string
* parameter bound in
* @throws Exception if SQL is not a string or is empty
*/
public function prepBind(
string $sql, int $id, string $param = 'ID', $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prepBind'); } // phpcs:ignore
try {
$stmt = $this->prepare($sql, $db);
} catch(Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBind'));
throw $e;
}
$stmt->bindParam(":$param", $id, PDO::PARAM_INT);
return $stmt;
}
/**
* Prepare a PDO statement with :STR parameter, bind the supplied
* ID value to the statement then return the statement for
* further binding and execution
*
* NOTE: By default, $str is bound to the SQL paramter ":STR".
* However, this can be overriden by passing an alternative
* string as the third parameter `$param`
*
* NOTE ALSO: If you pass a float as the second paramter (`$str`)
* PHP *should* silently coerce the float into a string.
*
* @param string $sql SQL statement string to be
* prepared
* @param string $str String (or float) to be
* bound in
* @param string $param Parameter name to bind to
* (if not ":STR")
* @param PDO|EnhancedPDO|null $db Override database
* connection object
*
* @return PDOStatement Query with supplied a single string
* parameter bound in
* @throws Exception if SQL is not a string or is empty
*/
public function prepBindStr(
string $sql, string $str, string $param = 'STR', $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prepBindStr'); } // phpcs:ignore
try {
$stmt = $this->prepare($sql, $db);
} catch(Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindStr'));
throw $e;
}
$stmt->bindParam(":$param", $str, PDO::PARAM_STR);
return $stmt;
}
/**
* Prepare a PDO statement that uses a "IN" clause as it's
* only clause, build a list of parameters for the IN clause and
* bind all the values to the parameters and return the statement.
*
* This assumes that the query contains an "IN" clause with a
* value of `[[PARAMS]]` e.g.
* ```sql
* SELECT *
* FROM users
* WHERE user_id IN ( [[PARAMS]] );
* ```
*
* @param string $sql SQL statement containing
* @param array $ins List of values
* @param integer $type PDO PARAM type
* [default: 1 (PDO::PARAM_INT)]
* @param PDO|EnhancedPDO|null $db Database connection object
*
* @return PDOStatement
*/
public function prepBindIn(
string $sql, array $ins, int $type = PDO::PARAM_INT, $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('prepBindIn'); } // phpcs:ignore
if (!is_string($sql) || $sql === '') {
throw new Exception(
'EnhancedPDO::prepBindIn() expects first parameter '.
'$sql to be a non-empty string'
);
}
$params = '';
$sep = '';
for ($a = 0, $c = count($ins); $a < $c; $a += 1) {
$params .= $sep.':IN_'.$a;
$sep = ', ';
}
$stmt = $this->prep(
str_replace('[[PARAMS]]', $params, $sql),
$db
);
for ($a = 0, $c = count($ins); $a < $c; $a += 1) {
$stmt->bindParam(':IN_'.$a, $ins[$a], $type);
}
return $stmt;
}
/**
* Prepare a PDO statement that uses a "IN" clause as it's
* only clause, build a list of parameters for the IN clause and
* bind all the values to the parameters, execute the statement
* and return it.
*
* This assumes that the query contains an "IN" clause with a
* value of `[[PARAMS]]` e.g.
* ```sql
* SELECT *
* FROM users
* WHERE user_id IN ( [[PARAMS]] );
* ```
*
* @param string $sql SQL statement containing
* @param array $ins List of values
* @param integer $type PDO PARAM type
* [default: 1 (PDO::PARAM_INT)]
* @param PDO|EnhancedPDO|null $db Database connection object
*
* @return PDOStatement
*/
public function prepBindExecIn(
string $sql, array $ins, int $type = PDO::PARAM_INT, $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('prepBindExecIn'); } // phpcs:ignore
try {
$stmt = $this->prepBindIn($sql, $ins, $type, $db);
} catch(Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExec'));
throw $e;
}
try {
$stmt = $this->execute($stmt, 2);
} catch(Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExec'));
throw $e;
}
return $stmt;
}
/**
* Take a pre-prepared PDO statement, bind a single ID to it,
* execute it then return the statement
*
* NOTE: When binding ID this assumes that the statement's SQL
* contains a single integer :ID parameter, however, you
* can override this by passing a different string as the
* third parameter.
* NOTE ALSO: If there are multiple parameters, they must be
* bound in to the statement before calling
* EnhancedPDO::bindExec()
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param ?integer $id ID of thing to be bound in
* (If ID is NULL bind step is skipped)
* @param string $param Parameter name to bind to
*
* @return PDOStatement
* @throws Exception if there is an error with the query
*/
public function bindExec(
PDOStatement $stmt, ?int $id = null, string $param = 'ID'
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::bindExec'); } // phpcs:ignore
if ($id !== null) {
$stmt->bindParam(":$param", $id, PDO::PARAM_INT);
}
try {
return $this->execute($stmt, 2);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'bindExec'));
throw $e;
}
}
/**
* Take a pre-prepared PDO statement, bind a single string
* (or float) value to it, execute it then return the statement.
*
* NOTE: By default, $str is bound to the SQL paramter ":STR".
* However, this can be overriden by passing an alternative
* string as the third parameter `$param`
*
* NOTE ALSO: If you pass a float as the second paramter (`$str`)
* PHP *should* silently coerce the float into a string.
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param string $str String (or float) value to be
* bound in.
* @param string $param Parameter name to bind to
* (if not "STR")
*
* @return PDOStatement
* @throws Exception if there is an error with the query
*/
public function bindExecStr(
PDOStatement $stmt, string $str, string $param = 'STR'
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::bindExecStr'); } // phpcs:ignore
$stmt->bindParam(":$param", $str, PDO::PARAM_STR);
try {
return $this->execute($stmt, 2);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'bindExecStr'));
throw $e;
}
}
/**
* Prepare a PDO statement with :ID parameter, bind the supplied
* ID value to the statement then execute the statement then
* return the statement for further use
*
* NOTE: When binding ID this assumes that the statement's SQL
* contains a single :ID parameter (and no other params)
*
* @param string $sql SQL statement string to be
* prepared
* @param ?integer $id ID of thing to be bound in
* (If ID is false bind step
* is skipped)
* @param string $param Parameter name to bind to
* @param PDO|EnhancedPDO|null $db Override database
* connection object
*
* @return PDOStatement Query with supplied parameters
* bound in and executed, ready for fetching
* (if appropriate).
* Or FALSE if
* @throws Exception if there is an error with the query
*/
public function prepBindExec(
string $sql, ?int $id = null, string $param = 'ID', $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prepBindExec'); } // phpcs:ignore
try {
$stmt = $this->prepare($sql, $db);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExec'));
throw $e;
}
if ($id !== null) {
$stmt->bindParam(':'.$param, $id, PDO::PARAM_INT);
}
try {
return $this->execute($stmt, 2);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExec'));
throw $e;
}
}
/**
* Prepare a PDO statement with :STR parameter, bind the supplied
* $str value to the statement then execute the statement then
* return the statement for further use
*
* NOTE: By default, $str is bound to the SQL paramter ":STR".
* However, this can be overriden by passing an alternative
* string as the third parameter `$param`
*
* NOTE ALSO: If you pass a float as the second paramter (`$str`)
* PHP *should* silently coerce the float into a string.
*
* @param string $sql SQL statement string to be
* prepared
* @param string $str String (or float) value to
* be bound in.
* @param string $param Parameter name to bind to
* (if not "STR")
* @param PDO|EnhancedPDO|null $db Override database
* connection object
*
* @return PDOStatement|false Query with supplied parameters
* bound in and executed, ready for fetching
* (if appropriate).
* Or FALSE if
* @throws Exception if there is an error with the query
*/
public function prepBindExecStr(
string $sql, string $str, string $param = 'STR', $db = null
) : PDOStatement {
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::prepBindExecStr'); } // phpcs:ignore
try {
$stmt = $this->prepare($sql, $db);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExecStr'));
throw $e;
}
$stmt->bindParam(':'.$param, $str, PDO::PARAM_STR);
try {
return $this->execute($stmt, 2);
} catch (Exception $e) {
// throw new Exception($this->_rethrow($e, 'prepBindExecStr'));
throw $e;
}
}
/**
* Bind string value to parameter in PDO statement
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param string $param Parameter name to bind to
* @param string $value String value to be bound in
*
* @return PDOStatement
*/
public function bindStr(
PDOStatement $stmt, string $param, string $value
) : PDOStatement {
$stmt->bindParam(':'.$param, $value, PDO::PARAM_STR);
return $stmt;
}
/**
* Bind integer value to parameter in PDO statement
*
* This is a useful shortcut when the value you want to bind in
* is the return value of a function or method. Using this saves
* you having to create a separate variable before you bind in
* the value.
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param string $param Parameter name to bind to
* @param integer $value String value to be bound in
*
* @return PDOStatement
*/
public function bindInt(
PDOStatement $stmt, string $param, int $value
) : PDOStatement {
$stmt->bindParam(':'.$param, $value, PDO::PARAM_INT);
return $stmt;
}
/**
* Bind integer value to parameter in PDO statement
*
* This is a useful shortcut when the value you want to bind in
* is the return value of a function or method. Using this saves
* you having to create a separate variable before you bind in
* the value.
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param string $param Parameter name to bind to
* @param boolean $value String value to be bound in
*
* @return PDOStatement
*/
public function bindBool(
PDOStatement $stmt, string $param, bool $value
) : PDOStatement {
$stmt->bindParam(':'.$param, $value, PDO::PARAM_BOOL);
return $stmt;
}
/**
* Bind integer value to parameter in PDO statement
*
* This is a useful shortcut when the value you want to bind in
* is the return value of a function or method. Using this saves
* you having to create a separate variable before you bind in
* the value.
*
* @param PDOStatement $stmt PDO Statement to which parameters
* are bound and executed
* @param string $param Parameter name to bind to
*
* @return PDOStatement
*/
public function bindNull(PDOStatement $stmt, string $param) : PDOStatement
{
$stmt->bindParam(':'.$param, null, PDO::PARAM_NULL);
return $stmt;
}
/**
* Get the ID of the last row inserted into the database.
*
* @return string|int|float
*/
public function getLastID()
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::getLastID'); } // phpcs:ignore
$output = $this->_db->lastInsertId();
return (is_numeric($output))
? $output * 1
: $output;
}
/**
* Is the PDO statement OK and will it return any rows
*
* @param mixed $stmt Value that should be a PDO statement but
* may also be false
*
* @return bool TRUE if statement is OK and some rows were
* selected or effected
*/
public function isOK($stmt) : bool
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::isOK'); } // phpcs:ignore
return ($stmt instanceof PDOStatement
&& $stmt->errorCode() === '00000');
}
/**
* Replace original method name with current method name
*
* @param Exception $e Exception being thrown
* @param string $method Method name where exception is being
* rethrown
*
* @return string
*/
private function _rethrow(Exception $e, string $method) : string
{
if (NEW_RELIC) { newrelic_add_custom_tracer('EnhancedPDO::_rethrow'); } // phpcs:ignore
return str_replace(
[
'prepare', 'prepBind', 'prepBindStr', 'execute',
'bindExec', 'bindExecStr', 'prepBindIn'
],
$method,
$e->getMessage()
);
}
}
if (!defined('NEW_RELIC')) {
define('NEW_RELIC', extension_loaded('newrelic'));
if (!function_exists('newrelic_add_custom_tracer')) {
function newrelic_add_custom_tracer() {} // phpcs:ignore
}
}