@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class ApcCache implements CacheInterface{ |
| 28 | 28 | |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | - * instance of the Log class |
|
| 43 | - */ |
|
| 44 | - if(is_object($logger)){ |
|
| 45 | - $this->logger = $logger; |
|
| 46 | - } |
|
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | - } |
|
| 42 | + * instance of the Log class |
|
| 43 | + */ |
|
| 44 | + if(is_object($logger)){ |
|
| 45 | + $this->logger = $logger; |
|
| 46 | + } |
|
| 47 | + else{ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
| 88 | 88 | $result = apc_store($key, $data, $ttl); |
| 89 | 89 | if($result === false){ |
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 90 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + else{ |
|
| 94 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | else{ |
| 114 | 114 | $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
| 115 | - return apc_delete($key); |
|
| 115 | + return apc_delete($key); |
|
| 116 | 116 | } |
| 117 | 117 | return false; |
| 118 | 118 | } |
@@ -178,28 +178,28 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | - * Return the Log instance |
|
| 182 | - * @return Log |
|
| 183 | - */ |
|
| 184 | - public function getLogger(){ |
|
| 185 | - return $this->logger; |
|
| 186 | - } |
|
| 181 | + * Return the Log instance |
|
| 182 | + * @return Log |
|
| 183 | + */ |
|
| 184 | + public function getLogger(){ |
|
| 185 | + return $this->logger; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Set the log instance |
|
| 190 | - * @param Log $logger the log object |
|
| 191 | - */ |
|
| 192 | - public function setLogger(Log $logger){ |
|
| 193 | - $this->logger = $logger; |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 188 | + /** |
|
| 189 | + * Set the log instance |
|
| 190 | + * @param Log $logger the log object |
|
| 191 | + */ |
|
| 192 | + public function setLogger(Log $logger){ |
|
| 193 | + $this->logger = $logger; |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | - * Return the array of cache information |
|
| 199 | - * |
|
| 200 | - * @param string $key the cache key to get the cache information |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 198 | + * Return the array of cache information |
|
| 199 | + * |
|
| 200 | + * @param string $key the cache key to get the cache information |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | 203 | private function _getCacheInfo($key){ |
| 204 | 204 | $caches = apc_cache_info('user'); |
| 205 | 205 | if(! empty($caches['cache_list'])){ |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class ApcCache implements CacheInterface{ |
|
| 27 | + class ApcCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The logger instance |
@@ -33,19 +33,19 @@ discard block |
||
| 33 | 33 | private $logger; |
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct(Log $logger = null){ |
|
| 37 | - if(! $this->isSupported()){ |
|
| 36 | + public function __construct(Log $logger = null) { |
|
| 37 | + if (!$this->isSupported()) { |
|
| 38 | 38 | show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * instance of the Log class |
| 43 | 43 | */ |
| 44 | - if(is_object($logger)){ |
|
| 44 | + if (is_object($logger)) { |
|
| 45 | 45 | $this->logger = $logger; |
| 46 | 46 | } |
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 47 | + else { |
|
| 48 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 49 | 49 | $this->logger->setLogger('Library::ApcCache'); |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -55,21 +55,21 @@ discard block |
||
| 55 | 55 | * @param string $key the key to identify the cache data |
| 56 | 56 | * @return mixed the cache data if exists else return false |
| 57 | 57 | */ |
| 58 | - public function get($key){ |
|
| 59 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | + public function get($key) { |
|
| 59 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 60 | 60 | $success = false; |
| 61 | 61 | $data = apc_fetch($key, $success); |
| 62 | - if($success === false){ |
|
| 63 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 62 | + if ($success === false) { |
|
| 63 | + $this->logger->info('No cache found for the key [' . $key . '], return false'); |
|
| 64 | 64 | return false; |
| 65 | 65 | } |
| 66 | - else{ |
|
| 66 | + else { |
|
| 67 | 67 | $cacheInfo = $this->_getCacheInfo($key); |
| 68 | 68 | $expire = time(); |
| 69 | - if($cacheInfo){ |
|
| 69 | + if ($cacheInfo) { |
|
| 70 | 70 | $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
| 71 | 71 | } |
| 72 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 72 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | 73 | return $data; |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -82,16 +82,16 @@ discard block |
||
| 82 | 82 | * @param integer $ttl the cache life time |
| 83 | 83 | * @return boolean true if success otherwise will return false |
| 84 | 84 | */ |
| 85 | - public function set($key, $data, $ttl = 0){ |
|
| 85 | + public function set($key, $data, $ttl = 0) { |
|
| 86 | 86 | $expire = time() + $ttl; |
| 87 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 87 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | 88 | $result = apc_store($key, $data, $ttl); |
| 89 | - if($result === false){ |
|
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 89 | + if ($result === false) { |
|
| 90 | + $this->logger->error('Can not write cache data for the key [' . $key . '], return false'); |
|
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 93 | + else { |
|
| 94 | + $this->logger->info('Cache data saved for the key [' . $key . ']'); |
|
| 95 | 95 | return true; |
| 96 | 96 | } |
| 97 | 97 | } |
@@ -103,15 +103,15 @@ discard block |
||
| 103 | 103 | * @return boolean true if the cache is deleted, false if can't delete |
| 104 | 104 | * the cache or the cache with the given key not exist |
| 105 | 105 | */ |
| 106 | - public function delete($key){ |
|
| 107 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 106 | + public function delete($key) { |
|
| 107 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 108 | 108 | $cacheInfo = $this->_getCacheInfo($key); |
| 109 | - if($cacheInfo === false){ |
|
| 109 | + if ($cacheInfo === false) { |
|
| 110 | 110 | $this->logger->info('This cache data does not exists skipping'); |
| 111 | 111 | return false; |
| 112 | 112 | } |
| 113 | - else{ |
|
| 114 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 113 | + else { |
|
| 114 | + $this->logger->info('Found cache data for the key [' . $key . '] remove it'); |
|
| 115 | 115 | return apc_delete($key); |
| 116 | 116 | } |
| 117 | 117 | return false; |
@@ -125,10 +125,10 @@ discard block |
||
| 125 | 125 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 126 | 126 | * 'ttl' => the time to live of the cache in second |
| 127 | 127 | */ |
| 128 | - public function getInfo($key){ |
|
| 129 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 128 | + public function getInfo($key) { |
|
| 129 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 130 | 130 | $cacheInfos = $this->_getCacheInfo($key); |
| 131 | - if($cacheInfos){ |
|
| 131 | + if ($cacheInfos) { |
|
| 132 | 132 | $data = array( |
| 133 | 133 | 'mtime' => $cacheInfos['creation_time'], |
| 134 | 134 | 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | ); |
| 137 | 137 | return $data; |
| 138 | 138 | } |
| 139 | - else{ |
|
| 139 | + else { |
|
| 140 | 140 | $this->logger->info('This cache does not exists skipping'); |
| 141 | 141 | return false; |
| 142 | 142 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | /** |
| 147 | 147 | * Used to delete expired cache data |
| 148 | 148 | */ |
| 149 | - public function deleteExpiredCache(){ |
|
| 149 | + public function deleteExpiredCache() { |
|
| 150 | 150 | //for APC[u] is done automatically |
| 151 | 151 | return true; |
| 152 | 152 | } |
@@ -154,14 +154,14 @@ discard block |
||
| 154 | 154 | /** |
| 155 | 155 | * Remove all cache data |
| 156 | 156 | */ |
| 157 | - public function clean(){ |
|
| 157 | + public function clean() { |
|
| 158 | 158 | $this->logger->debug('Deleting of all cache data'); |
| 159 | 159 | $cacheInfos = apc_cache_info('user'); |
| 160 | - if(empty($cacheInfos['cache_list'])){ |
|
| 160 | + if (empty($cacheInfos['cache_list'])) { |
|
| 161 | 161 | $this->logger->info('No cache data were found skipping'); |
| 162 | 162 | return false; |
| 163 | 163 | } |
| 164 | - else{ |
|
| 164 | + else { |
|
| 165 | 165 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 166 | 166 | return apc_clear_cache('user'); |
| 167 | 167 | } |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return bool |
| 175 | 175 | */ |
| 176 | - public function isSupported(){ |
|
| 176 | + public function isSupported() { |
|
| 177 | 177 | return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
| 178 | 178 | } |
| 179 | 179 | |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | * Return the Log instance |
| 182 | 182 | * @return Log |
| 183 | 183 | */ |
| 184 | - public function getLogger(){ |
|
| 184 | + public function getLogger() { |
|
| 185 | 185 | return $this->logger; |
| 186 | 186 | } |
| 187 | 187 | |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | * Set the log instance |
| 190 | 190 | * @param Log $logger the log object |
| 191 | 191 | */ |
| 192 | - public function setLogger(Log $logger){ |
|
| 192 | + public function setLogger(Log $logger) { |
|
| 193 | 193 | $this->logger = $logger; |
| 194 | 194 | return $this; |
| 195 | 195 | } |
@@ -200,12 +200,12 @@ discard block |
||
| 200 | 200 | * @param string $key the cache key to get the cache information |
| 201 | 201 | * @return array |
| 202 | 202 | */ |
| 203 | - private function _getCacheInfo($key){ |
|
| 203 | + private function _getCacheInfo($key) { |
|
| 204 | 204 | $caches = apc_cache_info('user'); |
| 205 | - if(! empty($caches['cache_list'])){ |
|
| 205 | + if (!empty($caches['cache_list'])) { |
|
| 206 | 206 | $cacheLists = $caches['cache_list']; |
| 207 | - foreach ($cacheLists as $c){ |
|
| 208 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 207 | + foreach ($cacheLists as $c) { |
|
| 208 | + if (isset($c['info']) && $c['info'] === $key) { |
|
| 209 | 209 | return $c; |
| 210 | 210 | } |
| 211 | 211 | } |
@@ -43,8 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | if(is_object($logger)){ |
| 45 | 45 | $this->logger = $logger; |
| 46 | - } |
|
| 47 | - else{ |
|
| 46 | + } else{ |
|
| 48 | 47 | $this->logger =& class_loader('Log', 'classes'); |
| 49 | 48 | $this->logger->setLogger('Library::ApcCache'); |
| 50 | 49 | } |
@@ -62,8 +61,7 @@ discard block |
||
| 62 | 61 | if($success === false){ |
| 63 | 62 | $this->logger->info('No cache found for the key ['. $key .'], return false'); |
| 64 | 63 | return false; |
| 65 | - } |
|
| 66 | - else{ |
|
| 64 | + } else{ |
|
| 67 | 65 | $cacheInfo = $this->_getCacheInfo($key); |
| 68 | 66 | $expire = time(); |
| 69 | 67 | if($cacheInfo){ |
@@ -89,8 +87,7 @@ discard block |
||
| 89 | 87 | if($result === false){ |
| 90 | 88 | $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
| 91 | 89 | return false; |
| 92 | - } |
|
| 93 | - else{ |
|
| 90 | + } else{ |
|
| 94 | 91 | $this->logger->info('Cache data saved for the key ['. $key .']'); |
| 95 | 92 | return true; |
| 96 | 93 | } |
@@ -109,8 +106,7 @@ discard block |
||
| 109 | 106 | if($cacheInfo === false){ |
| 110 | 107 | $this->logger->info('This cache data does not exists skipping'); |
| 111 | 108 | return false; |
| 112 | - } |
|
| 113 | - else{ |
|
| 109 | + } else{ |
|
| 114 | 110 | $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
| 115 | 111 | return apc_delete($key); |
| 116 | 112 | } |
@@ -135,8 +131,7 @@ discard block |
||
| 135 | 131 | 'ttl' => $cacheInfos['ttl'] |
| 136 | 132 | ); |
| 137 | 133 | return $data; |
| 138 | - } |
|
| 139 | - else{ |
|
| 134 | + } else{ |
|
| 140 | 135 | $this->logger->info('This cache does not exists skipping'); |
| 141 | 136 | return false; |
| 142 | 137 | } |
@@ -160,8 +155,7 @@ discard block |
||
| 160 | 155 | if(empty($cacheInfos['cache_list'])){ |
| 161 | 156 | $this->logger->info('No cache data were found skipping'); |
| 162 | 157 | return false; |
| 163 | - } |
|
| 164 | - else{ |
|
| 158 | + } else{ |
|
| 165 | 159 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 166 | 160 | return apc_clear_cache('user'); |
| 167 | 161 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,183 +22,183 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | class Database{ |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | - */ |
|
| 32 | - private $pdo = null; |
|
| 31 | + */ |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | - */ |
|
| 37 | + */ |
|
| 38 | 38 | private $databaseName = null; |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | - */ |
|
| 43 | + */ |
|
| 44 | 44 | private $select = '*'; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | - */ |
|
| 50 | - private $from = null; |
|
| 49 | + */ |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | - */ |
|
| 56 | - private $where = null; |
|
| 55 | + */ |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | - */ |
|
| 62 | - private $limit = null; |
|
| 61 | + */ |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | - */ |
|
| 68 | - private $join = null; |
|
| 67 | + */ |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | - */ |
|
| 74 | - private $orderBy = null; |
|
| 73 | + */ |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | - */ |
|
| 80 | - private $groupBy = null; |
|
| 79 | + */ |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | - */ |
|
| 86 | - private $having = null; |
|
| 85 | + */ |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | - */ |
|
| 92 | - private $numRows = 0; |
|
| 91 | + */ |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | - */ |
|
| 98 | - private $insertId = null; |
|
| 97 | + */ |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | - */ |
|
| 104 | - private $query = null; |
|
| 103 | + */ |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | - */ |
|
| 110 | - private $error = null; |
|
| 109 | + */ |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | - */ |
|
| 116 | - private $result = array(); |
|
| 115 | + */ |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | - */ |
|
| 122 | - private $prefix = null; |
|
| 121 | + */ |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | - */ |
|
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 127 | + */ |
|
| 128 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | - */ |
|
| 133 | + */ |
|
| 134 | 134 | private $cacheTtl = 0; |
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | - */ |
|
| 140 | - private $temporaryCacheTtl = 0; |
|
| 139 | + */ |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | - */ |
|
| 146 | - private $queryCount = 0; |
|
| 145 | + */ |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | - */ |
|
| 152 | - private $data = array(); |
|
| 151 | + */ |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | - */ |
|
| 158 | - private $config = array(); |
|
| 157 | + */ |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * The cache instance |
|
| 169 | - * @var CacheInterface |
|
| 170 | - */ |
|
| 171 | - private $cacheInstance = null; |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * The benchmark instance |
|
| 175 | - * @var Benchmark |
|
| 176 | - */ |
|
| 177 | - private $benchmarkInstance = null; |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Construct new database |
|
| 182 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | - * @param object $logger the log instance |
|
| 184 | - */ |
|
| 185 | - public function __construct($overwriteConfig = array(), Log $logger = null){ |
|
| 186 | - /** |
|
| 187 | - * instance of the Log class |
|
| 188 | - */ |
|
| 189 | - if(is_object($logger)){ |
|
| 190 | - $this->logger = $logger; |
|
| 191 | - } |
|
| 192 | - else{ |
|
| 193 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 194 | - $this->logger->setLogger('Library::Database'); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $db = array(); |
|
| 198 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 199 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 200 | - require CONFIG_PATH . 'database.php'; |
|
| 201 | - } |
|
| 164 | + private $logger = null; |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * The cache instance |
|
| 169 | + * @var CacheInterface |
|
| 170 | + */ |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * The benchmark instance |
|
| 175 | + * @var Benchmark |
|
| 176 | + */ |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Construct new database |
|
| 182 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | + * @param object $logger the log instance |
|
| 184 | + */ |
|
| 185 | + public function __construct($overwriteConfig = array(), Log $logger = null){ |
|
| 186 | + /** |
|
| 187 | + * instance of the Log class |
|
| 188 | + */ |
|
| 189 | + if(is_object($logger)){ |
|
| 190 | + $this->logger = $logger; |
|
| 191 | + } |
|
| 192 | + else{ |
|
| 193 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 194 | + $this->logger->setLogger('Library::Database'); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $db = array(); |
|
| 198 | + if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 199 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 200 | + require CONFIG_PATH . 'database.php'; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | 203 | if(! empty($overwriteConfig)){ |
| 204 | 204 | $db = array_merge($db, $overwriteConfig); |
@@ -211,1018 +211,1018 @@ discard block |
||
| 211 | 211 | $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
| 212 | 212 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
| 213 | 213 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
| 214 | - $port = ''; |
|
| 215 | - if(strstr($config['hostname'], ':')){ |
|
| 216 | - $p = explode(':', $config['hostname']); |
|
| 217 | - $port = isset($p[1]) ? $p[1] : ''; |
|
| 218 | - } |
|
| 214 | + $port = ''; |
|
| 215 | + if(strstr($config['hostname'], ':')){ |
|
| 216 | + $p = explode(':', $config['hostname']); |
|
| 217 | + $port = isset($p[1]) ? $p[1] : ''; |
|
| 218 | + } |
|
| 219 | 219 | $config['port'] = $port; |
| 220 | 220 | |
| 221 | 221 | $this->setDatabaseConfiguration($config); |
| 222 | 222 | |
| 223 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * This is used to connect to database |
|
| 228 | - * @return bool |
|
| 229 | - */ |
|
| 230 | - public function connect(){ |
|
| 231 | - $config = $this->getDatabaseConfiguration(); |
|
| 232 | - if(! empty($config)){ |
|
| 233 | - try{ |
|
| 234 | - $dsn = ''; |
|
| 235 | - if($config['driver'] == 'mysql' || $config['driver'] == '' || $config['driver'] == 'pgsql'){ |
|
| 236 | - $dsn = $config['driver'] . ':host=' . $config['hostname'] . ';' |
|
| 237 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 238 | - . 'dbname=' . $config['database']; |
|
| 239 | - } |
|
| 240 | - else if ($config['driver'] == 'sqlite'){ |
|
| 241 | - $dsn = 'sqlite:' . $config['database']; |
|
| 242 | - } |
|
| 243 | - else if($config['driver'] == 'oracle'){ |
|
| 244 | - $dsn = 'oci:dbname=' . $config['host'] . '/' . $config['database']; |
|
| 245 | - } |
|
| 223 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * This is used to connect to database |
|
| 228 | + * @return bool |
|
| 229 | + */ |
|
| 230 | + public function connect(){ |
|
| 231 | + $config = $this->getDatabaseConfiguration(); |
|
| 232 | + if(! empty($config)){ |
|
| 233 | + try{ |
|
| 234 | + $dsn = ''; |
|
| 235 | + if($config['driver'] == 'mysql' || $config['driver'] == '' || $config['driver'] == 'pgsql'){ |
|
| 236 | + $dsn = $config['driver'] . ':host=' . $config['hostname'] . ';' |
|
| 237 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 238 | + . 'dbname=' . $config['database']; |
|
| 239 | + } |
|
| 240 | + else if ($config['driver'] == 'sqlite'){ |
|
| 241 | + $dsn = 'sqlite:' . $config['database']; |
|
| 242 | + } |
|
| 243 | + else if($config['driver'] == 'oracle'){ |
|
| 244 | + $dsn = 'oci:dbname=' . $config['host'] . '/' . $config['database']; |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
| 248 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 249 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 250 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 251 | - return true; |
|
| 252 | - } |
|
| 253 | - catch (PDOException $e){ |
|
| 254 | - $this->logger->fatal($e->getMessage()); |
|
| 255 | - show_error('Cannot connect to Database.'); |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - else{ |
|
| 260 | - show_error('Database configuration is not set.'); |
|
| 261 | - return false; |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Set the SQL FROM statment |
|
| 267 | - * @param string|array $table the table name or array of table list |
|
| 268 | - * @return object the current Database instance |
|
| 269 | - */ |
|
| 270 | - public function from($table){ |
|
| 271 | - if(is_array($table)){ |
|
| 272 | - $froms = ''; |
|
| 273 | - foreach($table as $key){ |
|
| 274 | - $froms .= $this->prefix . $key . ', '; |
|
| 275 | - } |
|
| 276 | - $this->from = rtrim($froms, ', '); |
|
| 277 | - } |
|
| 278 | - else{ |
|
| 279 | - $this->from = $this->prefix . $table; |
|
| 280 | - } |
|
| 281 | - return $this; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Set the SQL SELECT statment |
|
| 286 | - * @param string|array $fields the field name or array of field list |
|
| 287 | - * @return object the current Database instance |
|
| 288 | - */ |
|
| 289 | - public function select($fields){ |
|
| 290 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 291 | - $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Set the SQL SELECT DISTINCT statment |
|
| 297 | - * @param string $field the field name to distinct |
|
| 298 | - * @return object the current Database instance |
|
| 299 | - */ |
|
| 300 | - public function distinct($field){ |
|
| 301 | - $distinct = ' DISTINCT ' . $field; |
|
| 302 | - $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 303 | - |
|
| 304 | - return $this; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Set the SQL function MAX in SELECT statment |
|
| 309 | - * @param string $field the field name |
|
| 310 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 311 | - * @return object the current Database instance |
|
| 312 | - */ |
|
| 313 | - public function max($field, $name = null){ |
|
| 314 | - $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 315 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 316 | - return $this; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Set the SQL function MIN in SELECT statment |
|
| 321 | - * @param string $field the field name |
|
| 322 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 323 | - * @return object the current Database instance |
|
| 324 | - */ |
|
| 325 | - public function min($field, $name = null){ |
|
| 326 | - $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 327 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 328 | - return $this; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Set the SQL function SUM in SELECT statment |
|
| 333 | - * @param string $field the field name |
|
| 334 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 335 | - * @return object the current Database instance |
|
| 336 | - */ |
|
| 337 | - public function sum($field, $name = null){ |
|
| 338 | - $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 339 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 340 | - return $this; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Set the SQL function COUNT in SELECT statment |
|
| 345 | - * @param string $field the field name |
|
| 346 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 347 | - * @return object the current Database instance |
|
| 348 | - */ |
|
| 349 | - public function count($field = '*', $name = null){ |
|
| 350 | - $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 351 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 352 | - return $this; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Set the SQL function AVG in SELECT statment |
|
| 357 | - * @param string $field the field name |
|
| 358 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 359 | - * @return object the current Database instance |
|
| 360 | - */ |
|
| 361 | - public function avg($field, $name = null){ |
|
| 362 | - $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 363 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 364 | - return $this; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Set the SQL JOIN statment |
|
| 369 | - * @param string $table the join table name |
|
| 370 | - * @param string $field1 the first field for join conditions |
|
| 371 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 372 | - * @param string $field2 the second field for join conditions |
|
| 373 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 374 | - * @return object the current Database instance |
|
| 375 | - */ |
|
| 376 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 377 | - $on = $field1; |
|
| 378 | - $table = $this->prefix . $table; |
|
| 379 | - if(! is_null($op)){ |
|
| 380 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 381 | - } |
|
| 382 | - if (is_null($this->join)){ |
|
| 383 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 384 | - } |
|
| 385 | - else{ |
|
| 386 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 387 | - } |
|
| 388 | - return $this; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Set the SQL INNER JOIN statment |
|
| 393 | - * @see Database::join() |
|
| 394 | - * @return object the current Database instance |
|
| 395 | - */ |
|
| 396 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Set the SQL LEFT JOIN statment |
|
| 402 | - * @see Database::join() |
|
| 403 | - * @return object the current Database instance |
|
| 404 | - */ |
|
| 405 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 406 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Set the SQL RIGHT JOIN statment |
|
| 411 | - * @see Database::join() |
|
| 412 | - * @return object the current Database instance |
|
| 413 | - */ |
|
| 414 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 415 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Set the SQL FULL OUTER JOIN statment |
|
| 420 | - * @see Database::join() |
|
| 421 | - * @return object the current Database instance |
|
| 422 | - */ |
|
| 423 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 424 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 429 | - * @see Database::join() |
|
| 430 | - * @return object the current Database instance |
|
| 431 | - */ |
|
| 432 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 433 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 438 | - * @see Database::join() |
|
| 439 | - * @return object the current Database instance |
|
| 440 | - */ |
|
| 441 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 442 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 447 | - * @param string|array $field the field name or array of field list |
|
| 448 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 449 | - * @return object the current Database instance |
|
| 450 | - */ |
|
| 451 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 452 | - if(is_array($field)){ |
|
| 453 | - foreach($field as $f){ |
|
| 454 | - $this->whereIsNull($f, $andOr); |
|
| 455 | - } |
|
| 456 | - } |
|
| 457 | - else{ |
|
| 458 | - if (! $this->where){ |
|
| 459 | - $this->where = $field.' IS NULL '; |
|
| 460 | - } |
|
| 461 | - else{ |
|
| 462 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 463 | - } |
|
| 464 | - } |
|
| 465 | - return $this; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 470 | - * @param string|array $field the field name or array of field list |
|
| 471 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 472 | - * @return object the current Database instance |
|
| 473 | - */ |
|
| 474 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 475 | - if(is_array($field)){ |
|
| 476 | - foreach($field as $f){ |
|
| 477 | - $this->whereIsNotNull($f, $andOr); |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - else{ |
|
| 481 | - if (! $this->where){ |
|
| 482 | - $this->where = $field.' IS NOT NULL '; |
|
| 483 | - } |
|
| 484 | - else{ |
|
| 485 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - return $this; |
|
| 489 | - } |
|
| 247 | + $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
| 248 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 249 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 250 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 251 | + return true; |
|
| 252 | + } |
|
| 253 | + catch (PDOException $e){ |
|
| 254 | + $this->logger->fatal($e->getMessage()); |
|
| 255 | + show_error('Cannot connect to Database.'); |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + else{ |
|
| 260 | + show_error('Database configuration is not set.'); |
|
| 261 | + return false; |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Set the SQL FROM statment |
|
| 267 | + * @param string|array $table the table name or array of table list |
|
| 268 | + * @return object the current Database instance |
|
| 269 | + */ |
|
| 270 | + public function from($table){ |
|
| 271 | + if(is_array($table)){ |
|
| 272 | + $froms = ''; |
|
| 273 | + foreach($table as $key){ |
|
| 274 | + $froms .= $this->prefix . $key . ', '; |
|
| 275 | + } |
|
| 276 | + $this->from = rtrim($froms, ', '); |
|
| 277 | + } |
|
| 278 | + else{ |
|
| 279 | + $this->from = $this->prefix . $table; |
|
| 280 | + } |
|
| 281 | + return $this; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Set the SQL SELECT statment |
|
| 286 | + * @param string|array $fields the field name or array of field list |
|
| 287 | + * @return object the current Database instance |
|
| 288 | + */ |
|
| 289 | + public function select($fields){ |
|
| 290 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 291 | + $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Set the SQL SELECT DISTINCT statment |
|
| 297 | + * @param string $field the field name to distinct |
|
| 298 | + * @return object the current Database instance |
|
| 299 | + */ |
|
| 300 | + public function distinct($field){ |
|
| 301 | + $distinct = ' DISTINCT ' . $field; |
|
| 302 | + $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 303 | + |
|
| 304 | + return $this; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Set the SQL function MAX in SELECT statment |
|
| 309 | + * @param string $field the field name |
|
| 310 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 311 | + * @return object the current Database instance |
|
| 312 | + */ |
|
| 313 | + public function max($field, $name = null){ |
|
| 314 | + $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 315 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 316 | + return $this; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Set the SQL function MIN in SELECT statment |
|
| 321 | + * @param string $field the field name |
|
| 322 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 323 | + * @return object the current Database instance |
|
| 324 | + */ |
|
| 325 | + public function min($field, $name = null){ |
|
| 326 | + $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 327 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 328 | + return $this; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Set the SQL function SUM in SELECT statment |
|
| 333 | + * @param string $field the field name |
|
| 334 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 335 | + * @return object the current Database instance |
|
| 336 | + */ |
|
| 337 | + public function sum($field, $name = null){ |
|
| 338 | + $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 339 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 340 | + return $this; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Set the SQL function COUNT in SELECT statment |
|
| 345 | + * @param string $field the field name |
|
| 346 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 347 | + * @return object the current Database instance |
|
| 348 | + */ |
|
| 349 | + public function count($field = '*', $name = null){ |
|
| 350 | + $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 351 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 352 | + return $this; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Set the SQL function AVG in SELECT statment |
|
| 357 | + * @param string $field the field name |
|
| 358 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 359 | + * @return object the current Database instance |
|
| 360 | + */ |
|
| 361 | + public function avg($field, $name = null){ |
|
| 362 | + $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 363 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 364 | + return $this; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Set the SQL JOIN statment |
|
| 369 | + * @param string $table the join table name |
|
| 370 | + * @param string $field1 the first field for join conditions |
|
| 371 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 372 | + * @param string $field2 the second field for join conditions |
|
| 373 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 374 | + * @return object the current Database instance |
|
| 375 | + */ |
|
| 376 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 377 | + $on = $field1; |
|
| 378 | + $table = $this->prefix . $table; |
|
| 379 | + if(! is_null($op)){ |
|
| 380 | + $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 381 | + } |
|
| 382 | + if (is_null($this->join)){ |
|
| 383 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 384 | + } |
|
| 385 | + else{ |
|
| 386 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 387 | + } |
|
| 388 | + return $this; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Set the SQL INNER JOIN statment |
|
| 393 | + * @see Database::join() |
|
| 394 | + * @return object the current Database instance |
|
| 395 | + */ |
|
| 396 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Set the SQL LEFT JOIN statment |
|
| 402 | + * @see Database::join() |
|
| 403 | + * @return object the current Database instance |
|
| 404 | + */ |
|
| 405 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 406 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Set the SQL RIGHT JOIN statment |
|
| 411 | + * @see Database::join() |
|
| 412 | + * @return object the current Database instance |
|
| 413 | + */ |
|
| 414 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 415 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Set the SQL FULL OUTER JOIN statment |
|
| 420 | + * @see Database::join() |
|
| 421 | + * @return object the current Database instance |
|
| 422 | + */ |
|
| 423 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 424 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 429 | + * @see Database::join() |
|
| 430 | + * @return object the current Database instance |
|
| 431 | + */ |
|
| 432 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 433 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 438 | + * @see Database::join() |
|
| 439 | + * @return object the current Database instance |
|
| 440 | + */ |
|
| 441 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 442 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 447 | + * @param string|array $field the field name or array of field list |
|
| 448 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 449 | + * @return object the current Database instance |
|
| 450 | + */ |
|
| 451 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 452 | + if(is_array($field)){ |
|
| 453 | + foreach($field as $f){ |
|
| 454 | + $this->whereIsNull($f, $andOr); |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | + else{ |
|
| 458 | + if (! $this->where){ |
|
| 459 | + $this->where = $field.' IS NULL '; |
|
| 460 | + } |
|
| 461 | + else{ |
|
| 462 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | + return $this; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 470 | + * @param string|array $field the field name or array of field list |
|
| 471 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 472 | + * @return object the current Database instance |
|
| 473 | + */ |
|
| 474 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 475 | + if(is_array($field)){ |
|
| 476 | + foreach($field as $f){ |
|
| 477 | + $this->whereIsNotNull($f, $andOr); |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + else{ |
|
| 481 | + if (! $this->where){ |
|
| 482 | + $this->where = $field.' IS NOT NULL '; |
|
| 483 | + } |
|
| 484 | + else{ |
|
| 485 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + return $this; |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - /** |
|
| 492 | - * Set the SQL WHERE CLAUSE statment |
|
| 493 | - * @param string|array $where the where field or array of field list |
|
| 494 | - * @param string $op the condition operator. If is null the default will be "=" |
|
| 495 | - * @param mixed $val the where value |
|
| 496 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 497 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 498 | - * @param boolean $escape whether to escape or not the $val |
|
| 499 | - * @return object the current Database instance |
|
| 500 | - */ |
|
| 501 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 502 | - if (is_array($where)){ |
|
| 503 | - $_where = array(); |
|
| 504 | - foreach ($where as $column => $data){ |
|
| 505 | - if(is_null($data)){ |
|
| 506 | - $data = ''; |
|
| 507 | - } |
|
| 508 | - $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
| 509 | - } |
|
| 510 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 511 | - } |
|
| 512 | - else{ |
|
| 513 | - if(is_array($op)){ |
|
| 514 | - $x = explode('?', $where); |
|
| 515 | - $w = ''; |
|
| 516 | - foreach($x as $k => $v){ |
|
| 517 | - if(! empty($v)){ |
|
| 518 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 519 | - $op[$k] = ''; |
|
| 520 | - } |
|
| 521 | - $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - $where = $w; |
|
| 525 | - } |
|
| 526 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 527 | - if(is_null($op)){ |
|
| 528 | - $op = ''; |
|
| 529 | - } |
|
| 530 | - $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 531 | - } |
|
| 532 | - else{ |
|
| 533 | - if(is_null($val)){ |
|
| 534 | - $val = ''; |
|
| 535 | - } |
|
| 536 | - $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - if (is_null($this->where)){ |
|
| 540 | - $this->where = $where; |
|
| 541 | - } |
|
| 542 | - else{ |
|
| 543 | - if(substr($this->where, -1) == '('){ |
|
| 544 | - $this->where = $this->where . ' ' . $where; |
|
| 545 | - } |
|
| 546 | - else{ |
|
| 547 | - $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - return $this; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 555 | - * @see Database::where() |
|
| 556 | - * @return object the current Database instance |
|
| 557 | - */ |
|
| 558 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 559 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 565 | - * @see Database::where() |
|
| 566 | - * @return object the current Database instance |
|
| 567 | - */ |
|
| 568 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 569 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 574 | - * @see Database::where() |
|
| 575 | - * @return object the current Database instance |
|
| 576 | - */ |
|
| 577 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 578 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Set the opened parenthesis for the complex SQL query |
|
| 583 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 584 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 585 | - * @return object the current Database instance |
|
| 586 | - */ |
|
| 587 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 588 | - if (is_null($this->where)){ |
|
| 589 | - $this->where = $type . ' ('; |
|
| 590 | - } |
|
| 591 | - else{ |
|
| 592 | - if(substr($this->where, -1) == '('){ |
|
| 593 | - $this->where .= $type . ' ('; |
|
| 594 | - } |
|
| 595 | - else{ |
|
| 596 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - return $this; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 604 | - * @see Database::groupStart() |
|
| 605 | - * @return object the current Database instance |
|
| 606 | - */ |
|
| 607 | - public function notGroupStart(){ |
|
| 608 | - return $this->groupStart('NOT'); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 613 | - * @see Database::groupStart() |
|
| 614 | - * @return object the current Database instance |
|
| 615 | - */ |
|
| 616 | - public function orGroupStart(){ |
|
| 617 | - return $this->groupStart('', ' OR'); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 622 | - * @see Database::groupStart() |
|
| 623 | - * @return object the current Database instance |
|
| 624 | - */ |
|
| 625 | - public function orNotGroupStart(){ |
|
| 626 | - return $this->groupStart('NOT', ' OR'); |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Close the parenthesis for the grouped SQL |
|
| 631 | - * @return object the current Database instance |
|
| 632 | - */ |
|
| 633 | - public function groupEnd(){ |
|
| 634 | - $this->where .= ')'; |
|
| 635 | - return $this; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 640 | - * @param string $field the field name for IN statment |
|
| 641 | - * @param array $keys the list of values used |
|
| 642 | - * @param string $type the condition separator type (NOT) |
|
| 643 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 644 | - * @param boolean $escape whether to escape or not the values |
|
| 645 | - * @return object the current Database instance |
|
| 646 | - */ |
|
| 647 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 648 | - if (is_array($keys)){ |
|
| 649 | - $_keys = array(); |
|
| 650 | - foreach ($keys as $k => $v){ |
|
| 651 | - if(is_null($v)){ |
|
| 652 | - $v = ''; |
|
| 653 | - } |
|
| 654 | - $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 655 | - } |
|
| 656 | - $keys = implode(', ', $_keys); |
|
| 657 | - if (is_null($this->where)){ |
|
| 658 | - $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 659 | - } |
|
| 660 | - else{ |
|
| 661 | - if(substr($this->where, -1) == '('){ |
|
| 662 | - $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 663 | - } |
|
| 664 | - else{ |
|
| 665 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - } |
|
| 669 | - return $this; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 674 | - * @see Database::in() |
|
| 675 | - * @return object the current Database instance |
|
| 676 | - */ |
|
| 677 | - public function notIn($field, array $keys, $escape = true){ |
|
| 678 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 683 | - * @see Database::in() |
|
| 684 | - * @return object the current Database instance |
|
| 685 | - */ |
|
| 686 | - public function orIn($field, array $keys, $escape = true){ |
|
| 687 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 692 | - * @see Database::in() |
|
| 693 | - * @return object the current Database instance |
|
| 694 | - */ |
|
| 695 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 696 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 701 | - * @param string $field the field used for the BETWEEN statment |
|
| 702 | - * @param mixed $value1 the BETWEEN begin value |
|
| 703 | - * @param mixed $value2 the BETWEEN end value |
|
| 704 | - * @param string $type the condition separator type (NOT) |
|
| 705 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 706 | - * @param boolean $escape whether to escape or not the values |
|
| 707 | - * @return object the current Database instance |
|
| 708 | - */ |
|
| 709 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 710 | - if(is_null($value1)){ |
|
| 711 | - $value1 = ''; |
|
| 712 | - } |
|
| 713 | - if(is_null($value2)){ |
|
| 714 | - $value2 = ''; |
|
| 715 | - } |
|
| 716 | - if (is_null($this->where)){ |
|
| 717 | - $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 718 | - } |
|
| 719 | - else{ |
|
| 720 | - if(substr($this->where, -1) == '('){ |
|
| 721 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 722 | - } |
|
| 723 | - else{ |
|
| 724 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 725 | - } |
|
| 726 | - } |
|
| 727 | - return $this; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 732 | - * @see Database::between() |
|
| 733 | - * @return object the current Database instance |
|
| 734 | - */ |
|
| 735 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 736 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 741 | - * @see Database::between() |
|
| 742 | - * @return object the current Database instance |
|
| 743 | - */ |
|
| 744 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 745 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - /** |
|
| 749 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 750 | - * @see Database::between() |
|
| 751 | - * @return object the current Database instance |
|
| 752 | - */ |
|
| 753 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 754 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 759 | - * @param string $field the field name used in LIKE statment |
|
| 760 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 761 | - * @param string $type the condition separator type (NOT) |
|
| 762 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 763 | - * @param boolean $escape whether to escape or not the values |
|
| 764 | - * @return object the current Database instance |
|
| 765 | - */ |
|
| 766 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 767 | - if(is_null($data)){ |
|
| 768 | - $data = ''; |
|
| 769 | - } |
|
| 770 | - $like = $escape ? $this->escape($data) : $data; |
|
| 771 | - if (is_null($this->where)){ |
|
| 772 | - $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 773 | - } |
|
| 774 | - else{ |
|
| 775 | - if(substr($this->where, -1) == '('){ |
|
| 776 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 777 | - } |
|
| 778 | - else{ |
|
| 779 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - return $this; |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - /** |
|
| 786 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 787 | - * @see Database::like() |
|
| 788 | - * @return object the current Database instance |
|
| 789 | - */ |
|
| 790 | - public function orLike($field, $data, $escape = true){ |
|
| 791 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 796 | - * @see Database::like() |
|
| 797 | - * @return object the current Database instance |
|
| 798 | - */ |
|
| 799 | - public function notLike($field, $data, $escape = true){ |
|
| 800 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 805 | - * @see Database::like() |
|
| 806 | - * @return object the current Database instance |
|
| 807 | - */ |
|
| 808 | - public function orNotLike($field, $data, $escape = true){ |
|
| 809 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * Set the SQL LIMIT statment |
|
| 814 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 815 | - * like LIMIT n; |
|
| 816 | - * @param int $limitEnd the limit count |
|
| 817 | - * @return object the current Database instance |
|
| 818 | - */ |
|
| 819 | - public function limit($limit, $limitEnd = null){ |
|
| 820 | - if(is_null($limit)){ |
|
| 821 | - return; |
|
| 822 | - } |
|
| 823 | - if (! is_null($limitEnd)){ |
|
| 824 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 825 | - } |
|
| 826 | - else{ |
|
| 827 | - $this->limit = $limit; |
|
| 828 | - } |
|
| 829 | - return $this; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 834 | - * @param string $orderBy the field name used for order |
|
| 835 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 836 | - * @return object the current Database instance |
|
| 837 | - */ |
|
| 838 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 839 | - if (! is_null($orderDir)){ |
|
| 840 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 841 | - } |
|
| 842 | - else{ |
|
| 843 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 844 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 845 | - } |
|
| 846 | - else{ |
|
| 847 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 848 | - } |
|
| 849 | - } |
|
| 850 | - return $this; |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - /** |
|
| 854 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 855 | - * @param string|array $field the field name used or array of field list |
|
| 856 | - * @return object the current Database instance |
|
| 857 | - */ |
|
| 858 | - public function groupBy($field){ |
|
| 859 | - if(is_array($field)){ |
|
| 860 | - $this->groupBy = implode(', ', $field); |
|
| 861 | - } |
|
| 862 | - else{ |
|
| 863 | - $this->groupBy = $field; |
|
| 864 | - } |
|
| 865 | - return $this; |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * Set the SQL HAVING CLAUSE statment |
|
| 870 | - * @param string $field the field name used for HAVING statment |
|
| 871 | - * @param string|array $op the operator used or array |
|
| 872 | - * @param mixed $val the value for HAVING comparaison |
|
| 873 | - * @param boolean $escape whether to escape or not the values |
|
| 874 | - * @return object the current Database instance |
|
| 875 | - */ |
|
| 876 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 877 | - if(is_array($op)){ |
|
| 878 | - $x = explode('?', $field); |
|
| 879 | - $w = ''; |
|
| 880 | - foreach($x as $k => $v){ |
|
| 881 | - if(!empty($v)){ |
|
| 882 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 883 | - $op[$k] = ''; |
|
| 884 | - } |
|
| 885 | - $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 886 | - } |
|
| 887 | - } |
|
| 888 | - $this->having = $w; |
|
| 889 | - } |
|
| 890 | - else if (! in_array($op, $this->operatorList)){ |
|
| 891 | - if(is_null($op)){ |
|
| 892 | - $op = ''; |
|
| 893 | - } |
|
| 894 | - $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 895 | - } |
|
| 896 | - else{ |
|
| 897 | - if(is_null($val)){ |
|
| 898 | - $val = ''; |
|
| 899 | - } |
|
| 900 | - $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 901 | - } |
|
| 902 | - return $this; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Return the number of rows returned by the current query |
|
| 907 | - * @return int |
|
| 908 | - */ |
|
| 909 | - public function numRows(){ |
|
| 910 | - return $this->numRows; |
|
| 911 | - } |
|
| 912 | - |
|
| 913 | - /** |
|
| 914 | - * Return the last insert id value |
|
| 915 | - * @return mixed |
|
| 916 | - */ |
|
| 917 | - public function insertId(){ |
|
| 918 | - return $this->insertId; |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 923 | - */ |
|
| 924 | - public function error(){ |
|
| 491 | + /** |
|
| 492 | + * Set the SQL WHERE CLAUSE statment |
|
| 493 | + * @param string|array $where the where field or array of field list |
|
| 494 | + * @param string $op the condition operator. If is null the default will be "=" |
|
| 495 | + * @param mixed $val the where value |
|
| 496 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 497 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 498 | + * @param boolean $escape whether to escape or not the $val |
|
| 499 | + * @return object the current Database instance |
|
| 500 | + */ |
|
| 501 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 502 | + if (is_array($where)){ |
|
| 503 | + $_where = array(); |
|
| 504 | + foreach ($where as $column => $data){ |
|
| 505 | + if(is_null($data)){ |
|
| 506 | + $data = ''; |
|
| 507 | + } |
|
| 508 | + $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
| 509 | + } |
|
| 510 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 511 | + } |
|
| 512 | + else{ |
|
| 513 | + if(is_array($op)){ |
|
| 514 | + $x = explode('?', $where); |
|
| 515 | + $w = ''; |
|
| 516 | + foreach($x as $k => $v){ |
|
| 517 | + if(! empty($v)){ |
|
| 518 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 519 | + $op[$k] = ''; |
|
| 520 | + } |
|
| 521 | + $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + $where = $w; |
|
| 525 | + } |
|
| 526 | + else if (! in_array((string)$op, $this->operatorList)){ |
|
| 527 | + if(is_null($op)){ |
|
| 528 | + $op = ''; |
|
| 529 | + } |
|
| 530 | + $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 531 | + } |
|
| 532 | + else{ |
|
| 533 | + if(is_null($val)){ |
|
| 534 | + $val = ''; |
|
| 535 | + } |
|
| 536 | + $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + if (is_null($this->where)){ |
|
| 540 | + $this->where = $where; |
|
| 541 | + } |
|
| 542 | + else{ |
|
| 543 | + if(substr($this->where, -1) == '('){ |
|
| 544 | + $this->where = $this->where . ' ' . $where; |
|
| 545 | + } |
|
| 546 | + else{ |
|
| 547 | + $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + return $this; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 555 | + * @see Database::where() |
|
| 556 | + * @return object the current Database instance |
|
| 557 | + */ |
|
| 558 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 559 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 565 | + * @see Database::where() |
|
| 566 | + * @return object the current Database instance |
|
| 567 | + */ |
|
| 568 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 569 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 574 | + * @see Database::where() |
|
| 575 | + * @return object the current Database instance |
|
| 576 | + */ |
|
| 577 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 578 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Set the opened parenthesis for the complex SQL query |
|
| 583 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 584 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 585 | + * @return object the current Database instance |
|
| 586 | + */ |
|
| 587 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 588 | + if (is_null($this->where)){ |
|
| 589 | + $this->where = $type . ' ('; |
|
| 590 | + } |
|
| 591 | + else{ |
|
| 592 | + if(substr($this->where, -1) == '('){ |
|
| 593 | + $this->where .= $type . ' ('; |
|
| 594 | + } |
|
| 595 | + else{ |
|
| 596 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + return $this; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 604 | + * @see Database::groupStart() |
|
| 605 | + * @return object the current Database instance |
|
| 606 | + */ |
|
| 607 | + public function notGroupStart(){ |
|
| 608 | + return $this->groupStart('NOT'); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 613 | + * @see Database::groupStart() |
|
| 614 | + * @return object the current Database instance |
|
| 615 | + */ |
|
| 616 | + public function orGroupStart(){ |
|
| 617 | + return $this->groupStart('', ' OR'); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 622 | + * @see Database::groupStart() |
|
| 623 | + * @return object the current Database instance |
|
| 624 | + */ |
|
| 625 | + public function orNotGroupStart(){ |
|
| 626 | + return $this->groupStart('NOT', ' OR'); |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Close the parenthesis for the grouped SQL |
|
| 631 | + * @return object the current Database instance |
|
| 632 | + */ |
|
| 633 | + public function groupEnd(){ |
|
| 634 | + $this->where .= ')'; |
|
| 635 | + return $this; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 640 | + * @param string $field the field name for IN statment |
|
| 641 | + * @param array $keys the list of values used |
|
| 642 | + * @param string $type the condition separator type (NOT) |
|
| 643 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 644 | + * @param boolean $escape whether to escape or not the values |
|
| 645 | + * @return object the current Database instance |
|
| 646 | + */ |
|
| 647 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 648 | + if (is_array($keys)){ |
|
| 649 | + $_keys = array(); |
|
| 650 | + foreach ($keys as $k => $v){ |
|
| 651 | + if(is_null($v)){ |
|
| 652 | + $v = ''; |
|
| 653 | + } |
|
| 654 | + $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 655 | + } |
|
| 656 | + $keys = implode(', ', $_keys); |
|
| 657 | + if (is_null($this->where)){ |
|
| 658 | + $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 659 | + } |
|
| 660 | + else{ |
|
| 661 | + if(substr($this->where, -1) == '('){ |
|
| 662 | + $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 663 | + } |
|
| 664 | + else{ |
|
| 665 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + } |
|
| 669 | + return $this; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 674 | + * @see Database::in() |
|
| 675 | + * @return object the current Database instance |
|
| 676 | + */ |
|
| 677 | + public function notIn($field, array $keys, $escape = true){ |
|
| 678 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 683 | + * @see Database::in() |
|
| 684 | + * @return object the current Database instance |
|
| 685 | + */ |
|
| 686 | + public function orIn($field, array $keys, $escape = true){ |
|
| 687 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 692 | + * @see Database::in() |
|
| 693 | + * @return object the current Database instance |
|
| 694 | + */ |
|
| 695 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 696 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 701 | + * @param string $field the field used for the BETWEEN statment |
|
| 702 | + * @param mixed $value1 the BETWEEN begin value |
|
| 703 | + * @param mixed $value2 the BETWEEN end value |
|
| 704 | + * @param string $type the condition separator type (NOT) |
|
| 705 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 706 | + * @param boolean $escape whether to escape or not the values |
|
| 707 | + * @return object the current Database instance |
|
| 708 | + */ |
|
| 709 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 710 | + if(is_null($value1)){ |
|
| 711 | + $value1 = ''; |
|
| 712 | + } |
|
| 713 | + if(is_null($value2)){ |
|
| 714 | + $value2 = ''; |
|
| 715 | + } |
|
| 716 | + if (is_null($this->where)){ |
|
| 717 | + $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 718 | + } |
|
| 719 | + else{ |
|
| 720 | + if(substr($this->where, -1) == '('){ |
|
| 721 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 722 | + } |
|
| 723 | + else{ |
|
| 724 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 725 | + } |
|
| 726 | + } |
|
| 727 | + return $this; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 732 | + * @see Database::between() |
|
| 733 | + * @return object the current Database instance |
|
| 734 | + */ |
|
| 735 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 736 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 741 | + * @see Database::between() |
|
| 742 | + * @return object the current Database instance |
|
| 743 | + */ |
|
| 744 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 745 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + /** |
|
| 749 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 750 | + * @see Database::between() |
|
| 751 | + * @return object the current Database instance |
|
| 752 | + */ |
|
| 753 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 754 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 759 | + * @param string $field the field name used in LIKE statment |
|
| 760 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 761 | + * @param string $type the condition separator type (NOT) |
|
| 762 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 763 | + * @param boolean $escape whether to escape or not the values |
|
| 764 | + * @return object the current Database instance |
|
| 765 | + */ |
|
| 766 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 767 | + if(is_null($data)){ |
|
| 768 | + $data = ''; |
|
| 769 | + } |
|
| 770 | + $like = $escape ? $this->escape($data) : $data; |
|
| 771 | + if (is_null($this->where)){ |
|
| 772 | + $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 773 | + } |
|
| 774 | + else{ |
|
| 775 | + if(substr($this->where, -1) == '('){ |
|
| 776 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 777 | + } |
|
| 778 | + else{ |
|
| 779 | + $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + return $this; |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + /** |
|
| 786 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 787 | + * @see Database::like() |
|
| 788 | + * @return object the current Database instance |
|
| 789 | + */ |
|
| 790 | + public function orLike($field, $data, $escape = true){ |
|
| 791 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 796 | + * @see Database::like() |
|
| 797 | + * @return object the current Database instance |
|
| 798 | + */ |
|
| 799 | + public function notLike($field, $data, $escape = true){ |
|
| 800 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + /** |
|
| 804 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 805 | + * @see Database::like() |
|
| 806 | + * @return object the current Database instance |
|
| 807 | + */ |
|
| 808 | + public function orNotLike($field, $data, $escape = true){ |
|
| 809 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * Set the SQL LIMIT statment |
|
| 814 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 815 | + * like LIMIT n; |
|
| 816 | + * @param int $limitEnd the limit count |
|
| 817 | + * @return object the current Database instance |
|
| 818 | + */ |
|
| 819 | + public function limit($limit, $limitEnd = null){ |
|
| 820 | + if(is_null($limit)){ |
|
| 821 | + return; |
|
| 822 | + } |
|
| 823 | + if (! is_null($limitEnd)){ |
|
| 824 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 825 | + } |
|
| 826 | + else{ |
|
| 827 | + $this->limit = $limit; |
|
| 828 | + } |
|
| 829 | + return $this; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + /** |
|
| 833 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 834 | + * @param string $orderBy the field name used for order |
|
| 835 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 836 | + * @return object the current Database instance |
|
| 837 | + */ |
|
| 838 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 839 | + if (! is_null($orderDir)){ |
|
| 840 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 841 | + } |
|
| 842 | + else{ |
|
| 843 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 844 | + $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 845 | + } |
|
| 846 | + else{ |
|
| 847 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 848 | + } |
|
| 849 | + } |
|
| 850 | + return $this; |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + /** |
|
| 854 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 855 | + * @param string|array $field the field name used or array of field list |
|
| 856 | + * @return object the current Database instance |
|
| 857 | + */ |
|
| 858 | + public function groupBy($field){ |
|
| 859 | + if(is_array($field)){ |
|
| 860 | + $this->groupBy = implode(', ', $field); |
|
| 861 | + } |
|
| 862 | + else{ |
|
| 863 | + $this->groupBy = $field; |
|
| 864 | + } |
|
| 865 | + return $this; |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * Set the SQL HAVING CLAUSE statment |
|
| 870 | + * @param string $field the field name used for HAVING statment |
|
| 871 | + * @param string|array $op the operator used or array |
|
| 872 | + * @param mixed $val the value for HAVING comparaison |
|
| 873 | + * @param boolean $escape whether to escape or not the values |
|
| 874 | + * @return object the current Database instance |
|
| 875 | + */ |
|
| 876 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 877 | + if(is_array($op)){ |
|
| 878 | + $x = explode('?', $field); |
|
| 879 | + $w = ''; |
|
| 880 | + foreach($x as $k => $v){ |
|
| 881 | + if(!empty($v)){ |
|
| 882 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 883 | + $op[$k] = ''; |
|
| 884 | + } |
|
| 885 | + $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 886 | + } |
|
| 887 | + } |
|
| 888 | + $this->having = $w; |
|
| 889 | + } |
|
| 890 | + else if (! in_array($op, $this->operatorList)){ |
|
| 891 | + if(is_null($op)){ |
|
| 892 | + $op = ''; |
|
| 893 | + } |
|
| 894 | + $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 895 | + } |
|
| 896 | + else{ |
|
| 897 | + if(is_null($val)){ |
|
| 898 | + $val = ''; |
|
| 899 | + } |
|
| 900 | + $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 901 | + } |
|
| 902 | + return $this; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Return the number of rows returned by the current query |
|
| 907 | + * @return int |
|
| 908 | + */ |
|
| 909 | + public function numRows(){ |
|
| 910 | + return $this->numRows; |
|
| 911 | + } |
|
| 912 | + |
|
| 913 | + /** |
|
| 914 | + * Return the last insert id value |
|
| 915 | + * @return mixed |
|
| 916 | + */ |
|
| 917 | + public function insertId(){ |
|
| 918 | + return $this->insertId; |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 923 | + */ |
|
| 924 | + public function error(){ |
|
| 925 | 925 | if($this->error){ |
| 926 | 926 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 927 | 927 | } |
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * Get the result of one record rows returned by the current query |
|
| 932 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 933 | - * If is string will determine the result type "array" or "object" |
|
| 934 | - * @return mixed the query SQL string or the record result |
|
| 935 | - */ |
|
| 936 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 937 | - $this->limit = 1; |
|
| 938 | - $query = $this->getAll(true); |
|
| 939 | - if($returnSQLQueryOrResultType === true){ |
|
| 940 | - return $query; |
|
| 941 | - } |
|
| 942 | - else{ |
|
| 943 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * Get the result of record rows list returned by the current query |
|
| 949 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 950 | - * If is string will determine the result type "array" or "object" |
|
| 951 | - * @return mixed the query SQL string or the record result |
|
| 952 | - */ |
|
| 953 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 954 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 955 | - if (! is_null($this->join)){ |
|
| 956 | - $query .= $this->join; |
|
| 957 | - } |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * Get the result of one record rows returned by the current query |
|
| 932 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 933 | + * If is string will determine the result type "array" or "object" |
|
| 934 | + * @return mixed the query SQL string or the record result |
|
| 935 | + */ |
|
| 936 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 937 | + $this->limit = 1; |
|
| 938 | + $query = $this->getAll(true); |
|
| 939 | + if($returnSQLQueryOrResultType === true){ |
|
| 940 | + return $query; |
|
| 941 | + } |
|
| 942 | + else{ |
|
| 943 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * Get the result of record rows list returned by the current query |
|
| 949 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 950 | + * If is string will determine the result type "array" or "object" |
|
| 951 | + * @return mixed the query SQL string or the record result |
|
| 952 | + */ |
|
| 953 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 954 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 955 | + if (! is_null($this->join)){ |
|
| 956 | + $query .= $this->join; |
|
| 957 | + } |
|
| 958 | 958 | |
| 959 | - if (! is_null($this->where)){ |
|
| 960 | - $query .= ' WHERE ' . $this->where; |
|
| 961 | - } |
|
| 959 | + if (! is_null($this->where)){ |
|
| 960 | + $query .= ' WHERE ' . $this->where; |
|
| 961 | + } |
|
| 962 | 962 | |
| 963 | - if (! is_null($this->groupBy)){ |
|
| 964 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 965 | - } |
|
| 963 | + if (! is_null($this->groupBy)){ |
|
| 964 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 965 | + } |
|
| 966 | 966 | |
| 967 | - if (! is_null($this->having)){ |
|
| 968 | - $query .= ' HAVING ' . $this->having; |
|
| 969 | - } |
|
| 967 | + if (! is_null($this->having)){ |
|
| 968 | + $query .= ' HAVING ' . $this->having; |
|
| 969 | + } |
|
| 970 | 970 | |
| 971 | - if (! is_null($this->orderBy)){ |
|
| 972 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 973 | - } |
|
| 971 | + if (! is_null($this->orderBy)){ |
|
| 972 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 973 | + } |
|
| 974 | 974 | |
| 975 | - if(! is_null($this->limit)){ |
|
| 976 | - $query .= ' LIMIT ' . $this->limit; |
|
| 977 | - } |
|
| 975 | + if(! is_null($this->limit)){ |
|
| 976 | + $query .= ' LIMIT ' . $this->limit; |
|
| 977 | + } |
|
| 978 | 978 | |
| 979 | 979 | if($returnSQLQueryOrResultType === true){ |
| 980 | - return $query; |
|
| 981 | - } |
|
| 982 | - else{ |
|
| 983 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 984 | - } |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - /** |
|
| 988 | - * Insert new record in the database |
|
| 989 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 990 | - * @param boolean $escape whether to escape or not the values |
|
| 991 | - * @return mixed the insert id of the new record or null |
|
| 992 | - */ |
|
| 993 | - public function insert($data = array(), $escape = true){ |
|
| 994 | - $column = array(); |
|
| 995 | - $val = array(); |
|
| 996 | - if(! $data && $this->getData()){ |
|
| 997 | - $columns = array_keys($this->getData()); |
|
| 998 | - $column = implode(',', $columns); |
|
| 999 | - $val = implode(', ', $this->getData()); |
|
| 1000 | - } |
|
| 1001 | - else{ |
|
| 1002 | - $columns = array_keys($data); |
|
| 1003 | - $column = implode(',', $columns); |
|
| 1004 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 1008 | - $query = $this->query($query); |
|
| 1009 | - |
|
| 1010 | - if ($query){ |
|
| 1011 | - if(! $this->pdo){ |
|
| 1012 | - $this->connect(); |
|
| 1013 | - } |
|
| 1014 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 1015 | - return $this->insertId(); |
|
| 1016 | - } |
|
| 1017 | - else{ |
|
| 980 | + return $query; |
|
| 981 | + } |
|
| 982 | + else{ |
|
| 983 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 984 | + } |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + /** |
|
| 988 | + * Insert new record in the database |
|
| 989 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 990 | + * @param boolean $escape whether to escape or not the values |
|
| 991 | + * @return mixed the insert id of the new record or null |
|
| 992 | + */ |
|
| 993 | + public function insert($data = array(), $escape = true){ |
|
| 994 | + $column = array(); |
|
| 995 | + $val = array(); |
|
| 996 | + if(! $data && $this->getData()){ |
|
| 997 | + $columns = array_keys($this->getData()); |
|
| 998 | + $column = implode(',', $columns); |
|
| 999 | + $val = implode(', ', $this->getData()); |
|
| 1000 | + } |
|
| 1001 | + else{ |
|
| 1002 | + $columns = array_keys($data); |
|
| 1003 | + $column = implode(',', $columns); |
|
| 1004 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 1008 | + $query = $this->query($query); |
|
| 1009 | + |
|
| 1010 | + if ($query){ |
|
| 1011 | + if(! $this->pdo){ |
|
| 1012 | + $this->connect(); |
|
| 1013 | + } |
|
| 1014 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 1015 | + return $this->insertId(); |
|
| 1016 | + } |
|
| 1017 | + else{ |
|
| 1018 | 1018 | return false; |
| 1019 | - } |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - /** |
|
| 1023 | - * Update record in the database |
|
| 1024 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 1025 | - * @param boolean $escape whether to escape or not the values |
|
| 1026 | - * @return mixed the update status |
|
| 1027 | - */ |
|
| 1028 | - public function update($data = array(), $escape = true){ |
|
| 1029 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1030 | - $values = array(); |
|
| 1031 | - if(! $data && $this->getData()){ |
|
| 1032 | - foreach ($this->getData() as $column => $val){ |
|
| 1033 | - $values[] = $column . ' = ' . $val; |
|
| 1034 | - } |
|
| 1035 | - } |
|
| 1036 | - else{ |
|
| 1037 | - foreach ($data as $column => $val){ |
|
| 1038 | - $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1039 | - } |
|
| 1040 | - } |
|
| 1041 | - $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
| 1042 | - if (! is_null($this->where)){ |
|
| 1043 | - $query .= ' WHERE ' . $this->where; |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - if (! is_null($this->orderBy)){ |
|
| 1047 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - if (! is_null($this->limit)){ |
|
| 1051 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | - } |
|
| 1053 | - return $this->query($query); |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - /** |
|
| 1057 | - * Delete the record in database |
|
| 1058 | - * @return mixed the delete status |
|
| 1059 | - */ |
|
| 1060 | - public function delete(){ |
|
| 1061 | - $query = 'DELETE FROM ' . $this->from; |
|
| 1062 | - |
|
| 1063 | - if (! is_null($this->where)){ |
|
| 1064 | - $query .= ' WHERE ' . $this->where; |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - if (! is_null($this->orderBy)){ |
|
| 1068 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1069 | - } |
|
| 1070 | - |
|
| 1071 | - if (! is_null($this->limit)){ |
|
| 1072 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - if($query == 'DELETE FROM ' . $this->from){ |
|
| 1076 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1077 | - } |
|
| 1078 | - return $this->query($query); |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - /** |
|
| 1082 | - * Execute an SQL query |
|
| 1083 | - * @param string $query the query SQL string |
|
| 1084 | - * @param boolean $all whether to return all record or not |
|
| 1085 | - * @param boolean $array return the result as array |
|
| 1086 | - * @return mixed the query result |
|
| 1087 | - */ |
|
| 1088 | - public function query($query, $all = true, $array = false){ |
|
| 1089 | - $this->reset(); |
|
| 1090 | - if(is_array($all)){ |
|
| 1091 | - $x = explode('?', $query); |
|
| 1092 | - $q = ''; |
|
| 1093 | - foreach($x as $k => $v){ |
|
| 1094 | - if(! empty($v)){ |
|
| 1095 | - $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1096 | - } |
|
| 1097 | - } |
|
| 1098 | - $query = $q; |
|
| 1099 | - } |
|
| 1100 | - |
|
| 1101 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1102 | - $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1103 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1104 | - //cache expire time |
|
| 1019 | + } |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + /** |
|
| 1023 | + * Update record in the database |
|
| 1024 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 1025 | + * @param boolean $escape whether to escape or not the values |
|
| 1026 | + * @return mixed the update status |
|
| 1027 | + */ |
|
| 1028 | + public function update($data = array(), $escape = true){ |
|
| 1029 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1030 | + $values = array(); |
|
| 1031 | + if(! $data && $this->getData()){ |
|
| 1032 | + foreach ($this->getData() as $column => $val){ |
|
| 1033 | + $values[] = $column . ' = ' . $val; |
|
| 1034 | + } |
|
| 1035 | + } |
|
| 1036 | + else{ |
|
| 1037 | + foreach ($data as $column => $val){ |
|
| 1038 | + $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1039 | + } |
|
| 1040 | + } |
|
| 1041 | + $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
| 1042 | + if (! is_null($this->where)){ |
|
| 1043 | + $query .= ' WHERE ' . $this->where; |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + if (! is_null($this->orderBy)){ |
|
| 1047 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + if (! is_null($this->limit)){ |
|
| 1051 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | + } |
|
| 1053 | + return $this->query($query); |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + /** |
|
| 1057 | + * Delete the record in database |
|
| 1058 | + * @return mixed the delete status |
|
| 1059 | + */ |
|
| 1060 | + public function delete(){ |
|
| 1061 | + $query = 'DELETE FROM ' . $this->from; |
|
| 1062 | + |
|
| 1063 | + if (! is_null($this->where)){ |
|
| 1064 | + $query .= ' WHERE ' . $this->where; |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + if (! is_null($this->orderBy)){ |
|
| 1068 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1069 | + } |
|
| 1070 | + |
|
| 1071 | + if (! is_null($this->limit)){ |
|
| 1072 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + if($query == 'DELETE FROM ' . $this->from){ |
|
| 1076 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1077 | + } |
|
| 1078 | + return $this->query($query); |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + /** |
|
| 1082 | + * Execute an SQL query |
|
| 1083 | + * @param string $query the query SQL string |
|
| 1084 | + * @param boolean $all whether to return all record or not |
|
| 1085 | + * @param boolean $array return the result as array |
|
| 1086 | + * @return mixed the query result |
|
| 1087 | + */ |
|
| 1088 | + public function query($query, $all = true, $array = false){ |
|
| 1089 | + $this->reset(); |
|
| 1090 | + if(is_array($all)){ |
|
| 1091 | + $x = explode('?', $query); |
|
| 1092 | + $q = ''; |
|
| 1093 | + foreach($x as $k => $v){ |
|
| 1094 | + if(! empty($v)){ |
|
| 1095 | + $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1096 | + } |
|
| 1097 | + } |
|
| 1098 | + $query = $q; |
|
| 1099 | + } |
|
| 1100 | + |
|
| 1101 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1102 | + $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1103 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1104 | + //cache expire time |
|
| 1105 | 1105 | $cacheExpire = $this->temporaryCacheTtl; |
| 1106 | 1106 | |
| 1107 | 1107 | //return to the initial cache time |
| 1108 | 1108 | $this->temporaryCacheTtl = $this->cacheTtl; |
| 1109 | 1109 | |
| 1110 | 1110 | //config for cache |
| 1111 | - $cacheEnable = get_config('cache_enable'); |
|
| 1111 | + $cacheEnable = get_config('cache_enable'); |
|
| 1112 | 1112 | |
| 1113 | 1113 | //the database cache content |
| 1114 | - $cacheContent = null; |
|
| 1114 | + $cacheContent = null; |
|
| 1115 | 1115 | |
| 1116 | 1116 | //this database query cache key |
| 1117 | - $cacheKey = null; |
|
| 1117 | + $cacheKey = null; |
|
| 1118 | 1118 | |
| 1119 | 1119 | //the cache manager instance |
| 1120 | - $cacheInstance = null; |
|
| 1120 | + $cacheInstance = null; |
|
| 1121 | 1121 | |
| 1122 | 1122 | //if can use cache feature for this query |
| 1123 | 1123 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1124 | 1124 | |
| 1125 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1126 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1127 | - $cacheKey = md5($query . $all . $array); |
|
| 1128 | - if(is_object($this->cacheInstance)){ |
|
| 1129 | - $cacheInstance = $this->cacheInstance; |
|
| 1130 | - } |
|
| 1131 | - else{ |
|
| 1132 | - $obj = & get_instance(); |
|
| 1133 | - $cacheInstance = $obj->cache; |
|
| 1134 | - } |
|
| 1135 | - $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1136 | - } |
|
| 1137 | - else{ |
|
| 1125 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1126 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1127 | + $cacheKey = md5($query . $all . $array); |
|
| 1128 | + if(is_object($this->cacheInstance)){ |
|
| 1129 | + $cacheInstance = $this->cacheInstance; |
|
| 1130 | + } |
|
| 1131 | + else{ |
|
| 1132 | + $obj = & get_instance(); |
|
| 1133 | + $cacheInstance = $obj->cache; |
|
| 1134 | + } |
|
| 1135 | + $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1136 | + } |
|
| 1137 | + else{ |
|
| 1138 | 1138 | $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 | - } |
|
| 1139 | + } |
|
| 1140 | 1140 | |
| 1141 | - if(! $this->pdo){ |
|
| 1142 | - $this->connect(); |
|
| 1143 | - } |
|
| 1141 | + if(! $this->pdo){ |
|
| 1142 | + $this->connect(); |
|
| 1143 | + } |
|
| 1144 | 1144 | |
| 1145 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1146 | - //for database query execution time |
|
| 1147 | - $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1148 | - $bench = null; |
|
| 1149 | - if(is_object($this->benchmarkInstance)){ |
|
| 1150 | - $bench = $this->benchmarkInstance; |
|
| 1151 | - } |
|
| 1152 | - else{ |
|
| 1153 | - $obj = & get_instance(); |
|
| 1154 | - $bench = $obj->benchmark; |
|
| 1155 | - } |
|
| 1156 | - $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1157 | - //Now execute the query |
|
| 1158 | - $sqlQuery = $this->pdo->query($this->query); |
|
| 1145 | + if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1146 | + //for database query execution time |
|
| 1147 | + $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1148 | + $bench = null; |
|
| 1149 | + if(is_object($this->benchmarkInstance)){ |
|
| 1150 | + $bench = $this->benchmarkInstance; |
|
| 1151 | + } |
|
| 1152 | + else{ |
|
| 1153 | + $obj = & get_instance(); |
|
| 1154 | + $bench = $obj->benchmark; |
|
| 1155 | + } |
|
| 1156 | + $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1157 | + //Now execute the query |
|
| 1158 | + $sqlQuery = $this->pdo->query($this->query); |
|
| 1159 | 1159 | |
| 1160 | - //get response time for this query |
|
| 1161 | - $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1162 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 1163 | - if($responseTime >= 1 ){ |
|
| 1164 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1165 | - } |
|
| 1166 | - if ($sqlQuery){ |
|
| 1167 | - $this->numRows = $sqlQuery->rowCount(); |
|
| 1168 | - if (($this->numRows > 0)){ |
|
| 1169 | - //if need return all result like list of record |
|
| 1170 | - if ($all){ |
|
| 1171 | - $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1172 | - } |
|
| 1173 | - else{ |
|
| 1174 | - $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1175 | - } |
|
| 1176 | - } |
|
| 1177 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1178 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1179 | - $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1180 | - } |
|
| 1181 | - } |
|
| 1182 | - else{ |
|
| 1183 | - $error = $this->pdo->errorInfo(); |
|
| 1184 | - $this->error = $error[2]; |
|
| 1185 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1186 | - $this->error(); |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1190 | - $queryStr = $this->pdo->query($this->query); |
|
| 1191 | - if($queryStr){ |
|
| 1192 | - $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1193 | - $this->numRows = $queryStr->rowCount(); |
|
| 1194 | - } |
|
| 1195 | - if (! $this->result){ |
|
| 1196 | - $error = $this->pdo->errorInfo(); |
|
| 1197 | - $this->error = $error[2]; |
|
| 1198 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1199 | - $this->error(); |
|
| 1200 | - } |
|
| 1201 | - } |
|
| 1202 | - else{ |
|
| 1203 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1204 | - $this->result = $cacheContent; |
|
| 1205 | - $this->numRows = count($this->result); |
|
| 1206 | - } |
|
| 1207 | - $this->queryCount++; |
|
| 1208 | - if(! $this->result){ |
|
| 1209 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1210 | - } |
|
| 1211 | - return $this->result; |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - /** |
|
| 1215 | - * Set database cache time to live |
|
| 1216 | - * @param integer $ttl the cache time to live in second |
|
| 1217 | - * @return object the current Database instance |
|
| 1218 | - */ |
|
| 1219 | - public function setCache($ttl = 0){ |
|
| 1220 | - if($ttl > 0){ |
|
| 1221 | - $this->cacheTtl = $ttl; |
|
| 1222 | - $this->temporaryCacheTtl = $ttl; |
|
| 1223 | - } |
|
| 1224 | - return $this; |
|
| 1225 | - } |
|
| 1160 | + //get response time for this query |
|
| 1161 | + $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1162 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 1163 | + if($responseTime >= 1 ){ |
|
| 1164 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1165 | + } |
|
| 1166 | + if ($sqlQuery){ |
|
| 1167 | + $this->numRows = $sqlQuery->rowCount(); |
|
| 1168 | + if (($this->numRows > 0)){ |
|
| 1169 | + //if need return all result like list of record |
|
| 1170 | + if ($all){ |
|
| 1171 | + $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1172 | + } |
|
| 1173 | + else{ |
|
| 1174 | + $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1175 | + } |
|
| 1176 | + } |
|
| 1177 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1178 | + $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1179 | + $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1180 | + } |
|
| 1181 | + } |
|
| 1182 | + else{ |
|
| 1183 | + $error = $this->pdo->errorInfo(); |
|
| 1184 | + $this->error = $error[2]; |
|
| 1185 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1186 | + $this->error(); |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1190 | + $queryStr = $this->pdo->query($this->query); |
|
| 1191 | + if($queryStr){ |
|
| 1192 | + $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1193 | + $this->numRows = $queryStr->rowCount(); |
|
| 1194 | + } |
|
| 1195 | + if (! $this->result){ |
|
| 1196 | + $error = $this->pdo->errorInfo(); |
|
| 1197 | + $this->error = $error[2]; |
|
| 1198 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1199 | + $this->error(); |
|
| 1200 | + } |
|
| 1201 | + } |
|
| 1202 | + else{ |
|
| 1203 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1204 | + $this->result = $cacheContent; |
|
| 1205 | + $this->numRows = count($this->result); |
|
| 1206 | + } |
|
| 1207 | + $this->queryCount++; |
|
| 1208 | + if(! $this->result){ |
|
| 1209 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1210 | + } |
|
| 1211 | + return $this->result; |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + /** |
|
| 1215 | + * Set database cache time to live |
|
| 1216 | + * @param integer $ttl the cache time to live in second |
|
| 1217 | + * @return object the current Database instance |
|
| 1218 | + */ |
|
| 1219 | + public function setCache($ttl = 0){ |
|
| 1220 | + if($ttl > 0){ |
|
| 1221 | + $this->cacheTtl = $ttl; |
|
| 1222 | + $this->temporaryCacheTtl = $ttl; |
|
| 1223 | + } |
|
| 1224 | + return $this; |
|
| 1225 | + } |
|
| 1226 | 1226 | |
| 1227 | 1227 | /** |
| 1228 | 1228 | * Enabled cache temporary for the current query not globally |
@@ -1230,186 +1230,186 @@ discard block |
||
| 1230 | 1230 | * @return object the current Database instance |
| 1231 | 1231 | */ |
| 1232 | 1232 | public function cached($ttl = 0){ |
| 1233 | - if($ttl > 0){ |
|
| 1234 | - $this->temporaryCacheTtl = $ttl; |
|
| 1235 | - } |
|
| 1233 | + if($ttl > 0){ |
|
| 1234 | + $this->temporaryCacheTtl = $ttl; |
|
| 1235 | + } |
|
| 1236 | + return $this; |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + /** |
|
| 1240 | + * Escape the data before execute query useful for security. |
|
| 1241 | + * @param mixed $data the data to be escaped |
|
| 1242 | + * @return mixed the data after escaped |
|
| 1243 | + */ |
|
| 1244 | + public function escape($data){ |
|
| 1245 | + if(is_null($data)){ |
|
| 1246 | + return null; |
|
| 1247 | + } |
|
| 1248 | + if(! $this->pdo){ |
|
| 1249 | + $this->connect(); |
|
| 1250 | + } |
|
| 1251 | + return $this->pdo->quote(trim($data)); |
|
| 1252 | + } |
|
| 1253 | + |
|
| 1254 | + /** |
|
| 1255 | + * Return the number query executed count for the current request |
|
| 1256 | + * @return int |
|
| 1257 | + */ |
|
| 1258 | + public function queryCount(){ |
|
| 1259 | + return $this->queryCount; |
|
| 1260 | + } |
|
| 1261 | + |
|
| 1262 | + /** |
|
| 1263 | + * Return the current query SQL string |
|
| 1264 | + * @return string |
|
| 1265 | + */ |
|
| 1266 | + public function getQuery(){ |
|
| 1267 | + return $this->query; |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + /** |
|
| 1271 | + * Return the application database name |
|
| 1272 | + * @return string |
|
| 1273 | + */ |
|
| 1274 | + public function getDatabaseName(){ |
|
| 1275 | + return $this->databaseName; |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + /** |
|
| 1279 | + * Return the database configuration |
|
| 1280 | + * @return array |
|
| 1281 | + */ |
|
| 1282 | + public function getDatabaseConfiguration(){ |
|
| 1283 | + return $this->config; |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + /** |
|
| 1287 | + * set the database configuration |
|
| 1288 | + * @param array $config the configuration |
|
| 1289 | + */ |
|
| 1290 | + public function setDatabaseConfiguration(array $config){ |
|
| 1291 | + $this->config = array_merge($this->config, $config); |
|
| 1292 | + $this->prefix = $this->config['prefix']; |
|
| 1293 | + $this->databaseName = $this->config['database']; |
|
| 1294 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1295 | + return $this; |
|
| 1296 | + } |
|
| 1297 | + |
|
| 1298 | + /** |
|
| 1299 | + * Return the PDO instance |
|
| 1300 | + * @return PDO |
|
| 1301 | + */ |
|
| 1302 | + public function getPdo(){ |
|
| 1303 | + return $this->pdo; |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + /** |
|
| 1307 | + * Set the PDO instance |
|
| 1308 | + * @param PDO $pdo the pdo object |
|
| 1309 | + */ |
|
| 1310 | + public function setPdo(PDO $pdo){ |
|
| 1311 | + $this->pdo = $pdo; |
|
| 1312 | + return $this; |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + |
|
| 1316 | + /** |
|
| 1317 | + * Return the Log instance |
|
| 1318 | + * @return Log |
|
| 1319 | + */ |
|
| 1320 | + public function getLogger(){ |
|
| 1321 | + return $this->logger; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + /** |
|
| 1325 | + * Set the log instance |
|
| 1326 | + * @param Log $logger the log object |
|
| 1327 | + */ |
|
| 1328 | + public function setLogger($logger){ |
|
| 1329 | + $this->logger = $logger; |
|
| 1330 | + return $this; |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + /** |
|
| 1334 | + * Return the cache instance |
|
| 1335 | + * @return CacheInterface |
|
| 1336 | + */ |
|
| 1337 | + public function getCacheInstance(){ |
|
| 1338 | + return $this->cacheInstance; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * Set the cache instance |
|
| 1343 | + * @param CacheInterface $cache the cache object |
|
| 1344 | + */ |
|
| 1345 | + public function setCacheInstance($cache){ |
|
| 1346 | + $this->cacheInstance = $cache; |
|
| 1347 | + return $this; |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * Return the benchmark instance |
|
| 1352 | + * @return Benchmark |
|
| 1353 | + */ |
|
| 1354 | + public function getBenchmark(){ |
|
| 1355 | + return $this->benchmarkInstance; |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * Set the benchmark instance |
|
| 1360 | + * @param Benchmark $cache the cache object |
|
| 1361 | + */ |
|
| 1362 | + public function setBenchmark($benchmark){ |
|
| 1363 | + $this->benchmarkInstance = $benchmark; |
|
| 1364 | + return $this; |
|
| 1365 | + } |
|
| 1366 | + |
|
| 1367 | + /** |
|
| 1368 | + * Return the data to be used for insert, update, etc. |
|
| 1369 | + * @return array |
|
| 1370 | + */ |
|
| 1371 | + public function getData(){ |
|
| 1372 | + return $this->data; |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + /** |
|
| 1376 | + * Set the data to be used for insert, update, etc. |
|
| 1377 | + * @param string $key the data key identified |
|
| 1378 | + * @param mixed $value the data value |
|
| 1379 | + * @param boolean $escape whether to escape or not the $value |
|
| 1380 | + * @return object the current Database instance |
|
| 1381 | + */ |
|
| 1382 | + public function setData($key, $value, $escape = true){ |
|
| 1383 | + $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1236 | 1384 | return $this; |
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - /** |
|
| 1240 | - * Escape the data before execute query useful for security. |
|
| 1241 | - * @param mixed $data the data to be escaped |
|
| 1242 | - * @return mixed the data after escaped |
|
| 1243 | - */ |
|
| 1244 | - public function escape($data){ |
|
| 1245 | - if(is_null($data)){ |
|
| 1246 | - return null; |
|
| 1247 | - } |
|
| 1248 | - if(! $this->pdo){ |
|
| 1249 | - $this->connect(); |
|
| 1250 | - } |
|
| 1251 | - return $this->pdo->quote(trim($data)); |
|
| 1252 | - } |
|
| 1253 | - |
|
| 1254 | - /** |
|
| 1255 | - * Return the number query executed count for the current request |
|
| 1256 | - * @return int |
|
| 1257 | - */ |
|
| 1258 | - public function queryCount(){ |
|
| 1259 | - return $this->queryCount; |
|
| 1260 | - } |
|
| 1261 | - |
|
| 1262 | - /** |
|
| 1263 | - * Return the current query SQL string |
|
| 1264 | - * @return string |
|
| 1265 | - */ |
|
| 1266 | - public function getQuery(){ |
|
| 1267 | - return $this->query; |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - /** |
|
| 1271 | - * Return the application database name |
|
| 1272 | - * @return string |
|
| 1273 | - */ |
|
| 1274 | - public function getDatabaseName(){ |
|
| 1275 | - return $this->databaseName; |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - /** |
|
| 1279 | - * Return the database configuration |
|
| 1280 | - * @return array |
|
| 1281 | - */ |
|
| 1282 | - public function getDatabaseConfiguration(){ |
|
| 1283 | - return $this->config; |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - /** |
|
| 1287 | - * set the database configuration |
|
| 1288 | - * @param array $config the configuration |
|
| 1289 | - */ |
|
| 1290 | - public function setDatabaseConfiguration(array $config){ |
|
| 1291 | - $this->config = array_merge($this->config, $config); |
|
| 1292 | - $this->prefix = $this->config['prefix']; |
|
| 1293 | - $this->databaseName = $this->config['database']; |
|
| 1294 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1295 | - return $this; |
|
| 1296 | - } |
|
| 1297 | - |
|
| 1298 | - /** |
|
| 1299 | - * Return the PDO instance |
|
| 1300 | - * @return PDO |
|
| 1301 | - */ |
|
| 1302 | - public function getPdo(){ |
|
| 1303 | - return $this->pdo; |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - /** |
|
| 1307 | - * Set the PDO instance |
|
| 1308 | - * @param PDO $pdo the pdo object |
|
| 1309 | - */ |
|
| 1310 | - public function setPdo(PDO $pdo){ |
|
| 1311 | - $this->pdo = $pdo; |
|
| 1312 | - return $this; |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - |
|
| 1316 | - /** |
|
| 1317 | - * Return the Log instance |
|
| 1318 | - * @return Log |
|
| 1319 | - */ |
|
| 1320 | - public function getLogger(){ |
|
| 1321 | - return $this->logger; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - /** |
|
| 1325 | - * Set the log instance |
|
| 1326 | - * @param Log $logger the log object |
|
| 1327 | - */ |
|
| 1328 | - public function setLogger($logger){ |
|
| 1329 | - $this->logger = $logger; |
|
| 1330 | - return $this; |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - /** |
|
| 1334 | - * Return the cache instance |
|
| 1335 | - * @return CacheInterface |
|
| 1336 | - */ |
|
| 1337 | - public function getCacheInstance(){ |
|
| 1338 | - return $this->cacheInstance; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * Set the cache instance |
|
| 1343 | - * @param CacheInterface $cache the cache object |
|
| 1344 | - */ |
|
| 1345 | - public function setCacheInstance($cache){ |
|
| 1346 | - $this->cacheInstance = $cache; |
|
| 1347 | - return $this; |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * Return the benchmark instance |
|
| 1352 | - * @return Benchmark |
|
| 1353 | - */ |
|
| 1354 | - public function getBenchmark(){ |
|
| 1355 | - return $this->benchmarkInstance; |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * Set the benchmark instance |
|
| 1360 | - * @param Benchmark $cache the cache object |
|
| 1361 | - */ |
|
| 1362 | - public function setBenchmark($benchmark){ |
|
| 1363 | - $this->benchmarkInstance = $benchmark; |
|
| 1364 | - return $this; |
|
| 1365 | - } |
|
| 1366 | - |
|
| 1367 | - /** |
|
| 1368 | - * Return the data to be used for insert, update, etc. |
|
| 1369 | - * @return array |
|
| 1370 | - */ |
|
| 1371 | - public function getData(){ |
|
| 1372 | - return $this->data; |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - /** |
|
| 1376 | - * Set the data to be used for insert, update, etc. |
|
| 1377 | - * @param string $key the data key identified |
|
| 1378 | - * @param mixed $value the data value |
|
| 1379 | - * @param boolean $escape whether to escape or not the $value |
|
| 1380 | - * @return object the current Database instance |
|
| 1381 | - */ |
|
| 1382 | - public function setData($key, $value, $escape = true){ |
|
| 1383 | - $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1384 | - return $this; |
|
| 1385 | - } |
|
| 1385 | + } |
|
| 1386 | 1386 | |
| 1387 | 1387 | |
| 1388 | 1388 | /** |
| 1389 | 1389 | * Reset the database class attributs to the initail values before each query. |
| 1390 | 1390 | */ |
| 1391 | 1391 | private function reset(){ |
| 1392 | - $this->select = '*'; |
|
| 1393 | - $this->from = null; |
|
| 1394 | - $this->where = null; |
|
| 1395 | - $this->limit = null; |
|
| 1396 | - $this->orderBy = null; |
|
| 1397 | - $this->groupBy = null; |
|
| 1398 | - $this->having = null; |
|
| 1399 | - $this->join = null; |
|
| 1400 | - $this->numRows = 0; |
|
| 1401 | - $this->insertId = null; |
|
| 1402 | - $this->query = null; |
|
| 1403 | - $this->error = null; |
|
| 1404 | - $this->result = array(); |
|
| 1405 | - $this->data = array(); |
|
| 1392 | + $this->select = '*'; |
|
| 1393 | + $this->from = null; |
|
| 1394 | + $this->where = null; |
|
| 1395 | + $this->limit = null; |
|
| 1396 | + $this->orderBy = null; |
|
| 1397 | + $this->groupBy = null; |
|
| 1398 | + $this->having = null; |
|
| 1399 | + $this->join = null; |
|
| 1400 | + $this->numRows = 0; |
|
| 1401 | + $this->insertId = null; |
|
| 1402 | + $this->query = null; |
|
| 1403 | + $this->error = null; |
|
| 1404 | + $this->result = array(); |
|
| 1405 | + $this->data = array(); |
|
| 1406 | 1406 | } |
| 1407 | 1407 | |
| 1408 | 1408 | /** |
| 1409 | 1409 | * The class destructor |
| 1410 | 1410 | */ |
| 1411 | 1411 | function __destruct(){ |
| 1412 | - $this->pdo = null; |
|
| 1412 | + $this->pdo = null; |
|
| 1413 | 1413 | } |
| 1414 | 1414 | |
| 1415 | 1415 | } |
@@ -23,158 +23,158 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Database{ |
|
| 26 | + class Database { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | 31 | */ |
| 32 | - private $pdo = null; |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - private $databaseName = null; |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | 43 | */ |
| 44 | - private $select = '*'; |
|
| 44 | + private $select = '*'; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | 49 | */ |
| 50 | - private $from = null; |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $where = null; |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - private $limit = null; |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | 67 | */ |
| 68 | - private $join = null; |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | 73 | */ |
| 74 | - private $orderBy = null; |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | 79 | */ |
| 80 | - private $groupBy = null; |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | 85 | */ |
| 86 | - private $having = null; |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | 91 | */ |
| 92 | - private $numRows = 0; |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | 97 | */ |
| 98 | - private $insertId = null; |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | 103 | */ |
| 104 | - private $query = null; |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | 109 | */ |
| 110 | - private $error = null; |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | 115 | */ |
| 116 | - private $result = array(); |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | 121 | */ |
| 122 | - private $prefix = null; |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | 127 | */ |
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 128 | + private $operatorList = array('=', '!=', '<', '>', '<=', '>=', '<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | 133 | */ |
| 134 | - private $cacheTtl = 0; |
|
| 134 | + private $cacheTtl = 0; |
|
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | 139 | */ |
| 140 | - private $temporaryCacheTtl = 0; |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | 145 | */ |
| 146 | - private $queryCount = 0; |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | 151 | */ |
| 152 | - private $data = array(); |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | 157 | */ |
| 158 | - private $config = array(); |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 164 | + private $logger = null; |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | 167 | /** |
| 168 | 168 | * The cache instance |
| 169 | 169 | * @var CacheInterface |
| 170 | 170 | */ |
| 171 | - private $cacheInstance = null; |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | 174 | * The benchmark instance |
| 175 | 175 | * @var Benchmark |
| 176 | 176 | */ |
| 177 | - private $benchmarkInstance = null; |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -182,25 +182,25 @@ discard block |
||
| 182 | 182 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 183 | 183 | * @param object $logger the log instance |
| 184 | 184 | */ |
| 185 | - public function __construct($overwriteConfig = array(), Log $logger = null){ |
|
| 185 | + public function __construct($overwriteConfig = array(), Log $logger = null) { |
|
| 186 | 186 | /** |
| 187 | 187 | * instance of the Log class |
| 188 | 188 | */ |
| 189 | - if(is_object($logger)){ |
|
| 189 | + if (is_object($logger)) { |
|
| 190 | 190 | $this->logger = $logger; |
| 191 | 191 | } |
| 192 | - else{ |
|
| 193 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 192 | + else { |
|
| 193 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 194 | 194 | $this->logger->setLogger('Library::Database'); |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | $db = array(); |
| 198 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 198 | + if (file_exists(CONFIG_PATH . 'database.php')) { |
|
| 199 | 199 | //here don't use require_once because somewhere user can create database instance directly |
| 200 | 200 | require CONFIG_PATH . 'database.php'; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - if(! empty($overwriteConfig)){ |
|
| 203 | + if (!empty($overwriteConfig)) { |
|
| 204 | 204 | $db = array_merge($db, $overwriteConfig); |
| 205 | 205 | } |
| 206 | 206 | $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
@@ -212,11 +212,11 @@ discard block |
||
| 212 | 212 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
| 213 | 213 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
| 214 | 214 | $port = ''; |
| 215 | - if(strstr($config['hostname'], ':')){ |
|
| 215 | + if (strstr($config['hostname'], ':')) { |
|
| 216 | 216 | $p = explode(':', $config['hostname']); |
| 217 | 217 | $port = isset($p[1]) ? $p[1] : ''; |
| 218 | 218 | } |
| 219 | - $config['port'] = $port; |
|
| 219 | + $config['port'] = $port; |
|
| 220 | 220 | |
| 221 | 221 | $this->setDatabaseConfiguration($config); |
| 222 | 222 | |
@@ -227,20 +227,20 @@ discard block |
||
| 227 | 227 | * This is used to connect to database |
| 228 | 228 | * @return bool |
| 229 | 229 | */ |
| 230 | - public function connect(){ |
|
| 230 | + public function connect() { |
|
| 231 | 231 | $config = $this->getDatabaseConfiguration(); |
| 232 | - if(! empty($config)){ |
|
| 233 | - try{ |
|
| 232 | + if (!empty($config)) { |
|
| 233 | + try { |
|
| 234 | 234 | $dsn = ''; |
| 235 | - if($config['driver'] == 'mysql' || $config['driver'] == '' || $config['driver'] == 'pgsql'){ |
|
| 235 | + if ($config['driver'] == 'mysql' || $config['driver'] == '' || $config['driver'] == 'pgsql') { |
|
| 236 | 236 | $dsn = $config['driver'] . ':host=' . $config['hostname'] . ';' |
| 237 | 237 | . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
| 238 | 238 | . 'dbname=' . $config['database']; |
| 239 | 239 | } |
| 240 | - else if ($config['driver'] == 'sqlite'){ |
|
| 240 | + else if ($config['driver'] == 'sqlite') { |
|
| 241 | 241 | $dsn = 'sqlite:' . $config['database']; |
| 242 | 242 | } |
| 243 | - else if($config['driver'] == 'oracle'){ |
|
| 243 | + else if ($config['driver'] == 'oracle') { |
|
| 244 | 244 | $dsn = 'oci:dbname=' . $config['host'] . '/' . $config['database']; |
| 245 | 245 | } |
| 246 | 246 | |
@@ -250,13 +250,13 @@ discard block |
||
| 250 | 250 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 251 | 251 | return true; |
| 252 | 252 | } |
| 253 | - catch (PDOException $e){ |
|
| 253 | + catch (PDOException $e) { |
|
| 254 | 254 | $this->logger->fatal($e->getMessage()); |
| 255 | 255 | show_error('Cannot connect to Database.'); |
| 256 | 256 | return false; |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | - else{ |
|
| 259 | + else { |
|
| 260 | 260 | show_error('Database configuration is not set.'); |
| 261 | 261 | return false; |
| 262 | 262 | } |
@@ -267,15 +267,15 @@ discard block |
||
| 267 | 267 | * @param string|array $table the table name or array of table list |
| 268 | 268 | * @return object the current Database instance |
| 269 | 269 | */ |
| 270 | - public function from($table){ |
|
| 271 | - if(is_array($table)){ |
|
| 270 | + public function from($table) { |
|
| 271 | + if (is_array($table)) { |
|
| 272 | 272 | $froms = ''; |
| 273 | - foreach($table as $key){ |
|
| 273 | + foreach ($table as $key) { |
|
| 274 | 274 | $froms .= $this->prefix . $key . ', '; |
| 275 | 275 | } |
| 276 | 276 | $this->from = rtrim($froms, ', '); |
| 277 | 277 | } |
| 278 | - else{ |
|
| 278 | + else { |
|
| 279 | 279 | $this->from = $this->prefix . $table; |
| 280 | 280 | } |
| 281 | 281 | return $this; |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | * @param string|array $fields the field name or array of field list |
| 287 | 287 | * @return object the current Database instance |
| 288 | 288 | */ |
| 289 | - public function select($fields){ |
|
| 289 | + public function select($fields) { |
|
| 290 | 290 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
| 291 | 291 | $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
| 292 | 292 | return $this; |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | * @param string $field the field name to distinct |
| 298 | 298 | * @return object the current Database instance |
| 299 | 299 | */ |
| 300 | - public function distinct($field){ |
|
| 300 | + public function distinct($field) { |
|
| 301 | 301 | $distinct = ' DISTINCT ' . $field; |
| 302 | 302 | $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
| 303 | 303 | |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @param string $name if is not null represent the alias used for this field in the result |
| 311 | 311 | * @return object the current Database instance |
| 312 | 312 | */ |
| 313 | - public function max($field, $name = null){ |
|
| 313 | + public function max($field, $name = null) { |
|
| 314 | 314 | $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 315 | 315 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 316 | 316 | return $this; |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | * @param string $name if is not null represent the alias used for this field in the result |
| 323 | 323 | * @return object the current Database instance |
| 324 | 324 | */ |
| 325 | - public function min($field, $name = null){ |
|
| 325 | + public function min($field, $name = null) { |
|
| 326 | 326 | $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 327 | 327 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 328 | 328 | return $this; |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | * @param string $name if is not null represent the alias used for this field in the result |
| 335 | 335 | * @return object the current Database instance |
| 336 | 336 | */ |
| 337 | - public function sum($field, $name = null){ |
|
| 337 | + public function sum($field, $name = null) { |
|
| 338 | 338 | $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 339 | 339 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 340 | 340 | return $this; |
@@ -346,7 +346,7 @@ discard block |
||
| 346 | 346 | * @param string $name if is not null represent the alias used for this field in the result |
| 347 | 347 | * @return object the current Database instance |
| 348 | 348 | */ |
| 349 | - public function count($field = '*', $name = null){ |
|
| 349 | + public function count($field = '*', $name = null) { |
|
| 350 | 350 | $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 351 | 351 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 352 | 352 | return $this; |
@@ -358,7 +358,7 @@ discard block |
||
| 358 | 358 | * @param string $name if is not null represent the alias used for this field in the result |
| 359 | 359 | * @return object the current Database instance |
| 360 | 360 | */ |
| 361 | - public function avg($field, $name = null){ |
|
| 361 | + public function avg($field, $name = null) { |
|
| 362 | 362 | $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 363 | 363 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 364 | 364 | return $this; |
@@ -373,16 +373,16 @@ discard block |
||
| 373 | 373 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
| 374 | 374 | * @return object the current Database instance |
| 375 | 375 | */ |
| 376 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 376 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
| 377 | 377 | $on = $field1; |
| 378 | 378 | $table = $this->prefix . $table; |
| 379 | - if(! is_null($op)){ |
|
| 380 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 379 | + if (!is_null($op)) { |
|
| 380 | + $on = (!in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 381 | 381 | } |
| 382 | - if (is_null($this->join)){ |
|
| 382 | + if (is_null($this->join)) { |
|
| 383 | 383 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 384 | 384 | } |
| 385 | - else{ |
|
| 385 | + else { |
|
| 386 | 386 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 387 | 387 | } |
| 388 | 388 | return $this; |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | * @see Database::join() |
| 394 | 394 | * @return object the current Database instance |
| 395 | 395 | */ |
| 396 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 396 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
| 397 | 397 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
| 398 | 398 | } |
| 399 | 399 | |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | * @see Database::join() |
| 403 | 403 | * @return object the current Database instance |
| 404 | 404 | */ |
| 405 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 405 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
| 406 | 406 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
| 407 | 407 | } |
| 408 | 408 | |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | * @see Database::join() |
| 412 | 412 | * @return object the current Database instance |
| 413 | 413 | */ |
| 414 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 414 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
| 415 | 415 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
| 416 | 416 | } |
| 417 | 417 | |
@@ -420,7 +420,7 @@ discard block |
||
| 420 | 420 | * @see Database::join() |
| 421 | 421 | * @return object the current Database instance |
| 422 | 422 | */ |
| 423 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 423 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 424 | 424 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
| 425 | 425 | } |
| 426 | 426 | |
@@ -429,7 +429,7 @@ discard block |
||
| 429 | 429 | * @see Database::join() |
| 430 | 430 | * @return object the current Database instance |
| 431 | 431 | */ |
| 432 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 432 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 433 | 433 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
| 434 | 434 | } |
| 435 | 435 | |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | * @see Database::join() |
| 439 | 439 | * @return object the current Database instance |
| 440 | 440 | */ |
| 441 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 441 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 442 | 442 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
| 443 | 443 | } |
| 444 | 444 | |
@@ -448,18 +448,18 @@ discard block |
||
| 448 | 448 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 449 | 449 | * @return object the current Database instance |
| 450 | 450 | */ |
| 451 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 452 | - if(is_array($field)){ |
|
| 453 | - foreach($field as $f){ |
|
| 451 | + public function whereIsNull($field, $andOr = 'AND') { |
|
| 452 | + if (is_array($field)) { |
|
| 453 | + foreach ($field as $f) { |
|
| 454 | 454 | $this->whereIsNull($f, $andOr); |
| 455 | 455 | } |
| 456 | 456 | } |
| 457 | - else{ |
|
| 458 | - if (! $this->where){ |
|
| 459 | - $this->where = $field.' IS NULL '; |
|
| 457 | + else { |
|
| 458 | + if (!$this->where) { |
|
| 459 | + $this->where = $field . ' IS NULL '; |
|
| 460 | 460 | } |
| 461 | - else{ |
|
| 462 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 461 | + else { |
|
| 462 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NULL '; |
|
| 463 | 463 | } |
| 464 | 464 | } |
| 465 | 465 | return $this; |
@@ -471,18 +471,18 @@ discard block |
||
| 471 | 471 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 472 | 472 | * @return object the current Database instance |
| 473 | 473 | */ |
| 474 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 475 | - if(is_array($field)){ |
|
| 476 | - foreach($field as $f){ |
|
| 474 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
| 475 | + if (is_array($field)) { |
|
| 476 | + foreach ($field as $f) { |
|
| 477 | 477 | $this->whereIsNotNull($f, $andOr); |
| 478 | 478 | } |
| 479 | 479 | } |
| 480 | - else{ |
|
| 481 | - if (! $this->where){ |
|
| 482 | - $this->where = $field.' IS NOT NULL '; |
|
| 480 | + else { |
|
| 481 | + if (!$this->where) { |
|
| 482 | + $this->where = $field . ' IS NOT NULL '; |
|
| 483 | 483 | } |
| 484 | - else{ |
|
| 485 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 484 | + else { |
|
| 485 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NOT NULL '; |
|
| 486 | 486 | } |
| 487 | 487 | } |
| 488 | 488 | return $this; |
@@ -498,24 +498,24 @@ discard block |
||
| 498 | 498 | * @param boolean $escape whether to escape or not the $val |
| 499 | 499 | * @return object the current Database instance |
| 500 | 500 | */ |
| 501 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 502 | - if (is_array($where)){ |
|
| 501 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
| 502 | + if (is_array($where)) { |
|
| 503 | 503 | $_where = array(); |
| 504 | - foreach ($where as $column => $data){ |
|
| 505 | - if(is_null($data)){ |
|
| 504 | + foreach ($where as $column => $data) { |
|
| 505 | + if (is_null($data)) { |
|
| 506 | 506 | $data = ''; |
| 507 | 507 | } |
| 508 | 508 | $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
| 509 | 509 | } |
| 510 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 510 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
| 511 | 511 | } |
| 512 | - else{ |
|
| 513 | - if(is_array($op)){ |
|
| 512 | + else { |
|
| 513 | + if (is_array($op)) { |
|
| 514 | 514 | $x = explode('?', $where); |
| 515 | 515 | $w = ''; |
| 516 | - foreach($x as $k => $v){ |
|
| 517 | - if(! empty($v)){ |
|
| 518 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 516 | + foreach ($x as $k => $v) { |
|
| 517 | + if (!empty($v)) { |
|
| 518 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 519 | 519 | $op[$k] = ''; |
| 520 | 520 | } |
| 521 | 521 | $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -523,28 +523,28 @@ discard block |
||
| 523 | 523 | } |
| 524 | 524 | $where = $w; |
| 525 | 525 | } |
| 526 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 527 | - if(is_null($op)){ |
|
| 526 | + else if (!in_array((string) $op, $this->operatorList)) { |
|
| 527 | + if (is_null($op)) { |
|
| 528 | 528 | $op = ''; |
| 529 | 529 | } |
| 530 | 530 | $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 531 | 531 | } |
| 532 | - else{ |
|
| 533 | - if(is_null($val)){ |
|
| 532 | + else { |
|
| 533 | + if (is_null($val)) { |
|
| 534 | 534 | $val = ''; |
| 535 | 535 | } |
| 536 | 536 | $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
| 537 | 537 | } |
| 538 | 538 | } |
| 539 | - if (is_null($this->where)){ |
|
| 539 | + if (is_null($this->where)) { |
|
| 540 | 540 | $this->where = $where; |
| 541 | 541 | } |
| 542 | - else{ |
|
| 543 | - if(substr($this->where, -1) == '('){ |
|
| 542 | + else { |
|
| 543 | + if (substr($this->where, -1) == '(') { |
|
| 544 | 544 | $this->where = $this->where . ' ' . $where; |
| 545 | 545 | } |
| 546 | - else{ |
|
| 547 | - $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 546 | + else { |
|
| 547 | + $this->where = $this->where . ' ' . $andOr . ' ' . $where; |
|
| 548 | 548 | } |
| 549 | 549 | } |
| 550 | 550 | return $this; |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | * @see Database::where() |
| 556 | 556 | * @return object the current Database instance |
| 557 | 557 | */ |
| 558 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 558 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
| 559 | 559 | return $this->where($where, $op, $val, '', 'OR', $escape); |
| 560 | 560 | } |
| 561 | 561 | |
@@ -565,7 +565,7 @@ discard block |
||
| 565 | 565 | * @see Database::where() |
| 566 | 566 | * @return object the current Database instance |
| 567 | 567 | */ |
| 568 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 568 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
| 569 | 569 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
| 570 | 570 | } |
| 571 | 571 | |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | * @see Database::where() |
| 575 | 575 | * @return object the current Database instance |
| 576 | 576 | */ |
| 577 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 577 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
| 578 | 578 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
| 579 | 579 | } |
| 580 | 580 | |
@@ -584,15 +584,15 @@ discard block |
||
| 584 | 584 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
| 585 | 585 | * @return object the current Database instance |
| 586 | 586 | */ |
| 587 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 588 | - if (is_null($this->where)){ |
|
| 587 | + public function groupStart($type = '', $andOr = ' AND') { |
|
| 588 | + if (is_null($this->where)) { |
|
| 589 | 589 | $this->where = $type . ' ('; |
| 590 | 590 | } |
| 591 | - else{ |
|
| 592 | - if(substr($this->where, -1) == '('){ |
|
| 591 | + else { |
|
| 592 | + if (substr($this->where, -1) == '(') { |
|
| 593 | 593 | $this->where .= $type . ' ('; |
| 594 | 594 | } |
| 595 | - else{ |
|
| 595 | + else { |
|
| 596 | 596 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 597 | 597 | } |
| 598 | 598 | } |
@@ -604,7 +604,7 @@ discard block |
||
| 604 | 604 | * @see Database::groupStart() |
| 605 | 605 | * @return object the current Database instance |
| 606 | 606 | */ |
| 607 | - public function notGroupStart(){ |
|
| 607 | + public function notGroupStart() { |
|
| 608 | 608 | return $this->groupStart('NOT'); |
| 609 | 609 | } |
| 610 | 610 | |
@@ -613,7 +613,7 @@ discard block |
||
| 613 | 613 | * @see Database::groupStart() |
| 614 | 614 | * @return object the current Database instance |
| 615 | 615 | */ |
| 616 | - public function orGroupStart(){ |
|
| 616 | + public function orGroupStart() { |
|
| 617 | 617 | return $this->groupStart('', ' OR'); |
| 618 | 618 | } |
| 619 | 619 | |
@@ -622,7 +622,7 @@ discard block |
||
| 622 | 622 | * @see Database::groupStart() |
| 623 | 623 | * @return object the current Database instance |
| 624 | 624 | */ |
| 625 | - public function orNotGroupStart(){ |
|
| 625 | + public function orNotGroupStart() { |
|
| 626 | 626 | return $this->groupStart('NOT', ' OR'); |
| 627 | 627 | } |
| 628 | 628 | |
@@ -630,7 +630,7 @@ discard block |
||
| 630 | 630 | * Close the parenthesis for the grouped SQL |
| 631 | 631 | * @return object the current Database instance |
| 632 | 632 | */ |
| 633 | - public function groupEnd(){ |
|
| 633 | + public function groupEnd() { |
|
| 634 | 634 | $this->where .= ')'; |
| 635 | 635 | return $this; |
| 636 | 636 | } |
@@ -644,25 +644,25 @@ discard block |
||
| 644 | 644 | * @param boolean $escape whether to escape or not the values |
| 645 | 645 | * @return object the current Database instance |
| 646 | 646 | */ |
| 647 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 648 | - if (is_array($keys)){ |
|
| 647 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
| 648 | + if (is_array($keys)) { |
|
| 649 | 649 | $_keys = array(); |
| 650 | - foreach ($keys as $k => $v){ |
|
| 651 | - if(is_null($v)){ |
|
| 650 | + foreach ($keys as $k => $v) { |
|
| 651 | + if (is_null($v)) { |
|
| 652 | 652 | $v = ''; |
| 653 | 653 | } |
| 654 | 654 | $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
| 655 | 655 | } |
| 656 | 656 | $keys = implode(', ', $_keys); |
| 657 | - if (is_null($this->where)){ |
|
| 657 | + if (is_null($this->where)) { |
|
| 658 | 658 | $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
| 659 | 659 | } |
| 660 | - else{ |
|
| 661 | - if(substr($this->where, -1) == '('){ |
|
| 662 | - $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 660 | + else { |
|
| 661 | + if (substr($this->where, -1) == '(') { |
|
| 662 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 663 | 663 | } |
| 664 | - else{ |
|
| 665 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 664 | + else { |
|
| 665 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 666 | 666 | } |
| 667 | 667 | } |
| 668 | 668 | } |
@@ -674,7 +674,7 @@ discard block |
||
| 674 | 674 | * @see Database::in() |
| 675 | 675 | * @return object the current Database instance |
| 676 | 676 | */ |
| 677 | - public function notIn($field, array $keys, $escape = true){ |
|
| 677 | + public function notIn($field, array $keys, $escape = true) { |
|
| 678 | 678 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
| 679 | 679 | } |
| 680 | 680 | |
@@ -683,7 +683,7 @@ discard block |
||
| 683 | 683 | * @see Database::in() |
| 684 | 684 | * @return object the current Database instance |
| 685 | 685 | */ |
| 686 | - public function orIn($field, array $keys, $escape = true){ |
|
| 686 | + public function orIn($field, array $keys, $escape = true) { |
|
| 687 | 687 | return $this->in($field, $keys, '', 'OR', $escape); |
| 688 | 688 | } |
| 689 | 689 | |
@@ -692,7 +692,7 @@ discard block |
||
| 692 | 692 | * @see Database::in() |
| 693 | 693 | * @return object the current Database instance |
| 694 | 694 | */ |
| 695 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 695 | + public function orNotIn($field, array $keys, $escape = true) { |
|
| 696 | 696 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
| 697 | 697 | } |
| 698 | 698 | |
@@ -706,21 +706,21 @@ discard block |
||
| 706 | 706 | * @param boolean $escape whether to escape or not the values |
| 707 | 707 | * @return object the current Database instance |
| 708 | 708 | */ |
| 709 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 710 | - if(is_null($value1)){ |
|
| 709 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
| 710 | + if (is_null($value1)) { |
|
| 711 | 711 | $value1 = ''; |
| 712 | 712 | } |
| 713 | - if(is_null($value2)){ |
|
| 713 | + if (is_null($value2)) { |
|
| 714 | 714 | $value2 = ''; |
| 715 | 715 | } |
| 716 | - if (is_null($this->where)){ |
|
| 716 | + if (is_null($this->where)) { |
|
| 717 | 717 | $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 718 | 718 | } |
| 719 | - else{ |
|
| 720 | - if(substr($this->where, -1) == '('){ |
|
| 719 | + else { |
|
| 720 | + if (substr($this->where, -1) == '(') { |
|
| 721 | 721 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 722 | 722 | } |
| 723 | - else{ |
|
| 723 | + else { |
|
| 724 | 724 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 725 | 725 | } |
| 726 | 726 | } |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | * @see Database::between() |
| 733 | 733 | * @return object the current Database instance |
| 734 | 734 | */ |
| 735 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 735 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
| 736 | 736 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
| 737 | 737 | } |
| 738 | 738 | |
@@ -741,7 +741,7 @@ discard block |
||
| 741 | 741 | * @see Database::between() |
| 742 | 742 | * @return object the current Database instance |
| 743 | 743 | */ |
| 744 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 744 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
| 745 | 745 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
| 746 | 746 | } |
| 747 | 747 | |
@@ -750,7 +750,7 @@ discard block |
||
| 750 | 750 | * @see Database::between() |
| 751 | 751 | * @return object the current Database instance |
| 752 | 752 | */ |
| 753 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 753 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
| 754 | 754 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
| 755 | 755 | } |
| 756 | 756 | |
@@ -763,20 +763,20 @@ discard block |
||
| 763 | 763 | * @param boolean $escape whether to escape or not the values |
| 764 | 764 | * @return object the current Database instance |
| 765 | 765 | */ |
| 766 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 767 | - if(is_null($data)){ |
|
| 766 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
| 767 | + if (is_null($data)) { |
|
| 768 | 768 | $data = ''; |
| 769 | 769 | } |
| 770 | 770 | $like = $escape ? $this->escape($data) : $data; |
| 771 | - if (is_null($this->where)){ |
|
| 771 | + if (is_null($this->where)) { |
|
| 772 | 772 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 773 | 773 | } |
| 774 | - else{ |
|
| 775 | - if(substr($this->where, -1) == '('){ |
|
| 774 | + else { |
|
| 775 | + if (substr($this->where, -1) == '(') { |
|
| 776 | 776 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 777 | 777 | } |
| 778 | - else{ |
|
| 779 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 778 | + else { |
|
| 779 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 780 | 780 | } |
| 781 | 781 | } |
| 782 | 782 | return $this; |
@@ -787,7 +787,7 @@ discard block |
||
| 787 | 787 | * @see Database::like() |
| 788 | 788 | * @return object the current Database instance |
| 789 | 789 | */ |
| 790 | - public function orLike($field, $data, $escape = true){ |
|
| 790 | + public function orLike($field, $data, $escape = true) { |
|
| 791 | 791 | return $this->like($field, $data, '', 'OR', $escape); |
| 792 | 792 | } |
| 793 | 793 | |
@@ -796,7 +796,7 @@ discard block |
||
| 796 | 796 | * @see Database::like() |
| 797 | 797 | * @return object the current Database instance |
| 798 | 798 | */ |
| 799 | - public function notLike($field, $data, $escape = true){ |
|
| 799 | + public function notLike($field, $data, $escape = true) { |
|
| 800 | 800 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
| 801 | 801 | } |
| 802 | 802 | |
@@ -805,7 +805,7 @@ discard block |
||
| 805 | 805 | * @see Database::like() |
| 806 | 806 | * @return object the current Database instance |
| 807 | 807 | */ |
| 808 | - public function orNotLike($field, $data, $escape = true){ |
|
| 808 | + public function orNotLike($field, $data, $escape = true) { |
|
| 809 | 809 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
| 810 | 810 | } |
| 811 | 811 | |
@@ -816,14 +816,14 @@ discard block |
||
| 816 | 816 | * @param int $limitEnd the limit count |
| 817 | 817 | * @return object the current Database instance |
| 818 | 818 | */ |
| 819 | - public function limit($limit, $limitEnd = null){ |
|
| 820 | - if(is_null($limit)){ |
|
| 819 | + public function limit($limit, $limitEnd = null) { |
|
| 820 | + if (is_null($limit)) { |
|
| 821 | 821 | return; |
| 822 | 822 | } |
| 823 | - if (! is_null($limitEnd)){ |
|
| 823 | + if (!is_null($limitEnd)) { |
|
| 824 | 824 | $this->limit = $limit . ', ' . $limitEnd; |
| 825 | 825 | } |
| 826 | - else{ |
|
| 826 | + else { |
|
| 827 | 827 | $this->limit = $limit; |
| 828 | 828 | } |
| 829 | 829 | return $this; |
@@ -835,16 +835,16 @@ discard block |
||
| 835 | 835 | * @param string $orderDir the order direction (ASC or DESC) |
| 836 | 836 | * @return object the current Database instance |
| 837 | 837 | */ |
| 838 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 839 | - if (! is_null($orderDir)){ |
|
| 840 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 838 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
| 839 | + if (!is_null($orderDir)) { |
|
| 840 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 841 | 841 | } |
| 842 | - else{ |
|
| 843 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 844 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 842 | + else { |
|
| 843 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
| 844 | + $this->orderBy = !$this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 845 | 845 | } |
| 846 | - else{ |
|
| 847 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 846 | + else { |
|
| 847 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 848 | 848 | } |
| 849 | 849 | } |
| 850 | 850 | return $this; |
@@ -855,11 +855,11 @@ discard block |
||
| 855 | 855 | * @param string|array $field the field name used or array of field list |
| 856 | 856 | * @return object the current Database instance |
| 857 | 857 | */ |
| 858 | - public function groupBy($field){ |
|
| 859 | - if(is_array($field)){ |
|
| 858 | + public function groupBy($field) { |
|
| 859 | + if (is_array($field)) { |
|
| 860 | 860 | $this->groupBy = implode(', ', $field); |
| 861 | 861 | } |
| 862 | - else{ |
|
| 862 | + else { |
|
| 863 | 863 | $this->groupBy = $field; |
| 864 | 864 | } |
| 865 | 865 | return $this; |
@@ -873,13 +873,13 @@ discard block |
||
| 873 | 873 | * @param boolean $escape whether to escape or not the values |
| 874 | 874 | * @return object the current Database instance |
| 875 | 875 | */ |
| 876 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 877 | - if(is_array($op)){ |
|
| 876 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
| 877 | + if (is_array($op)) { |
|
| 878 | 878 | $x = explode('?', $field); |
| 879 | 879 | $w = ''; |
| 880 | - foreach($x as $k => $v){ |
|
| 881 | - if(!empty($v)){ |
|
| 882 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 880 | + foreach ($x as $k => $v) { |
|
| 881 | + if (!empty($v)) { |
|
| 882 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 883 | 883 | $op[$k] = ''; |
| 884 | 884 | } |
| 885 | 885 | $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -887,14 +887,14 @@ discard block |
||
| 887 | 887 | } |
| 888 | 888 | $this->having = $w; |
| 889 | 889 | } |
| 890 | - else if (! in_array($op, $this->operatorList)){ |
|
| 891 | - if(is_null($op)){ |
|
| 890 | + else if (!in_array($op, $this->operatorList)) { |
|
| 891 | + if (is_null($op)) { |
|
| 892 | 892 | $op = ''; |
| 893 | 893 | } |
| 894 | 894 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 895 | 895 | } |
| 896 | - else{ |
|
| 897 | - if(is_null($val)){ |
|
| 896 | + else { |
|
| 897 | + if (is_null($val)) { |
|
| 898 | 898 | $val = ''; |
| 899 | 899 | } |
| 900 | 900 | $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
@@ -906,7 +906,7 @@ discard block |
||
| 906 | 906 | * Return the number of rows returned by the current query |
| 907 | 907 | * @return int |
| 908 | 908 | */ |
| 909 | - public function numRows(){ |
|
| 909 | + public function numRows() { |
|
| 910 | 910 | return $this->numRows; |
| 911 | 911 | } |
| 912 | 912 | |
@@ -914,15 +914,15 @@ discard block |
||
| 914 | 914 | * Return the last insert id value |
| 915 | 915 | * @return mixed |
| 916 | 916 | */ |
| 917 | - public function insertId(){ |
|
| 917 | + public function insertId() { |
|
| 918 | 918 | return $this->insertId; |
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | /** |
| 922 | 922 | * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
| 923 | 923 | */ |
| 924 | - public function error(){ |
|
| 925 | - if($this->error){ |
|
| 924 | + public function error() { |
|
| 925 | + if ($this->error) { |
|
| 926 | 926 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 927 | 927 | } |
| 928 | 928 | } |
@@ -933,14 +933,14 @@ discard block |
||
| 933 | 933 | * If is string will determine the result type "array" or "object" |
| 934 | 934 | * @return mixed the query SQL string or the record result |
| 935 | 935 | */ |
| 936 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 936 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 937 | 937 | $this->limit = 1; |
| 938 | 938 | $query = $this->getAll(true); |
| 939 | - if($returnSQLQueryOrResultType === true){ |
|
| 939 | + if ($returnSQLQueryOrResultType === true) { |
|
| 940 | 940 | return $query; |
| 941 | 941 | } |
| 942 | - else{ |
|
| 943 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 942 | + else { |
|
| 943 | + return $this->query($query, false, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 944 | 944 | } |
| 945 | 945 | } |
| 946 | 946 | |
@@ -950,37 +950,37 @@ discard block |
||
| 950 | 950 | * If is string will determine the result type "array" or "object" |
| 951 | 951 | * @return mixed the query SQL string or the record result |
| 952 | 952 | */ |
| 953 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 953 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 954 | 954 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 955 | - if (! is_null($this->join)){ |
|
| 955 | + if (!is_null($this->join)) { |
|
| 956 | 956 | $query .= $this->join; |
| 957 | 957 | } |
| 958 | 958 | |
| 959 | - if (! is_null($this->where)){ |
|
| 959 | + if (!is_null($this->where)) { |
|
| 960 | 960 | $query .= ' WHERE ' . $this->where; |
| 961 | 961 | } |
| 962 | 962 | |
| 963 | - if (! is_null($this->groupBy)){ |
|
| 963 | + if (!is_null($this->groupBy)) { |
|
| 964 | 964 | $query .= ' GROUP BY ' . $this->groupBy; |
| 965 | 965 | } |
| 966 | 966 | |
| 967 | - if (! is_null($this->having)){ |
|
| 967 | + if (!is_null($this->having)) { |
|
| 968 | 968 | $query .= ' HAVING ' . $this->having; |
| 969 | 969 | } |
| 970 | 970 | |
| 971 | - if (! is_null($this->orderBy)){ |
|
| 971 | + if (!is_null($this->orderBy)) { |
|
| 972 | 972 | $query .= ' ORDER BY ' . $this->orderBy; |
| 973 | 973 | } |
| 974 | 974 | |
| 975 | - if(! is_null($this->limit)){ |
|
| 975 | + if (!is_null($this->limit)) { |
|
| 976 | 976 | $query .= ' LIMIT ' . $this->limit; |
| 977 | 977 | } |
| 978 | 978 | |
| 979 | - if($returnSQLQueryOrResultType === true){ |
|
| 979 | + if ($returnSQLQueryOrResultType === true) { |
|
| 980 | 980 | return $query; |
| 981 | 981 | } |
| 982 | - else{ |
|
| 983 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 982 | + else { |
|
| 983 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 984 | 984 | } |
| 985 | 985 | } |
| 986 | 986 | |
@@ -990,15 +990,15 @@ discard block |
||
| 990 | 990 | * @param boolean $escape whether to escape or not the values |
| 991 | 991 | * @return mixed the insert id of the new record or null |
| 992 | 992 | */ |
| 993 | - public function insert($data = array(), $escape = true){ |
|
| 993 | + public function insert($data = array(), $escape = true) { |
|
| 994 | 994 | $column = array(); |
| 995 | 995 | $val = array(); |
| 996 | - if(! $data && $this->getData()){ |
|
| 996 | + if (!$data && $this->getData()) { |
|
| 997 | 997 | $columns = array_keys($this->getData()); |
| 998 | 998 | $column = implode(',', $columns); |
| 999 | 999 | $val = implode(', ', $this->getData()); |
| 1000 | 1000 | } |
| 1001 | - else{ |
|
| 1001 | + else { |
|
| 1002 | 1002 | $columns = array_keys($data); |
| 1003 | 1003 | $column = implode(',', $columns); |
| 1004 | 1004 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -1007,14 +1007,14 @@ discard block |
||
| 1007 | 1007 | $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
| 1008 | 1008 | $query = $this->query($query); |
| 1009 | 1009 | |
| 1010 | - if ($query){ |
|
| 1011 | - if(! $this->pdo){ |
|
| 1010 | + if ($query) { |
|
| 1011 | + if (!$this->pdo) { |
|
| 1012 | 1012 | $this->connect(); |
| 1013 | 1013 | } |
| 1014 | 1014 | $this->insertId = $this->pdo->lastInsertId(); |
| 1015 | 1015 | return $this->insertId(); |
| 1016 | 1016 | } |
| 1017 | - else{ |
|
| 1017 | + else { |
|
| 1018 | 1018 | return false; |
| 1019 | 1019 | } |
| 1020 | 1020 | } |
@@ -1025,29 +1025,29 @@ discard block |
||
| 1025 | 1025 | * @param boolean $escape whether to escape or not the values |
| 1026 | 1026 | * @return mixed the update status |
| 1027 | 1027 | */ |
| 1028 | - public function update($data = array(), $escape = true){ |
|
| 1028 | + public function update($data = array(), $escape = true) { |
|
| 1029 | 1029 | $query = 'UPDATE ' . $this->from . ' SET '; |
| 1030 | 1030 | $values = array(); |
| 1031 | - if(! $data && $this->getData()){ |
|
| 1032 | - foreach ($this->getData() as $column => $val){ |
|
| 1031 | + if (!$data && $this->getData()) { |
|
| 1032 | + foreach ($this->getData() as $column => $val) { |
|
| 1033 | 1033 | $values[] = $column . ' = ' . $val; |
| 1034 | 1034 | } |
| 1035 | 1035 | } |
| 1036 | - else{ |
|
| 1037 | - foreach ($data as $column => $val){ |
|
| 1036 | + else { |
|
| 1037 | + foreach ($data as $column => $val) { |
|
| 1038 | 1038 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1039 | 1039 | } |
| 1040 | 1040 | } |
| 1041 | 1041 | $query .= (is_array($data) ? implode(', ', $values) : $data); |
| 1042 | - if (! is_null($this->where)){ |
|
| 1042 | + if (!is_null($this->where)) { |
|
| 1043 | 1043 | $query .= ' WHERE ' . $this->where; |
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | - if (! is_null($this->orderBy)){ |
|
| 1046 | + if (!is_null($this->orderBy)) { |
|
| 1047 | 1047 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | - if (! is_null($this->limit)){ |
|
| 1050 | + if (!is_null($this->limit)) { |
|
| 1051 | 1051 | $query .= ' LIMIT ' . $this->limit; |
| 1052 | 1052 | } |
| 1053 | 1053 | return $this->query($query); |
@@ -1057,22 +1057,22 @@ discard block |
||
| 1057 | 1057 | * Delete the record in database |
| 1058 | 1058 | * @return mixed the delete status |
| 1059 | 1059 | */ |
| 1060 | - public function delete(){ |
|
| 1060 | + public function delete() { |
|
| 1061 | 1061 | $query = 'DELETE FROM ' . $this->from; |
| 1062 | 1062 | |
| 1063 | - if (! is_null($this->where)){ |
|
| 1063 | + if (!is_null($this->where)) { |
|
| 1064 | 1064 | $query .= ' WHERE ' . $this->where; |
| 1065 | 1065 | } |
| 1066 | 1066 | |
| 1067 | - if (! is_null($this->orderBy)){ |
|
| 1067 | + if (!is_null($this->orderBy)) { |
|
| 1068 | 1068 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1069 | 1069 | } |
| 1070 | 1070 | |
| 1071 | - if (! is_null($this->limit)){ |
|
| 1071 | + if (!is_null($this->limit)) { |
|
| 1072 | 1072 | $query .= ' LIMIT ' . $this->limit; |
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | - if($query == 'DELETE FROM ' . $this->from){ |
|
| 1075 | + if ($query == 'DELETE FROM ' . $this->from) { |
|
| 1076 | 1076 | $query = 'TRUNCATE TABLE ' . $this->from; |
| 1077 | 1077 | } |
| 1078 | 1078 | return $this->query($query); |
@@ -1085,13 +1085,13 @@ discard block |
||
| 1085 | 1085 | * @param boolean $array return the result as array |
| 1086 | 1086 | * @return mixed the query result |
| 1087 | 1087 | */ |
| 1088 | - public function query($query, $all = true, $array = false){ |
|
| 1088 | + public function query($query, $all = true, $array = false) { |
|
| 1089 | 1089 | $this->reset(); |
| 1090 | - if(is_array($all)){ |
|
| 1090 | + if (is_array($all)) { |
|
| 1091 | 1091 | $x = explode('?', $query); |
| 1092 | 1092 | $q = ''; |
| 1093 | - foreach($x as $k => $v){ |
|
| 1094 | - if(! empty($v)){ |
|
| 1093 | + foreach ($x as $k => $v) { |
|
| 1094 | + if (!empty($v)) { |
|
| 1095 | 1095 | $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
| 1096 | 1096 | } |
| 1097 | 1097 | } |
@@ -1100,7 +1100,7 @@ discard block |
||
| 1100 | 1100 | |
| 1101 | 1101 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 1102 | 1102 | $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
| 1103 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1103 | + $this->logger->info('Execute SQL query [' . $this->query . '], return type: ' . ($array ? 'ARRAY' : 'OBJECT') . ', return as list: ' . ($all ? 'YES' : 'NO')); |
|
| 1104 | 1104 | //cache expire time |
| 1105 | 1105 | $cacheExpire = $this->temporaryCacheTtl; |
| 1106 | 1106 | |
@@ -1122,34 +1122,34 @@ discard block |
||
| 1122 | 1122 | //if can use cache feature for this query |
| 1123 | 1123 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1124 | 1124 | |
| 1125 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1125 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1126 | 1126 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 1127 | 1127 | $cacheKey = md5($query . $all . $array); |
| 1128 | - if(is_object($this->cacheInstance)){ |
|
| 1128 | + if (is_object($this->cacheInstance)) { |
|
| 1129 | 1129 | $cacheInstance = $this->cacheInstance; |
| 1130 | 1130 | } |
| 1131 | - else{ |
|
| 1131 | + else { |
|
| 1132 | 1132 | $obj = & get_instance(); |
| 1133 | 1133 | $cacheInstance = $obj->cache; |
| 1134 | 1134 | } |
| 1135 | 1135 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1136 | 1136 | } |
| 1137 | - else{ |
|
| 1137 | + else { |
|
| 1138 | 1138 | $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 | 1139 | } |
| 1140 | 1140 | |
| 1141 | - if(! $this->pdo){ |
|
| 1141 | + if (!$this->pdo) { |
|
| 1142 | 1142 | $this->connect(); |
| 1143 | 1143 | } |
| 1144 | 1144 | |
| 1145 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1145 | + if (!$cacheContent && $sqlSELECTQuery) { |
|
| 1146 | 1146 | //for database query execution time |
| 1147 | 1147 | $benchmarkMarkerKey = md5($query . $all . $array); |
| 1148 | 1148 | $bench = null; |
| 1149 | - if(is_object($this->benchmarkInstance)){ |
|
| 1149 | + if (is_object($this->benchmarkInstance)) { |
|
| 1150 | 1150 | $bench = $this->benchmarkInstance; |
| 1151 | 1151 | } |
| 1152 | - else{ |
|
| 1152 | + else { |
|
| 1153 | 1153 | $obj = & get_instance(); |
| 1154 | 1154 | $bench = $obj->benchmark; |
| 1155 | 1155 | } |
@@ -1160,52 +1160,52 @@ discard block |
||
| 1160 | 1160 | //get response time for this query |
| 1161 | 1161 | $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
| 1162 | 1162 | //TODO use the configuration value for the high response time currently is 1 second |
| 1163 | - if($responseTime >= 1 ){ |
|
| 1164 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1163 | + if ($responseTime >= 1) { |
|
| 1164 | + $this->logger->warning('High response time while processing database query [' . $query . ']. The response time is [' . $responseTime . '] sec.'); |
|
| 1165 | 1165 | } |
| 1166 | - if ($sqlQuery){ |
|
| 1166 | + if ($sqlQuery) { |
|
| 1167 | 1167 | $this->numRows = $sqlQuery->rowCount(); |
| 1168 | - if (($this->numRows > 0)){ |
|
| 1168 | + if (($this->numRows > 0)) { |
|
| 1169 | 1169 | //if need return all result like list of record |
| 1170 | - if ($all){ |
|
| 1170 | + if ($all) { |
|
| 1171 | 1171 | $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
| 1172 | 1172 | } |
| 1173 | - else{ |
|
| 1173 | + else { |
|
| 1174 | 1174 | $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
| 1175 | 1175 | } |
| 1176 | 1176 | } |
| 1177 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1178 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1177 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1178 | + $this->logger->info('Save the result for query [' . $this->query . '] into cache for future use'); |
|
| 1179 | 1179 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1180 | 1180 | } |
| 1181 | 1181 | } |
| 1182 | - else{ |
|
| 1182 | + else { |
|
| 1183 | 1183 | $error = $this->pdo->errorInfo(); |
| 1184 | 1184 | $this->error = $error[2]; |
| 1185 | 1185 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1186 | 1186 | $this->error(); |
| 1187 | 1187 | } |
| 1188 | 1188 | } |
| 1189 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1189 | + else if ((!$cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)) { |
|
| 1190 | 1190 | $queryStr = $this->pdo->query($this->query); |
| 1191 | - if($queryStr){ |
|
| 1191 | + if ($queryStr) { |
|
| 1192 | 1192 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1193 | 1193 | $this->numRows = $queryStr->rowCount(); |
| 1194 | 1194 | } |
| 1195 | - if (! $this->result){ |
|
| 1195 | + if (!$this->result) { |
|
| 1196 | 1196 | $error = $this->pdo->errorInfo(); |
| 1197 | 1197 | $this->error = $error[2]; |
| 1198 | 1198 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1199 | 1199 | $this->error(); |
| 1200 | 1200 | } |
| 1201 | 1201 | } |
| 1202 | - else{ |
|
| 1203 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1202 | + else { |
|
| 1203 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 1204 | 1204 | $this->result = $cacheContent; |
| 1205 | 1205 | $this->numRows = count($this->result); |
| 1206 | 1206 | } |
| 1207 | 1207 | $this->queryCount++; |
| 1208 | - if(! $this->result){ |
|
| 1208 | + if (!$this->result) { |
|
| 1209 | 1209 | $this->logger->info('No result where found for the query [' . $query . ']'); |
| 1210 | 1210 | } |
| 1211 | 1211 | return $this->result; |
@@ -1216,8 +1216,8 @@ discard block |
||
| 1216 | 1216 | * @param integer $ttl the cache time to live in second |
| 1217 | 1217 | * @return object the current Database instance |
| 1218 | 1218 | */ |
| 1219 | - public function setCache($ttl = 0){ |
|
| 1220 | - if($ttl > 0){ |
|
| 1219 | + public function setCache($ttl = 0) { |
|
| 1220 | + if ($ttl > 0) { |
|
| 1221 | 1221 | $this->cacheTtl = $ttl; |
| 1222 | 1222 | $this->temporaryCacheTtl = $ttl; |
| 1223 | 1223 | } |
@@ -1229,8 +1229,8 @@ discard block |
||
| 1229 | 1229 | * @param integer $ttl the cache time to live in second |
| 1230 | 1230 | * @return object the current Database instance |
| 1231 | 1231 | */ |
| 1232 | - public function cached($ttl = 0){ |
|
| 1233 | - if($ttl > 0){ |
|
| 1232 | + public function cached($ttl = 0) { |
|
| 1233 | + if ($ttl > 0) { |
|
| 1234 | 1234 | $this->temporaryCacheTtl = $ttl; |
| 1235 | 1235 | } |
| 1236 | 1236 | return $this; |
@@ -1241,11 +1241,11 @@ discard block |
||
| 1241 | 1241 | * @param mixed $data the data to be escaped |
| 1242 | 1242 | * @return mixed the data after escaped |
| 1243 | 1243 | */ |
| 1244 | - public function escape($data){ |
|
| 1245 | - if(is_null($data)){ |
|
| 1244 | + public function escape($data) { |
|
| 1245 | + if (is_null($data)) { |
|
| 1246 | 1246 | return null; |
| 1247 | 1247 | } |
| 1248 | - if(! $this->pdo){ |
|
| 1248 | + if (!$this->pdo) { |
|
| 1249 | 1249 | $this->connect(); |
| 1250 | 1250 | } |
| 1251 | 1251 | return $this->pdo->quote(trim($data)); |
@@ -1255,7 +1255,7 @@ discard block |
||
| 1255 | 1255 | * Return the number query executed count for the current request |
| 1256 | 1256 | * @return int |
| 1257 | 1257 | */ |
| 1258 | - public function queryCount(){ |
|
| 1258 | + public function queryCount() { |
|
| 1259 | 1259 | return $this->queryCount; |
| 1260 | 1260 | } |
| 1261 | 1261 | |
@@ -1263,7 +1263,7 @@ discard block |
||
| 1263 | 1263 | * Return the current query SQL string |
| 1264 | 1264 | * @return string |
| 1265 | 1265 | */ |
| 1266 | - public function getQuery(){ |
|
| 1266 | + public function getQuery() { |
|
| 1267 | 1267 | return $this->query; |
| 1268 | 1268 | } |
| 1269 | 1269 | |
@@ -1271,7 +1271,7 @@ discard block |
||
| 1271 | 1271 | * Return the application database name |
| 1272 | 1272 | * @return string |
| 1273 | 1273 | */ |
| 1274 | - public function getDatabaseName(){ |
|
| 1274 | + public function getDatabaseName() { |
|
| 1275 | 1275 | return $this->databaseName; |
| 1276 | 1276 | } |
| 1277 | 1277 | |
@@ -1279,7 +1279,7 @@ discard block |
||
| 1279 | 1279 | * Return the database configuration |
| 1280 | 1280 | * @return array |
| 1281 | 1281 | */ |
| 1282 | - public function getDatabaseConfiguration(){ |
|
| 1282 | + public function getDatabaseConfiguration() { |
|
| 1283 | 1283 | return $this->config; |
| 1284 | 1284 | } |
| 1285 | 1285 | |
@@ -1287,7 +1287,7 @@ discard block |
||
| 1287 | 1287 | * set the database configuration |
| 1288 | 1288 | * @param array $config the configuration |
| 1289 | 1289 | */ |
| 1290 | - public function setDatabaseConfiguration(array $config){ |
|
| 1290 | + public function setDatabaseConfiguration(array $config) { |
|
| 1291 | 1291 | $this->config = array_merge($this->config, $config); |
| 1292 | 1292 | $this->prefix = $this->config['prefix']; |
| 1293 | 1293 | $this->databaseName = $this->config['database']; |
@@ -1299,7 +1299,7 @@ discard block |
||
| 1299 | 1299 | * Return the PDO instance |
| 1300 | 1300 | * @return PDO |
| 1301 | 1301 | */ |
| 1302 | - public function getPdo(){ |
|
| 1302 | + public function getPdo() { |
|
| 1303 | 1303 | return $this->pdo; |
| 1304 | 1304 | } |
| 1305 | 1305 | |
@@ -1307,7 +1307,7 @@ discard block |
||
| 1307 | 1307 | * Set the PDO instance |
| 1308 | 1308 | * @param PDO $pdo the pdo object |
| 1309 | 1309 | */ |
| 1310 | - public function setPdo(PDO $pdo){ |
|
| 1310 | + public function setPdo(PDO $pdo) { |
|
| 1311 | 1311 | $this->pdo = $pdo; |
| 1312 | 1312 | return $this; |
| 1313 | 1313 | } |
@@ -1317,7 +1317,7 @@ discard block |
||
| 1317 | 1317 | * Return the Log instance |
| 1318 | 1318 | * @return Log |
| 1319 | 1319 | */ |
| 1320 | - public function getLogger(){ |
|
| 1320 | + public function getLogger() { |
|
| 1321 | 1321 | return $this->logger; |
| 1322 | 1322 | } |
| 1323 | 1323 | |
@@ -1325,7 +1325,7 @@ discard block |
||
| 1325 | 1325 | * Set the log instance |
| 1326 | 1326 | * @param Log $logger the log object |
| 1327 | 1327 | */ |
| 1328 | - public function setLogger($logger){ |
|
| 1328 | + public function setLogger($logger) { |
|
| 1329 | 1329 | $this->logger = $logger; |
| 1330 | 1330 | return $this; |
| 1331 | 1331 | } |
@@ -1334,7 +1334,7 @@ discard block |
||
| 1334 | 1334 | * Return the cache instance |
| 1335 | 1335 | * @return CacheInterface |
| 1336 | 1336 | */ |
| 1337 | - public function getCacheInstance(){ |
|
| 1337 | + public function getCacheInstance() { |
|
| 1338 | 1338 | return $this->cacheInstance; |
| 1339 | 1339 | } |
| 1340 | 1340 | |
@@ -1342,7 +1342,7 @@ discard block |
||
| 1342 | 1342 | * Set the cache instance |
| 1343 | 1343 | * @param CacheInterface $cache the cache object |
| 1344 | 1344 | */ |
| 1345 | - public function setCacheInstance($cache){ |
|
| 1345 | + public function setCacheInstance($cache) { |
|
| 1346 | 1346 | $this->cacheInstance = $cache; |
| 1347 | 1347 | return $this; |
| 1348 | 1348 | } |
@@ -1351,7 +1351,7 @@ discard block |
||
| 1351 | 1351 | * Return the benchmark instance |
| 1352 | 1352 | * @return Benchmark |
| 1353 | 1353 | */ |
| 1354 | - public function getBenchmark(){ |
|
| 1354 | + public function getBenchmark() { |
|
| 1355 | 1355 | return $this->benchmarkInstance; |
| 1356 | 1356 | } |
| 1357 | 1357 | |
@@ -1359,7 +1359,7 @@ discard block |
||
| 1359 | 1359 | * Set the benchmark instance |
| 1360 | 1360 | * @param Benchmark $cache the cache object |
| 1361 | 1361 | */ |
| 1362 | - public function setBenchmark($benchmark){ |
|
| 1362 | + public function setBenchmark($benchmark) { |
|
| 1363 | 1363 | $this->benchmarkInstance = $benchmark; |
| 1364 | 1364 | return $this; |
| 1365 | 1365 | } |
@@ -1368,7 +1368,7 @@ discard block |
||
| 1368 | 1368 | * Return the data to be used for insert, update, etc. |
| 1369 | 1369 | * @return array |
| 1370 | 1370 | */ |
| 1371 | - public function getData(){ |
|
| 1371 | + public function getData() { |
|
| 1372 | 1372 | return $this->data; |
| 1373 | 1373 | } |
| 1374 | 1374 | |
@@ -1379,7 +1379,7 @@ discard block |
||
| 1379 | 1379 | * @param boolean $escape whether to escape or not the $value |
| 1380 | 1380 | * @return object the current Database instance |
| 1381 | 1381 | */ |
| 1382 | - public function setData($key, $value, $escape = true){ |
|
| 1382 | + public function setData($key, $value, $escape = true) { |
|
| 1383 | 1383 | $this->data[$key] = $escape ? $this->escape($value) : $value; |
| 1384 | 1384 | return $this; |
| 1385 | 1385 | } |
@@ -1388,7 +1388,7 @@ discard block |
||
| 1388 | 1388 | /** |
| 1389 | 1389 | * Reset the database class attributs to the initail values before each query. |
| 1390 | 1390 | */ |
| 1391 | - private function reset(){ |
|
| 1391 | + private function reset() { |
|
| 1392 | 1392 | $this->select = '*'; |
| 1393 | 1393 | $this->from = null; |
| 1394 | 1394 | $this->where = null; |
@@ -1408,7 +1408,7 @@ discard block |
||
| 1408 | 1408 | /** |
| 1409 | 1409 | * The class destructor |
| 1410 | 1410 | */ |
| 1411 | - function __destruct(){ |
|
| 1411 | + function __destruct() { |
|
| 1412 | 1412 | $this->pdo = null; |
| 1413 | 1413 | } |
| 1414 | 1414 | |
@@ -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); |