@@ -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 | } |
@@ -236,11 +235,9 @@ discard block |
||
| 236 | 235 | $dsn = $config['driver'] . ':host=' . $config['hostname'] . ';' |
| 237 | 236 | . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
| 238 | 237 | . 'dbname=' . $config['database']; |
| 239 | - } |
|
| 240 | - else if ($config['driver'] == 'sqlite'){ |
|
| 238 | + } else if ($config['driver'] == 'sqlite'){ |
|
| 241 | 239 | $dsn = 'sqlite:' . $config['database']; |
| 242 | - } |
|
| 243 | - else if($config['driver'] == 'oracle'){ |
|
| 240 | + } else if($config['driver'] == 'oracle'){ |
|
| 244 | 241 | $dsn = 'oci:dbname=' . $config['host'] . '/' . $config['database']; |
| 245 | 242 | } |
| 246 | 243 | |
@@ -249,14 +246,12 @@ discard block |
||
| 249 | 246 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 250 | 247 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 251 | 248 | return true; |
| 252 | - } |
|
| 253 | - catch (PDOException $e){ |
|
| 249 | + } catch (PDOException $e){ |
|
| 254 | 250 | $this->logger->fatal($e->getMessage()); |
| 255 | 251 | show_error('Cannot connect to Database.'); |
| 256 | 252 | return false; |
| 257 | 253 | } |
| 258 | - } |
|
| 259 | - else{ |
|
| 254 | + } else{ |
|
| 260 | 255 | show_error('Database configuration is not set.'); |
| 261 | 256 | return false; |
| 262 | 257 | } |
@@ -274,8 +269,7 @@ discard block |
||
| 274 | 269 | $froms .= $this->prefix . $key . ', '; |
| 275 | 270 | } |
| 276 | 271 | $this->from = rtrim($froms, ', '); |
| 277 | - } |
|
| 278 | - else{ |
|
| 272 | + } else{ |
|
| 279 | 273 | $this->from = $this->prefix . $table; |
| 280 | 274 | } |
| 281 | 275 | return $this; |
@@ -381,8 +375,7 @@ discard block |
||
| 381 | 375 | } |
| 382 | 376 | if (is_null($this->join)){ |
| 383 | 377 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 384 | - } |
|
| 385 | - else{ |
|
| 378 | + } else{ |
|
| 386 | 379 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 387 | 380 | } |
| 388 | 381 | return $this; |
@@ -453,12 +446,10 @@ discard block |
||
| 453 | 446 | foreach($field as $f){ |
| 454 | 447 | $this->whereIsNull($f, $andOr); |
| 455 | 448 | } |
| 456 | - } |
|
| 457 | - else{ |
|
| 449 | + } else{ |
|
| 458 | 450 | if (! $this->where){ |
| 459 | 451 | $this->where = $field.' IS NULL '; |
| 460 | - } |
|
| 461 | - else{ |
|
| 452 | + } else{ |
|
| 462 | 453 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
| 463 | 454 | } |
| 464 | 455 | } |
@@ -476,12 +467,10 @@ discard block |
||
| 476 | 467 | foreach($field as $f){ |
| 477 | 468 | $this->whereIsNotNull($f, $andOr); |
| 478 | 469 | } |
| 479 | - } |
|
| 480 | - else{ |
|
| 470 | + } else{ |
|
| 481 | 471 | if (! $this->where){ |
| 482 | 472 | $this->where = $field.' IS NOT NULL '; |
| 483 | - } |
|
| 484 | - else{ |
|
| 473 | + } else{ |
|
| 485 | 474 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
| 486 | 475 | } |
| 487 | 476 | } |
@@ -508,8 +497,7 @@ discard block |
||
| 508 | 497 | $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
| 509 | 498 | } |
| 510 | 499 | $where = implode(' '.$andOr.' ', $_where); |
| 511 | - } |
|
| 512 | - else{ |
|
| 500 | + } else{ |
|
| 513 | 501 | if(is_array($op)){ |
| 514 | 502 | $x = explode('?', $where); |
| 515 | 503 | $w = ''; |
@@ -522,14 +510,12 @@ discard block |
||
| 522 | 510 | } |
| 523 | 511 | } |
| 524 | 512 | $where = $w; |
| 525 | - } |
|
| 526 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 513 | + } else if (! in_array((string)$op, $this->operatorList)){ |
|
| 527 | 514 | if(is_null($op)){ |
| 528 | 515 | $op = ''; |
| 529 | 516 | } |
| 530 | 517 | $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 531 | - } |
|
| 532 | - else{ |
|
| 518 | + } else{ |
|
| 533 | 519 | if(is_null($val)){ |
| 534 | 520 | $val = ''; |
| 535 | 521 | } |
@@ -538,12 +524,10 @@ discard block |
||
| 538 | 524 | } |
| 539 | 525 | if (is_null($this->where)){ |
| 540 | 526 | $this->where = $where; |
| 541 | - } |
|
| 542 | - else{ |
|
| 527 | + } else{ |
|
| 543 | 528 | if(substr($this->where, -1) == '('){ |
| 544 | 529 | $this->where = $this->where . ' ' . $where; |
| 545 | - } |
|
| 546 | - else{ |
|
| 530 | + } else{ |
|
| 547 | 531 | $this->where = $this->where . ' '.$andOr.' ' . $where; |
| 548 | 532 | } |
| 549 | 533 | } |
@@ -587,12 +571,10 @@ discard block |
||
| 587 | 571 | public function groupStart($type = '', $andOr = ' AND'){ |
| 588 | 572 | if (is_null($this->where)){ |
| 589 | 573 | $this->where = $type . ' ('; |
| 590 | - } |
|
| 591 | - else{ |
|
| 574 | + } else{ |
|
| 592 | 575 | if(substr($this->where, -1) == '('){ |
| 593 | 576 | $this->where .= $type . ' ('; |
| 594 | - } |
|
| 595 | - else{ |
|
| 577 | + } else{ |
|
| 596 | 578 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 597 | 579 | } |
| 598 | 580 | } |
@@ -656,12 +638,10 @@ discard block |
||
| 656 | 638 | $keys = implode(', ', $_keys); |
| 657 | 639 | if (is_null($this->where)){ |
| 658 | 640 | $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
| 659 | - } |
|
| 660 | - else{ |
|
| 641 | + } else{ |
|
| 661 | 642 | if(substr($this->where, -1) == '('){ |
| 662 | 643 | $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
| 663 | - } |
|
| 664 | - else{ |
|
| 644 | + } else{ |
|
| 665 | 645 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
| 666 | 646 | } |
| 667 | 647 | } |
@@ -715,12 +695,10 @@ discard block |
||
| 715 | 695 | } |
| 716 | 696 | if (is_null($this->where)){ |
| 717 | 697 | $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 718 | - } |
|
| 719 | - else{ |
|
| 698 | + } else{ |
|
| 720 | 699 | if(substr($this->where, -1) == '('){ |
| 721 | 700 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 722 | - } |
|
| 723 | - else{ |
|
| 701 | + } else{ |
|
| 724 | 702 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 725 | 703 | } |
| 726 | 704 | } |
@@ -770,12 +748,10 @@ discard block |
||
| 770 | 748 | $like = $escape ? $this->escape($data) : $data; |
| 771 | 749 | if (is_null($this->where)){ |
| 772 | 750 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 773 | - } |
|
| 774 | - else{ |
|
| 751 | + } else{ |
|
| 775 | 752 | if(substr($this->where, -1) == '('){ |
| 776 | 753 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 777 | - } |
|
| 778 | - else{ |
|
| 754 | + } else{ |
|
| 779 | 755 | $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 780 | 756 | } |
| 781 | 757 | } |
@@ -822,8 +798,7 @@ discard block |
||
| 822 | 798 | } |
| 823 | 799 | if (! is_null($limitEnd)){ |
| 824 | 800 | $this->limit = $limit . ', ' . $limitEnd; |
| 825 | - } |
|
| 826 | - else{ |
|
| 801 | + } else{ |
|
| 827 | 802 | $this->limit = $limit; |
| 828 | 803 | } |
| 829 | 804 | return $this; |
@@ -838,12 +813,10 @@ discard block |
||
| 838 | 813 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 839 | 814 | if (! is_null($orderDir)){ |
| 840 | 815 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
| 841 | - } |
|
| 842 | - else{ |
|
| 816 | + } else{ |
|
| 843 | 817 | if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
| 844 | 818 | $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 845 | - } |
|
| 846 | - else{ |
|
| 819 | + } else{ |
|
| 847 | 820 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
| 848 | 821 | } |
| 849 | 822 | } |
@@ -858,8 +831,7 @@ discard block |
||
| 858 | 831 | public function groupBy($field){ |
| 859 | 832 | if(is_array($field)){ |
| 860 | 833 | $this->groupBy = implode(', ', $field); |
| 861 | - } |
|
| 862 | - else{ |
|
| 834 | + } else{ |
|
| 863 | 835 | $this->groupBy = $field; |
| 864 | 836 | } |
| 865 | 837 | return $this; |
@@ -886,14 +858,12 @@ discard block |
||
| 886 | 858 | } |
| 887 | 859 | } |
| 888 | 860 | $this->having = $w; |
| 889 | - } |
|
| 890 | - else if (! in_array($op, $this->operatorList)){ |
|
| 861 | + } else if (! in_array($op, $this->operatorList)){ |
|
| 891 | 862 | if(is_null($op)){ |
| 892 | 863 | $op = ''; |
| 893 | 864 | } |
| 894 | 865 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 895 | - } |
|
| 896 | - else{ |
|
| 866 | + } else{ |
|
| 897 | 867 | if(is_null($val)){ |
| 898 | 868 | $val = ''; |
| 899 | 869 | } |
@@ -938,8 +908,7 @@ discard block |
||
| 938 | 908 | $query = $this->getAll(true); |
| 939 | 909 | if($returnSQLQueryOrResultType === true){ |
| 940 | 910 | return $query; |
| 941 | - } |
|
| 942 | - else{ |
|
| 911 | + } else{ |
|
| 943 | 912 | return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 944 | 913 | } |
| 945 | 914 | } |
@@ -978,8 +947,7 @@ discard block |
||
| 978 | 947 | |
| 979 | 948 | if($returnSQLQueryOrResultType === true){ |
| 980 | 949 | return $query; |
| 981 | - } |
|
| 982 | - else{ |
|
| 950 | + } else{ |
|
| 983 | 951 | return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 984 | 952 | } |
| 985 | 953 | } |
@@ -997,8 +965,7 @@ discard block |
||
| 997 | 965 | $columns = array_keys($this->getData()); |
| 998 | 966 | $column = implode(',', $columns); |
| 999 | 967 | $val = implode(', ', $this->getData()); |
| 1000 | - } |
|
| 1001 | - else{ |
|
| 968 | + } else{ |
|
| 1002 | 969 | $columns = array_keys($data); |
| 1003 | 970 | $column = implode(',', $columns); |
| 1004 | 971 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -1013,8 +980,7 @@ discard block |
||
| 1013 | 980 | } |
| 1014 | 981 | $this->insertId = $this->pdo->lastInsertId(); |
| 1015 | 982 | return $this->insertId(); |
| 1016 | - } |
|
| 1017 | - else{ |
|
| 983 | + } else{ |
|
| 1018 | 984 | return false; |
| 1019 | 985 | } |
| 1020 | 986 | } |
@@ -1032,8 +998,7 @@ discard block |
||
| 1032 | 998 | foreach ($this->getData() as $column => $val){ |
| 1033 | 999 | $values[] = $column . ' = ' . $val; |
| 1034 | 1000 | } |
| 1035 | - } |
|
| 1036 | - else{ |
|
| 1001 | + } else{ |
|
| 1037 | 1002 | foreach ($data as $column => $val){ |
| 1038 | 1003 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1039 | 1004 | } |
@@ -1127,14 +1092,12 @@ discard block |
||
| 1127 | 1092 | $cacheKey = md5($query . $all . $array); |
| 1128 | 1093 | if(is_object($this->cacheInstance)){ |
| 1129 | 1094 | $cacheInstance = $this->cacheInstance; |
| 1130 | - } |
|
| 1131 | - else{ |
|
| 1095 | + } else{ |
|
| 1132 | 1096 | $obj = & get_instance(); |
| 1133 | 1097 | $cacheInstance = $obj->cache; |
| 1134 | 1098 | } |
| 1135 | 1099 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1136 | - } |
|
| 1137 | - else{ |
|
| 1100 | + } else{ |
|
| 1138 | 1101 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1139 | 1102 | } |
| 1140 | 1103 | |
@@ -1148,8 +1111,7 @@ discard block |
||
| 1148 | 1111 | $bench = null; |
| 1149 | 1112 | if(is_object($this->benchmarkInstance)){ |
| 1150 | 1113 | $bench = $this->benchmarkInstance; |
| 1151 | - } |
|
| 1152 | - else{ |
|
| 1114 | + } else{ |
|
| 1153 | 1115 | $obj = & get_instance(); |
| 1154 | 1116 | $bench = $obj->benchmark; |
| 1155 | 1117 | } |
@@ -1169,8 +1131,7 @@ discard block |
||
| 1169 | 1131 | //if need return all result like list of record |
| 1170 | 1132 | if ($all){ |
| 1171 | 1133 | $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
| 1172 | - } |
|
| 1173 | - else{ |
|
| 1134 | + } else{ |
|
| 1174 | 1135 | $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
| 1175 | 1136 | } |
| 1176 | 1137 | } |
@@ -1178,15 +1139,13 @@ discard block |
||
| 1178 | 1139 | $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
| 1179 | 1140 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1180 | 1141 | } |
| 1181 | - } |
|
| 1182 | - else{ |
|
| 1142 | + } else{ |
|
| 1183 | 1143 | $error = $this->pdo->errorInfo(); |
| 1184 | 1144 | $this->error = $error[2]; |
| 1185 | 1145 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1186 | 1146 | $this->error(); |
| 1187 | 1147 | } |
| 1188 | - } |
|
| 1189 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1148 | + } else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1190 | 1149 | $queryStr = $this->pdo->query($this->query); |
| 1191 | 1150 | if($queryStr){ |
| 1192 | 1151 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
@@ -1198,8 +1157,7 @@ discard block |
||
| 1198 | 1157 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1199 | 1158 | $this->error(); |
| 1200 | 1159 | } |
| 1201 | - } |
|
| 1202 | - else{ |
|
| 1160 | + } else{ |
|
| 1203 | 1161 | $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
| 1204 | 1162 | $this->result = $cacheContent; |
| 1205 | 1163 | $this->numRows = count($this->result); |