-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPDODb.php
1621 lines (1431 loc) · 44.7 KB
/
PDODb.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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PDODb Class
*
* @category Database Access
* @package PDODb
* @author Jeffery Way <[email protected]>
* @author Josh Campbell <[email protected]>
* @author Alexander V. Butenko <[email protected]>
* @author Vasiliy A. Ulin <[email protected]>
* @copyright Copyright (c) 2010-2016
* @license http://opensource.org/licenses/lgpl-3.0.html The GNU Lesser General Public License, version 3.0
* @link http://github.com/joshcam/PHP-MySQLi-Database-Class
* @version 1.1.0
*/
class PDODb
{
/**
* Database credentials
* @var string
*/
private $connectionParams = [
'type' => 'mysql',
'host' => null,
'username' => null,
'password' => null,
'dbname' => null,
'port' => null,
'charset' => null
];
/**
* FOR UPDATE flag
* @var bool
*/
private $forUpdate = false;
/**
* Dynamic type list for group by condition value
* @var array
*/
private $groupBy = [];
/**
* An array that holds having conditions
* @var array
*/
private $having = [];
/**
* Static instance of self
* @var PDODb
*/
private static $instance;
/**
* Is Subquery object
* @var bool
*/
private $isSubQuery = false;
/**
* An array that holds where joins
* @var array
*/
private $join = [];
/**
* Last error infromation
* @var array
*/
private $lastError = [];
/**
* Last error code
* @var string
*/
private $lastErrorCode = '';
/**
* Name of the auto increment column
* @var string
*/
private $lastInsertId = null;
/**
* The previously executed SQL query
* @var string
*/
private $lastQuery = '';
/**
* LOCK IN SHARE MODE flag
* @var bool
*/
private $lockInShareMode = false;
/**
* Should join() results be nested by table
* @var bool
*/
private $nestJoin = false;
/**
* Dynamic type list for order by condition value
* @var array
*/
private $orderBy = [];
/**
* Rows per 1 page on paginate() method
* @var int
*/
private $pageLimit = 10;
/**
* Binded params
* @var array
*/
private $params = [];
/**
* PDO instance
* @var PDO
*/
private $pdo;
/**
* Database prefix
* @var string
*/
private $prefix = '';
/**
* Query string
* @var string
*/
private $query = '';
/**
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE
* @var array
*/
private $queryOptions = [];
/**
* Query type
* @var string
*/
private $queryType = '';
/**
* Type of returned result
* @var string
*/
private $returnType = PDO::FETCH_ASSOC;
/**
* Number of affected rows
* @var int
*/
private $rowCount = 0;
/**
* Transaction flag
* @var bool
*/
private $transaction = false;
/**
* Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
* @var int
*/
public $totalCount = 0;
/**
* Total pages of paginate() method
* @var int
*/
public $totalPages = 0;
/**
* Column names for update when using onDuplicate method
* @var array
*/
protected $updateColumns = null;
/**
* Option to use generator (yield) on get() and rawQuery methods
* @var bool
*/
private $useGenerator = false;
/**
* An array that holds where conditions
* @var array
*/
private $where = [];
/**
* @param string|array|object $type
* @param string $host
* @param string $username
* @param string $password
* @param string $dbname
* @param int $port
* @param string $charset
*/
public function __construct($type, $host = null, $username = null, $password = null, $dbname = null, $port = null, $charset = null)
{
if (is_array($type)) { // if params were passed as array
$this->connectionParams = $type;
} elseif (is_object($type)) { // if type is set as pdo object
$this->pdo = $type;
} else {
foreach ($this->connectionParams as $key => $param) {
if (isset($$key) && !is_null($$key)) {
$this->connectionParams[$key] = $$key;
}
}
}
if (isset($this->connectionParams['prefix'])) {
$this->setPrefix($this->connectionParams['prefix']);
}
if (isset($this->connectionParams['isSubQuery'])) {
$this->isSubQuery = true;
return;
}
self::$instance = $this;
}
/**
* Abstraction method that will build the part of the WHERE conditions
*
* @param string $operator
* @param array $conditions
*/
private function buildCondition($operator, $conditions)
{
if (empty($conditions)) {
return;
}
//Prepare the where portion of the query
$this->query .= ' '.$operator;
foreach ($conditions as $cond) {
list ($concat, $varName, $operator, $val) = $cond;
$this->query .= " ".$concat." ".$varName;
switch (strtolower($operator)) {
case 'not in':
case 'in':
$comparison = ' '.$operator.' (';
if (is_object($val)) {
$comparison .= $this->buildPair("", $val);
} else {
foreach ($val as $v) {
$comparison .= ' ?,';
$this->params[] = $v;
}
}
$this->query .= rtrim($comparison, ',').' ) ';
break;
case 'not between':
case 'between':
$this->query .= " $operator ? AND ? ";
$this->params = array_merge($this->params, $val);
break;
case 'not exists':
case 'exists':
$this->query.= $operator.$this->buildPair("", $val);
break;
default:
if (is_array($val)) {
$this->params = array_merge($this->params, $val);
} elseif ($val === null) {
$this->query .= ' '.$operator." NULL";
} elseif ($val != 'DBNULL' || $val == '0') {
$this->query .= $this->buildPair($operator, $val);
}
}
}
}
/**
* Insert/Update query helper
*
* @param array $tableData
* @param array $tableColumns
* @param bool $isInsert INSERT operation flag
* @throws Exception
*/
private function buildDataPairs($tableData, $tableColumns, $isInsert)
{
foreach ($tableColumns as $column) {
$value = $tableData[$column];
if (!$isInsert) {
if (strpos($column, '.') === false) {
$this->query .= "`".$column."` = ";
} else {
$this->query .= str_replace('.', '.`', $column)."` = ";
}
}
// Subquery value
if ($value instanceof PDODb) {
$this->query .= $this->buildPair("", $value).", ";
continue;
}
// Simple value
if (!is_array($value)) {
$this->query .= '?, ';
$this->params[] = $value;
continue;
}
// Function value
$key = key($value);
$val = $value[$key];
switch ($key) {
case '[I]':
$this->query .= $column.$val.", ";
break;
case '[F]':
$this->query .= $val[0].", ";
if (!empty($val[1])) {
foreach ($val[1] as $param) {
$this->params[] = $param;
}
}
break;
case '[N]':
if ($val == null) {
$this->query .= "!".$column.", ";
} else {
$this->query .= "!".$val.", ";
}
break;
default:
throw new Exception("Wrong operation");
}
}
$this->query = rtrim($this->query, ', ');
}
/**
* Abstraction method that will build the GROUP BY part of the WHERE statement
*
* @return void
*/
protected function buildGroupBy()
{
if (empty($this->groupBy)) {
return;
}
$this->query .= " GROUP BY ";
foreach ($this->groupBy as $key => $value) {
$this->query .= $value.", ";
}
$this->query = rtrim($this->query, ', ')." ";
}
/**
* Build insert query
*
* @param string $tableName
* @param array $insertData
* @param string $operation
* @return int|bool
*/
private function buildInsert($tableName, $insertData, $operation)
{
$this->query = $operation.implode(' ', $this->queryOptions).' INTO '.$this->getTableName($tableName);
$this->queryType = $operation;
$stmt = $this->buildQuery(null, $insertData);
$status = $stmt->execute();
$this->rowCount = $stmt->rowCount();
$this->lastError = $stmt->errorInfo();
$this->lastErrorCode = $stmt->errorCode();
$this->reset();
if ($status && $this->pdo()->lastInsertId() > 0) {
return (int) $this->pdo()->lastInsertId();
}
return $status;
}
/**
* Abstraction method that will build the LIMIT part of the WHERE statement
*
* @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count
* @return void
*/
protected function _buildLimit($numRows)
{
if (!isset($numRows)) {
return;
}
if (is_array($numRows)) {
$this->query .= ' LIMIT '.(int) $numRows[0].', '.(int) $numRows[1];
} else {
$this->query .= ' LIMIT '.(int) $numRows;
}
}
/**
* Abstraction method that will build an INSERT or UPDATE part of the query
*
* @param array $tableData
* @return void
*/
private function buildInsertQuery($tableData)
{
if (!is_array($tableData)) {
return;
}
$isInsert = in_array($this->queryType, ['REPLACE', 'INSERT']);
$dataColumns = array_keys($tableData);
if ($isInsert) {
if (isset($dataColumns[0])) $this->query .= ' (`'.implode('`, `', $dataColumns).'`) ';
$this->query .= ' VALUES (';
} else {
$this->query .= " SET ";
}
$this->buildDataPairs($tableData, $dataColumns, $isInsert);
if ($isInsert) {
$this->query .= ')';
}
}
/**
* Abstraction method that will build an JOIN part of the query
*
* @return void
*/
private function buildJoin()
{
if (empty($this->join)) {
return;
}
foreach ($this->join as $data) {
list ($joinType, $joinTable, $joinCondition) = $data;
if (is_object($joinTable)) {
$joinStr = $this->buildPair("", $joinTable);
} else {
$joinStr = $joinTable;
}
$this->query .= " ".$joinType." JOIN ".$joinStr.
(false !== stripos($joinCondition, 'using') ? " " : " ON ")
.$joinCondition;
}
}
/**
* Abstraction method that will build the LIMIT part of the WHERE statement
*
* @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count
* @return void
*/
private function buildLimit($numRows)
{
if (!isset($numRows)) {
return;
}
if (is_array($numRows)) {
$this->query .= ' LIMIT '.(int) $numRows[0].', '.(int) $numRows[1];
} else {
$this->query .= ' LIMIT '.(int) $numRows;
}
}
/**
* Helper function to add variables into the query statement
*
* @param array $tableData Variable with values
*/
protected function buildOnDuplicate($tableData)
{
if (is_array($this->updateColumns) && !empty($this->updateColumns)) {
$this->query .= " ON DUPLICATE KEY UPDATE ";
if ($this->lastInsertId) {
$this->query .= $this->lastInsertId."=LAST_INSERT_ID (".$this->lastInsertId."), ";
}
foreach ($this->updateColumns as $key => $val) {
// skip all params without a value
if (is_numeric($key)) {
$this->updateColumns[$val] = '';
unset($this->updateColumns[$key]);
} else {
$tableData[$key] = $val;
}
}
$this->buildDataPairs($tableData, array_keys($this->updateColumns), false);
}
}
/**
* Abstraction method that will build the LIMIT part of the WHERE statement
*
* @return void
*/
private function buildOrderBy()
{
if (empty($this->orderBy)) {
return;
}
$this->query .= " ORDER BY ";
foreach ($this->orderBy as $prop => $value) {
if (strtolower(str_replace(" ", "", $prop)) == 'rand()') {
$this->query .= "RAND(), ";
} else {
$this->query .= $prop." ".$value.", ";
}
}
$this->query = rtrim($this->query, ', ')." ";
}
/**
* Helper function to add variables into bind parameters array and will return
* its SQL part of the query according to operator in ' $operator ?' or
* ' $operator ($subquery) ' formats
*
* @param string $operator
* @param mixed $value Variable with values
* @return string
*/
private function buildPair($operator, $value)
{
if (!is_object($value)) {
$this->params[] = $value;
return ' '.$operator.' ? ';
}
$subQuery = $value->getSubQuery();
foreach ($subQuery['params'] as $value) {
$this->params[] = $value;
}
return " ".$operator." (".$subQuery['query'].") ".$subQuery['alias'];
}
/**
* Abstraction method that will compile the WHERE statement,
* any passed update data, and the desired rows.
* It then builds the SQL query.
*
* @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count
* @param array $tableData Should contain an array of data for updating the database.
* @return PDOStatement Returns the $stmt object.
*/
private function buildQuery($numRows, $tableData = null)
{
$this->buildJoin();
$this->buildInsertQuery($tableData);
$this->buildCondition('WHERE', $this->where);
$this->buildGroupBy();
$this->buildCondition('HAVING', $this->having);
$this->buildOrderBy();
$this->buildLimit($numRows);
$this->buildOnDuplicate($tableData);
if ($this->isSubQuery) {
return;
}
return $this->prepare();
}
/**
* Return query result
*
* @param PDOStatement $stmt
* @return array
*/
private function buildResult($stmt)
{
if ($this->useGenerator) {
return $this->buildResultGenerator($stmt);
} else {
return $stmt->fetchAll($this->returnType);
}
}
/**
* Return generator object
* @param PDOStatement $stmt
* @return Generator
*/
private function buildResultGenerator($stmt)
{
while ($row = $stmt->fetch($this->returnType)) {
yield $row;
}
}
/**
* Shutdown handler to rollback uncommited operations in order to keep
* atomic operations sane.
*
* @uses pdo->rollback();
*/
public function checkTransactionStatus()
{
if (!$this->transaction) {
return;
}
$this->rollback();
}
/**
* Transaction commit
*
* @uses pdo->commit();
* @return bool
*/
public function commit()
{
$result = $this->pdo()->commit();
$this->transaction = false;
return $result;
}
/**
* A method to connect to the database
*
* @throws Exception
* @return void
*/
public function connect()
{
if (empty($this->connectionParams['type'])) {
throw new Exception('DB Type is not set.');
}
$connectionString = $this->connectionParams['type'].':';
$connectionParams = ['host', 'dbname', 'port', 'charset'];
foreach ($connectionParams as $connectionParam) {
if (!empty($this->connectionParams[$connectionParam])) {
$connectionString .= $connectionParam.'='.$this->connectionParams[$connectionParam].';';
}
}
$connectionString = rtrim($connectionString, ';');
$this->pdo = new PDO($connectionString, $this->connectionParams['username'], $this->connectionParams['password']);
}
/**
* Method returns a copy of a PDODb subquery object
*
* @return PDODb new PDODb object
*/
public function copy()
{
$copy = clone $this;
$copy->pdo = null;
return $copy;
}
/**
* Method generates decrimental function call
*
* @param int $num increment by int or float. 1 by default
* @return array
*/
public function dec($num = 1)
{
if (!is_numeric($num)) {
throw new Exception('Argument supplied to dec must be a number');
}
return array("[I]" => "-".$num);
}
/**
* Delete query. Call the "where" method first.
*
* @param string $tableName The name of the database table to work with.
* @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count
* @return bool Indicates success. 0 or 1.
*/
public function delete($tableName, $numRows = null)
{
if ($this->isSubQuery) {
return;
}
$table = $this->prefix.$tableName;
if (count($this->join)) {
$this->query = "DELETE ".preg_replace('/.* (.*)/', '$1', $table)." FROM ".$table;
} else {
$this->query = "DELETE FROM ".$table;
}
$stmt = $this->buildQuery($numRows);
$stmt->execute();
$this->lastError = $stmt->errorInfo();
$this->lastErrorCode = $stmt->errorCode();
$this->rowCount = $stmt->rowCount();
$this->reset();
return ($this->rowCount > 0);
}
/**
* This method is needed for prepared statements. They require
* the data type of the field to be bound with "i" s", etc.
* This function takes the input, determines what type it is,
* and then updates the param_type.
*
* @param mixed $item Input to determine the type.
* @return string The joined parameter types.
*/
private function determineType($item)
{
switch (gettype($item)) {
case 'NULL':
return PDO::PARAM_NULL;
case 'string':
return PDO::PARAM_STR;
case 'boolean':
return PDO::PARAM_BOOL;
case 'integer':
return PDO::PARAM_INT;
case 'blob':
return PDO::PARAM_LOB;
case 'double':
return PDO::PARAM_STR;
default:
return PDO::PARAM_STR;
}
}
/**
* A method of returning the static instance to allow access to the
* instantiated object from within another class.
* Inheriting this class would require reloading connection info.
*
* @uses $db = PDODb::getInstance();
* @return PDODb Returns the current instance.
*/
public static function getInstance()
{
return self::$instance;
}
/**
* Method returns db error
*
* @return string
*/
public function getLastError()
{
if (!$this->pdo) {
return "pdo is null";
}
return $this->lastError;
}
/**
* Method returns db error code
*
* @return int
*/
public function getLastErrorCode()
{
return $this->lastErrorCode;
}
/**
* Get last insert id
*
* @return int
*/
public function getLastInsertId()
{
return $this->pdo()->lastInsertId();
}
/**
* Method returns last executed query
*
* @return string
*/
public function getLastQuery()
{
return $this->lastQuery;
}
/**
* Method returns params
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Get count of affected rows
*
* @return int
*/
public function getRowCount()
{
return $this->rowCount;
}
/**
* Mostly internal method to get query and its params out of subquery object
* after get() and getAll()
*
* @return array
*/
public function getSubQuery()
{
if (!$this->isSubQuery) {
return null;
}
$val = ['query' => $this->query,
'params' => $this->params,
'alias' => $this->connectionParams['host']
];
$this->reset();
return $val;
}
/**
* Get table name with prefix
*
* @param string $tableName
* @return string
*/
private function getTableName($tableName)
{
return strpos($tableName, '.') !== false ? $tableName : $this->prefix.$tableName;
}
/**
* This method allows you to specify multiple (method chaining optional) GROUP BY statements for SQL queries.
*
* @uses $PDODb->groupBy('name');
* @param string $groupByField The name of the database field.
* @return PDODb
*/
public function groupBy($groupByField)
{
$groupByField = preg_replace("/[^-a-z0-9\.\(\),_\*]+/i", '', $groupByField);
$this->groupBy[] = $groupByField;
return $this;
}
/**
* A convenient function that returns TRUE if exists at least an element that
* satisfy the where condition specified calling the "where" method before this one.
*
* @param string $tableName The name of the database table to work with.
*
* @return array Contains the returned rows from the select query.
*/
public function has($tableName)
{
$result = $this->getOne($tableName);
return $result ? true : false;
}
/**
* This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries.
*
* @uses $PDODb->having('SUM(tags) > 10')
* @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field.
* @param string $operator Comparison operator. Default is =
* @return PDODb
*/
public function having($havingProp, $havingValue = 'DBNULL', $operator = '=', $cond = 'AND')
{
// forkaround for an old operation api
if (is_array($havingValue) && ($key = key($havingValue)) != "0") {
$operator = $key;
$havingValue = $havingValue[$key];
}
if (count($this->having) == 0) {
$cond = '';
}
$this->having[] = array($cond, $havingProp, $operator, $havingValue);
return $this;
}
/**
* Escape harmful characters which might affect a query.
*
* @param mixed $value The value to escape.
* @return string The escaped string.
*/
public function escape($value)
{
return $this->pdo()->quote($value, $this->determineType($value));
}
/**
* Method generates user defined function call
*
* @param string $expr user function body
* @param array $bindParams
* @return array
*/
public function func($expr, $bindParams = null)
{
return ["[F]" => [$expr, $bindParams]];
}
/**
* A convenient SELECT * function.
*
* @param string $tableName The name of the database table to work with.
* @param int|array $numRows Array to define SQL limit in format Array ($count, $offset)
* or only $count
* @param string $columns Desired columns
* @return array Contains the returned rows from the select query.
*/
public function get($tableName, $numRows = null, $columns = '*')
{
if (empty($columns)) {
$columns = '*';
}
$column = is_array($columns) ? implode(', ', $columns) : $columns;
$this->query = 'SELECT '.implode(' ', $this->queryOptions).' '.
$column." FROM ".$this->getTableName($tableName);
$stmt = $this->buildQuery($numRows);
if ($this->isSubQuery) {
return $this;
}
$stmt->execute();
$this->lastError = $stmt->errorInfo();
$this->lastErrorCode = $stmt->errorCode();
$this->rowCount = $stmt->rowCount();
if (in_array('SQL_CALC_FOUND_ROWS', $this->queryOptions)) {
$totalStmt = $this->pdo()->query('SELECT FOUND_ROWS()');
$this->totalCount = $totalStmt->fetchColumn();
}
$result = $this->buildResult($stmt);
$this->reset();
return $result;
}
/**
* A convenient SELECT * function to get one record.
*
* @param string $tableName The name of the database table to work with.
* @param string $columns Desired columns
* @return array Contains the returned rows from the select query.
*/
public function getOne($tableName, $columns = '*')
{
$result = $this->get($tableName, 1, $columns);
if ($result instanceof PDODb) {
return $result;
}