@@ -188,8 +188,7 @@ discard block |
||
188 | 188 | */ |
189 | 189 | if(is_object($logger)){ |
190 | 190 | $this->logger = $logger; |
191 | - } |
|
192 | - else{ |
|
191 | + } else{ |
|
193 | 192 | $this->logger =& class_loader('Log', 'classes'); |
194 | 193 | $this->logger->setLogger('Library::Database'); |
195 | 194 | } |
@@ -251,14 +250,12 @@ discard block |
||
251 | 250 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
252 | 251 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
253 | 252 | return true; |
254 | - } |
|
255 | - catch (PDOException $e){ |
|
253 | + } catch (PDOException $e){ |
|
256 | 254 | $this->logger->fatal($e->getMessage()); |
257 | 255 | show_error('Cannot connect to Database.'); |
258 | 256 | return false; |
259 | 257 | } |
260 | - } |
|
261 | - else{ |
|
258 | + } else{ |
|
262 | 259 | show_error('Database configuration is not set.'); |
263 | 260 | return false; |
264 | 261 | } |
@@ -276,8 +273,7 @@ discard block |
||
276 | 273 | $froms .= $this->prefix . $key . ', '; |
277 | 274 | } |
278 | 275 | $this->from = rtrim($froms, ', '); |
279 | - } |
|
280 | - else{ |
|
276 | + } else{ |
|
281 | 277 | $this->from = $this->prefix . $table; |
282 | 278 | } |
283 | 279 | return $this; |
@@ -383,8 +379,7 @@ discard block |
||
383 | 379 | } |
384 | 380 | if (is_null($this->join)){ |
385 | 381 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
386 | - } |
|
387 | - else{ |
|
382 | + } else{ |
|
388 | 383 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
389 | 384 | } |
390 | 385 | return $this; |
@@ -455,12 +450,10 @@ discard block |
||
455 | 450 | foreach($field as $f){ |
456 | 451 | $this->whereIsNull($f, $andOr); |
457 | 452 | } |
458 | - } |
|
459 | - else{ |
|
453 | + } else{ |
|
460 | 454 | if (! $this->where){ |
461 | 455 | $this->where = $field.' IS NULL '; |
462 | - } |
|
463 | - else{ |
|
456 | + } else{ |
|
464 | 457 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
465 | 458 | } |
466 | 459 | } |
@@ -478,12 +471,10 @@ discard block |
||
478 | 471 | foreach($field as $f){ |
479 | 472 | $this->whereIsNotNull($f, $andOr); |
480 | 473 | } |
481 | - } |
|
482 | - else{ |
|
474 | + } else{ |
|
483 | 475 | if (! $this->where){ |
484 | 476 | $this->where = $field.' IS NOT NULL '; |
485 | - } |
|
486 | - else{ |
|
477 | + } else{ |
|
487 | 478 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
488 | 479 | } |
489 | 480 | } |
@@ -510,8 +501,7 @@ discard block |
||
510 | 501 | $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
511 | 502 | } |
512 | 503 | $where = implode(' '.$andOr.' ', $_where); |
513 | - } |
|
514 | - else{ |
|
504 | + } else{ |
|
515 | 505 | if(is_array($op)){ |
516 | 506 | $x = explode('?', $where); |
517 | 507 | $w = ''; |
@@ -524,14 +514,12 @@ discard block |
||
524 | 514 | } |
525 | 515 | } |
526 | 516 | $where = $w; |
527 | - } |
|
528 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
517 | + } else if (! in_array((string)$op, $this->operatorList)){ |
|
529 | 518 | if(is_null($op)){ |
530 | 519 | $op = ''; |
531 | 520 | } |
532 | 521 | $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
533 | - } |
|
534 | - else{ |
|
522 | + } else{ |
|
535 | 523 | if(is_null($val)){ |
536 | 524 | $val = ''; |
537 | 525 | } |
@@ -540,12 +528,10 @@ discard block |
||
540 | 528 | } |
541 | 529 | if (is_null($this->where)){ |
542 | 530 | $this->where = $where; |
543 | - } |
|
544 | - else{ |
|
531 | + } else{ |
|
545 | 532 | if(substr($this->where, -1) == '('){ |
546 | 533 | $this->where = $this->where . ' ' . $where; |
547 | - } |
|
548 | - else{ |
|
534 | + } else{ |
|
549 | 535 | $this->where = $this->where . ' '.$andOr.' ' . $where; |
550 | 536 | } |
551 | 537 | } |
@@ -589,12 +575,10 @@ discard block |
||
589 | 575 | public function groupStart($type = '', $andOr = ' AND'){ |
590 | 576 | if (is_null($this->where)){ |
591 | 577 | $this->where = $type . ' ('; |
592 | - } |
|
593 | - else{ |
|
578 | + } else{ |
|
594 | 579 | if(substr($this->where, -1) == '('){ |
595 | 580 | $this->where .= $type . ' ('; |
596 | - } |
|
597 | - else{ |
|
581 | + } else{ |
|
598 | 582 | $this->where .= $andOr . ' ' . $type . ' ('; |
599 | 583 | } |
600 | 584 | } |
@@ -658,12 +642,10 @@ discard block |
||
658 | 642 | $keys = implode(', ', $_keys); |
659 | 643 | if (is_null($this->where)){ |
660 | 644 | $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
661 | - } |
|
662 | - else{ |
|
645 | + } else{ |
|
663 | 646 | if(substr($this->where, -1) == '('){ |
664 | 647 | $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
665 | - } |
|
666 | - else{ |
|
648 | + } else{ |
|
667 | 649 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
668 | 650 | } |
669 | 651 | } |
@@ -717,12 +699,10 @@ discard block |
||
717 | 699 | } |
718 | 700 | if (is_null($this->where)){ |
719 | 701 | $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
720 | - } |
|
721 | - else{ |
|
702 | + } else{ |
|
722 | 703 | if(substr($this->where, -1) == '('){ |
723 | 704 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
724 | - } |
|
725 | - else{ |
|
705 | + } else{ |
|
726 | 706 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
727 | 707 | } |
728 | 708 | } |
@@ -772,12 +752,10 @@ discard block |
||
772 | 752 | $like = $escape ? $this->escape($data) : $data; |
773 | 753 | if (is_null($this->where)){ |
774 | 754 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
775 | - } |
|
776 | - else{ |
|
755 | + } else{ |
|
777 | 756 | if(substr($this->where, -1) == '('){ |
778 | 757 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
779 | - } |
|
780 | - else{ |
|
758 | + } else{ |
|
781 | 759 | $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
782 | 760 | } |
783 | 761 | } |
@@ -824,8 +802,7 @@ discard block |
||
824 | 802 | } |
825 | 803 | if (! is_null($limitEnd)){ |
826 | 804 | $this->limit = $limit . ', ' . $limitEnd; |
827 | - } |
|
828 | - else{ |
|
805 | + } else{ |
|
829 | 806 | $this->limit = $limit; |
830 | 807 | } |
831 | 808 | return $this; |
@@ -840,12 +817,10 @@ discard block |
||
840 | 817 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
841 | 818 | if (! is_null($orderDir)){ |
842 | 819 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
843 | - } |
|
844 | - else{ |
|
820 | + } else{ |
|
845 | 821 | if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
846 | 822 | $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
847 | - } |
|
848 | - else{ |
|
823 | + } else{ |
|
849 | 824 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
850 | 825 | } |
851 | 826 | } |
@@ -860,8 +835,7 @@ discard block |
||
860 | 835 | public function groupBy($field){ |
861 | 836 | if(is_array($field)){ |
862 | 837 | $this->groupBy = implode(', ', $field); |
863 | - } |
|
864 | - else{ |
|
838 | + } else{ |
|
865 | 839 | $this->groupBy = $field; |
866 | 840 | } |
867 | 841 | return $this; |
@@ -888,14 +862,12 @@ discard block |
||
888 | 862 | } |
889 | 863 | } |
890 | 864 | $this->having = $w; |
891 | - } |
|
892 | - else if (! in_array($op, $this->operatorList)){ |
|
865 | + } else if (! in_array($op, $this->operatorList)){ |
|
893 | 866 | if(is_null($op)){ |
894 | 867 | $op = ''; |
895 | 868 | } |
896 | 869 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
897 | - } |
|
898 | - else{ |
|
870 | + } else{ |
|
899 | 871 | if(is_null($val)){ |
900 | 872 | $val = ''; |
901 | 873 | } |
@@ -940,8 +912,7 @@ discard block |
||
940 | 912 | $query = $this->getAll(true); |
941 | 913 | if($returnSQLQueryOrResultType === true){ |
942 | 914 | return $query; |
943 | - } |
|
944 | - else{ |
|
915 | + } else{ |
|
945 | 916 | return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
946 | 917 | } |
947 | 918 | } |
@@ -980,8 +951,7 @@ discard block |
||
980 | 951 | |
981 | 952 | if($returnSQLQueryOrResultType === true){ |
982 | 953 | return $query; |
983 | - } |
|
984 | - else{ |
|
954 | + } else{ |
|
985 | 955 | return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
986 | 956 | } |
987 | 957 | } |
@@ -999,8 +969,7 @@ discard block |
||
999 | 969 | $columns = array_keys($this->getData()); |
1000 | 970 | $column = implode(',', $columns); |
1001 | 971 | $val = implode(', ', $this->getData()); |
1002 | - } |
|
1003 | - else{ |
|
972 | + } else{ |
|
1004 | 973 | $columns = array_keys($data); |
1005 | 974 | $column = implode(',', $columns); |
1006 | 975 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -1015,8 +984,7 @@ discard block |
||
1015 | 984 | } |
1016 | 985 | $this->insertId = $this->pdo->lastInsertId(); |
1017 | 986 | return $this->insertId(); |
1018 | - } |
|
1019 | - else{ |
|
987 | + } else{ |
|
1020 | 988 | return false; |
1021 | 989 | } |
1022 | 990 | } |
@@ -1034,8 +1002,7 @@ discard block |
||
1034 | 1002 | foreach ($this->getData() as $column => $val){ |
1035 | 1003 | $values[] = $column . ' = ' . $val; |
1036 | 1004 | } |
1037 | - } |
|
1038 | - else{ |
|
1005 | + } else{ |
|
1039 | 1006 | foreach ($data as $column => $val){ |
1040 | 1007 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
1041 | 1008 | } |
@@ -1129,14 +1096,12 @@ discard block |
||
1129 | 1096 | $cacheKey = md5($query . $all . $array); |
1130 | 1097 | if(is_object($this->cacheInstance)){ |
1131 | 1098 | $cacheInstance = $this->cacheInstance; |
1132 | - } |
|
1133 | - else{ |
|
1099 | + } else{ |
|
1134 | 1100 | $obj = & get_instance(); |
1135 | 1101 | $cacheInstance = $obj->cache; |
1136 | 1102 | } |
1137 | 1103 | $cacheContent = $cacheInstance->get($cacheKey); |
1138 | - } |
|
1139 | - else{ |
|
1104 | + } else{ |
|
1140 | 1105 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
1141 | 1106 | } |
1142 | 1107 | |
@@ -1150,8 +1115,7 @@ discard block |
||
1150 | 1115 | $bench = null; |
1151 | 1116 | if(is_object($this->benchmarkInstance)){ |
1152 | 1117 | $bench = $this->benchmarkInstance; |
1153 | - } |
|
1154 | - else{ |
|
1118 | + } else{ |
|
1155 | 1119 | $obj = & get_instance(); |
1156 | 1120 | $bench = $obj->benchmark; |
1157 | 1121 | } |
@@ -1169,15 +1133,13 @@ discard block |
||
1169 | 1133 | //if need return all result like list of record |
1170 | 1134 | if ($all){ |
1171 | 1135 | $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
1172 | - } |
|
1173 | - else{ |
|
1136 | + } else{ |
|
1174 | 1137 | $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
1175 | 1138 | } |
1176 | 1139 | //Sqlite and pgsql always return 0 when using rowCount() |
1177 | 1140 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
1178 | 1141 | $this->numRows = count($this->result); |
1179 | - } |
|
1180 | - else{ |
|
1142 | + } else{ |
|
1181 | 1143 | $this->numRows = $sqlQuery->rowCount(); |
1182 | 1144 | } |
1183 | 1145 | |
@@ -1185,23 +1147,20 @@ discard block |
||
1185 | 1147 | $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
1186 | 1148 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
1187 | 1149 | } |
1188 | - } |
|
1189 | - else{ |
|
1150 | + } else{ |
|
1190 | 1151 | $error = $this->pdo->errorInfo(); |
1191 | 1152 | $this->error = isset($error[2]) ? $error[2] : ''; |
1192 | 1153 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
1193 | 1154 | $this->error(); |
1194 | 1155 | } |
1195 | - } |
|
1196 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
1156 | + } else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
1197 | 1157 | $queryStr = $this->pdo->query($this->query); |
1198 | 1158 | if($queryStr){ |
1199 | 1159 | //Sqlite and pgsql always return 0 when using rowCount() |
1200 | 1160 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
1201 | 1161 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
1202 | 1162 | $this->numRows = 1; |
1203 | - } |
|
1204 | - else{ |
|
1163 | + } else{ |
|
1205 | 1164 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
1206 | 1165 | $this->numRows = $queryStr->rowCount(); |
1207 | 1166 | } |
@@ -1212,8 +1171,7 @@ discard block |
||
1212 | 1171 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
1213 | 1172 | $this->error(); |
1214 | 1173 | } |
1215 | - } |
|
1216 | - else{ |
|
1174 | + } else{ |
|
1217 | 1175 | $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
1218 | 1176 | $this->result = $cacheContent; |
1219 | 1177 | $this->numRows = count($this->result); |