@@ -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 | /** |
| 28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
@@ -76,27 +76,27 @@ discard block |
||
| 76 | 76 | private $logger; |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Instance of the Loader class |
|
| 80 | - * @var Loader |
|
| 81 | - */ |
|
| 82 | - protected $loader = null; |
|
| 79 | + * Instance of the Loader class |
|
| 80 | + * @var Loader |
|
| 81 | + */ |
|
| 82 | + protected $loader = null; |
|
| 83 | 83 | |
| 84 | 84 | public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
| 85 | 85 | /** |
| 86 | - * instance of the Log class |
|
| 87 | - */ |
|
| 88 | - if(is_object($logger)){ |
|
| 89 | - $this->setLogger($logger); |
|
| 90 | - } |
|
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | - $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if(is_object($loader)){ |
|
| 97 | - $this->setLoader($loader); |
|
| 98 | - } |
|
| 99 | - $this->OBJ = & get_instance(); |
|
| 86 | + * instance of the Log class |
|
| 87 | + */ |
|
| 88 | + if(is_object($logger)){ |
|
| 89 | + $this->setLogger($logger); |
|
| 90 | + } |
|
| 91 | + else{ |
|
| 92 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | + $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if(is_object($loader)){ |
|
| 97 | + $this->setLoader($loader); |
|
| 98 | + } |
|
| 99 | + $this->OBJ = & get_instance(); |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | if(is_object($modelInstance)){ |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | $this->getLoader()->library('Browser'); |
| 200 | 200 | } |
| 201 | 201 | else{ |
| 202 | - Loader::functions('user_agent'); |
|
| 203 | - Loader::library('Browser'); |
|
| 204 | - } |
|
| 202 | + Loader::functions('user_agent'); |
|
| 203 | + Loader::library('Browser'); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | 206 | $ip = get_ip(); |
| 207 | 207 | $keyValue = $instance->getKeyValue(); |
@@ -239,9 +239,9 @@ discard block |
||
| 239 | 239 | $this->getLoader()->library('Browser'); |
| 240 | 240 | } |
| 241 | 241 | else{ |
| 242 | - Loader::functions('user_agent'); |
|
| 243 | - Loader::library('Browser'); |
|
| 244 | - } |
|
| 242 | + Loader::functions('user_agent'); |
|
| 243 | + Loader::library('Browser'); |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | $ip = get_ip(); |
| 247 | 247 | $keyValue = $instance->getKeyValue(); |
@@ -325,75 +325,75 @@ discard block |
||
| 325 | 325 | |
| 326 | 326 | |
| 327 | 327 | /** |
| 328 | - * Return the loader instance |
|
| 329 | - * @return Loader the loader instance |
|
| 330 | - */ |
|
| 331 | - public function getLoader(){ |
|
| 332 | - return $this->loader; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * set the loader instance for future use |
|
| 337 | - * @param Loader $loader the loader object |
|
| 338 | - */ |
|
| 339 | - public function setLoader($loader){ |
|
| 340 | - $this->loader = $loader; |
|
| 341 | - return $this; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Return the model instance |
|
| 346 | - * @return DBSessionHandlerModel the model instance |
|
| 347 | - */ |
|
| 348 | - public function getModelInstance(){ |
|
| 349 | - return $this->modelInstanceName; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * set the model instance for future use |
|
| 354 | - * @param DBSessionHandlerModel $modelInstance the model object |
|
| 355 | - */ |
|
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | - $this->modelInstanceName = $modelInstance; |
|
| 358 | - return $this; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Return the Log instance |
|
| 363 | - * @return Log |
|
| 364 | - */ |
|
| 365 | - public function getLogger(){ |
|
| 366 | - return $this->logger; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the log instance |
|
| 371 | - * @param Log $logger the log object |
|
| 372 | - */ |
|
| 373 | - public function setLogger(Log $logger){ |
|
| 374 | - $this->logger = $logger; |
|
| 375 | - return $this; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Set the model instance using the configuration for session |
|
| 380 | - */ |
|
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 382 | - $modelName = get_config('session_save_path'); |
|
| 328 | + * Return the loader instance |
|
| 329 | + * @return Loader the loader instance |
|
| 330 | + */ |
|
| 331 | + public function getLoader(){ |
|
| 332 | + return $this->loader; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * set the loader instance for future use |
|
| 337 | + * @param Loader $loader the loader object |
|
| 338 | + */ |
|
| 339 | + public function setLoader($loader){ |
|
| 340 | + $this->loader = $loader; |
|
| 341 | + return $this; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Return the model instance |
|
| 346 | + * @return DBSessionHandlerModel the model instance |
|
| 347 | + */ |
|
| 348 | + public function getModelInstance(){ |
|
| 349 | + return $this->modelInstanceName; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * set the model instance for future use |
|
| 354 | + * @param DBSessionHandlerModel $modelInstance the model object |
|
| 355 | + */ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | + $this->modelInstanceName = $modelInstance; |
|
| 358 | + return $this; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Return the Log instance |
|
| 363 | + * @return Log |
|
| 364 | + */ |
|
| 365 | + public function getLogger(){ |
|
| 366 | + return $this->logger; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the log instance |
|
| 371 | + * @param Log $logger the log object |
|
| 372 | + */ |
|
| 373 | + public function setLogger(Log $logger){ |
|
| 374 | + $this->logger = $logger; |
|
| 375 | + return $this; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Set the model instance using the configuration for session |
|
| 380 | + */ |
|
| 381 | + private function setModelInstanceFromConfig(){ |
|
| 382 | + $modelName = get_config('session_save_path'); |
|
| 383 | 383 | $this->logger->info('The database session model: ' . $modelName); |
| 384 | 384 | if($this->getLoader()){ |
| 385 | 385 | $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
| 386 | 386 | } |
| 387 | 387 | //@codeCoverageIgnoreStart |
| 388 | 388 | else{ |
| 389 | - Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | - } |
|
| 391 | - if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 389 | + Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | + } |
|
| 391 | + if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 392 | 392 | show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
| 393 | 393 | } |
| 394 | 394 | //@codeCoverageIgnoreEnd |
| 395 | 395 | |
| 396 | 396 | //set model instance |
| 397 | 397 | $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
| 398 | - } |
|
| 398 | + } |
|
| 399 | 399 | } |
@@ -27,11 +27,11 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
| 29 | 29 | */ |
| 30 | - if( !interface_exists('SessionHandlerInterface')){ |
|
| 30 | + if (!interface_exists('SessionHandlerInterface')) { |
|
| 31 | 31 | show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
| 34 | + class DBSessionHandler implements SessionHandlerInterface { |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The encryption method to use to encrypt session data in database |
@@ -81,25 +81,25 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | protected $loader = null; |
| 83 | 83 | |
| 84 | - public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 84 | + public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null) { |
|
| 85 | 85 | /** |
| 86 | 86 | * instance of the Log class |
| 87 | 87 | */ |
| 88 | - if(is_object($logger)){ |
|
| 88 | + if (is_object($logger)) { |
|
| 89 | 89 | $this->setLogger($logger); |
| 90 | 90 | } |
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 91 | + else { |
|
| 92 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 93 | 93 | $this->logger->setLogger('Library::DBSessionHandler'); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - if(is_object($loader)){ |
|
| 96 | + if (is_object($loader)) { |
|
| 97 | 97 | $this->setLoader($loader); |
| 98 | 98 | } |
| 99 | 99 | $this->OBJ = & get_instance(); |
| 100 | 100 | |
| 101 | 101 | |
| 102 | - if(is_object($modelInstance)){ |
|
| 102 | + if (is_object($modelInstance)) { |
|
| 103 | 103 | $this->setModelInstance($modelInstance); |
| 104 | 104 | } |
| 105 | 105 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | * Set the session secret used to encrypt the data in database |
| 109 | 109 | * @param string $secret the base64 string secret |
| 110 | 110 | */ |
| 111 | - public function setSessionSecret($secret){ |
|
| 111 | + public function setSessionSecret($secret) { |
|
| 112 | 112 | $this->sessionSecret = $secret; |
| 113 | 113 | return $this; |
| 114 | 114 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * Return the session secret |
| 118 | 118 | * @return string |
| 119 | 119 | */ |
| 120 | - public function getSessionSecret(){ |
|
| 120 | + public function getSessionSecret() { |
|
| 121 | 121 | return $this->sessionSecret; |
| 122 | 122 | } |
| 123 | 123 | |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * Set the initializer vector for openssl |
| 127 | 127 | * @param string $key the session secret used |
| 128 | 128 | */ |
| 129 | - public function setInitializerVector($key){ |
|
| 129 | + public function setInitializerVector($key) { |
|
| 130 | 130 | $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
| 131 | 131 | $key = base64_decode($key); |
| 132 | 132 | $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * Return the initializer vector |
| 138 | 138 | * @return string |
| 139 | 139 | */ |
| 140 | - public function getInitializerVector(){ |
|
| 140 | + public function getInitializerVector() { |
|
| 141 | 141 | return $this->iv; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -147,16 +147,16 @@ discard block |
||
| 147 | 147 | * @param string $sessionName the session name |
| 148 | 148 | * @return boolean |
| 149 | 149 | */ |
| 150 | - public function open($savePath, $sessionName){ |
|
| 150 | + public function open($savePath, $sessionName) { |
|
| 151 | 151 | $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
| 152 | 152 | //try to check if session secret is set before |
| 153 | - if(! $this->getSessionSecret()){ |
|
| 153 | + if (!$this->getSessionSecret()) { |
|
| 154 | 154 | $secret = get_config('session_secret', false); |
| 155 | 155 | $this->setSessionSecret($secret); |
| 156 | 156 | } |
| 157 | 157 | $this->logger->info('Session secret: ' . $this->getSessionSecret()); |
| 158 | 158 | |
| 159 | - if(! $this->getModelInstance()){ |
|
| 159 | + if (!$this->getModelInstance()) { |
|
| 160 | 160 | $this->setModelInstanceFromConfig(); |
| 161 | 161 | } |
| 162 | 162 | $this->setInitializerVector($this->getSessionSecret()); |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | //set session tables columns |
| 165 | 165 | $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
| 166 | 166 | |
| 167 | - if(empty($this->sessionTableColumns)){ |
|
| 167 | + if (empty($this->sessionTableColumns)) { |
|
| 168 | 168 | show_error('The session handler is "database" but the table columns not set'); |
| 169 | 169 | } |
| 170 | 170 | $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | * Close the session |
| 181 | 181 | * @return boolean |
| 182 | 182 | */ |
| 183 | - public function close(){ |
|
| 183 | + public function close() { |
|
| 184 | 184 | $this->logger->debug('Closing database session handler'); |
| 185 | 185 | return true; |
| 186 | 186 | } |
@@ -190,15 +190,15 @@ discard block |
||
| 190 | 190 | * @param string $sid the session id to use |
| 191 | 191 | * @return mixed the session data in serialiaze format |
| 192 | 192 | */ |
| 193 | - public function read($sid){ |
|
| 193 | + public function read($sid) { |
|
| 194 | 194 | $this->logger->debug('Reading database session data for SID: ' . $sid); |
| 195 | 195 | $instance = $this->getModelInstance(); |
| 196 | 196 | $columns = $this->sessionTableColumns; |
| 197 | - if($this->getLoader()){ |
|
| 197 | + if ($this->getLoader()) { |
|
| 198 | 198 | $this->getLoader()->functions('user_agent'); |
| 199 | 199 | $this->getLoader()->library('Browser'); |
| 200 | 200 | } |
| 201 | - else{ |
|
| 201 | + else { |
|
| 202 | 202 | Loader::functions('user_agent'); |
| 203 | 203 | Loader::library('Browser'); |
| 204 | 204 | } |
@@ -206,13 +206,13 @@ discard block |
||
| 206 | 206 | $ip = get_ip(); |
| 207 | 207 | $keyValue = $instance->getKeyValue(); |
| 208 | 208 | $host = @gethostbyaddr($ip) or null; |
| 209 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 209 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 210 | 210 | |
| 211 | 211 | $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
| 212 | - if($data && isset($data->{$columns['sdata']})){ |
|
| 212 | + if ($data && isset($data->{$columns['sdata']})) { |
|
| 213 | 213 | //checking inactivity |
| 214 | 214 | $timeInactivity = time() - get_config('session_inactivity_time', 100); |
| 215 | - if($data->{$columns['stime']} < $timeInactivity){ |
|
| 215 | + if ($data->{$columns['stime']} < $timeInactivity) { |
|
| 216 | 216 | $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
| 217 | 217 | $this->destroy($sid); |
| 218 | 218 | return false; |
@@ -229,16 +229,16 @@ discard block |
||
| 229 | 229 | * @param mixed $data the session data to save in serialize format |
| 230 | 230 | * @return boolean |
| 231 | 231 | */ |
| 232 | - public function write($sid, $data){ |
|
| 232 | + public function write($sid, $data) { |
|
| 233 | 233 | $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
| 234 | 234 | $instance = $this->getModelInstance(); |
| 235 | 235 | $columns = $this->sessionTableColumns; |
| 236 | 236 | |
| 237 | - if($this->getLoader()){ |
|
| 237 | + if ($this->getLoader()) { |
|
| 238 | 238 | $this->getLoader()->functions('user_agent'); |
| 239 | 239 | $this->getLoader()->library('Browser'); |
| 240 | 240 | } |
| 241 | - else{ |
|
| 241 | + else { |
|
| 242 | 242 | Loader::functions('user_agent'); |
| 243 | 243 | Loader::library('Browser'); |
| 244 | 244 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $ip = get_ip(); |
| 247 | 247 | $keyValue = $instance->getKeyValue(); |
| 248 | 248 | $host = @gethostbyaddr($ip) or null; |
| 249 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 249 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 250 | 250 | $data = $this->encode($data); |
| 251 | 251 | $params = array( |
| 252 | 252 | $columns['sid'] => $sid, |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | ); |
| 260 | 260 | $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
| 261 | 261 | $exists = $instance->get($sid); |
| 262 | - if($exists){ |
|
| 262 | + if ($exists) { |
|
| 263 | 263 | $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
| 264 | 264 | //update |
| 265 | 265 | unset($params[$columns['sid']]); |
| 266 | 266 | $instance->update($sid, $params); |
| 267 | 267 | } |
| 268 | - else{ |
|
| 268 | + else { |
|
| 269 | 269 | $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
| 270 | 270 | $instance->insert($params); |
| 271 | 271 | } |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * @param string $sid the session id value |
| 279 | 279 | * @return boolean |
| 280 | 280 | */ |
| 281 | - public function destroy($sid){ |
|
| 281 | + public function destroy($sid) { |
|
| 282 | 282 | $this->logger->debug('Destroy of session data for SID: ' . $sid); |
| 283 | 283 | $instance = $this->modelInstanceName; |
| 284 | 284 | $instance->delete($sid); |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | * @param ineteger $maxLifetime the max lifetime |
| 291 | 291 | * @return boolean |
| 292 | 292 | */ |
| 293 | - public function gc($maxLifetime){ |
|
| 293 | + public function gc($maxLifetime) { |
|
| 294 | 294 | $instance = $this->modelInstanceName; |
| 295 | 295 | $time = time() - $maxLifetime; |
| 296 | 296 | $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | * @param mixed $data the session data to encode |
| 304 | 304 | * @return mixed the encoded session data |
| 305 | 305 | */ |
| 306 | - public function encode($data){ |
|
| 306 | + public function encode($data) { |
|
| 307 | 307 | $key = base64_decode($this->sessionSecret); |
| 308 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 308 | + $dataEncrypted = openssl_encrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | 309 | $output = base64_encode($dataEncrypted); |
| 310 | 310 | return $output; |
| 311 | 311 | } |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | * @param mixed $data the data to decode |
| 317 | 317 | * @return mixed the decoded data |
| 318 | 318 | */ |
| 319 | - public function decode($data){ |
|
| 319 | + public function decode($data) { |
|
| 320 | 320 | $key = base64_decode($this->sessionSecret); |
| 321 | 321 | $data = base64_decode($data); |
| 322 | 322 | $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | * Return the loader instance |
| 329 | 329 | * @return Loader the loader instance |
| 330 | 330 | */ |
| 331 | - public function getLoader(){ |
|
| 331 | + public function getLoader() { |
|
| 332 | 332 | return $this->loader; |
| 333 | 333 | } |
| 334 | 334 | |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * set the loader instance for future use |
| 337 | 337 | * @param Loader $loader the loader object |
| 338 | 338 | */ |
| 339 | - public function setLoader($loader){ |
|
| 339 | + public function setLoader($loader) { |
|
| 340 | 340 | $this->loader = $loader; |
| 341 | 341 | return $this; |
| 342 | 342 | } |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | * Return the model instance |
| 346 | 346 | * @return DBSessionHandlerModel the model instance |
| 347 | 347 | */ |
| 348 | - public function getModelInstance(){ |
|
| 348 | + public function getModelInstance() { |
|
| 349 | 349 | return $this->modelInstanceName; |
| 350 | 350 | } |
| 351 | 351 | |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | * set the model instance for future use |
| 354 | 354 | * @param DBSessionHandlerModel $modelInstance the model object |
| 355 | 355 | */ |
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance) { |
|
| 357 | 357 | $this->modelInstanceName = $modelInstance; |
| 358 | 358 | return $this; |
| 359 | 359 | } |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * Return the Log instance |
| 363 | 363 | * @return Log |
| 364 | 364 | */ |
| 365 | - public function getLogger(){ |
|
| 365 | + public function getLogger() { |
|
| 366 | 366 | return $this->logger; |
| 367 | 367 | } |
| 368 | 368 | |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | * Set the log instance |
| 371 | 371 | * @param Log $logger the log object |
| 372 | 372 | */ |
| 373 | - public function setLogger(Log $logger){ |
|
| 373 | + public function setLogger(Log $logger) { |
|
| 374 | 374 | $this->logger = $logger; |
| 375 | 375 | return $this; |
| 376 | 376 | } |
@@ -378,18 +378,18 @@ discard block |
||
| 378 | 378 | /** |
| 379 | 379 | * Set the model instance using the configuration for session |
| 380 | 380 | */ |
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 381 | + private function setModelInstanceFromConfig() { |
|
| 382 | 382 | $modelName = get_config('session_save_path'); |
| 383 | 383 | $this->logger->info('The database session model: ' . $modelName); |
| 384 | - if($this->getLoader()){ |
|
| 384 | + if ($this->getLoader()) { |
|
| 385 | 385 | $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
| 386 | 386 | } |
| 387 | 387 | //@codeCoverageIgnoreStart |
| 388 | - else{ |
|
| 388 | + else { |
|
| 389 | 389 | Loader::model($modelName, 'dbsessionhandlerinstance'); |
| 390 | 390 | } |
| 391 | - if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 392 | - show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 391 | + if (isset($this->OBJ->dbsessionhandlerinstance) && !$this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) { |
|
| 392 | + show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 393 | 393 | } |
| 394 | 394 | //@codeCoverageIgnoreEnd |
| 395 | 395 | |
@@ -87,8 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | if(is_object($logger)){ |
| 89 | 89 | $this->setLogger($logger); |
| 90 | - } |
|
| 91 | - else{ |
|
| 90 | + } else{ |
|
| 92 | 91 | $this->logger =& class_loader('Log', 'classes'); |
| 93 | 92 | $this->logger->setLogger('Library::DBSessionHandler'); |
| 94 | 93 | } |
@@ -197,8 +196,7 @@ discard block |
||
| 197 | 196 | if($this->getLoader()){ |
| 198 | 197 | $this->getLoader()->functions('user_agent'); |
| 199 | 198 | $this->getLoader()->library('Browser'); |
| 200 | - } |
|
| 201 | - else{ |
|
| 199 | + } else{ |
|
| 202 | 200 | Loader::functions('user_agent'); |
| 203 | 201 | Loader::library('Browser'); |
| 204 | 202 | } |
@@ -237,8 +235,7 @@ discard block |
||
| 237 | 235 | if($this->getLoader()){ |
| 238 | 236 | $this->getLoader()->functions('user_agent'); |
| 239 | 237 | $this->getLoader()->library('Browser'); |
| 240 | - } |
|
| 241 | - else{ |
|
| 238 | + } else{ |
|
| 242 | 239 | Loader::functions('user_agent'); |
| 243 | 240 | Loader::library('Browser'); |
| 244 | 241 | } |
@@ -264,8 +261,7 @@ discard block |
||
| 264 | 261 | //update |
| 265 | 262 | unset($params[$columns['sid']]); |
| 266 | 263 | $instance->update($sid, $params); |
| 267 | - } |
|
| 268 | - else{ |
|
| 264 | + } else{ |
|
| 269 | 265 | $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
| 270 | 266 | $instance->insert($params); |
| 271 | 267 | } |
@@ -1,595 +1,595 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | - * validation-in-model support, event callbacks and more. |
|
| 31 | - * |
|
| 32 | - * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | - * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | - */ |
|
| 35 | - |
|
| 36 | - class Model{ |
|
| 37 | - |
|
| 38 | - /* -------------------------------------------------------------- |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | + * validation-in-model support, event callbacks and more. |
|
| 31 | + * |
|
| 32 | + * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | + * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | + */ |
|
| 35 | + |
|
| 36 | + class Model{ |
|
| 37 | + |
|
| 38 | + /* -------------------------------------------------------------- |
|
| 39 | 39 | * VARIABLES |
| 40 | 40 | * ------------------------------------------------------------ */ |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This model's default database table. Automatically |
|
| 44 | - * guessed by pluralising the model name. |
|
| 45 | - */ |
|
| 46 | - protected $_table; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The database connection object. Will be set to the default |
|
| 50 | - * connection. This allows individual models to use different DBs |
|
| 51 | - * without overwriting the global database connection. |
|
| 52 | - */ |
|
| 53 | - protected $_database; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * This model's default primary key or unique identifier. |
|
| 57 | - * Used by the get(), update() and delete() functions. |
|
| 58 | - */ |
|
| 59 | - protected $primary_key = 'id'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Support for soft deletes and this model's 'deleted' key |
|
| 63 | - */ |
|
| 64 | - protected $soft_delete = false; |
|
| 65 | - protected $soft_delete_key = 'is_deleted'; |
|
| 66 | - protected $_temporary_with_deleted = FALSE; |
|
| 67 | - protected $_temporary_only_deleted = FALSE; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The various callbacks available to the model. Each are |
|
| 71 | - * simple lists of method names (methods will be run on $this). |
|
| 72 | - */ |
|
| 73 | - protected $before_create = array(); |
|
| 74 | - protected $after_create = array(); |
|
| 75 | - protected $before_update = array(); |
|
| 76 | - protected $after_update = array(); |
|
| 77 | - protected $before_get = array(); |
|
| 78 | - protected $after_get = array(); |
|
| 79 | - protected $before_delete = array(); |
|
| 80 | - protected $after_delete = array(); |
|
| 81 | - |
|
| 82 | - protected $callback_parameters = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Protected, non-modifiable attributes |
|
| 86 | - */ |
|
| 87 | - protected $protected_attributes = array(); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | - * => array to customise the class name and primary key |
|
| 92 | - */ |
|
| 93 | - protected $belongs_to = array(); |
|
| 94 | - protected $has_many = array(); |
|
| 95 | - |
|
| 96 | - protected $_with = array(); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * An array of validation rules. This needs to be the same format |
|
| 100 | - * as validation rules passed to the FormValidation library. |
|
| 101 | - */ |
|
| 102 | - protected $validate = array(); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Optionally skip the validation. Used in conjunction with |
|
| 106 | - * skip_validation() to skip data validation for any future calls. |
|
| 107 | - */ |
|
| 108 | - protected $skip_validation = FALSE; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * By default we return our results as objects. If we need to override |
|
| 112 | - * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | - */ |
|
| 114 | - protected $return_type = 'object'; |
|
| 115 | - protected $_temporary_return_type = NULL; |
|
| 42 | + /** |
|
| 43 | + * This model's default database table. Automatically |
|
| 44 | + * guessed by pluralising the model name. |
|
| 45 | + */ |
|
| 46 | + protected $_table; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The database connection object. Will be set to the default |
|
| 50 | + * connection. This allows individual models to use different DBs |
|
| 51 | + * without overwriting the global database connection. |
|
| 52 | + */ |
|
| 53 | + protected $_database; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * This model's default primary key or unique identifier. |
|
| 57 | + * Used by the get(), update() and delete() functions. |
|
| 58 | + */ |
|
| 59 | + protected $primary_key = 'id'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Support for soft deletes and this model's 'deleted' key |
|
| 63 | + */ |
|
| 64 | + protected $soft_delete = false; |
|
| 65 | + protected $soft_delete_key = 'is_deleted'; |
|
| 66 | + protected $_temporary_with_deleted = FALSE; |
|
| 67 | + protected $_temporary_only_deleted = FALSE; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The various callbacks available to the model. Each are |
|
| 71 | + * simple lists of method names (methods will be run on $this). |
|
| 72 | + */ |
|
| 73 | + protected $before_create = array(); |
|
| 74 | + protected $after_create = array(); |
|
| 75 | + protected $before_update = array(); |
|
| 76 | + protected $after_update = array(); |
|
| 77 | + protected $before_get = array(); |
|
| 78 | + protected $after_get = array(); |
|
| 79 | + protected $before_delete = array(); |
|
| 80 | + protected $after_delete = array(); |
|
| 81 | + |
|
| 82 | + protected $callback_parameters = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Protected, non-modifiable attributes |
|
| 86 | + */ |
|
| 87 | + protected $protected_attributes = array(); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | + * => array to customise the class name and primary key |
|
| 92 | + */ |
|
| 93 | + protected $belongs_to = array(); |
|
| 94 | + protected $has_many = array(); |
|
| 95 | + |
|
| 96 | + protected $_with = array(); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * An array of validation rules. This needs to be the same format |
|
| 100 | + * as validation rules passed to the FormValidation library. |
|
| 101 | + */ |
|
| 102 | + protected $validate = array(); |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Optionally skip the validation. Used in conjunction with |
|
| 106 | + * skip_validation() to skip data validation for any future calls. |
|
| 107 | + */ |
|
| 108 | + protected $skip_validation = FALSE; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * By default we return our results as objects. If we need to override |
|
| 112 | + * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | + */ |
|
| 114 | + protected $return_type = 'object'; |
|
| 115 | + protected $_temporary_return_type = NULL; |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 118 | + /** |
|
| 119 | 119 | The database cache time |
| 120 | - */ |
|
| 121 | - protected $dbCacheTime = 0; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Instance of the Loader class |
|
| 125 | - * @var Loader |
|
| 126 | - */ |
|
| 127 | - protected $loaderInstance = null; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Instance of the FormValidation library |
|
| 131 | - * @var FormValidation |
|
| 132 | - */ |
|
| 133 | - protected $formValidationInstance = null; |
|
| 134 | - |
|
| 135 | - /* -------------------------------------------------------------- |
|
| 120 | + */ |
|
| 121 | + protected $dbCacheTime = 0; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Instance of the Loader class |
|
| 125 | + * @var Loader |
|
| 126 | + */ |
|
| 127 | + protected $loaderInstance = null; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Instance of the FormValidation library |
|
| 131 | + * @var FormValidation |
|
| 132 | + */ |
|
| 133 | + protected $formValidationInstance = null; |
|
| 134 | + |
|
| 135 | + /* -------------------------------------------------------------- |
|
| 136 | 136 | * GENERIC METHODS |
| 137 | 137 | * ------------------------------------------------------------ */ |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | - * try our best to guess the table name. |
|
| 142 | - */ |
|
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if(is_object($db)){ |
|
| 145 | - $this->setDatabaseInstance($db); |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $obj = & get_instance(); |
|
| 149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | - /** |
|
| 151 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | - * to prevent duplication |
|
| 153 | - */ |
|
| 154 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | - array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | - $this->_temporary_return_type = $this->return_type; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /* -------------------------------------------------------------- |
|
| 139 | + /** |
|
| 140 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | + * try our best to guess the table name. |
|
| 142 | + */ |
|
| 143 | + public function __construct(Database $db = null){ |
|
| 144 | + if(is_object($db)){ |
|
| 145 | + $this->setDatabaseInstance($db); |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $obj = & get_instance(); |
|
| 149 | + if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | + /** |
|
| 151 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | + * to prevent duplication |
|
| 153 | + */ |
|
| 154 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | + array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | + $this->_temporary_return_type = $this->return_type; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /* -------------------------------------------------------------- |
|
| 164 | 164 | * CRUD INTERFACE |
| 165 | 165 | * ------------------------------------------------------------ */ |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | - */ |
|
| 170 | - public function get($primary_value) |
|
| 171 | - { |
|
| 172 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | - * any valid value to $this->_database->where(). |
|
| 178 | - */ |
|
| 179 | - public function get_by() |
|
| 180 | - { |
|
| 181 | - $where = func_get_args(); |
|
| 182 | - |
|
| 183 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | - { |
|
| 185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $this->_set_where($where); |
|
| 189 | - |
|
| 190 | - $this->trigger('before_get'); |
|
| 167 | + /** |
|
| 168 | + * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | + */ |
|
| 170 | + public function get($primary_value) |
|
| 171 | + { |
|
| 172 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | + * any valid value to $this->_database->where(). |
|
| 178 | + */ |
|
| 179 | + public function get_by() |
|
| 180 | + { |
|
| 181 | + $where = func_get_args(); |
|
| 182 | + |
|
| 183 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | + { |
|
| 185 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $this->_set_where($where); |
|
| 189 | + |
|
| 190 | + $this->trigger('before_get'); |
|
| 191 | 191 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 192 | - $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | - $this->_temporary_return_type = $this->return_type; |
|
| 194 | - $row = $this->trigger('after_get', $row); |
|
| 195 | - $this->_with = array(); |
|
| 196 | - return $row; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Fetch an array of records based on an array of primary values. |
|
| 201 | - */ |
|
| 202 | - public function get_many($values) |
|
| 203 | - { |
|
| 204 | - $this->_database->in($this->primary_key, $values); |
|
| 205 | - return $this->get_all(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | - */ |
|
| 211 | - public function get_many_by() |
|
| 212 | - { |
|
| 213 | - $where = func_get_args(); |
|
| 214 | - $this->_set_where($where); |
|
| 215 | - return $this->get_all(); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | - * to $this->_database->get() with scoped methods. |
|
| 221 | - */ |
|
| 222 | - public function get_all() |
|
| 223 | - { |
|
| 224 | - $this->trigger('before_get'); |
|
| 225 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | - { |
|
| 227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | - } |
|
| 192 | + $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | + $this->_temporary_return_type = $this->return_type; |
|
| 194 | + $row = $this->trigger('after_get', $row); |
|
| 195 | + $this->_with = array(); |
|
| 196 | + return $row; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Fetch an array of records based on an array of primary values. |
|
| 201 | + */ |
|
| 202 | + public function get_many($values) |
|
| 203 | + { |
|
| 204 | + $this->_database->in($this->primary_key, $values); |
|
| 205 | + return $this->get_all(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | + */ |
|
| 211 | + public function get_many_by() |
|
| 212 | + { |
|
| 213 | + $where = func_get_args(); |
|
| 214 | + $this->_set_where($where); |
|
| 215 | + return $this->get_all(); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | + * to $this->_database->get() with scoped methods. |
|
| 221 | + */ |
|
| 222 | + public function get_all() |
|
| 223 | + { |
|
| 224 | + $this->trigger('before_get'); |
|
| 225 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | + { |
|
| 227 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | + } |
|
| 229 | 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 230 | - $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | - $this->_temporary_return_type = $this->return_type; |
|
| 232 | - |
|
| 233 | - foreach ($result as $key => &$row) |
|
| 234 | - { |
|
| 235 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | - } |
|
| 237 | - $this->_with = array(); |
|
| 238 | - return $result; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Insert a new row into the table. $data should be an associative array |
|
| 243 | - * of data to be inserted. Returns newly created ID. |
|
| 244 | - */ |
|
| 245 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | - { |
|
| 247 | - if ($skip_validation === FALSE) |
|
| 248 | - { |
|
| 249 | - $data = $this->validate($data); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ($data !== FALSE) |
|
| 253 | - { |
|
| 254 | - $data = $this->trigger('before_create', $data); |
|
| 255 | - $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | - $insert_id = $this->_database->insertId(); |
|
| 257 | - $this->trigger('after_create', $insert_id); |
|
| 258 | - return $insert_id; |
|
| 259 | - } |
|
| 260 | - else |
|
| 261 | - { |
|
| 262 | - return FALSE; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | - */ |
|
| 269 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | - { |
|
| 271 | - $ids = array(); |
|
| 272 | - foreach ($data as $key => $row) |
|
| 273 | - { |
|
| 274 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | - } |
|
| 276 | - return $ids; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Updated a record based on the primary value. |
|
| 281 | - */ |
|
| 282 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | - { |
|
| 284 | - $data = $this->trigger('before_update', $data); |
|
| 285 | - if ($skip_validation === FALSE) |
|
| 286 | - { |
|
| 287 | - $data = $this->validate($data); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ($data !== FALSE) |
|
| 291 | - { |
|
| 292 | - $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | - ->from($this->_table) |
|
| 294 | - ->update($data, $escape); |
|
| 295 | - $this->trigger('after_update', array($data, $result)); |
|
| 296 | - return $result; |
|
| 297 | - } |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - return FALSE; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Update many records, based on an array of primary values. |
|
| 306 | - */ |
|
| 307 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | - { |
|
| 309 | - $data = $this->trigger('before_update', $data); |
|
| 310 | - if ($skip_validation === FALSE) |
|
| 311 | - { |
|
| 312 | - $data = $this->validate($data); |
|
| 313 | - } |
|
| 314 | - if ($data !== FALSE) |
|
| 315 | - { |
|
| 316 | - $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | - ->from($this->_table) |
|
| 318 | - ->update($data, $escape); |
|
| 319 | - $this->trigger('after_update', array($data, $result)); |
|
| 320 | - return $result; |
|
| 321 | - } |
|
| 322 | - else |
|
| 323 | - { |
|
| 324 | - return FALSE; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | - */ |
|
| 331 | - public function update_by() |
|
| 332 | - { |
|
| 333 | - $args = func_get_args(); |
|
| 334 | - $data = array(); |
|
| 335 | - if(count($args) == 2){ |
|
| 336 | - if(is_array($args[1])){ |
|
| 337 | - $data = array_pop($args); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - else if(count($args) == 3){ |
|
| 341 | - if(is_array($args[2])){ |
|
| 342 | - $data = array_pop($args); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - $data = $this->trigger('before_update', $data); |
|
| 346 | - if ($this->validate($data) !== FALSE) |
|
| 347 | - { |
|
| 348 | - $this->_set_where($args); |
|
| 349 | - $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | - $this->trigger('after_update', array($data, $result)); |
|
| 351 | - return $result; |
|
| 352 | - } |
|
| 353 | - else |
|
| 354 | - { |
|
| 355 | - return FALSE; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Update all records |
|
| 361 | - */ |
|
| 362 | - public function update_all($data = array(), $escape = true) |
|
| 363 | - { |
|
| 364 | - $data = $this->trigger('before_update', $data); |
|
| 365 | - $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | - $this->trigger('after_update', array($data, $result)); |
|
| 367 | - return $result; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Delete a row from the table by the primary value |
|
| 372 | - */ |
|
| 373 | - public function delete($id) |
|
| 374 | - { |
|
| 375 | - $this->trigger('before_delete', $id); |
|
| 376 | - $this->_database->where($this->primary_key, $id); |
|
| 377 | - if ($this->soft_delete) |
|
| 378 | - { |
|
| 379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | - } |
|
| 381 | - else |
|
| 382 | - { |
|
| 383 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $this->trigger('after_delete', $result); |
|
| 387 | - return $result; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | - */ |
|
| 393 | - public function delete_by() |
|
| 394 | - { |
|
| 395 | - $where = func_get_args(); |
|
| 396 | - $where = $this->trigger('before_delete', $where); |
|
| 397 | - $this->_set_where($where); |
|
| 398 | - if ($this->soft_delete) |
|
| 399 | - { |
|
| 400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | - } |
|
| 402 | - else |
|
| 403 | - { |
|
| 404 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | - } |
|
| 406 | - $this->trigger('after_delete', $result); |
|
| 407 | - return $result; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Delete many rows from the database table by multiple primary values |
|
| 412 | - */ |
|
| 413 | - public function delete_many($primary_values) |
|
| 414 | - { |
|
| 415 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | - $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | - if ($this->soft_delete) |
|
| 418 | - { |
|
| 419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | - } |
|
| 421 | - else |
|
| 422 | - { |
|
| 423 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | - } |
|
| 425 | - $this->trigger('after_delete', $result); |
|
| 426 | - return $result; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Truncates the table |
|
| 432 | - */ |
|
| 433 | - public function truncate() |
|
| 434 | - { |
|
| 435 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | - return $result; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /* -------------------------------------------------------------- |
|
| 230 | + $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | + $this->_temporary_return_type = $this->return_type; |
|
| 232 | + |
|
| 233 | + foreach ($result as $key => &$row) |
|
| 234 | + { |
|
| 235 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | + } |
|
| 237 | + $this->_with = array(); |
|
| 238 | + return $result; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Insert a new row into the table. $data should be an associative array |
|
| 243 | + * of data to be inserted. Returns newly created ID. |
|
| 244 | + */ |
|
| 245 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | + { |
|
| 247 | + if ($skip_validation === FALSE) |
|
| 248 | + { |
|
| 249 | + $data = $this->validate($data); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ($data !== FALSE) |
|
| 253 | + { |
|
| 254 | + $data = $this->trigger('before_create', $data); |
|
| 255 | + $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | + $insert_id = $this->_database->insertId(); |
|
| 257 | + $this->trigger('after_create', $insert_id); |
|
| 258 | + return $insert_id; |
|
| 259 | + } |
|
| 260 | + else |
|
| 261 | + { |
|
| 262 | + return FALSE; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | + */ |
|
| 269 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | + { |
|
| 271 | + $ids = array(); |
|
| 272 | + foreach ($data as $key => $row) |
|
| 273 | + { |
|
| 274 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | + } |
|
| 276 | + return $ids; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Updated a record based on the primary value. |
|
| 281 | + */ |
|
| 282 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | + { |
|
| 284 | + $data = $this->trigger('before_update', $data); |
|
| 285 | + if ($skip_validation === FALSE) |
|
| 286 | + { |
|
| 287 | + $data = $this->validate($data); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ($data !== FALSE) |
|
| 291 | + { |
|
| 292 | + $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | + ->from($this->_table) |
|
| 294 | + ->update($data, $escape); |
|
| 295 | + $this->trigger('after_update', array($data, $result)); |
|
| 296 | + return $result; |
|
| 297 | + } |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + return FALSE; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Update many records, based on an array of primary values. |
|
| 306 | + */ |
|
| 307 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | + { |
|
| 309 | + $data = $this->trigger('before_update', $data); |
|
| 310 | + if ($skip_validation === FALSE) |
|
| 311 | + { |
|
| 312 | + $data = $this->validate($data); |
|
| 313 | + } |
|
| 314 | + if ($data !== FALSE) |
|
| 315 | + { |
|
| 316 | + $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | + ->from($this->_table) |
|
| 318 | + ->update($data, $escape); |
|
| 319 | + $this->trigger('after_update', array($data, $result)); |
|
| 320 | + return $result; |
|
| 321 | + } |
|
| 322 | + else |
|
| 323 | + { |
|
| 324 | + return FALSE; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | + */ |
|
| 331 | + public function update_by() |
|
| 332 | + { |
|
| 333 | + $args = func_get_args(); |
|
| 334 | + $data = array(); |
|
| 335 | + if(count($args) == 2){ |
|
| 336 | + if(is_array($args[1])){ |
|
| 337 | + $data = array_pop($args); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + else if(count($args) == 3){ |
|
| 341 | + if(is_array($args[2])){ |
|
| 342 | + $data = array_pop($args); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + $data = $this->trigger('before_update', $data); |
|
| 346 | + if ($this->validate($data) !== FALSE) |
|
| 347 | + { |
|
| 348 | + $this->_set_where($args); |
|
| 349 | + $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | + $this->trigger('after_update', array($data, $result)); |
|
| 351 | + return $result; |
|
| 352 | + } |
|
| 353 | + else |
|
| 354 | + { |
|
| 355 | + return FALSE; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Update all records |
|
| 361 | + */ |
|
| 362 | + public function update_all($data = array(), $escape = true) |
|
| 363 | + { |
|
| 364 | + $data = $this->trigger('before_update', $data); |
|
| 365 | + $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | + $this->trigger('after_update', array($data, $result)); |
|
| 367 | + return $result; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Delete a row from the table by the primary value |
|
| 372 | + */ |
|
| 373 | + public function delete($id) |
|
| 374 | + { |
|
| 375 | + $this->trigger('before_delete', $id); |
|
| 376 | + $this->_database->where($this->primary_key, $id); |
|
| 377 | + if ($this->soft_delete) |
|
| 378 | + { |
|
| 379 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | + } |
|
| 381 | + else |
|
| 382 | + { |
|
| 383 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $this->trigger('after_delete', $result); |
|
| 387 | + return $result; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | + */ |
|
| 393 | + public function delete_by() |
|
| 394 | + { |
|
| 395 | + $where = func_get_args(); |
|
| 396 | + $where = $this->trigger('before_delete', $where); |
|
| 397 | + $this->_set_where($where); |
|
| 398 | + if ($this->soft_delete) |
|
| 399 | + { |
|
| 400 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | + } |
|
| 402 | + else |
|
| 403 | + { |
|
| 404 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | + } |
|
| 406 | + $this->trigger('after_delete', $result); |
|
| 407 | + return $result; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Delete many rows from the database table by multiple primary values |
|
| 412 | + */ |
|
| 413 | + public function delete_many($primary_values) |
|
| 414 | + { |
|
| 415 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | + $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | + if ($this->soft_delete) |
|
| 418 | + { |
|
| 419 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | + } |
|
| 421 | + else |
|
| 422 | + { |
|
| 423 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | + } |
|
| 425 | + $this->trigger('after_delete', $result); |
|
| 426 | + return $result; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Truncates the table |
|
| 432 | + */ |
|
| 433 | + public function truncate() |
|
| 434 | + { |
|
| 435 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | + return $result; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /* -------------------------------------------------------------- |
|
| 440 | 440 | * RELATIONSHIPS |
| 441 | 441 | * ------------------------------------------------------------ */ |
| 442 | 442 | |
| 443 | - public function with($relationship) |
|
| 444 | - { |
|
| 445 | - $this->_with[] = $relationship; |
|
| 446 | - if (!in_array('relate', $this->after_get)) |
|
| 447 | - { |
|
| 448 | - $this->after_get[] = 'relate'; |
|
| 449 | - } |
|
| 450 | - return $this; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - public function relate($row) |
|
| 454 | - { |
|
| 455 | - if (empty($row)) |
|
| 456 | - { |
|
| 457 | - return $row; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - foreach ($this->belongs_to as $key => $value) |
|
| 461 | - { |
|
| 462 | - if (is_string($value)) |
|
| 463 | - { |
|
| 464 | - $relationship = $value; |
|
| 465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | - } |
|
| 467 | - else |
|
| 468 | - { |
|
| 469 | - $relationship = $key; |
|
| 470 | - $options = $value; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - if (in_array($relationship, $this->_with)) |
|
| 474 | - { |
|
| 475 | - if(is_object($this->loaderInstance)){ |
|
| 476 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | - } |
|
| 478 | - else{ |
|
| 479 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | - } |
|
| 481 | - if (is_object($row)) |
|
| 482 | - { |
|
| 483 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | - } |
|
| 485 | - else |
|
| 486 | - { |
|
| 487 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - foreach ($this->has_many as $key => $value) |
|
| 493 | - { |
|
| 494 | - if (is_string($value)) |
|
| 495 | - { |
|
| 496 | - $relationship = $value; |
|
| 497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | - } |
|
| 499 | - else |
|
| 500 | - { |
|
| 501 | - $relationship = $key; |
|
| 502 | - $options = $value; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - if (in_array($relationship, $this->_with)) |
|
| 506 | - { |
|
| 507 | - if(is_object($this->loaderInstance)){ |
|
| 508 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | - } |
|
| 510 | - else{ |
|
| 511 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | - } |
|
| 513 | - if (is_object($row)) |
|
| 514 | - { |
|
| 515 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | - } |
|
| 517 | - else |
|
| 518 | - { |
|
| 519 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - return $row; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /* -------------------------------------------------------------- |
|
| 443 | + public function with($relationship) |
|
| 444 | + { |
|
| 445 | + $this->_with[] = $relationship; |
|
| 446 | + if (!in_array('relate', $this->after_get)) |
|
| 447 | + { |
|
| 448 | + $this->after_get[] = 'relate'; |
|
| 449 | + } |
|
| 450 | + return $this; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + public function relate($row) |
|
| 454 | + { |
|
| 455 | + if (empty($row)) |
|
| 456 | + { |
|
| 457 | + return $row; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + foreach ($this->belongs_to as $key => $value) |
|
| 461 | + { |
|
| 462 | + if (is_string($value)) |
|
| 463 | + { |
|
| 464 | + $relationship = $value; |
|
| 465 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | + } |
|
| 467 | + else |
|
| 468 | + { |
|
| 469 | + $relationship = $key; |
|
| 470 | + $options = $value; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + if (in_array($relationship, $this->_with)) |
|
| 474 | + { |
|
| 475 | + if(is_object($this->loaderInstance)){ |
|
| 476 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | + } |
|
| 478 | + else{ |
|
| 479 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | + } |
|
| 481 | + if (is_object($row)) |
|
| 482 | + { |
|
| 483 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | + } |
|
| 485 | + else |
|
| 486 | + { |
|
| 487 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + foreach ($this->has_many as $key => $value) |
|
| 493 | + { |
|
| 494 | + if (is_string($value)) |
|
| 495 | + { |
|
| 496 | + $relationship = $value; |
|
| 497 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | + } |
|
| 499 | + else |
|
| 500 | + { |
|
| 501 | + $relationship = $key; |
|
| 502 | + $options = $value; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + if (in_array($relationship, $this->_with)) |
|
| 506 | + { |
|
| 507 | + if(is_object($this->loaderInstance)){ |
|
| 508 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | + } |
|
| 510 | + else{ |
|
| 511 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | + } |
|
| 513 | + if (is_object($row)) |
|
| 514 | + { |
|
| 515 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | + } |
|
| 517 | + else |
|
| 518 | + { |
|
| 519 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + return $row; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /* -------------------------------------------------------------- |
|
| 527 | 527 | * UTILITY METHODS |
| 528 | 528 | * ------------------------------------------------------------ */ |
| 529 | 529 | |
| 530 | - /** |
|
| 531 | - * Retrieve and generate a form_dropdown friendly array |
|
| 532 | - */ |
|
| 533 | - public function dropdown() |
|
| 534 | - { |
|
| 535 | - $args = func_get_args(); |
|
| 536 | - if(count($args) == 2) |
|
| 537 | - { |
|
| 538 | - list($key, $value) = $args; |
|
| 539 | - } |
|
| 540 | - else |
|
| 541 | - { |
|
| 542 | - $key = $this->primary_key; |
|
| 543 | - $value = $args[0]; |
|
| 544 | - } |
|
| 545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | - { |
|
| 548 | - $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | - } |
|
| 550 | - $result = $this->_database->select(array($key, $value)) |
|
| 551 | - ->from($this->_table) |
|
| 552 | - ->getAll(); |
|
| 553 | - $options = array(); |
|
| 554 | - foreach ($result as $row) |
|
| 555 | - { |
|
| 556 | - $options[$row->{$key}] = $row->{$value}; |
|
| 557 | - } |
|
| 558 | - $options = $this->trigger('after_dropdown', $options); |
|
| 559 | - return $options; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | - */ |
|
| 565 | - public function count_by() |
|
| 566 | - { |
|
| 567 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | - { |
|
| 569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | - } |
|
| 571 | - $where = func_get_args(); |
|
| 572 | - $this->_set_where($where); |
|
| 573 | - $this->_database->from($this->_table)->getAll(); |
|
| 574 | - return $this->_database->numRows(); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | - */ |
|
| 580 | - public function count_all() |
|
| 581 | - { |
|
| 582 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | - { |
|
| 584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | - } |
|
| 586 | - $this->_database->from($this->_table)->getAll(); |
|
| 587 | - return $this->_database->numRows(); |
|
| 588 | - } |
|
| 530 | + /** |
|
| 531 | + * Retrieve and generate a form_dropdown friendly array |
|
| 532 | + */ |
|
| 533 | + public function dropdown() |
|
| 534 | + { |
|
| 535 | + $args = func_get_args(); |
|
| 536 | + if(count($args) == 2) |
|
| 537 | + { |
|
| 538 | + list($key, $value) = $args; |
|
| 539 | + } |
|
| 540 | + else |
|
| 541 | + { |
|
| 542 | + $key = $this->primary_key; |
|
| 543 | + $value = $args[0]; |
|
| 544 | + } |
|
| 545 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | + { |
|
| 548 | + $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | + } |
|
| 550 | + $result = $this->_database->select(array($key, $value)) |
|
| 551 | + ->from($this->_table) |
|
| 552 | + ->getAll(); |
|
| 553 | + $options = array(); |
|
| 554 | + foreach ($result as $row) |
|
| 555 | + { |
|
| 556 | + $options[$row->{$key}] = $row->{$value}; |
|
| 557 | + } |
|
| 558 | + $options = $this->trigger('after_dropdown', $options); |
|
| 559 | + return $options; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | + */ |
|
| 565 | + public function count_by() |
|
| 566 | + { |
|
| 567 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | + { |
|
| 569 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | + } |
|
| 571 | + $where = func_get_args(); |
|
| 572 | + $this->_set_where($where); |
|
| 573 | + $this->_database->from($this->_table)->getAll(); |
|
| 574 | + return $this->_database->numRows(); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | + */ |
|
| 580 | + public function count_all() |
|
| 581 | + { |
|
| 582 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | + { |
|
| 584 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | + } |
|
| 586 | + $this->_database->from($this->_table)->getAll(); |
|
| 587 | + return $this->_database->numRows(); |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | 590 | /** |
| 591 | - * Enabled cache temporary |
|
| 592 | - */ |
|
| 591 | + * Enabled cache temporary |
|
| 592 | + */ |
|
| 593 | 593 | public function cached($ttl = 0){ |
| 594 | 594 | if($ttl > 0){ |
| 595 | 595 | $this->_database = $this->_database->cached($ttl); |
@@ -597,390 +597,390 @@ discard block |
||
| 597 | 597 | return $this; |
| 598 | 598 | } |
| 599 | 599 | |
| 600 | - /** |
|
| 601 | - * Tell the class to skip the insert validation |
|
| 602 | - */ |
|
| 603 | - public function skip_validation() |
|
| 604 | - { |
|
| 605 | - $this->skip_validation = TRUE; |
|
| 606 | - return $this; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Get the skip validation status |
|
| 611 | - */ |
|
| 612 | - public function get_skip_validation() |
|
| 613 | - { |
|
| 614 | - return $this->skip_validation; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | - */ |
|
| 620 | - public function get_next_id() |
|
| 621 | - { |
|
| 622 | - return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | - ->from('information_schema.TABLES') |
|
| 624 | - ->where('TABLE_NAME', $this->_table) |
|
| 625 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Getter for the table name |
|
| 630 | - */ |
|
| 631 | - public function table() |
|
| 632 | - { |
|
| 633 | - return $this->_table; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /* -------------------------------------------------------------- |
|
| 600 | + /** |
|
| 601 | + * Tell the class to skip the insert validation |
|
| 602 | + */ |
|
| 603 | + public function skip_validation() |
|
| 604 | + { |
|
| 605 | + $this->skip_validation = TRUE; |
|
| 606 | + return $this; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Get the skip validation status |
|
| 611 | + */ |
|
| 612 | + public function get_skip_validation() |
|
| 613 | + { |
|
| 614 | + return $this->skip_validation; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | + */ |
|
| 620 | + public function get_next_id() |
|
| 621 | + { |
|
| 622 | + return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | + ->from('information_schema.TABLES') |
|
| 624 | + ->where('TABLE_NAME', $this->_table) |
|
| 625 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Getter for the table name |
|
| 630 | + */ |
|
| 631 | + public function table() |
|
| 632 | + { |
|
| 633 | + return $this->_table; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /* -------------------------------------------------------------- |
|
| 637 | 637 | * GLOBAL SCOPES |
| 638 | 638 | * ------------------------------------------------------------ */ |
| 639 | 639 | |
| 640 | - /** |
|
| 641 | - * Return the next call as an array rather than an object |
|
| 642 | - */ |
|
| 643 | - public function as_array() |
|
| 644 | - { |
|
| 645 | - $this->_temporary_return_type = 'array'; |
|
| 646 | - return $this; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Return the next call as an object rather than an array |
|
| 651 | - */ |
|
| 652 | - public function as_object() |
|
| 653 | - { |
|
| 654 | - $this->_temporary_return_type = 'object'; |
|
| 655 | - return $this; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Don't care about soft deleted rows on the next call |
|
| 660 | - */ |
|
| 661 | - public function with_deleted() |
|
| 662 | - { |
|
| 663 | - $this->_temporary_with_deleted = TRUE; |
|
| 664 | - return $this; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * Only get deleted rows on the next call |
|
| 669 | - */ |
|
| 670 | - public function only_deleted() |
|
| 671 | - { |
|
| 672 | - $this->_temporary_only_deleted = TRUE; |
|
| 673 | - return $this; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /* -------------------------------------------------------------- |
|
| 640 | + /** |
|
| 641 | + * Return the next call as an array rather than an object |
|
| 642 | + */ |
|
| 643 | + public function as_array() |
|
| 644 | + { |
|
| 645 | + $this->_temporary_return_type = 'array'; |
|
| 646 | + return $this; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Return the next call as an object rather than an array |
|
| 651 | + */ |
|
| 652 | + public function as_object() |
|
| 653 | + { |
|
| 654 | + $this->_temporary_return_type = 'object'; |
|
| 655 | + return $this; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Don't care about soft deleted rows on the next call |
|
| 660 | + */ |
|
| 661 | + public function with_deleted() |
|
| 662 | + { |
|
| 663 | + $this->_temporary_with_deleted = TRUE; |
|
| 664 | + return $this; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * Only get deleted rows on the next call |
|
| 669 | + */ |
|
| 670 | + public function only_deleted() |
|
| 671 | + { |
|
| 672 | + $this->_temporary_only_deleted = TRUE; |
|
| 673 | + return $this; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /* -------------------------------------------------------------- |
|
| 677 | 677 | * OBSERVERS |
| 678 | 678 | * ------------------------------------------------------------ */ |
| 679 | 679 | |
| 680 | - /** |
|
| 681 | - * MySQL DATETIME created_at and updated_at |
|
| 682 | - */ |
|
| 683 | - public function created_at($row) |
|
| 684 | - { |
|
| 685 | - if (is_object($row)) |
|
| 686 | - { |
|
| 687 | - $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | - } |
|
| 689 | - else |
|
| 690 | - { |
|
| 691 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - return $row; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - public function updated_at($row) |
|
| 698 | - { |
|
| 699 | - if (is_object($row)) |
|
| 700 | - { |
|
| 701 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | - } |
|
| 703 | - else |
|
| 704 | - { |
|
| 705 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | - } |
|
| 707 | - return $row; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Serialises data for you automatically, allowing you to pass |
|
| 712 | - * through objects and let it handle the serialisation in the background |
|
| 713 | - */ |
|
| 714 | - public function serialize($row) |
|
| 715 | - { |
|
| 716 | - foreach ($this->callback_parameters as $column) |
|
| 717 | - { |
|
| 718 | - $row[$column] = serialize($row[$column]); |
|
| 719 | - } |
|
| 720 | - return $row; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - public function unserialize($row) |
|
| 724 | - { |
|
| 725 | - foreach ($this->callback_parameters as $column) |
|
| 726 | - { |
|
| 727 | - if (is_array($row)) |
|
| 728 | - { |
|
| 729 | - $row[$column] = unserialize($row[$column]); |
|
| 730 | - } |
|
| 731 | - else |
|
| 732 | - { |
|
| 733 | - $row->$column = unserialize($row->$column); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - return $row; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Protect attributes by removing them from $row array |
|
| 741 | - */ |
|
| 742 | - public function protect_attributes($row) |
|
| 743 | - { |
|
| 744 | - foreach ($this->protected_attributes as $attr) |
|
| 745 | - { |
|
| 746 | - if (is_object($row)) |
|
| 747 | - { |
|
| 680 | + /** |
|
| 681 | + * MySQL DATETIME created_at and updated_at |
|
| 682 | + */ |
|
| 683 | + public function created_at($row) |
|
| 684 | + { |
|
| 685 | + if (is_object($row)) |
|
| 686 | + { |
|
| 687 | + $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | + } |
|
| 689 | + else |
|
| 690 | + { |
|
| 691 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + return $row; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + public function updated_at($row) |
|
| 698 | + { |
|
| 699 | + if (is_object($row)) |
|
| 700 | + { |
|
| 701 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | + } |
|
| 703 | + else |
|
| 704 | + { |
|
| 705 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | + } |
|
| 707 | + return $row; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Serialises data for you automatically, allowing you to pass |
|
| 712 | + * through objects and let it handle the serialisation in the background |
|
| 713 | + */ |
|
| 714 | + public function serialize($row) |
|
| 715 | + { |
|
| 716 | + foreach ($this->callback_parameters as $column) |
|
| 717 | + { |
|
| 718 | + $row[$column] = serialize($row[$column]); |
|
| 719 | + } |
|
| 720 | + return $row; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + public function unserialize($row) |
|
| 724 | + { |
|
| 725 | + foreach ($this->callback_parameters as $column) |
|
| 726 | + { |
|
| 727 | + if (is_array($row)) |
|
| 728 | + { |
|
| 729 | + $row[$column] = unserialize($row[$column]); |
|
| 730 | + } |
|
| 731 | + else |
|
| 732 | + { |
|
| 733 | + $row->$column = unserialize($row->$column); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + return $row; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Protect attributes by removing them from $row array |
|
| 741 | + */ |
|
| 742 | + public function protect_attributes($row) |
|
| 743 | + { |
|
| 744 | + foreach ($this->protected_attributes as $attr) |
|
| 745 | + { |
|
| 746 | + if (is_object($row)) |
|
| 747 | + { |
|
| 748 | 748 | if(isset($row->$attr)){ |
| 749 | 749 | unset($row->$attr); |
| 750 | 750 | } |
| 751 | - } |
|
| 752 | - else |
|
| 753 | - { |
|
| 751 | + } |
|
| 752 | + else |
|
| 753 | + { |
|
| 754 | 754 | if(isset($row[$attr])){ |
| 755 | 755 | unset($row[$attr]); |
| 756 | 756 | } |
| 757 | - } |
|
| 758 | - } |
|
| 759 | - return $row; |
|
| 760 | - } |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + return $row; |
|
| 760 | + } |
|
| 761 | 761 | |
| 762 | 762 | /** |
| 763 | - * Return the database instance |
|
| 764 | - * @return Database the database instance |
|
| 765 | - */ |
|
| 766 | - public function getDatabaseInstance(){ |
|
| 767 | - return $this->_database; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * set the Database instance for future use |
|
| 772 | - * @param Database $db the database object |
|
| 773 | - */ |
|
| 774 | - public function setDatabaseInstance($db){ |
|
| 775 | - $this->_database = $db; |
|
| 776 | - if($this->dbCacheTime > 0){ |
|
| 777 | - $this->_database->setCache($this->dbCacheTime); |
|
| 778 | - } |
|
| 779 | - return $this; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Return the loader instance |
|
| 784 | - * @return Loader the loader instance |
|
| 785 | - */ |
|
| 786 | - public function getLoader(){ |
|
| 787 | - return $this->loaderInstance; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * set the loader instance for future use |
|
| 792 | - * @param Loader $loader the loader object |
|
| 793 | - */ |
|
| 794 | - public function setLoader($loader){ |
|
| 795 | - $this->loaderInstance = $loader; |
|
| 796 | - return $this; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - /** |
|
| 800 | - * Return the FormValidation instance |
|
| 801 | - * @return FormValidation the form validation instance |
|
| 802 | - */ |
|
| 803 | - public function getFormValidation(){ |
|
| 804 | - return $this->formValidationInstance; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * set the form validation instance for future use |
|
| 809 | - * @param FormValidation $fv the form validation object |
|
| 810 | - */ |
|
| 811 | - public function setFormValidation($fv){ |
|
| 812 | - $this->formValidationInstance = $fv; |
|
| 813 | - return $this; |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - /* -------------------------------------------------------------- |
|
| 763 | + * Return the database instance |
|
| 764 | + * @return Database the database instance |
|
| 765 | + */ |
|
| 766 | + public function getDatabaseInstance(){ |
|
| 767 | + return $this->_database; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * set the Database instance for future use |
|
| 772 | + * @param Database $db the database object |
|
| 773 | + */ |
|
| 774 | + public function setDatabaseInstance($db){ |
|
| 775 | + $this->_database = $db; |
|
| 776 | + if($this->dbCacheTime > 0){ |
|
| 777 | + $this->_database->setCache($this->dbCacheTime); |
|
| 778 | + } |
|
| 779 | + return $this; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Return the loader instance |
|
| 784 | + * @return Loader the loader instance |
|
| 785 | + */ |
|
| 786 | + public function getLoader(){ |
|
| 787 | + return $this->loaderInstance; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * set the loader instance for future use |
|
| 792 | + * @param Loader $loader the loader object |
|
| 793 | + */ |
|
| 794 | + public function setLoader($loader){ |
|
| 795 | + $this->loaderInstance = $loader; |
|
| 796 | + return $this; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + /** |
|
| 800 | + * Return the FormValidation instance |
|
| 801 | + * @return FormValidation the form validation instance |
|
| 802 | + */ |
|
| 803 | + public function getFormValidation(){ |
|
| 804 | + return $this->formValidationInstance; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * set the form validation instance for future use |
|
| 809 | + * @param FormValidation $fv the form validation object |
|
| 810 | + */ |
|
| 811 | + public function setFormValidation($fv){ |
|
| 812 | + $this->formValidationInstance = $fv; |
|
| 813 | + return $this; |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + /* -------------------------------------------------------------- |
|
| 817 | 817 | * QUERY BUILDER DIRECT ACCESS METHODS |
| 818 | 818 | * ------------------------------------------------------------ */ |
| 819 | 819 | |
| 820 | - /** |
|
| 821 | - * A wrapper to $this->_database->orderBy() |
|
| 822 | - */ |
|
| 823 | - public function order_by($criteria, $order = 'ASC') |
|
| 824 | - { |
|
| 825 | - if ( is_array($criteria) ) |
|
| 826 | - { |
|
| 827 | - foreach ($criteria as $key => $value) |
|
| 828 | - { |
|
| 829 | - $this->_database->orderBy($key, $value); |
|
| 830 | - } |
|
| 831 | - } |
|
| 832 | - else |
|
| 833 | - { |
|
| 834 | - $this->_database->orderBy($criteria, $order); |
|
| 835 | - } |
|
| 836 | - return $this; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * A wrapper to $this->_database->limit() |
|
| 841 | - */ |
|
| 842 | - public function limit($offset = 0, $limit = 10) |
|
| 843 | - { |
|
| 844 | - $this->_database->limit($offset, $limit); |
|
| 845 | - return $this; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /* -------------------------------------------------------------- |
|
| 820 | + /** |
|
| 821 | + * A wrapper to $this->_database->orderBy() |
|
| 822 | + */ |
|
| 823 | + public function order_by($criteria, $order = 'ASC') |
|
| 824 | + { |
|
| 825 | + if ( is_array($criteria) ) |
|
| 826 | + { |
|
| 827 | + foreach ($criteria as $key => $value) |
|
| 828 | + { |
|
| 829 | + $this->_database->orderBy($key, $value); |
|
| 830 | + } |
|
| 831 | + } |
|
| 832 | + else |
|
| 833 | + { |
|
| 834 | + $this->_database->orderBy($criteria, $order); |
|
| 835 | + } |
|
| 836 | + return $this; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * A wrapper to $this->_database->limit() |
|
| 841 | + */ |
|
| 842 | + public function limit($offset = 0, $limit = 10) |
|
| 843 | + { |
|
| 844 | + $this->_database->limit($offset, $limit); |
|
| 845 | + return $this; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /* -------------------------------------------------------------- |
|
| 849 | 849 | * INTERNAL METHODS |
| 850 | 850 | * ------------------------------------------------------------ */ |
| 851 | 851 | |
| 852 | - /** |
|
| 853 | - * Trigger an event and call its observers. Pass through the event name |
|
| 854 | - * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | - * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | - */ |
|
| 857 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | - { |
|
| 859 | - if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | - { |
|
| 861 | - foreach ($this->$event as $method) |
|
| 862 | - { |
|
| 863 | - if (strpos($method, '(')) |
|
| 864 | - { |
|
| 865 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | - |
|
| 867 | - $method = $matches[1]; |
|
| 868 | - $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | - } |
|
| 870 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | - } |
|
| 872 | - } |
|
| 873 | - return $data; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - /** |
|
| 877 | - * Run validation on the passed data |
|
| 878 | - */ |
|
| 879 | - protected function validate(array $data) |
|
| 880 | - { |
|
| 881 | - if($this->skip_validation) |
|
| 882 | - { |
|
| 883 | - return $data; |
|
| 884 | - } |
|
| 885 | - if(!empty($this->validate)) |
|
| 886 | - { |
|
| 887 | - $fv = null; |
|
| 888 | - if(is_object($this->formValidationInstance)){ |
|
| 889 | - $fv = $this->formValidationInstance; |
|
| 890 | - } |
|
| 891 | - else{ |
|
| 892 | - Loader::library('FormValidation'); |
|
| 893 | - $fv = $this->formvalidation; |
|
| 894 | - $this->setFormValidation($fv); |
|
| 895 | - } |
|
| 896 | - if(is_array($this->validate)) |
|
| 897 | - { |
|
| 898 | - $fv->setData($data); |
|
| 899 | - $fv->setRules($this->validate); |
|
| 900 | - |
|
| 901 | - if ($fv->run()) |
|
| 902 | - { |
|
| 903 | - return $data; |
|
| 904 | - } |
|
| 905 | - else |
|
| 906 | - { |
|
| 907 | - return FALSE; |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - else { |
|
| 911 | - return $data; |
|
| 912 | - } |
|
| 913 | - } |
|
| 914 | - else |
|
| 915 | - { |
|
| 916 | - return $data; |
|
| 917 | - } |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Set WHERE parameters, cleverly |
|
| 923 | - */ |
|
| 924 | - protected function _set_where($params) |
|
| 925 | - { |
|
| 926 | - if (count($params) == 1 && is_array($params[0])) |
|
| 927 | - { |
|
| 928 | - foreach ($params[0] as $field => $filter) |
|
| 929 | - { |
|
| 930 | - if (is_array($filter)) |
|
| 931 | - { |
|
| 932 | - $this->_database->in($field, $filter); |
|
| 933 | - } |
|
| 934 | - else |
|
| 935 | - { |
|
| 936 | - if (is_int($field)) |
|
| 937 | - { |
|
| 938 | - $this->_database->where($filter); |
|
| 939 | - } |
|
| 940 | - else |
|
| 941 | - { |
|
| 942 | - $this->_database->where($field, $filter); |
|
| 943 | - } |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - } |
|
| 947 | - else if (count($params) == 1) |
|
| 948 | - { |
|
| 949 | - $this->_database->where($params[0]); |
|
| 950 | - } |
|
| 951 | - else if(count($params) == 2) |
|
| 952 | - { |
|
| 953 | - if (is_array($params[1])) |
|
| 954 | - { |
|
| 955 | - $this->_database->in($params[0], $params[1]); |
|
| 956 | - } |
|
| 957 | - else |
|
| 958 | - { |
|
| 959 | - $this->_database->where($params[0], $params[1]); |
|
| 960 | - } |
|
| 961 | - } |
|
| 962 | - else if(count($params) == 3) |
|
| 963 | - { |
|
| 964 | - $this->_database->where($params[0], $params[1], $params[2]); |
|
| 965 | - } |
|
| 966 | - else |
|
| 967 | - { |
|
| 968 | - if (is_array($params[1])) |
|
| 969 | - { |
|
| 970 | - $this->_database->in($params[0], $params[1]); |
|
| 971 | - } |
|
| 972 | - else |
|
| 973 | - { |
|
| 974 | - $this->_database->where($params[0], $params[1]); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - /** |
|
| 852 | + /** |
|
| 853 | + * Trigger an event and call its observers. Pass through the event name |
|
| 854 | + * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | + * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | + */ |
|
| 857 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | + { |
|
| 859 | + if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | + { |
|
| 861 | + foreach ($this->$event as $method) |
|
| 862 | + { |
|
| 863 | + if (strpos($method, '(')) |
|
| 864 | + { |
|
| 865 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | + |
|
| 867 | + $method = $matches[1]; |
|
| 868 | + $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | + } |
|
| 870 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | + } |
|
| 872 | + } |
|
| 873 | + return $data; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + /** |
|
| 877 | + * Run validation on the passed data |
|
| 878 | + */ |
|
| 879 | + protected function validate(array $data) |
|
| 880 | + { |
|
| 881 | + if($this->skip_validation) |
|
| 882 | + { |
|
| 883 | + return $data; |
|
| 884 | + } |
|
| 885 | + if(!empty($this->validate)) |
|
| 886 | + { |
|
| 887 | + $fv = null; |
|
| 888 | + if(is_object($this->formValidationInstance)){ |
|
| 889 | + $fv = $this->formValidationInstance; |
|
| 890 | + } |
|
| 891 | + else{ |
|
| 892 | + Loader::library('FormValidation'); |
|
| 893 | + $fv = $this->formvalidation; |
|
| 894 | + $this->setFormValidation($fv); |
|
| 895 | + } |
|
| 896 | + if(is_array($this->validate)) |
|
| 897 | + { |
|
| 898 | + $fv->setData($data); |
|
| 899 | + $fv->setRules($this->validate); |
|
| 900 | + |
|
| 901 | + if ($fv->run()) |
|
| 902 | + { |
|
| 903 | + return $data; |
|
| 904 | + } |
|
| 905 | + else |
|
| 906 | + { |
|
| 907 | + return FALSE; |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + else { |
|
| 911 | + return $data; |
|
| 912 | + } |
|
| 913 | + } |
|
| 914 | + else |
|
| 915 | + { |
|
| 916 | + return $data; |
|
| 917 | + } |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Set WHERE parameters, cleverly |
|
| 923 | + */ |
|
| 924 | + protected function _set_where($params) |
|
| 925 | + { |
|
| 926 | + if (count($params) == 1 && is_array($params[0])) |
|
| 927 | + { |
|
| 928 | + foreach ($params[0] as $field => $filter) |
|
| 929 | + { |
|
| 930 | + if (is_array($filter)) |
|
| 931 | + { |
|
| 932 | + $this->_database->in($field, $filter); |
|
| 933 | + } |
|
| 934 | + else |
|
| 935 | + { |
|
| 936 | + if (is_int($field)) |
|
| 937 | + { |
|
| 938 | + $this->_database->where($filter); |
|
| 939 | + } |
|
| 940 | + else |
|
| 941 | + { |
|
| 942 | + $this->_database->where($field, $filter); |
|
| 943 | + } |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + } |
|
| 947 | + else if (count($params) == 1) |
|
| 948 | + { |
|
| 949 | + $this->_database->where($params[0]); |
|
| 950 | + } |
|
| 951 | + else if(count($params) == 2) |
|
| 952 | + { |
|
| 953 | + if (is_array($params[1])) |
|
| 954 | + { |
|
| 955 | + $this->_database->in($params[0], $params[1]); |
|
| 956 | + } |
|
| 957 | + else |
|
| 958 | + { |
|
| 959 | + $this->_database->where($params[0], $params[1]); |
|
| 960 | + } |
|
| 961 | + } |
|
| 962 | + else if(count($params) == 3) |
|
| 963 | + { |
|
| 964 | + $this->_database->where($params[0], $params[1], $params[2]); |
|
| 965 | + } |
|
| 966 | + else |
|
| 967 | + { |
|
| 968 | + if (is_array($params[1])) |
|
| 969 | + { |
|
| 970 | + $this->_database->in($params[0], $params[1]); |
|
| 971 | + } |
|
| 972 | + else |
|
| 973 | + { |
|
| 974 | + $this->_database->where($params[0], $params[1]); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | 980 | Shortcut to controller |
| 981 | - */ |
|
| 982 | - public function __get($key){ |
|
| 983 | - return get_instance()->{$key}; |
|
| 984 | - } |
|
| 981 | + */ |
|
| 982 | + public function __get($key){ |
|
| 983 | + return get_instance()->{$key}; |
|
| 984 | + } |
|
| 985 | 985 | |
| 986 | - } |
|
| 986 | + } |
|
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | - class Model{ |
|
| 36 | + class Model { |
|
| 37 | 37 | |
| 38 | 38 | /* -------------------------------------------------------------- |
| 39 | 39 | * VARIABLES |
@@ -140,13 +140,13 @@ discard block |
||
| 140 | 140 | * Initialise the model, tie into the CodeIgniter superobject and |
| 141 | 141 | * try our best to guess the table name. |
| 142 | 142 | */ |
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if(is_object($db)){ |
|
| 143 | + public function __construct(Database $db = null) { |
|
| 144 | + if (is_object($db)) { |
|
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | 146 | } |
| 147 | - else{ |
|
| 147 | + else { |
|
| 148 | 148 | $obj = & get_instance(); |
| 149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
| 149 | + if (isset($obj->database) && is_object($obj->database)) { |
|
| 150 | 150 | /** |
| 151 | 151 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
| 152 | 152 | * to prevent duplication |
@@ -182,13 +182,13 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 184 | 184 | { |
| 185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 185 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | $this->_set_where($where); |
| 189 | 189 | |
| 190 | 190 | $this->trigger('before_get'); |
| 191 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 191 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 192 | 192 | $row = $this->_database->from($this->_table)->get($type); |
| 193 | 193 | $this->_temporary_return_type = $this->return_type; |
| 194 | 194 | $row = $this->trigger('after_get', $row); |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | $this->trigger('before_get'); |
| 225 | 225 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 226 | 226 | { |
| 227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 227 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 228 | 228 | } |
| 229 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 229 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 230 | 230 | $result = $this->_database->from($this->_table)->getAll($type); |
| 231 | 231 | $this->_temporary_return_type = $this->return_type; |
| 232 | 232 | |
@@ -332,13 +332,13 @@ discard block |
||
| 332 | 332 | { |
| 333 | 333 | $args = func_get_args(); |
| 334 | 334 | $data = array(); |
| 335 | - if(count($args) == 2){ |
|
| 336 | - if(is_array($args[1])){ |
|
| 335 | + if (count($args) == 2) { |
|
| 336 | + if (is_array($args[1])) { |
|
| 337 | 337 | $data = array_pop($args); |
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | - else if(count($args) == 3){ |
|
| 341 | - if(is_array($args[2])){ |
|
| 340 | + else if (count($args) == 3) { |
|
| 341 | + if (is_array($args[2])) { |
|
| 342 | 342 | $data = array_pop($args); |
| 343 | 343 | } |
| 344 | 344 | } |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | $this->_database->where($this->primary_key, $id); |
| 377 | 377 | if ($this->soft_delete) |
| 378 | 378 | { |
| 379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 379 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 380 | 380 | } |
| 381 | 381 | else |
| 382 | 382 | { |
@@ -397,7 +397,7 @@ discard block |
||
| 397 | 397 | $this->_set_where($where); |
| 398 | 398 | if ($this->soft_delete) |
| 399 | 399 | { |
| 400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 400 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 401 | 401 | } |
| 402 | 402 | else |
| 403 | 403 | { |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | $this->_database->in($this->primary_key, $primary_values); |
| 417 | 417 | if ($this->soft_delete) |
| 418 | 418 | { |
| 419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 419 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 420 | 420 | } |
| 421 | 421 | else |
| 422 | 422 | { |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | if (is_string($value)) |
| 463 | 463 | { |
| 464 | 464 | $relationship = $value; |
| 465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 465 | + $options = array('primary_key' => $value . '_id', 'model' => $value . '_model'); |
|
| 466 | 466 | } |
| 467 | 467 | else |
| 468 | 468 | { |
@@ -472,10 +472,10 @@ discard block |
||
| 472 | 472 | |
| 473 | 473 | if (in_array($relationship, $this->_with)) |
| 474 | 474 | { |
| 475 | - if(is_object($this->loaderInstance)){ |
|
| 475 | + if (is_object($this->loaderInstance)) { |
|
| 476 | 476 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 477 | 477 | } |
| 478 | - else{ |
|
| 478 | + else { |
|
| 479 | 479 | Loader::model($options['model'], $relationship . '_model'); |
| 480 | 480 | } |
| 481 | 481 | if (is_object($row)) |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | if (is_string($value)) |
| 495 | 495 | { |
| 496 | 496 | $relationship = $value; |
| 497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 497 | + $options = array('primary_key' => $this->_table . '_id', 'model' => $value . '_model'); |
|
| 498 | 498 | } |
| 499 | 499 | else |
| 500 | 500 | { |
@@ -504,10 +504,10 @@ discard block |
||
| 504 | 504 | |
| 505 | 505 | if (in_array($relationship, $this->_with)) |
| 506 | 506 | { |
| 507 | - if(is_object($this->loaderInstance)){ |
|
| 507 | + if (is_object($this->loaderInstance)) { |
|
| 508 | 508 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 509 | 509 | } |
| 510 | - else{ |
|
| 510 | + else { |
|
| 511 | 511 | Loader::model($options['model'], $relationship . '_model'); |
| 512 | 512 | } |
| 513 | 513 | if (is_object($row)) |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | public function dropdown() |
| 534 | 534 | { |
| 535 | 535 | $args = func_get_args(); |
| 536 | - if(count($args) == 2) |
|
| 536 | + if (count($args) == 2) |
|
| 537 | 537 | { |
| 538 | 538 | list($key, $value) = $args; |
| 539 | 539 | } |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | $key = $this->primary_key; |
| 543 | 543 | $value = $args[0]; |
| 544 | 544 | } |
| 545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 545 | + $this->trigger('before_dropdown', array($key, $value)); |
|
| 546 | 546 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 547 | 547 | { |
| 548 | 548 | $this->_database->where($this->soft_delete_key, FALSE); |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | { |
| 567 | 567 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 568 | 568 | { |
| 569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 569 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 570 | 570 | } |
| 571 | 571 | $where = func_get_args(); |
| 572 | 572 | $this->_set_where($where); |
@@ -581,7 +581,7 @@ discard block |
||
| 581 | 581 | { |
| 582 | 582 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 583 | 583 | { |
| 584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 584 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 585 | 585 | } |
| 586 | 586 | $this->_database->from($this->_table)->getAll(); |
| 587 | 587 | return $this->_database->numRows(); |
@@ -590,8 +590,8 @@ discard block |
||
| 590 | 590 | /** |
| 591 | 591 | * Enabled cache temporary |
| 592 | 592 | */ |
| 593 | - public function cached($ttl = 0){ |
|
| 594 | - if($ttl > 0){ |
|
| 593 | + public function cached($ttl = 0) { |
|
| 594 | + if ($ttl > 0) { |
|
| 595 | 595 | $this->_database = $this->_database->cached($ttl); |
| 596 | 596 | } |
| 597 | 597 | return $this; |
@@ -745,13 +745,13 @@ discard block |
||
| 745 | 745 | { |
| 746 | 746 | if (is_object($row)) |
| 747 | 747 | { |
| 748 | - if(isset($row->$attr)){ |
|
| 748 | + if (isset($row->$attr)) { |
|
| 749 | 749 | unset($row->$attr); |
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | else |
| 753 | 753 | { |
| 754 | - if(isset($row[$attr])){ |
|
| 754 | + if (isset($row[$attr])) { |
|
| 755 | 755 | unset($row[$attr]); |
| 756 | 756 | } |
| 757 | 757 | } |
@@ -763,7 +763,7 @@ discard block |
||
| 763 | 763 | * Return the database instance |
| 764 | 764 | * @return Database the database instance |
| 765 | 765 | */ |
| 766 | - public function getDatabaseInstance(){ |
|
| 766 | + public function getDatabaseInstance() { |
|
| 767 | 767 | return $this->_database; |
| 768 | 768 | } |
| 769 | 769 | |
@@ -771,9 +771,9 @@ discard block |
||
| 771 | 771 | * set the Database instance for future use |
| 772 | 772 | * @param Database $db the database object |
| 773 | 773 | */ |
| 774 | - public function setDatabaseInstance($db){ |
|
| 774 | + public function setDatabaseInstance($db) { |
|
| 775 | 775 | $this->_database = $db; |
| 776 | - if($this->dbCacheTime > 0){ |
|
| 776 | + if ($this->dbCacheTime > 0) { |
|
| 777 | 777 | $this->_database->setCache($this->dbCacheTime); |
| 778 | 778 | } |
| 779 | 779 | return $this; |
@@ -783,7 +783,7 @@ discard block |
||
| 783 | 783 | * Return the loader instance |
| 784 | 784 | * @return Loader the loader instance |
| 785 | 785 | */ |
| 786 | - public function getLoader(){ |
|
| 786 | + public function getLoader() { |
|
| 787 | 787 | return $this->loaderInstance; |
| 788 | 788 | } |
| 789 | 789 | |
@@ -791,7 +791,7 @@ discard block |
||
| 791 | 791 | * set the loader instance for future use |
| 792 | 792 | * @param Loader $loader the loader object |
| 793 | 793 | */ |
| 794 | - public function setLoader($loader){ |
|
| 794 | + public function setLoader($loader) { |
|
| 795 | 795 | $this->loaderInstance = $loader; |
| 796 | 796 | return $this; |
| 797 | 797 | } |
@@ -800,7 +800,7 @@ discard block |
||
| 800 | 800 | * Return the FormValidation instance |
| 801 | 801 | * @return FormValidation the form validation instance |
| 802 | 802 | */ |
| 803 | - public function getFormValidation(){ |
|
| 803 | + public function getFormValidation() { |
|
| 804 | 804 | return $this->formValidationInstance; |
| 805 | 805 | } |
| 806 | 806 | |
@@ -808,7 +808,7 @@ discard block |
||
| 808 | 808 | * set the form validation instance for future use |
| 809 | 809 | * @param FormValidation $fv the form validation object |
| 810 | 810 | */ |
| 811 | - public function setFormValidation($fv){ |
|
| 811 | + public function setFormValidation($fv) { |
|
| 812 | 812 | $this->formValidationInstance = $fv; |
| 813 | 813 | return $this; |
| 814 | 814 | } |
@@ -822,7 +822,7 @@ discard block |
||
| 822 | 822 | */ |
| 823 | 823 | public function order_by($criteria, $order = 'ASC') |
| 824 | 824 | { |
| 825 | - if ( is_array($criteria) ) |
|
| 825 | + if (is_array($criteria)) |
|
| 826 | 826 | { |
| 827 | 827 | foreach ($criteria as $key => $value) |
| 828 | 828 | { |
@@ -878,22 +878,22 @@ discard block |
||
| 878 | 878 | */ |
| 879 | 879 | protected function validate(array $data) |
| 880 | 880 | { |
| 881 | - if($this->skip_validation) |
|
| 881 | + if ($this->skip_validation) |
|
| 882 | 882 | { |
| 883 | 883 | return $data; |
| 884 | 884 | } |
| 885 | - if(!empty($this->validate)) |
|
| 885 | + if (!empty($this->validate)) |
|
| 886 | 886 | { |
| 887 | 887 | $fv = null; |
| 888 | - if(is_object($this->formValidationInstance)){ |
|
| 888 | + if (is_object($this->formValidationInstance)) { |
|
| 889 | 889 | $fv = $this->formValidationInstance; |
| 890 | 890 | } |
| 891 | - else{ |
|
| 891 | + else { |
|
| 892 | 892 | Loader::library('FormValidation'); |
| 893 | 893 | $fv = $this->formvalidation; |
| 894 | 894 | $this->setFormValidation($fv); |
| 895 | 895 | } |
| 896 | - if(is_array($this->validate)) |
|
| 896 | + if (is_array($this->validate)) |
|
| 897 | 897 | { |
| 898 | 898 | $fv->setData($data); |
| 899 | 899 | $fv->setRules($this->validate); |
@@ -948,7 +948,7 @@ discard block |
||
| 948 | 948 | { |
| 949 | 949 | $this->_database->where($params[0]); |
| 950 | 950 | } |
| 951 | - else if(count($params) == 2) |
|
| 951 | + else if (count($params) == 2) |
|
| 952 | 952 | { |
| 953 | 953 | if (is_array($params[1])) |
| 954 | 954 | { |
@@ -959,7 +959,7 @@ discard block |
||
| 959 | 959 | $this->_database->where($params[0], $params[1]); |
| 960 | 960 | } |
| 961 | 961 | } |
| 962 | - else if(count($params) == 3) |
|
| 962 | + else if (count($params) == 3) |
|
| 963 | 963 | { |
| 964 | 964 | $this->_database->where($params[0], $params[1], $params[2]); |
| 965 | 965 | } |
@@ -979,7 +979,7 @@ discard block |
||
| 979 | 979 | /** |
| 980 | 980 | Shortcut to controller |
| 981 | 981 | */ |
| 982 | - public function __get($key){ |
|
| 982 | + public function __get($key) { |
|
| 983 | 983 | return get_instance()->{$key}; |
| 984 | 984 | } |
| 985 | 985 | |
@@ -143,8 +143,7 @@ discard block |
||
| 143 | 143 | public function __construct(Database $db = null){ |
| 144 | 144 | if(is_object($db)){ |
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | - } |
|
| 147 | - else{ |
|
| 146 | + } else{ |
|
| 148 | 147 | $obj = & get_instance(); |
| 149 | 148 | if(isset($obj->database) && is_object($obj->database)){ |
| 150 | 149 | /** |
@@ -256,8 +255,7 @@ discard block |
||
| 256 | 255 | $insert_id = $this->_database->insertId(); |
| 257 | 256 | $this->trigger('after_create', $insert_id); |
| 258 | 257 | return $insert_id; |
| 259 | - } |
|
| 260 | - else |
|
| 258 | + } else |
|
| 261 | 259 | { |
| 262 | 260 | return FALSE; |
| 263 | 261 | } |
@@ -294,8 +292,7 @@ discard block |
||
| 294 | 292 | ->update($data, $escape); |
| 295 | 293 | $this->trigger('after_update', array($data, $result)); |
| 296 | 294 | return $result; |
| 297 | - } |
|
| 298 | - else |
|
| 295 | + } else |
|
| 299 | 296 | { |
| 300 | 297 | return FALSE; |
| 301 | 298 | } |
@@ -318,8 +315,7 @@ discard block |
||
| 318 | 315 | ->update($data, $escape); |
| 319 | 316 | $this->trigger('after_update', array($data, $result)); |
| 320 | 317 | return $result; |
| 321 | - } |
|
| 322 | - else |
|
| 318 | + } else |
|
| 323 | 319 | { |
| 324 | 320 | return FALSE; |
| 325 | 321 | } |
@@ -336,8 +332,7 @@ discard block |
||
| 336 | 332 | if(is_array($args[1])){ |
| 337 | 333 | $data = array_pop($args); |
| 338 | 334 | } |
| 339 | - } |
|
| 340 | - else if(count($args) == 3){ |
|
| 335 | + } else if(count($args) == 3){ |
|
| 341 | 336 | if(is_array($args[2])){ |
| 342 | 337 | $data = array_pop($args); |
| 343 | 338 | } |
@@ -349,8 +344,7 @@ discard block |
||
| 349 | 344 | $result = $this->_database->from($this->_table)->update($data); |
| 350 | 345 | $this->trigger('after_update', array($data, $result)); |
| 351 | 346 | return $result; |
| 352 | - } |
|
| 353 | - else |
|
| 347 | + } else |
|
| 354 | 348 | { |
| 355 | 349 | return FALSE; |
| 356 | 350 | } |
@@ -377,8 +371,7 @@ discard block |
||
| 377 | 371 | if ($this->soft_delete) |
| 378 | 372 | { |
| 379 | 373 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 380 | - } |
|
| 381 | - else |
|
| 374 | + } else |
|
| 382 | 375 | { |
| 383 | 376 | $result = $this->_database->from($this->_table)->delete(); |
| 384 | 377 | } |
@@ -398,8 +391,7 @@ discard block |
||
| 398 | 391 | if ($this->soft_delete) |
| 399 | 392 | { |
| 400 | 393 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 401 | - } |
|
| 402 | - else |
|
| 394 | + } else |
|
| 403 | 395 | { |
| 404 | 396 | $result = $this->_database->from($this->_table)->delete(); |
| 405 | 397 | } |
@@ -417,8 +409,7 @@ discard block |
||
| 417 | 409 | if ($this->soft_delete) |
| 418 | 410 | { |
| 419 | 411 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 420 | - } |
|
| 421 | - else |
|
| 412 | + } else |
|
| 422 | 413 | { |
| 423 | 414 | $result = $this->_database->from($this->_table)->delete(); |
| 424 | 415 | } |
@@ -463,8 +454,7 @@ discard block |
||
| 463 | 454 | { |
| 464 | 455 | $relationship = $value; |
| 465 | 456 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
| 466 | - } |
|
| 467 | - else |
|
| 457 | + } else |
|
| 468 | 458 | { |
| 469 | 459 | $relationship = $key; |
| 470 | 460 | $options = $value; |
@@ -474,15 +464,13 @@ discard block |
||
| 474 | 464 | { |
| 475 | 465 | if(is_object($this->loaderInstance)){ |
| 476 | 466 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 477 | - } |
|
| 478 | - else{ |
|
| 467 | + } else{ |
|
| 479 | 468 | Loader::model($options['model'], $relationship . '_model'); |
| 480 | 469 | } |
| 481 | 470 | if (is_object($row)) |
| 482 | 471 | { |
| 483 | 472 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
| 484 | - } |
|
| 485 | - else |
|
| 473 | + } else |
|
| 486 | 474 | { |
| 487 | 475 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
| 488 | 476 | } |
@@ -495,8 +483,7 @@ discard block |
||
| 495 | 483 | { |
| 496 | 484 | $relationship = $value; |
| 497 | 485 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
| 498 | - } |
|
| 499 | - else |
|
| 486 | + } else |
|
| 500 | 487 | { |
| 501 | 488 | $relationship = $key; |
| 502 | 489 | $options = $value; |
@@ -506,15 +493,13 @@ discard block |
||
| 506 | 493 | { |
| 507 | 494 | if(is_object($this->loaderInstance)){ |
| 508 | 495 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 509 | - } |
|
| 510 | - else{ |
|
| 496 | + } else{ |
|
| 511 | 497 | Loader::model($options['model'], $relationship . '_model'); |
| 512 | 498 | } |
| 513 | 499 | if (is_object($row)) |
| 514 | 500 | { |
| 515 | 501 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
| 516 | - } |
|
| 517 | - else |
|
| 502 | + } else |
|
| 518 | 503 | { |
| 519 | 504 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
| 520 | 505 | } |
@@ -536,8 +521,7 @@ discard block |
||
| 536 | 521 | if(count($args) == 2) |
| 537 | 522 | { |
| 538 | 523 | list($key, $value) = $args; |
| 539 | - } |
|
| 540 | - else |
|
| 524 | + } else |
|
| 541 | 525 | { |
| 542 | 526 | $key = $this->primary_key; |
| 543 | 527 | $value = $args[0]; |
@@ -685,8 +669,7 @@ discard block |
||
| 685 | 669 | if (is_object($row)) |
| 686 | 670 | { |
| 687 | 671 | $row->created_at = date('Y-m-d H:i:s'); |
| 688 | - } |
|
| 689 | - else |
|
| 672 | + } else |
|
| 690 | 673 | { |
| 691 | 674 | $row['created_at'] = date('Y-m-d H:i:s'); |
| 692 | 675 | } |
@@ -699,8 +682,7 @@ discard block |
||
| 699 | 682 | if (is_object($row)) |
| 700 | 683 | { |
| 701 | 684 | $row->updated_at = date('Y-m-d H:i:s'); |
| 702 | - } |
|
| 703 | - else |
|
| 685 | + } else |
|
| 704 | 686 | { |
| 705 | 687 | $row['updated_at'] = date('Y-m-d H:i:s'); |
| 706 | 688 | } |
@@ -727,8 +709,7 @@ discard block |
||
| 727 | 709 | if (is_array($row)) |
| 728 | 710 | { |
| 729 | 711 | $row[$column] = unserialize($row[$column]); |
| 730 | - } |
|
| 731 | - else |
|
| 712 | + } else |
|
| 732 | 713 | { |
| 733 | 714 | $row->$column = unserialize($row->$column); |
| 734 | 715 | } |
@@ -748,8 +729,7 @@ discard block |
||
| 748 | 729 | if(isset($row->$attr)){ |
| 749 | 730 | unset($row->$attr); |
| 750 | 731 | } |
| 751 | - } |
|
| 752 | - else |
|
| 732 | + } else |
|
| 753 | 733 | { |
| 754 | 734 | if(isset($row[$attr])){ |
| 755 | 735 | unset($row[$attr]); |
@@ -828,8 +808,7 @@ discard block |
||
| 828 | 808 | { |
| 829 | 809 | $this->_database->orderBy($key, $value); |
| 830 | 810 | } |
| 831 | - } |
|
| 832 | - else |
|
| 811 | + } else |
|
| 833 | 812 | { |
| 834 | 813 | $this->_database->orderBy($criteria, $order); |
| 835 | 814 | } |
@@ -887,8 +866,7 @@ discard block |
||
| 887 | 866 | $fv = null; |
| 888 | 867 | if(is_object($this->formValidationInstance)){ |
| 889 | 868 | $fv = $this->formValidationInstance; |
| 890 | - } |
|
| 891 | - else{ |
|
| 869 | + } else{ |
|
| 892 | 870 | Loader::library('FormValidation'); |
| 893 | 871 | $fv = $this->formvalidation; |
| 894 | 872 | $this->setFormValidation($fv); |
@@ -901,17 +879,14 @@ discard block |
||
| 901 | 879 | if ($fv->run()) |
| 902 | 880 | { |
| 903 | 881 | return $data; |
| 904 | - } |
|
| 905 | - else |
|
| 882 | + } else |
|
| 906 | 883 | { |
| 907 | 884 | return FALSE; |
| 908 | 885 | } |
| 909 | - } |
|
| 910 | - else { |
|
| 886 | + } else { |
|
| 911 | 887 | return $data; |
| 912 | 888 | } |
| 913 | - } |
|
| 914 | - else |
|
| 889 | + } else |
|
| 915 | 890 | { |
| 916 | 891 | return $data; |
| 917 | 892 | } |
@@ -930,46 +905,38 @@ discard block |
||
| 930 | 905 | if (is_array($filter)) |
| 931 | 906 | { |
| 932 | 907 | $this->_database->in($field, $filter); |
| 933 | - } |
|
| 934 | - else |
|
| 908 | + } else |
|
| 935 | 909 | { |
| 936 | 910 | if (is_int($field)) |
| 937 | 911 | { |
| 938 | 912 | $this->_database->where($filter); |
| 939 | - } |
|
| 940 | - else |
|
| 913 | + } else |
|
| 941 | 914 | { |
| 942 | 915 | $this->_database->where($field, $filter); |
| 943 | 916 | } |
| 944 | 917 | } |
| 945 | 918 | } |
| 946 | - } |
|
| 947 | - else if (count($params) == 1) |
|
| 919 | + } else if (count($params) == 1) |
|
| 948 | 920 | { |
| 949 | 921 | $this->_database->where($params[0]); |
| 950 | - } |
|
| 951 | - else if(count($params) == 2) |
|
| 922 | + } else if(count($params) == 2) |
|
| 952 | 923 | { |
| 953 | 924 | if (is_array($params[1])) |
| 954 | 925 | { |
| 955 | 926 | $this->_database->in($params[0], $params[1]); |
| 956 | - } |
|
| 957 | - else |
|
| 927 | + } else |
|
| 958 | 928 | { |
| 959 | 929 | $this->_database->where($params[0], $params[1]); |
| 960 | 930 | } |
| 961 | - } |
|
| 962 | - else if(count($params) == 3) |
|
| 931 | + } else if(count($params) == 3) |
|
| 963 | 932 | { |
| 964 | 933 | $this->_database->where($params[0], $params[1], $params[2]); |
| 965 | - } |
|
| 966 | - else |
|
| 934 | + } else |
|
| 967 | 935 | { |
| 968 | 936 | if (is_array($params[1])) |
| 969 | 937 | { |
| 970 | 938 | $this->_database->in($params[0], $params[1]); |
| 971 | - } |
|
| 972 | - else |
|
| 939 | + } else |
|
| 973 | 940 | { |
| 974 | 941 | $this->_database->where($params[0], $params[1]); |
| 975 | 942 | } |
@@ -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 FileCache implements CacheInterface{ |
| 28 | 28 | |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | 58 | if(! extension_loaded('zlib')){ |
@@ -80,26 +80,26 @@ discard block |
||
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $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', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $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', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | |
@@ -122,25 +122,25 @@ discard block |
||
| 122 | 122 | } |
| 123 | 123 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
| 124 | 124 | //Serializing along with the TTL |
| 125 | - $cacheData = serialize(array( |
|
| 125 | + $cacheData = serialize(array( |
|
| 126 | 126 | 'mtime' => time(), |
| 127 | 127 | 'expire' => $expire, |
| 128 | 128 | 'data' => $data, |
| 129 | 129 | 'ttl' => $ttl |
| 130 | 130 | ) |
| 131 | 131 | ); |
| 132 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 133 | - if(! $result){ |
|
| 134 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 135 | - fclose($handle); |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - else{ |
|
| 139 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 140 | - fclose($handle); |
|
| 132 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 133 | + if(! $result){ |
|
| 134 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 135 | + fclose($handle); |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + else{ |
|
| 139 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 140 | + fclose($handle); |
|
| 141 | 141 | chmod($filePath, 0640); |
| 142 | 142 | return true; |
| 143 | - } |
|
| 143 | + } |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | else{ |
| 162 | 162 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 163 | - @unlink($filePath); |
|
| 163 | + @unlink($filePath); |
|
| 164 | 164 | return true; |
| 165 | 165 | } |
| 166 | 166 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | } |
| 184 | 184 | else{ |
| 185 | 185 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 186 | - $data = file_get_contents($filePath); |
|
| 186 | + $data = file_get_contents($filePath); |
|
| 187 | 187 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 188 | 188 | if(! $data){ |
| 189 | 189 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
@@ -223,17 +223,17 @@ discard block |
||
| 223 | 223 | foreach ($list as $file) { |
| 224 | 224 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 225 | 225 | $data = file_get_contents($file); |
| 226 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 227 | - if(! $data){ |
|
| 228 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 229 | - } |
|
| 230 | - else if(time() > $data['expire']){ |
|
| 231 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 232 | - @unlink($file); |
|
| 233 | - } |
|
| 234 | - else{ |
|
| 235 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 236 | - } |
|
| 226 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 227 | + if(! $data){ |
|
| 228 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 229 | + } |
|
| 230 | + else if(time() > $data['expire']){ |
|
| 231 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 232 | + @unlink($file); |
|
| 233 | + } |
|
| 234 | + else{ |
|
| 235 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 236 | + } |
|
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | } |
@@ -256,19 +256,19 @@ discard block |
||
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * @return boolean |
|
| 261 | - */ |
|
| 262 | - public function isCompressCacheData(){ |
|
| 263 | - return $this->compressCacheData; |
|
| 264 | - } |
|
| 259 | + /** |
|
| 260 | + * @return boolean |
|
| 261 | + */ |
|
| 262 | + public function isCompressCacheData(){ |
|
| 263 | + return $this->compressCacheData; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @param boolean $compressCacheData |
|
| 268 | - * |
|
| 269 | - * @return self |
|
| 270 | - */ |
|
| 271 | - public function setCompressCacheData($status = true){ |
|
| 266 | + /** |
|
| 267 | + * @param boolean $compressCacheData |
|
| 268 | + * |
|
| 269 | + * @return self |
|
| 270 | + */ |
|
| 271 | + public function setCompressCacheData($status = true){ |
|
| 272 | 272 | //if Zlib extension is not loaded set compressCacheData to false |
| 273 | 273 | if($status === true && ! extension_loaded('zlib')){ |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | $this->compressCacheData = $status; |
| 280 | 280 | } |
| 281 | 281 | return $this; |
| 282 | - } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | 284 | /** |
| 285 | 285 | * Check whether the cache feature for the handle is supported |
@@ -291,28 +291,28 @@ discard block |
||
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | - * Return the Log instance |
|
| 295 | - * @return Log |
|
| 296 | - */ |
|
| 297 | - public function getLogger(){ |
|
| 298 | - return $this->logger; |
|
| 299 | - } |
|
| 294 | + * Return the Log instance |
|
| 295 | + * @return Log |
|
| 296 | + */ |
|
| 297 | + public function getLogger(){ |
|
| 298 | + return $this->logger; |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * Set the log instance |
|
| 303 | - * @param Log $logger the log object |
|
| 304 | - */ |
|
| 305 | - public function setLogger(Log $logger){ |
|
| 306 | - $this->logger = $logger; |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 301 | + /** |
|
| 302 | + * Set the log instance |
|
| 303 | + * @param Log $logger the log object |
|
| 304 | + */ |
|
| 305 | + public function setLogger(Log $logger){ |
|
| 306 | + $this->logger = $logger; |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | 310 | /** |
| 311 | - * Get the cache file full path for the given key |
|
| 312 | - * |
|
| 313 | - * @param $key the cache item key |
|
| 314 | - * @return string the full cache file path for this key |
|
| 315 | - */ |
|
| 311 | + * Get the cache file full path for the given key |
|
| 312 | + * |
|
| 313 | + * @param $key the cache item key |
|
| 314 | + * @return string the full cache file path for this key |
|
| 315 | + */ |
|
| 316 | 316 | private function getFilePath($key){ |
| 317 | 317 | return CACHE_PATH . md5($key) . '.cache'; |
| 318 | 318 | } |
@@ -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 FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | private $logger; |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 42 | + public function __construct(Log $logger = null) { |
|
| 43 | + if (!$this->isSupported()) { |
|
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | 47 | * instance of the Log class |
| 48 | 48 | */ |
| 49 | - if(is_object($logger)){ |
|
| 49 | + if (is_object($logger)) { |
|
| 50 | 50 | $this->logger = $logger; |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 52 | + else { |
|
| 53 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 54 | 54 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | - if(! extension_loaded('zlib')){ |
|
| 58 | + if (!extension_loaded('zlib')) { |
|
| 59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 60 | 60 | $this->compressCacheData = false; |
| 61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | * @param string $key the key to identify the cache data |
| 67 | 67 | * @return mixed the cache data if exists else return false |
| 68 | 68 | */ |
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 69 | + public function get($key) { |
|
| 70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 71 | 71 | $filePath = $this->getFilePath($key); |
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 72 | + if (!file_exists($filePath)) { |
|
| 73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if( ! $handle){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath, 'r'); |
|
| 78 | + if (!$handle) { |
|
| 79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | $data = file_get_contents($filePath); |
| 85 | 85 | fclose($handle); |
| 86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 87 | + if (!$data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
| 90 | 90 | unlink($filePath); |
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | 93 | if (time() > $data['expire']) { |
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 95 | 95 | // Unlinking when the file was expired |
| 96 | 96 | unlink($filePath); |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 100 | - $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', $data['expire']) . ']'); |
|
| 99 | + else { |
|
| 100 | + $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', $data['expire']) . ']'); |
|
| 101 | 101 | return $data['data']; |
| 102 | 102 | } |
| 103 | 103 | return false; |
@@ -111,13 +111,13 @@ discard block |
||
| 111 | 111 | * @param integer $ttl the cache life time |
| 112 | 112 | * @return boolean true if success otherwise will return false |
| 113 | 113 | */ |
| 114 | - public function set($key, $data, $ttl = 0){ |
|
| 114 | + public function set($key, $data, $ttl = 0) { |
|
| 115 | 115 | $expire = time() + $ttl; |
| 116 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 117 | 117 | $filePath = $this->getFilePath($key); |
| 118 | - $handle = fopen($filePath,'w'); |
|
| 119 | - if( ! $handle){ |
|
| 120 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 118 | + $handle = fopen($filePath, 'w'); |
|
| 119 | + if (!$handle) { |
|
| 120 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | 123 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -130,13 +130,13 @@ discard block |
||
| 130 | 130 | ) |
| 131 | 131 | ); |
| 132 | 132 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 133 | - if(! $result){ |
|
| 134 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 133 | + if (!$result) { |
|
| 134 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 135 | 135 | fclose($handle); |
| 136 | 136 | return false; |
| 137 | 137 | } |
| 138 | - else{ |
|
| 139 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 138 | + else { |
|
| 139 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 140 | 140 | fclose($handle); |
| 141 | 141 | chmod($filePath, 0640); |
| 142 | 142 | return true; |
@@ -150,16 +150,16 @@ discard block |
||
| 150 | 150 | * @return boolean true if the cache is delete, false if can't delete |
| 151 | 151 | * the cache or the cache with the given key not exist |
| 152 | 152 | */ |
| 153 | - public function delete($key){ |
|
| 154 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 153 | + public function delete($key) { |
|
| 154 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 155 | 155 | $filePath = $this->getFilePath($key); |
| 156 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 157 | - if(! file_exists($filePath)){ |
|
| 156 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 157 | + if (!file_exists($filePath)) { |
|
| 158 | 158 | $this->logger->info('This cache file does not exists skipping'); |
| 159 | 159 | return false; |
| 160 | 160 | } |
| 161 | - else{ |
|
| 162 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 161 | + else { |
|
| 162 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 163 | 163 | @unlink($filePath); |
| 164 | 164 | return true; |
| 165 | 165 | } |
@@ -173,25 +173,25 @@ discard block |
||
| 173 | 173 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 174 | 174 | * 'ttl' => the time to live of the cache in second |
| 175 | 175 | */ |
| 176 | - public function getInfo($key){ |
|
| 177 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 176 | + public function getInfo($key) { |
|
| 177 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 178 | 178 | $filePath = $this->getFilePath($key); |
| 179 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 180 | - if(! file_exists($filePath)){ |
|
| 179 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 180 | + if (!file_exists($filePath)) { |
|
| 181 | 181 | $this->logger->info('This cache file does not exists skipping'); |
| 182 | 182 | return false; |
| 183 | 183 | } |
| 184 | - else{ |
|
| 185 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 184 | + else { |
|
| 185 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 186 | 186 | $data = file_get_contents($filePath); |
| 187 | 187 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 188 | - if(! $data){ |
|
| 188 | + if (!$data) { |
|
| 189 | 189 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 190 | 190 | return false; |
| 191 | 191 | } |
| 192 | - else{ |
|
| 192 | + else { |
|
| 193 | 193 | $this->logger->info('This cache data is OK check for expire'); |
| 194 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 194 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 195 | 195 | $this->logger->info('This cache not yet expired return cache informations'); |
| 196 | 196 | $info = array( |
| 197 | 197 | 'mtime' => $data['mtime'], |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | ); |
| 201 | 201 | return $info; |
| 202 | 202 | } |
| 203 | - else{ |
|
| 203 | + else { |
|
| 204 | 204 | $this->logger->info('This cache already expired return false'); |
| 205 | 205 | return false; |
| 206 | 206 | } |
@@ -212,26 +212,26 @@ discard block |
||
| 212 | 212 | /** |
| 213 | 213 | * Used to delete expired cache data |
| 214 | 214 | */ |
| 215 | - public function deleteExpiredCache(){ |
|
| 215 | + public function deleteExpiredCache() { |
|
| 216 | 216 | $this->logger->debug('Deleting of expired cache files'); |
| 217 | 217 | $list = glob(CACHE_PATH . '*.cache'); |
| 218 | - if(! $list){ |
|
| 218 | + if (!$list) { |
|
| 219 | 219 | $this->logger->info('No cache files were found skipping'); |
| 220 | 220 | } |
| 221 | - else{ |
|
| 221 | + else { |
|
| 222 | 222 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 223 | 223 | foreach ($list as $file) { |
| 224 | 224 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 225 | 225 | $data = file_get_contents($file); |
| 226 | 226 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 227 | - if(! $data){ |
|
| 227 | + if (!$data) { |
|
| 228 | 228 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 229 | 229 | } |
| 230 | - else if(time() > $data['expire']){ |
|
| 230 | + else if (time() > $data['expire']) { |
|
| 231 | 231 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 232 | 232 | @unlink($file); |
| 233 | 233 | } |
| 234 | - else{ |
|
| 234 | + else { |
|
| 235 | 235 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 236 | 236 | } |
| 237 | 237 | } |
@@ -241,13 +241,13 @@ discard block |
||
| 241 | 241 | /** |
| 242 | 242 | * Remove all file from cache folder |
| 243 | 243 | */ |
| 244 | - public function clean(){ |
|
| 244 | + public function clean() { |
|
| 245 | 245 | $this->logger->debug('Deleting of all cache files'); |
| 246 | 246 | $list = glob(CACHE_PATH . '*.cache'); |
| 247 | - if(! $list){ |
|
| 247 | + if (!$list) { |
|
| 248 | 248 | $this->logger->info('No cache files were found skipping'); |
| 249 | 249 | } |
| 250 | - else{ |
|
| 250 | + else { |
|
| 251 | 251 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 252 | 252 | foreach ($list as $file) { |
| 253 | 253 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | /** |
| 260 | 260 | * @return boolean |
| 261 | 261 | */ |
| 262 | - public function isCompressCacheData(){ |
|
| 262 | + public function isCompressCacheData() { |
|
| 263 | 263 | return $this->compressCacheData; |
| 264 | 264 | } |
| 265 | 265 | |
@@ -268,14 +268,14 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @return self |
| 270 | 270 | */ |
| 271 | - public function setCompressCacheData($status = true){ |
|
| 271 | + public function setCompressCacheData($status = true) { |
|
| 272 | 272 | //if Zlib extension is not loaded set compressCacheData to false |
| 273 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 273 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 274 | 274 | |
| 275 | 275 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 276 | 276 | $this->compressCacheData = false; |
| 277 | 277 | } |
| 278 | - else{ |
|
| 278 | + else { |
|
| 279 | 279 | $this->compressCacheData = $status; |
| 280 | 280 | } |
| 281 | 281 | return $this; |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | * |
| 287 | 287 | * @return bool |
| 288 | 288 | */ |
| 289 | - public function isSupported(){ |
|
| 289 | + public function isSupported() { |
|
| 290 | 290 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 291 | 291 | } |
| 292 | 292 | |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | * Return the Log instance |
| 295 | 295 | * @return Log |
| 296 | 296 | */ |
| 297 | - public function getLogger(){ |
|
| 297 | + public function getLogger() { |
|
| 298 | 298 | return $this->logger; |
| 299 | 299 | } |
| 300 | 300 | |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | * Set the log instance |
| 303 | 303 | * @param Log $logger the log object |
| 304 | 304 | */ |
| 305 | - public function setLogger(Log $logger){ |
|
| 305 | + public function setLogger(Log $logger) { |
|
| 306 | 306 | $this->logger = $logger; |
| 307 | 307 | return $this; |
| 308 | 308 | } |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | * @param $key the cache item key |
| 314 | 314 | * @return string the full cache file path for this key |
| 315 | 315 | */ |
| 316 | - private function getFilePath($key){ |
|
| 316 | + private function getFilePath($key) { |
|
| 317 | 317 | return CACHE_PATH . md5($key) . '.cache'; |
| 318 | 318 | } |
| 319 | 319 | } |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | if(is_object($logger)){ |
| 50 | 50 | $this->logger = $logger; |
| 51 | - } |
|
| 52 | - else{ |
|
| 51 | + } else{ |
|
| 53 | 52 | $this->logger =& class_loader('Log', 'classes'); |
| 54 | 53 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 54 | } |
@@ -95,8 +94,7 @@ discard block |
||
| 95 | 94 | // Unlinking when the file was expired |
| 96 | 95 | unlink($filePath); |
| 97 | 96 | return false; |
| 98 | - } |
|
| 99 | - else{ |
|
| 97 | + } else{ |
|
| 100 | 98 | $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', $data['expire']) . ']'); |
| 101 | 99 | return $data['data']; |
| 102 | 100 | } |
@@ -134,8 +132,7 @@ discard block |
||
| 134 | 132 | $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
| 135 | 133 | fclose($handle); |
| 136 | 134 | return false; |
| 137 | - } |
|
| 138 | - else{ |
|
| 135 | + } else{ |
|
| 139 | 136 | $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
| 140 | 137 | fclose($handle); |
| 141 | 138 | chmod($filePath, 0640); |
@@ -157,8 +154,7 @@ discard block |
||
| 157 | 154 | if(! file_exists($filePath)){ |
| 158 | 155 | $this->logger->info('This cache file does not exists skipping'); |
| 159 | 156 | return false; |
| 160 | - } |
|
| 161 | - else{ |
|
| 157 | + } else{ |
|
| 162 | 158 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 163 | 159 | @unlink($filePath); |
| 164 | 160 | return true; |
@@ -180,16 +176,14 @@ discard block |
||
| 180 | 176 | if(! file_exists($filePath)){ |
| 181 | 177 | $this->logger->info('This cache file does not exists skipping'); |
| 182 | 178 | return false; |
| 183 | - } |
|
| 184 | - else{ |
|
| 179 | + } else{ |
|
| 185 | 180 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 186 | 181 | $data = file_get_contents($filePath); |
| 187 | 182 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 188 | 183 | if(! $data){ |
| 189 | 184 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 190 | 185 | return false; |
| 191 | - } |
|
| 192 | - else{ |
|
| 186 | + } else{ |
|
| 193 | 187 | $this->logger->info('This cache data is OK check for expire'); |
| 194 | 188 | if(isset($data['expire']) && $data['expire'] > time()){ |
| 195 | 189 | $this->logger->info('This cache not yet expired return cache informations'); |
@@ -199,8 +193,7 @@ discard block |
||
| 199 | 193 | 'ttl' => $data['ttl'] |
| 200 | 194 | ); |
| 201 | 195 | return $info; |
| 202 | - } |
|
| 203 | - else{ |
|
| 196 | + } else{ |
|
| 204 | 197 | $this->logger->info('This cache already expired return false'); |
| 205 | 198 | return false; |
| 206 | 199 | } |
@@ -217,8 +210,7 @@ discard block |
||
| 217 | 210 | $list = glob(CACHE_PATH . '*.cache'); |
| 218 | 211 | if(! $list){ |
| 219 | 212 | $this->logger->info('No cache files were found skipping'); |
| 220 | - } |
|
| 221 | - else{ |
|
| 213 | + } else{ |
|
| 222 | 214 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 223 | 215 | foreach ($list as $file) { |
| 224 | 216 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -226,12 +218,10 @@ discard block |
||
| 226 | 218 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 227 | 219 | if(! $data){ |
| 228 | 220 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 229 | - } |
|
| 230 | - else if(time() > $data['expire']){ |
|
| 221 | + } else if(time() > $data['expire']){ |
|
| 231 | 222 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 232 | 223 | @unlink($file); |
| 233 | - } |
|
| 234 | - else{ |
|
| 224 | + } else{ |
|
| 235 | 225 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 236 | 226 | } |
| 237 | 227 | } |
@@ -246,8 +236,7 @@ discard block |
||
| 246 | 236 | $list = glob(CACHE_PATH . '*.cache'); |
| 247 | 237 | if(! $list){ |
| 248 | 238 | $this->logger->info('No cache files were found skipping'); |
| 249 | - } |
|
| 250 | - else{ |
|
| 239 | + } else{ |
|
| 251 | 240 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 252 | 241 | foreach ($list as $file) { |
| 253 | 242 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -274,8 +263,7 @@ discard block |
||
| 274 | 263 | |
| 275 | 264 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 276 | 265 | $this->compressCacheData = false; |
| 277 | - } |
|
| 278 | - else{ |
|
| 266 | + } else{ |
|
| 279 | 267 | $this->compressCacheData = $status; |
| 280 | 268 | } |
| 281 | 269 | return $this; |
@@ -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 | - class DBSessionModel extends DBSessionHandlerModel{ |
|
| 2 | + class DBSessionModel extends DBSessionHandlerModel { |
|
| 3 | 3 | |
| 4 | 4 | protected $_table = 'ses'; |
| 5 | 5 | protected $primary_key = 's_id'; |
@@ -14,14 +14,14 @@ discard block |
||
| 14 | 14 | 'skey' => 'test_id' //VARCHAR(255) |
| 15 | 15 | ); |
| 16 | 16 | |
| 17 | - public function deleteByTime($time){ |
|
| 17 | + public function deleteByTime($time) { |
|
| 18 | 18 | $this->_database->from($this->_table) |
| 19 | 19 | ->where('s_time', '<', $time) |
| 20 | 20 | ->delete(); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - public function getKeyValue(){ |
|
| 24 | + public function getKeyValue() { |
|
| 25 | 25 | $user_id = 0; |
| 26 | 26 | return $user_id; |
| 27 | 27 | } |
@@ -1,10 +1,10 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - $pdo = new PDO('sqlite:D:\wamp\www\tnh-fw\tests\assets\db_tests.db', '', ''); |
|
| 4 | - $pdo->exec("SET NAMES 'UTF8' COLLATE 'utf8_general_ci'"); |
|
| 5 | - $pdo->exec("SET CHARACTER SET 'UTF8'"); |
|
| 6 | - $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 7 | - $pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY); |
|
| 3 | + $pdo = new PDO('sqlite:D:\wamp\www\tnh-fw\tests\assets\db_tests.db', '', ''); |
|
| 4 | + $pdo->exec("SET NAMES 'UTF8' COLLATE 'utf8_general_ci'"); |
|
| 5 | + $pdo->exec("SET CHARACTER SET 'UTF8'"); |
|
| 6 | + $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 7 | + $pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY); |
|
| 8 | 8 | $r = $pdo->query("SELECT * FROM ses WHERE s_id = 'tony' LIMIT 1"); |
| 9 | 9 | |
| 10 | 10 | $nb = $r->rowCount(); |
@@ -21,21 +21,21 @@ discard block |
||
| 21 | 21 | * You should have received a copy of the GNU General Public License |
| 22 | 22 | * along with this program; if not, write to the Free Software |
| 23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | - */ |
|
| 24 | + */ |
|
| 25 | 25 | |
| 26 | 26 | error_reporting(E_ALL | E_STRICT); |
| 27 | 27 | ini_set('display_errors', 1); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | - */ |
|
| 30 | + * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | + */ |
|
| 32 | 32 | define('DS', DIRECTORY_SEPARATOR); |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | - * The root directory of the application. |
|
| 36 | - * |
|
| 37 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | - */ |
|
| 35 | + * The root directory of the application. |
|
| 36 | + * |
|
| 37 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | + */ |
|
| 39 | 39 | define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
| 40 | 40 | |
| 41 | 41 | //tests dir path |
@@ -43,176 +43,176 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * The path to the directory. |
|
| 47 | - * |
|
| 48 | - * That contains your static files (javascript, css, images, etc.) |
|
| 49 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | - */ |
|
| 46 | + * The path to the directory. |
|
| 47 | + * |
|
| 48 | + * That contains your static files (javascript, css, images, etc.) |
|
| 49 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | + */ |
|
| 51 | 51 | define('ASSETS_PATH', 'assets/'); |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * The path to the directory of your cache files. |
|
| 55 | - * |
|
| 56 | - * This feature is available currently for database and views. |
|
| 57 | - */ |
|
| 54 | + * The path to the directory of your cache files. |
|
| 55 | + * |
|
| 56 | + * This feature is available currently for database and views. |
|
| 57 | + */ |
|
| 58 | 58 | define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | - * Custom application path for tests |
|
| 62 | - */ |
|
| 61 | + * Custom application path for tests |
|
| 62 | + */ |
|
| 63 | 63 | define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * The path to the controller directory of your application. |
|
| 67 | - * |
|
| 68 | - * If you already know the MVC architecture you know what a controller means; |
|
| 69 | - * it is he who makes the business logic of your application in general. |
|
| 70 | - */ |
|
| 66 | + * The path to the controller directory of your application. |
|
| 67 | + * |
|
| 68 | + * If you already know the MVC architecture you know what a controller means; |
|
| 69 | + * it is he who makes the business logic of your application in general. |
|
| 70 | + */ |
|
| 71 | 71 | define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * The path to the directory of your model classes of your application. |
|
| 75 | - * |
|
| 76 | - * If you already know the MVC architecture you know what a model means; |
|
| 77 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | - */ |
|
| 74 | + * The path to the directory of your model classes of your application. |
|
| 75 | + * |
|
| 76 | + * If you already know the MVC architecture you know what a model means; |
|
| 77 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | + */ |
|
| 79 | 79 | define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | - * The path to the directory of your views. |
|
| 83 | - * |
|
| 84 | - * If you already know the MVC architecture you know what a view means, |
|
| 85 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | - * everything displayed in the browser interface, etc. |
|
| 87 | - */ |
|
| 82 | + * The path to the directory of your views. |
|
| 83 | + * |
|
| 84 | + * If you already know the MVC architecture you know what a view means, |
|
| 85 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | + * everything displayed in the browser interface, etc. |
|
| 87 | + */ |
|
| 88 | 88 | define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * The path to the configuration directory. |
|
| 92 | - * |
|
| 93 | - * That contains most of the configuration files for your |
|
| 94 | - * application (database, class loading file, functions, etc.) |
|
| 95 | - */ |
|
| 91 | + * The path to the configuration directory. |
|
| 92 | + * |
|
| 93 | + * That contains most of the configuration files for your |
|
| 94 | + * application (database, class loading file, functions, etc.) |
|
| 95 | + */ |
|
| 96 | 96 | define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * The core directory |
|
| 100 | - * |
|
| 101 | - * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | - * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | - */ |
|
| 99 | + * The core directory |
|
| 100 | + * |
|
| 101 | + * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | + * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | + */ |
|
| 104 | 104 | define('CORE_PATH', ROOT_PATH . 'core' . DS); |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | - * The path to the directory of core classes that used by the system. |
|
| 108 | - * |
|
| 109 | - * It contains PHP classes that are used by the framework internally. |
|
| 110 | - */ |
|
| 107 | + * The path to the directory of core classes that used by the system. |
|
| 108 | + * |
|
| 109 | + * It contains PHP classes that are used by the framework internally. |
|
| 110 | + */ |
|
| 111 | 111 | define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The path to the directory of core classes for the cache used by the system. |
|
| 115 | - * |
|
| 116 | - * It contains PHP classes for the cache drivers. |
|
| 117 | - */ |
|
| 114 | + * The path to the directory of core classes for the cache used by the system. |
|
| 115 | + * |
|
| 116 | + * It contains PHP classes for the cache drivers. |
|
| 117 | + */ |
|
| 118 | 118 | define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * The path to the directory of core classes for the model used by the system. |
|
| 122 | - * |
|
| 123 | - * It contains PHP classes for the models. |
|
| 124 | - */ |
|
| 121 | + * The path to the directory of core classes for the model used by the system. |
|
| 122 | + * |
|
| 123 | + * It contains PHP classes for the models. |
|
| 124 | + */ |
|
| 125 | 125 | define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | - * The path to the directory of functions or helper systems. |
|
| 129 | - * |
|
| 130 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | - */ |
|
| 128 | + * The path to the directory of functions or helper systems. |
|
| 129 | + * |
|
| 130 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | + */ |
|
| 132 | 132 | define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | - * The path to the core directory of languages files. |
|
| 136 | - * |
|
| 137 | - */ |
|
| 135 | + * The path to the core directory of languages files. |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | 138 | define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | - * The path to the system library directory. |
|
| 142 | - * |
|
| 143 | - * Which contains the libraries most often used in your web application, as for the |
|
| 144 | - * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | - */ |
|
| 141 | + * The path to the system library directory. |
|
| 142 | + * |
|
| 143 | + * Which contains the libraries most often used in your web application, as for the |
|
| 144 | + * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | + */ |
|
| 146 | 146 | define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | - * The path to the system view directory. |
|
| 150 | - * |
|
| 151 | - * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | - */ |
|
| 149 | + * The path to the system view directory. |
|
| 150 | + * |
|
| 151 | + * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | + */ |
|
| 153 | 153 | define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | - * The path to the directory of your PHP personal functions or helper. |
|
| 157 | - * |
|
| 158 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | - * because if you update the system you may lose them. |
|
| 161 | - */ |
|
| 156 | + * The path to the directory of your PHP personal functions or helper. |
|
| 157 | + * |
|
| 158 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | + * because if you update the system you may lose them. |
|
| 161 | + */ |
|
| 162 | 162 | define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | - * The path to the app directory of personal language. |
|
| 166 | - * |
|
| 167 | - * This feature is not yet available. |
|
| 168 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | - */ |
|
| 165 | + * The path to the app directory of personal language. |
|
| 166 | + * |
|
| 167 | + * This feature is not yet available. |
|
| 168 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | + */ |
|
| 170 | 170 | define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
| 171 | 171 | |
| 172 | 172 | /** |
| 173 | - * The path to the directory of your personal libraries |
|
| 174 | - * |
|
| 175 | - * It contains your PHP classes, package, etc. |
|
| 176 | - * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | - * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | - */ |
|
| 173 | + * The path to the directory of your personal libraries |
|
| 174 | + * |
|
| 175 | + * It contains your PHP classes, package, etc. |
|
| 176 | + * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | + * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | + */ |
|
| 179 | 179 | define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * The path to the directory that contains the log files. |
|
| 183 | - * |
|
| 184 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | - * details see the documentation of your web server. |
|
| 187 | - * Example for Unix or linux with apache web server: |
|
| 188 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | - */ |
|
| 182 | + * The path to the directory that contains the log files. |
|
| 183 | + * |
|
| 184 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | + * details see the documentation of your web server. |
|
| 187 | + * Example for Unix or linux with apache web server: |
|
| 188 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | + */ |
|
| 191 | 191 | define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | - * The path to the modules directory. |
|
| 195 | - * |
|
| 196 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | - */ |
|
| 194 | + * The path to the modules directory. |
|
| 195 | + * |
|
| 196 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | + */ |
|
| 199 | 199 | define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | - * The path to the directory of sources external to your application. |
|
| 203 | - * |
|
| 204 | - * If you have already used "composer" you know what that means. |
|
| 205 | - */ |
|
| 202 | + * The path to the directory of sources external to your application. |
|
| 203 | + * |
|
| 204 | + * If you have already used "composer" you know what that means. |
|
| 205 | + */ |
|
| 206 | 206 | define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
| 207 | 207 | |
| 208 | 208 | /** |
| 209 | - * The front controller of your application. |
|
| 210 | - * |
|
| 211 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | - * your application by using the rewrite module URL of your web server . |
|
| 213 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | - */ |
|
| 209 | + * The front controller of your application. |
|
| 210 | + * |
|
| 211 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | + * your application by using the rewrite module URL of your web server . |
|
| 213 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | + */ |
|
| 216 | 216 | define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -221,14 +221,14 @@ discard block |
||
| 221 | 221 | define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | - * The environment of your application (production, test, development). |
|
| 225 | - * |
|
| 226 | - * if your application is still in development you use the value "development" |
|
| 227 | - * so you will have the display of the error messages, etc. |
|
| 228 | - * Once you finish the development of your application that is to put it online |
|
| 229 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | - * the loading of the system, will be fast. |
|
| 231 | - */ |
|
| 224 | + * The environment of your application (production, test, development). |
|
| 225 | + * |
|
| 226 | + * if your application is still in development you use the value "development" |
|
| 227 | + * so you will have the display of the error messages, etc. |
|
| 228 | + * Once you finish the development of your application that is to put it online |
|
| 229 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | + * the loading of the system, will be fast. |
|
| 231 | + */ |
|
| 232 | 232 | define('ENVIRONMENT', 'testing'); |
| 233 | 233 | |
| 234 | 234 | |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | require_once 'include/testsUtil.php'; |
| 260 | 260 | |
| 261 | 261 | /** |
| 262 | - * Setting of the PHP error message handling function |
|
| 263 | - */ |
|
| 262 | + * Setting of the PHP error message handling function |
|
| 263 | + */ |
|
| 264 | 264 | set_error_handler('php_error_handler'); |
| 265 | 265 | |
| 266 | 266 | /** |
| 267 | - * Setting of the PHP error exception handling function |
|
| 268 | - */ |
|
| 267 | + * Setting of the PHP error exception handling function |
|
| 268 | + */ |
|
| 269 | 269 | set_exception_handler('php_exception_handler'); |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | /** |
| 61 | 61 | * Custom application path for tests |
| 62 | 62 | */ |
| 63 | - define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
| 63 | + define('APPS_PATH', TESTS_PATH . 'hmvc' . DS); |
|
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * The path to the controller directory of your application. |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
| 197 | 197 | * in HMVC architecture (hierichical, controllers, models, views). |
| 198 | 198 | */ |
| 199 | - define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
| 199 | + define('MODULE_PATH', dirname(realpath(__FILE__)) . DS . 'hmvc' . DS . 'modules' . DS); |
|
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | 202 | * The path to the directory of sources external to your application. |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
| 236 | 236 | $_SESSION = array(); |
| 237 | 237 | |
| 238 | - if(! isset($_SERVER['REMOTE_ADDR'])){ |
|
| 238 | + if (!isset($_SERVER['REMOTE_ADDR'])) { |
|
| 239 | 239 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
| 240 | 240 | } |
| 241 | 241 | |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | require_once 'include/autoloader.php'; |
| 244 | 244 | |
| 245 | 245 | //check for composer autoload file if exists include it |
| 246 | - if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 246 | + if (file_exists(VENDOR_PATH . 'autoload.php')) { |
|
| 247 | 247 | require_once VENDOR_PATH . 'autoload.php'; |
| 248 | 248 | |
| 249 | 249 | //define the class alias for vstream |
@@ -24,20 +24,20 @@ |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
| 27 | - public function testFunctionGetConfigKeyNotExist(){ |
|
| 27 | + public function testFunctionGetConfigKeyNotExist() { |
|
| 28 | 28 | $key = 'foo'; |
| 29 | 29 | $cfg = get_config($key); |
| 30 | 30 | $this->assertNull($cfg); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function testFunctionGetConfigKeyNotExistUsingDefaultValue(){ |
|
| 33 | + public function testFunctionGetConfigKeyNotExistUsingDefaultValue() { |
|
| 34 | 34 | $key = 'foo'; |
| 35 | 35 | $expected = 'bar'; |
| 36 | 36 | $cfg = get_config($key, $expected); |
| 37 | 37 | $this->assertEquals($cfg, $expected); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - public function testFunctionGetConfigAfterSet(){ |
|
| 40 | + public function testFunctionGetConfigAfterSet() { |
|
| 41 | 41 | $key = 'foo'; |
| 42 | 42 | $expected = 'bar'; |
| 43 | 43 | $c = new Config(); |
@@ -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,188 +22,188 @@ 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); |
| 205 | 205 | } |
| 206 | - $config = array(); |
|
| 206 | + $config = array(); |
|
| 207 | 207 | $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
| 208 | 208 | $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
| 209 | 209 | $config['password'] = isset($db['password']) ? $db['password'] : ''; |
@@ -212,1031 +212,1031 @@ discard block |
||
| 212 | 212 | $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
| 213 | 213 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
| 214 | 214 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
| 215 | - $port = ''; |
|
| 216 | - if(strstr($config['hostname'], ':')){ |
|
| 217 | - $p = explode(':', $config['hostname']); |
|
| 218 | - $port = isset($p[1]) ? $p[1] : ''; |
|
| 219 | - $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 220 | - } |
|
| 215 | + $port = ''; |
|
| 216 | + if(strstr($config['hostname'], ':')){ |
|
| 217 | + $p = explode(':', $config['hostname']); |
|
| 218 | + $port = isset($p[1]) ? $p[1] : ''; |
|
| 219 | + $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 220 | + } |
|
| 221 | 221 | $config['port'] = $port; |
| 222 | 222 | |
| 223 | 223 | $this->setDatabaseConfiguration($config); |
| 224 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * This is used to connect to database |
|
| 229 | - * @return bool |
|
| 230 | - */ |
|
| 231 | - public function connect(){ |
|
| 232 | - $config = $this->getDatabaseConfiguration(); |
|
| 233 | - if(! empty($config)){ |
|
| 234 | - try{ |
|
| 235 | - $driverDsnMap = array( |
|
| 236 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 237 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 238 | - . 'dbname=' . $config['database'], |
|
| 239 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 240 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 241 | - . 'dbname=' . $config['database'], |
|
| 242 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 243 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 244 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 245 | - . '/' . $config['database'] |
|
| 246 | - ); |
|
| 247 | - $dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 224 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * This is used to connect to database |
|
| 229 | + * @return bool |
|
| 230 | + */ |
|
| 231 | + public function connect(){ |
|
| 232 | + $config = $this->getDatabaseConfiguration(); |
|
| 233 | + if(! empty($config)){ |
|
| 234 | + try{ |
|
| 235 | + $driverDsnMap = array( |
|
| 236 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 237 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 238 | + . 'dbname=' . $config['database'], |
|
| 239 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 240 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 241 | + . 'dbname=' . $config['database'], |
|
| 242 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 243 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 244 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 245 | + . '/' . $config['database'] |
|
| 246 | + ); |
|
| 247 | + $dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 248 | 248 | |
| 249 | - $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
| 250 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 251 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 252 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 253 | - return true; |
|
| 254 | - } |
|
| 255 | - catch (PDOException $e){ |
|
| 256 | - $this->logger->fatal($e->getMessage()); |
|
| 257 | - show_error('Cannot connect to Database.'); |
|
| 258 | - return false; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - else{ |
|
| 262 | - show_error('Database configuration is not set.'); |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Set the SQL FROM statment |
|
| 269 | - * @param string|array $table the table name or array of table list |
|
| 270 | - * @return object the current Database instance |
|
| 271 | - */ |
|
| 272 | - public function from($table){ |
|
| 273 | - if(is_array($table)){ |
|
| 274 | - $froms = ''; |
|
| 275 | - foreach($table as $key){ |
|
| 276 | - $froms .= $this->prefix . $key . ', '; |
|
| 277 | - } |
|
| 278 | - $this->from = rtrim($froms, ', '); |
|
| 279 | - } |
|
| 280 | - else{ |
|
| 281 | - $this->from = $this->prefix . $table; |
|
| 282 | - } |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Set the SQL SELECT statment |
|
| 288 | - * @param string|array $fields the field name or array of field list |
|
| 289 | - * @return object the current Database instance |
|
| 290 | - */ |
|
| 291 | - public function select($fields){ |
|
| 292 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 293 | - $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 294 | - return $this; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Set the SQL SELECT DISTINCT statment |
|
| 299 | - * @param string $field the field name to distinct |
|
| 300 | - * @return object the current Database instance |
|
| 301 | - */ |
|
| 302 | - public function distinct($field){ |
|
| 303 | - $distinct = ' DISTINCT ' . $field; |
|
| 304 | - $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 305 | - |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Set the SQL function MAX in SELECT statment |
|
| 311 | - * @param string $field the field name |
|
| 312 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 313 | - * @return object the current Database instance |
|
| 314 | - */ |
|
| 315 | - public function max($field, $name = null){ |
|
| 316 | - $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 317 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 318 | - return $this; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Set the SQL function MIN in SELECT statment |
|
| 323 | - * @param string $field the field name |
|
| 324 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 325 | - * @return object the current Database instance |
|
| 326 | - */ |
|
| 327 | - public function min($field, $name = null){ |
|
| 328 | - $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 329 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 330 | - return $this; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Set the SQL function SUM in SELECT statment |
|
| 335 | - * @param string $field the field name |
|
| 336 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 337 | - * @return object the current Database instance |
|
| 338 | - */ |
|
| 339 | - public function sum($field, $name = null){ |
|
| 340 | - $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 341 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 342 | - return $this; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Set the SQL function COUNT in SELECT statment |
|
| 347 | - * @param string $field the field name |
|
| 348 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 349 | - * @return object the current Database instance |
|
| 350 | - */ |
|
| 351 | - public function count($field = '*', $name = null){ |
|
| 352 | - $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 353 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 354 | - return $this; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Set the SQL function AVG in SELECT statment |
|
| 359 | - * @param string $field the field name |
|
| 360 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 361 | - * @return object the current Database instance |
|
| 362 | - */ |
|
| 363 | - public function avg($field, $name = null){ |
|
| 364 | - $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 365 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 366 | - return $this; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the SQL JOIN statment |
|
| 371 | - * @param string $table the join table name |
|
| 372 | - * @param string $field1 the first field for join conditions |
|
| 373 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 374 | - * @param string $field2 the second field for join conditions |
|
| 375 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 376 | - * @return object the current Database instance |
|
| 377 | - */ |
|
| 378 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 379 | - $on = $field1; |
|
| 380 | - $table = $this->prefix . $table; |
|
| 381 | - if(! is_null($op)){ |
|
| 382 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 383 | - } |
|
| 384 | - if (is_null($this->join)){ |
|
| 385 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 386 | - } |
|
| 387 | - else{ |
|
| 388 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 389 | - } |
|
| 390 | - return $this; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Set the SQL INNER JOIN statment |
|
| 395 | - * @see Database::join() |
|
| 396 | - * @return object the current Database instance |
|
| 397 | - */ |
|
| 398 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 399 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Set the SQL LEFT JOIN statment |
|
| 404 | - * @see Database::join() |
|
| 405 | - * @return object the current Database instance |
|
| 406 | - */ |
|
| 407 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 408 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Set the SQL RIGHT JOIN statment |
|
| 413 | - * @see Database::join() |
|
| 414 | - * @return object the current Database instance |
|
| 415 | - */ |
|
| 416 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 417 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Set the SQL FULL OUTER JOIN statment |
|
| 422 | - * @see Database::join() |
|
| 423 | - * @return object the current Database instance |
|
| 424 | - */ |
|
| 425 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 426 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 431 | - * @see Database::join() |
|
| 432 | - * @return object the current Database instance |
|
| 433 | - */ |
|
| 434 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 435 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 440 | - * @see Database::join() |
|
| 441 | - * @return object the current Database instance |
|
| 442 | - */ |
|
| 443 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 444 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 449 | - * @param string|array $field the field name or array of field list |
|
| 450 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 451 | - * @return object the current Database instance |
|
| 452 | - */ |
|
| 453 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 454 | - if(is_array($field)){ |
|
| 455 | - foreach($field as $f){ |
|
| 456 | - $this->whereIsNull($f, $andOr); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - else{ |
|
| 460 | - if (! $this->where){ |
|
| 461 | - $this->where = $field.' IS NULL '; |
|
| 462 | - } |
|
| 463 | - else{ |
|
| 464 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - return $this; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 472 | - * @param string|array $field the field name or array of field list |
|
| 473 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 474 | - * @return object the current Database instance |
|
| 475 | - */ |
|
| 476 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 477 | - if(is_array($field)){ |
|
| 478 | - foreach($field as $f){ |
|
| 479 | - $this->whereIsNotNull($f, $andOr); |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - else{ |
|
| 483 | - if (! $this->where){ |
|
| 484 | - $this->where = $field.' IS NOT NULL '; |
|
| 485 | - } |
|
| 486 | - else{ |
|
| 487 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - return $this; |
|
| 491 | - } |
|
| 249 | + $this->pdo = new PDO($dsn, $config['username'], $config['password']); |
|
| 250 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 251 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 252 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 253 | + return true; |
|
| 254 | + } |
|
| 255 | + catch (PDOException $e){ |
|
| 256 | + $this->logger->fatal($e->getMessage()); |
|
| 257 | + show_error('Cannot connect to Database.'); |
|
| 258 | + return false; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + else{ |
|
| 262 | + show_error('Database configuration is not set.'); |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Set the SQL FROM statment |
|
| 269 | + * @param string|array $table the table name or array of table list |
|
| 270 | + * @return object the current Database instance |
|
| 271 | + */ |
|
| 272 | + public function from($table){ |
|
| 273 | + if(is_array($table)){ |
|
| 274 | + $froms = ''; |
|
| 275 | + foreach($table as $key){ |
|
| 276 | + $froms .= $this->prefix . $key . ', '; |
|
| 277 | + } |
|
| 278 | + $this->from = rtrim($froms, ', '); |
|
| 279 | + } |
|
| 280 | + else{ |
|
| 281 | + $this->from = $this->prefix . $table; |
|
| 282 | + } |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Set the SQL SELECT statment |
|
| 288 | + * @param string|array $fields the field name or array of field list |
|
| 289 | + * @return object the current Database instance |
|
| 290 | + */ |
|
| 291 | + public function select($fields){ |
|
| 292 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 293 | + $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 294 | + return $this; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Set the SQL SELECT DISTINCT statment |
|
| 299 | + * @param string $field the field name to distinct |
|
| 300 | + * @return object the current Database instance |
|
| 301 | + */ |
|
| 302 | + public function distinct($field){ |
|
| 303 | + $distinct = ' DISTINCT ' . $field; |
|
| 304 | + $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 305 | + |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Set the SQL function MAX in SELECT statment |
|
| 311 | + * @param string $field the field name |
|
| 312 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 313 | + * @return object the current Database instance |
|
| 314 | + */ |
|
| 315 | + public function max($field, $name = null){ |
|
| 316 | + $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 317 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 318 | + return $this; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Set the SQL function MIN in SELECT statment |
|
| 323 | + * @param string $field the field name |
|
| 324 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 325 | + * @return object the current Database instance |
|
| 326 | + */ |
|
| 327 | + public function min($field, $name = null){ |
|
| 328 | + $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 329 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 330 | + return $this; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Set the SQL function SUM in SELECT statment |
|
| 335 | + * @param string $field the field name |
|
| 336 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 337 | + * @return object the current Database instance |
|
| 338 | + */ |
|
| 339 | + public function sum($field, $name = null){ |
|
| 340 | + $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 341 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 342 | + return $this; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Set the SQL function COUNT in SELECT statment |
|
| 347 | + * @param string $field the field name |
|
| 348 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 349 | + * @return object the current Database instance |
|
| 350 | + */ |
|
| 351 | + public function count($field = '*', $name = null){ |
|
| 352 | + $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 353 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 354 | + return $this; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Set the SQL function AVG in SELECT statment |
|
| 359 | + * @param string $field the field name |
|
| 360 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 361 | + * @return object the current Database instance |
|
| 362 | + */ |
|
| 363 | + public function avg($field, $name = null){ |
|
| 364 | + $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 365 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 366 | + return $this; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the SQL JOIN statment |
|
| 371 | + * @param string $table the join table name |
|
| 372 | + * @param string $field1 the first field for join conditions |
|
| 373 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 374 | + * @param string $field2 the second field for join conditions |
|
| 375 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 376 | + * @return object the current Database instance |
|
| 377 | + */ |
|
| 378 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 379 | + $on = $field1; |
|
| 380 | + $table = $this->prefix . $table; |
|
| 381 | + if(! is_null($op)){ |
|
| 382 | + $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 383 | + } |
|
| 384 | + if (is_null($this->join)){ |
|
| 385 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 386 | + } |
|
| 387 | + else{ |
|
| 388 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 389 | + } |
|
| 390 | + return $this; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Set the SQL INNER JOIN statment |
|
| 395 | + * @see Database::join() |
|
| 396 | + * @return object the current Database instance |
|
| 397 | + */ |
|
| 398 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 399 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Set the SQL LEFT JOIN statment |
|
| 404 | + * @see Database::join() |
|
| 405 | + * @return object the current Database instance |
|
| 406 | + */ |
|
| 407 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 408 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Set the SQL RIGHT JOIN statment |
|
| 413 | + * @see Database::join() |
|
| 414 | + * @return object the current Database instance |
|
| 415 | + */ |
|
| 416 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 417 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Set the SQL FULL OUTER JOIN statment |
|
| 422 | + * @see Database::join() |
|
| 423 | + * @return object the current Database instance |
|
| 424 | + */ |
|
| 425 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 426 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 431 | + * @see Database::join() |
|
| 432 | + * @return object the current Database instance |
|
| 433 | + */ |
|
| 434 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 435 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 440 | + * @see Database::join() |
|
| 441 | + * @return object the current Database instance |
|
| 442 | + */ |
|
| 443 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 444 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 449 | + * @param string|array $field the field name or array of field list |
|
| 450 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 451 | + * @return object the current Database instance |
|
| 452 | + */ |
|
| 453 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 454 | + if(is_array($field)){ |
|
| 455 | + foreach($field as $f){ |
|
| 456 | + $this->whereIsNull($f, $andOr); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + else{ |
|
| 460 | + if (! $this->where){ |
|
| 461 | + $this->where = $field.' IS NULL '; |
|
| 462 | + } |
|
| 463 | + else{ |
|
| 464 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + return $this; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 472 | + * @param string|array $field the field name or array of field list |
|
| 473 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 474 | + * @return object the current Database instance |
|
| 475 | + */ |
|
| 476 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 477 | + if(is_array($field)){ |
|
| 478 | + foreach($field as $f){ |
|
| 479 | + $this->whereIsNotNull($f, $andOr); |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + else{ |
|
| 483 | + if (! $this->where){ |
|
| 484 | + $this->where = $field.' IS NOT NULL '; |
|
| 485 | + } |
|
| 486 | + else{ |
|
| 487 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + return $this; |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - /** |
|
| 494 | - * Set the SQL WHERE CLAUSE statment |
|
| 495 | - * @param string|array $where the where field or array of field list |
|
| 496 | - * @param string $op the condition operator. If is null the default will be "=" |
|
| 497 | - * @param mixed $val the where value |
|
| 498 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 499 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 500 | - * @param boolean $escape whether to escape or not the $val |
|
| 501 | - * @return object the current Database instance |
|
| 502 | - */ |
|
| 503 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 504 | - if (is_array($where)){ |
|
| 505 | - $_where = array(); |
|
| 506 | - foreach ($where as $column => $data){ |
|
| 507 | - if(is_null($data)){ |
|
| 508 | - $data = ''; |
|
| 509 | - } |
|
| 510 | - $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
| 511 | - } |
|
| 512 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 513 | - } |
|
| 514 | - else{ |
|
| 515 | - if(is_array($op)){ |
|
| 516 | - $x = explode('?', $where); |
|
| 517 | - $w = ''; |
|
| 518 | - foreach($x as $k => $v){ |
|
| 519 | - if(! empty($v)){ |
|
| 520 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 521 | - $op[$k] = ''; |
|
| 522 | - } |
|
| 523 | - $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - $where = $w; |
|
| 527 | - } |
|
| 528 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 529 | - if(is_null($op)){ |
|
| 530 | - $op = ''; |
|
| 531 | - } |
|
| 532 | - $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 533 | - } |
|
| 534 | - else{ |
|
| 535 | - if(is_null($val)){ |
|
| 536 | - $val = ''; |
|
| 537 | - } |
|
| 538 | - $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - if (is_null($this->where)){ |
|
| 542 | - $this->where = $where; |
|
| 543 | - } |
|
| 544 | - else{ |
|
| 545 | - if(substr($this->where, -1) == '('){ |
|
| 546 | - $this->where = $this->where . ' ' . $where; |
|
| 547 | - } |
|
| 548 | - else{ |
|
| 549 | - $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 550 | - } |
|
| 551 | - } |
|
| 552 | - return $this; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 557 | - * @see Database::where() |
|
| 558 | - * @return object the current Database instance |
|
| 559 | - */ |
|
| 560 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 561 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 567 | - * @see Database::where() |
|
| 568 | - * @return object the current Database instance |
|
| 569 | - */ |
|
| 570 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 571 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 576 | - * @see Database::where() |
|
| 577 | - * @return object the current Database instance |
|
| 578 | - */ |
|
| 579 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 580 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Set the opened parenthesis for the complex SQL query |
|
| 585 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 586 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 587 | - * @return object the current Database instance |
|
| 588 | - */ |
|
| 589 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 590 | - if (is_null($this->where)){ |
|
| 591 | - $this->where = $type . ' ('; |
|
| 592 | - } |
|
| 593 | - else{ |
|
| 594 | - if(substr($this->where, -1) == '('){ |
|
| 595 | - $this->where .= $type . ' ('; |
|
| 596 | - } |
|
| 597 | - else{ |
|
| 598 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - return $this; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 606 | - * @see Database::groupStart() |
|
| 607 | - * @return object the current Database instance |
|
| 608 | - */ |
|
| 609 | - public function notGroupStart(){ |
|
| 610 | - return $this->groupStart('NOT'); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 615 | - * @see Database::groupStart() |
|
| 616 | - * @return object the current Database instance |
|
| 617 | - */ |
|
| 618 | - public function orGroupStart(){ |
|
| 619 | - return $this->groupStart('', ' OR'); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 624 | - * @see Database::groupStart() |
|
| 625 | - * @return object the current Database instance |
|
| 626 | - */ |
|
| 627 | - public function orNotGroupStart(){ |
|
| 628 | - return $this->groupStart('NOT', ' OR'); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Close the parenthesis for the grouped SQL |
|
| 633 | - * @return object the current Database instance |
|
| 634 | - */ |
|
| 635 | - public function groupEnd(){ |
|
| 636 | - $this->where .= ')'; |
|
| 637 | - return $this; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 642 | - * @param string $field the field name for IN statment |
|
| 643 | - * @param array $keys the list of values used |
|
| 644 | - * @param string $type the condition separator type (NOT) |
|
| 645 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 646 | - * @param boolean $escape whether to escape or not the values |
|
| 647 | - * @return object the current Database instance |
|
| 648 | - */ |
|
| 649 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 650 | - if (is_array($keys)){ |
|
| 651 | - $_keys = array(); |
|
| 652 | - foreach ($keys as $k => $v){ |
|
| 653 | - if(is_null($v)){ |
|
| 654 | - $v = ''; |
|
| 655 | - } |
|
| 656 | - $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 657 | - } |
|
| 658 | - $keys = implode(', ', $_keys); |
|
| 659 | - if (is_null($this->where)){ |
|
| 660 | - $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 661 | - } |
|
| 662 | - else{ |
|
| 663 | - if(substr($this->where, -1) == '('){ |
|
| 664 | - $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 665 | - } |
|
| 666 | - else{ |
|
| 667 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 668 | - } |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - return $this; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 676 | - * @see Database::in() |
|
| 677 | - * @return object the current Database instance |
|
| 678 | - */ |
|
| 679 | - public function notIn($field, array $keys, $escape = true){ |
|
| 680 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 685 | - * @see Database::in() |
|
| 686 | - * @return object the current Database instance |
|
| 687 | - */ |
|
| 688 | - public function orIn($field, array $keys, $escape = true){ |
|
| 689 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 694 | - * @see Database::in() |
|
| 695 | - * @return object the current Database instance |
|
| 696 | - */ |
|
| 697 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 698 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 703 | - * @param string $field the field used for the BETWEEN statment |
|
| 704 | - * @param mixed $value1 the BETWEEN begin value |
|
| 705 | - * @param mixed $value2 the BETWEEN end value |
|
| 706 | - * @param string $type the condition separator type (NOT) |
|
| 707 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 708 | - * @param boolean $escape whether to escape or not the values |
|
| 709 | - * @return object the current Database instance |
|
| 710 | - */ |
|
| 711 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 712 | - if(is_null($value1)){ |
|
| 713 | - $value1 = ''; |
|
| 714 | - } |
|
| 715 | - if(is_null($value2)){ |
|
| 716 | - $value2 = ''; |
|
| 717 | - } |
|
| 718 | - if (is_null($this->where)){ |
|
| 719 | - $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 720 | - } |
|
| 721 | - else{ |
|
| 722 | - if(substr($this->where, -1) == '('){ |
|
| 723 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 724 | - } |
|
| 725 | - else{ |
|
| 726 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 727 | - } |
|
| 728 | - } |
|
| 729 | - return $this; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 734 | - * @see Database::between() |
|
| 735 | - * @return object the current Database instance |
|
| 736 | - */ |
|
| 737 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 738 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 743 | - * @see Database::between() |
|
| 744 | - * @return object the current Database instance |
|
| 745 | - */ |
|
| 746 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 747 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 752 | - * @see Database::between() |
|
| 753 | - * @return object the current Database instance |
|
| 754 | - */ |
|
| 755 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 756 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 761 | - * @param string $field the field name used in LIKE statment |
|
| 762 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 763 | - * @param string $type the condition separator type (NOT) |
|
| 764 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 765 | - * @param boolean $escape whether to escape or not the values |
|
| 766 | - * @return object the current Database instance |
|
| 767 | - */ |
|
| 768 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 769 | - if(is_null($data)){ |
|
| 770 | - $data = ''; |
|
| 771 | - } |
|
| 772 | - $like = $escape ? $this->escape($data) : $data; |
|
| 773 | - if (is_null($this->where)){ |
|
| 774 | - $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 775 | - } |
|
| 776 | - else{ |
|
| 777 | - if(substr($this->where, -1) == '('){ |
|
| 778 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 779 | - } |
|
| 780 | - else{ |
|
| 781 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 782 | - } |
|
| 783 | - } |
|
| 784 | - return $this; |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 789 | - * @see Database::like() |
|
| 790 | - * @return object the current Database instance |
|
| 791 | - */ |
|
| 792 | - public function orLike($field, $data, $escape = true){ |
|
| 793 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - /** |
|
| 797 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 798 | - * @see Database::like() |
|
| 799 | - * @return object the current Database instance |
|
| 800 | - */ |
|
| 801 | - public function notLike($field, $data, $escape = true){ |
|
| 802 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 807 | - * @see Database::like() |
|
| 808 | - * @return object the current Database instance |
|
| 809 | - */ |
|
| 810 | - public function orNotLike($field, $data, $escape = true){ |
|
| 811 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * Set the SQL LIMIT statment |
|
| 816 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 817 | - * like LIMIT n; |
|
| 818 | - * @param int $limitEnd the limit count |
|
| 819 | - * @return object the current Database instance |
|
| 820 | - */ |
|
| 821 | - public function limit($limit, $limitEnd = null){ |
|
| 822 | - if(is_null($limit)){ |
|
| 823 | - return; |
|
| 824 | - } |
|
| 825 | - if (! is_null($limitEnd)){ |
|
| 826 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 827 | - } |
|
| 828 | - else{ |
|
| 829 | - $this->limit = $limit; |
|
| 830 | - } |
|
| 831 | - return $this; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 836 | - * @param string $orderBy the field name used for order |
|
| 837 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 838 | - * @return object the current Database instance |
|
| 839 | - */ |
|
| 840 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 841 | - if (! is_null($orderDir)){ |
|
| 842 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 843 | - } |
|
| 844 | - else{ |
|
| 845 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 846 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 847 | - } |
|
| 848 | - else{ |
|
| 849 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - return $this; |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - /** |
|
| 856 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 857 | - * @param string|array $field the field name used or array of field list |
|
| 858 | - * @return object the current Database instance |
|
| 859 | - */ |
|
| 860 | - public function groupBy($field){ |
|
| 861 | - if(is_array($field)){ |
|
| 862 | - $this->groupBy = implode(', ', $field); |
|
| 863 | - } |
|
| 864 | - else{ |
|
| 865 | - $this->groupBy = $field; |
|
| 866 | - } |
|
| 867 | - return $this; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * Set the SQL HAVING CLAUSE statment |
|
| 872 | - * @param string $field the field name used for HAVING statment |
|
| 873 | - * @param string|array $op the operator used or array |
|
| 874 | - * @param mixed $val the value for HAVING comparaison |
|
| 875 | - * @param boolean $escape whether to escape or not the values |
|
| 876 | - * @return object the current Database instance |
|
| 877 | - */ |
|
| 878 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 879 | - if(is_array($op)){ |
|
| 880 | - $x = explode('?', $field); |
|
| 881 | - $w = ''; |
|
| 882 | - foreach($x as $k => $v){ |
|
| 883 | - if(!empty($v)){ |
|
| 884 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 885 | - $op[$k] = ''; |
|
| 886 | - } |
|
| 887 | - $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 888 | - } |
|
| 889 | - } |
|
| 890 | - $this->having = $w; |
|
| 891 | - } |
|
| 892 | - else if (! in_array($op, $this->operatorList)){ |
|
| 893 | - if(is_null($op)){ |
|
| 894 | - $op = ''; |
|
| 895 | - } |
|
| 896 | - $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 897 | - } |
|
| 898 | - else{ |
|
| 899 | - if(is_null($val)){ |
|
| 900 | - $val = ''; |
|
| 901 | - } |
|
| 902 | - $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 903 | - } |
|
| 904 | - return $this; |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - /** |
|
| 908 | - * Return the number of rows returned by the current query |
|
| 909 | - * @return int |
|
| 910 | - */ |
|
| 911 | - public function numRows(){ |
|
| 912 | - return $this->numRows; |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - /** |
|
| 916 | - * Return the last insert id value |
|
| 917 | - * @return mixed |
|
| 918 | - */ |
|
| 919 | - public function insertId(){ |
|
| 920 | - return $this->insertId; |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - /** |
|
| 924 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 925 | - */ |
|
| 926 | - public function error(){ |
|
| 493 | + /** |
|
| 494 | + * Set the SQL WHERE CLAUSE statment |
|
| 495 | + * @param string|array $where the where field or array of field list |
|
| 496 | + * @param string $op the condition operator. If is null the default will be "=" |
|
| 497 | + * @param mixed $val the where value |
|
| 498 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 499 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 500 | + * @param boolean $escape whether to escape or not the $val |
|
| 501 | + * @return object the current Database instance |
|
| 502 | + */ |
|
| 503 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 504 | + if (is_array($where)){ |
|
| 505 | + $_where = array(); |
|
| 506 | + foreach ($where as $column => $data){ |
|
| 507 | + if(is_null($data)){ |
|
| 508 | + $data = ''; |
|
| 509 | + } |
|
| 510 | + $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
|
| 511 | + } |
|
| 512 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 513 | + } |
|
| 514 | + else{ |
|
| 515 | + if(is_array($op)){ |
|
| 516 | + $x = explode('?', $where); |
|
| 517 | + $w = ''; |
|
| 518 | + foreach($x as $k => $v){ |
|
| 519 | + if(! empty($v)){ |
|
| 520 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 521 | + $op[$k] = ''; |
|
| 522 | + } |
|
| 523 | + $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + $where = $w; |
|
| 527 | + } |
|
| 528 | + else if (! in_array((string)$op, $this->operatorList)){ |
|
| 529 | + if(is_null($op)){ |
|
| 530 | + $op = ''; |
|
| 531 | + } |
|
| 532 | + $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 533 | + } |
|
| 534 | + else{ |
|
| 535 | + if(is_null($val)){ |
|
| 536 | + $val = ''; |
|
| 537 | + } |
|
| 538 | + $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + if (is_null($this->where)){ |
|
| 542 | + $this->where = $where; |
|
| 543 | + } |
|
| 544 | + else{ |
|
| 545 | + if(substr($this->where, -1) == '('){ |
|
| 546 | + $this->where = $this->where . ' ' . $where; |
|
| 547 | + } |
|
| 548 | + else{ |
|
| 549 | + $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 550 | + } |
|
| 551 | + } |
|
| 552 | + return $this; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 557 | + * @see Database::where() |
|
| 558 | + * @return object the current Database instance |
|
| 559 | + */ |
|
| 560 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 561 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 567 | + * @see Database::where() |
|
| 568 | + * @return object the current Database instance |
|
| 569 | + */ |
|
| 570 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 571 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 576 | + * @see Database::where() |
|
| 577 | + * @return object the current Database instance |
|
| 578 | + */ |
|
| 579 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 580 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Set the opened parenthesis for the complex SQL query |
|
| 585 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 586 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 587 | + * @return object the current Database instance |
|
| 588 | + */ |
|
| 589 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 590 | + if (is_null($this->where)){ |
|
| 591 | + $this->where = $type . ' ('; |
|
| 592 | + } |
|
| 593 | + else{ |
|
| 594 | + if(substr($this->where, -1) == '('){ |
|
| 595 | + $this->where .= $type . ' ('; |
|
| 596 | + } |
|
| 597 | + else{ |
|
| 598 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + return $this; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 606 | + * @see Database::groupStart() |
|
| 607 | + * @return object the current Database instance |
|
| 608 | + */ |
|
| 609 | + public function notGroupStart(){ |
|
| 610 | + return $this->groupStart('NOT'); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 615 | + * @see Database::groupStart() |
|
| 616 | + * @return object the current Database instance |
|
| 617 | + */ |
|
| 618 | + public function orGroupStart(){ |
|
| 619 | + return $this->groupStart('', ' OR'); |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 624 | + * @see Database::groupStart() |
|
| 625 | + * @return object the current Database instance |
|
| 626 | + */ |
|
| 627 | + public function orNotGroupStart(){ |
|
| 628 | + return $this->groupStart('NOT', ' OR'); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Close the parenthesis for the grouped SQL |
|
| 633 | + * @return object the current Database instance |
|
| 634 | + */ |
|
| 635 | + public function groupEnd(){ |
|
| 636 | + $this->where .= ')'; |
|
| 637 | + return $this; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 642 | + * @param string $field the field name for IN statment |
|
| 643 | + * @param array $keys the list of values used |
|
| 644 | + * @param string $type the condition separator type (NOT) |
|
| 645 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 646 | + * @param boolean $escape whether to escape or not the values |
|
| 647 | + * @return object the current Database instance |
|
| 648 | + */ |
|
| 649 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 650 | + if (is_array($keys)){ |
|
| 651 | + $_keys = array(); |
|
| 652 | + foreach ($keys as $k => $v){ |
|
| 653 | + if(is_null($v)){ |
|
| 654 | + $v = ''; |
|
| 655 | + } |
|
| 656 | + $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 657 | + } |
|
| 658 | + $keys = implode(', ', $_keys); |
|
| 659 | + if (is_null($this->where)){ |
|
| 660 | + $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 661 | + } |
|
| 662 | + else{ |
|
| 663 | + if(substr($this->where, -1) == '('){ |
|
| 664 | + $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 665 | + } |
|
| 666 | + else{ |
|
| 667 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 668 | + } |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + return $this; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 676 | + * @see Database::in() |
|
| 677 | + * @return object the current Database instance |
|
| 678 | + */ |
|
| 679 | + public function notIn($field, array $keys, $escape = true){ |
|
| 680 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 685 | + * @see Database::in() |
|
| 686 | + * @return object the current Database instance |
|
| 687 | + */ |
|
| 688 | + public function orIn($field, array $keys, $escape = true){ |
|
| 689 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 694 | + * @see Database::in() |
|
| 695 | + * @return object the current Database instance |
|
| 696 | + */ |
|
| 697 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 698 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 703 | + * @param string $field the field used for the BETWEEN statment |
|
| 704 | + * @param mixed $value1 the BETWEEN begin value |
|
| 705 | + * @param mixed $value2 the BETWEEN end value |
|
| 706 | + * @param string $type the condition separator type (NOT) |
|
| 707 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 708 | + * @param boolean $escape whether to escape or not the values |
|
| 709 | + * @return object the current Database instance |
|
| 710 | + */ |
|
| 711 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 712 | + if(is_null($value1)){ |
|
| 713 | + $value1 = ''; |
|
| 714 | + } |
|
| 715 | + if(is_null($value2)){ |
|
| 716 | + $value2 = ''; |
|
| 717 | + } |
|
| 718 | + if (is_null($this->where)){ |
|
| 719 | + $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 720 | + } |
|
| 721 | + else{ |
|
| 722 | + if(substr($this->where, -1) == '('){ |
|
| 723 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 724 | + } |
|
| 725 | + else{ |
|
| 726 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 727 | + } |
|
| 728 | + } |
|
| 729 | + return $this; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 734 | + * @see Database::between() |
|
| 735 | + * @return object the current Database instance |
|
| 736 | + */ |
|
| 737 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 738 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 743 | + * @see Database::between() |
|
| 744 | + * @return object the current Database instance |
|
| 745 | + */ |
|
| 746 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 747 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 752 | + * @see Database::between() |
|
| 753 | + * @return object the current Database instance |
|
| 754 | + */ |
|
| 755 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 756 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 761 | + * @param string $field the field name used in LIKE statment |
|
| 762 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 763 | + * @param string $type the condition separator type (NOT) |
|
| 764 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 765 | + * @param boolean $escape whether to escape or not the values |
|
| 766 | + * @return object the current Database instance |
|
| 767 | + */ |
|
| 768 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 769 | + if(is_null($data)){ |
|
| 770 | + $data = ''; |
|
| 771 | + } |
|
| 772 | + $like = $escape ? $this->escape($data) : $data; |
|
| 773 | + if (is_null($this->where)){ |
|
| 774 | + $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 775 | + } |
|
| 776 | + else{ |
|
| 777 | + if(substr($this->where, -1) == '('){ |
|
| 778 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 779 | + } |
|
| 780 | + else{ |
|
| 781 | + $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | + return $this; |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 789 | + * @see Database::like() |
|
| 790 | + * @return object the current Database instance |
|
| 791 | + */ |
|
| 792 | + public function orLike($field, $data, $escape = true){ |
|
| 793 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + /** |
|
| 797 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 798 | + * @see Database::like() |
|
| 799 | + * @return object the current Database instance |
|
| 800 | + */ |
|
| 801 | + public function notLike($field, $data, $escape = true){ |
|
| 802 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 807 | + * @see Database::like() |
|
| 808 | + * @return object the current Database instance |
|
| 809 | + */ |
|
| 810 | + public function orNotLike($field, $data, $escape = true){ |
|
| 811 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * Set the SQL LIMIT statment |
|
| 816 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 817 | + * like LIMIT n; |
|
| 818 | + * @param int $limitEnd the limit count |
|
| 819 | + * @return object the current Database instance |
|
| 820 | + */ |
|
| 821 | + public function limit($limit, $limitEnd = null){ |
|
| 822 | + if(is_null($limit)){ |
|
| 823 | + return; |
|
| 824 | + } |
|
| 825 | + if (! is_null($limitEnd)){ |
|
| 826 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 827 | + } |
|
| 828 | + else{ |
|
| 829 | + $this->limit = $limit; |
|
| 830 | + } |
|
| 831 | + return $this; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 836 | + * @param string $orderBy the field name used for order |
|
| 837 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 838 | + * @return object the current Database instance |
|
| 839 | + */ |
|
| 840 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 841 | + if (! is_null($orderDir)){ |
|
| 842 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 843 | + } |
|
| 844 | + else{ |
|
| 845 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 846 | + $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 847 | + } |
|
| 848 | + else{ |
|
| 849 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + return $this; |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + /** |
|
| 856 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 857 | + * @param string|array $field the field name used or array of field list |
|
| 858 | + * @return object the current Database instance |
|
| 859 | + */ |
|
| 860 | + public function groupBy($field){ |
|
| 861 | + if(is_array($field)){ |
|
| 862 | + $this->groupBy = implode(', ', $field); |
|
| 863 | + } |
|
| 864 | + else{ |
|
| 865 | + $this->groupBy = $field; |
|
| 866 | + } |
|
| 867 | + return $this; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + /** |
|
| 871 | + * Set the SQL HAVING CLAUSE statment |
|
| 872 | + * @param string $field the field name used for HAVING statment |
|
| 873 | + * @param string|array $op the operator used or array |
|
| 874 | + * @param mixed $val the value for HAVING comparaison |
|
| 875 | + * @param boolean $escape whether to escape or not the values |
|
| 876 | + * @return object the current Database instance |
|
| 877 | + */ |
|
| 878 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 879 | + if(is_array($op)){ |
|
| 880 | + $x = explode('?', $field); |
|
| 881 | + $w = ''; |
|
| 882 | + foreach($x as $k => $v){ |
|
| 883 | + if(!empty($v)){ |
|
| 884 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 885 | + $op[$k] = ''; |
|
| 886 | + } |
|
| 887 | + $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 888 | + } |
|
| 889 | + } |
|
| 890 | + $this->having = $w; |
|
| 891 | + } |
|
| 892 | + else if (! in_array($op, $this->operatorList)){ |
|
| 893 | + if(is_null($op)){ |
|
| 894 | + $op = ''; |
|
| 895 | + } |
|
| 896 | + $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 897 | + } |
|
| 898 | + else{ |
|
| 899 | + if(is_null($val)){ |
|
| 900 | + $val = ''; |
|
| 901 | + } |
|
| 902 | + $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 903 | + } |
|
| 904 | + return $this; |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + /** |
|
| 908 | + * Return the number of rows returned by the current query |
|
| 909 | + * @return int |
|
| 910 | + */ |
|
| 911 | + public function numRows(){ |
|
| 912 | + return $this->numRows; |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + /** |
|
| 916 | + * Return the last insert id value |
|
| 917 | + * @return mixed |
|
| 918 | + */ |
|
| 919 | + public function insertId(){ |
|
| 920 | + return $this->insertId; |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + /** |
|
| 924 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 925 | + */ |
|
| 926 | + public function error(){ |
|
| 927 | 927 | if($this->error){ |
| 928 | 928 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 929 | 929 | } |
| 930 | - } |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * Get the result of one record rows returned by the current query |
|
| 934 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 935 | - * If is string will determine the result type "array" or "object" |
|
| 936 | - * @return mixed the query SQL string or the record result |
|
| 937 | - */ |
|
| 938 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 939 | - $this->limit = 1; |
|
| 940 | - $query = $this->getAll(true); |
|
| 941 | - if($returnSQLQueryOrResultType === true){ |
|
| 942 | - return $query; |
|
| 943 | - } |
|
| 944 | - else{ |
|
| 945 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 946 | - } |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - /** |
|
| 950 | - * Get the result of record rows list returned by the current query |
|
| 951 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 952 | - * If is string will determine the result type "array" or "object" |
|
| 953 | - * @return mixed the query SQL string or the record result |
|
| 954 | - */ |
|
| 955 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 956 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 957 | - if (! is_null($this->join)){ |
|
| 958 | - $query .= $this->join; |
|
| 959 | - } |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * Get the result of one record rows returned by the current query |
|
| 934 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 935 | + * If is string will determine the result type "array" or "object" |
|
| 936 | + * @return mixed the query SQL string or the record result |
|
| 937 | + */ |
|
| 938 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 939 | + $this->limit = 1; |
|
| 940 | + $query = $this->getAll(true); |
|
| 941 | + if($returnSQLQueryOrResultType === true){ |
|
| 942 | + return $query; |
|
| 943 | + } |
|
| 944 | + else{ |
|
| 945 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 946 | + } |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + /** |
|
| 950 | + * Get the result of record rows list returned by the current query |
|
| 951 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 952 | + * If is string will determine the result type "array" or "object" |
|
| 953 | + * @return mixed the query SQL string or the record result |
|
| 954 | + */ |
|
| 955 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 956 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 957 | + if (! is_null($this->join)){ |
|
| 958 | + $query .= $this->join; |
|
| 959 | + } |
|
| 960 | 960 | |
| 961 | - if (! is_null($this->where)){ |
|
| 962 | - $query .= ' WHERE ' . $this->where; |
|
| 963 | - } |
|
| 961 | + if (! is_null($this->where)){ |
|
| 962 | + $query .= ' WHERE ' . $this->where; |
|
| 963 | + } |
|
| 964 | 964 | |
| 965 | - if (! is_null($this->groupBy)){ |
|
| 966 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 967 | - } |
|
| 965 | + if (! is_null($this->groupBy)){ |
|
| 966 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 967 | + } |
|
| 968 | 968 | |
| 969 | - if (! is_null($this->having)){ |
|
| 970 | - $query .= ' HAVING ' . $this->having; |
|
| 971 | - } |
|
| 969 | + if (! is_null($this->having)){ |
|
| 970 | + $query .= ' HAVING ' . $this->having; |
|
| 971 | + } |
|
| 972 | 972 | |
| 973 | - if (! is_null($this->orderBy)){ |
|
| 974 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 975 | - } |
|
| 973 | + if (! is_null($this->orderBy)){ |
|
| 974 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 975 | + } |
|
| 976 | 976 | |
| 977 | - if(! is_null($this->limit)){ |
|
| 978 | - $query .= ' LIMIT ' . $this->limit; |
|
| 979 | - } |
|
| 977 | + if(! is_null($this->limit)){ |
|
| 978 | + $query .= ' LIMIT ' . $this->limit; |
|
| 979 | + } |
|
| 980 | 980 | |
| 981 | 981 | if($returnSQLQueryOrResultType === true){ |
| 982 | - return $query; |
|
| 983 | - } |
|
| 984 | - else{ |
|
| 985 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 986 | - } |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * Insert new record in the database |
|
| 991 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 992 | - * @param boolean $escape whether to escape or not the values |
|
| 993 | - * @return mixed the insert id of the new record or null |
|
| 994 | - */ |
|
| 995 | - public function insert($data = array(), $escape = true){ |
|
| 996 | - $column = array(); |
|
| 997 | - $val = array(); |
|
| 998 | - if(! $data && $this->getData()){ |
|
| 999 | - $columns = array_keys($this->getData()); |
|
| 1000 | - $column = implode(',', $columns); |
|
| 1001 | - $val = implode(', ', $this->getData()); |
|
| 1002 | - } |
|
| 1003 | - else{ |
|
| 1004 | - $columns = array_keys($data); |
|
| 1005 | - $column = implode(',', $columns); |
|
| 1006 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 1010 | - $query = $this->query($query); |
|
| 1011 | - |
|
| 1012 | - if ($query){ |
|
| 1013 | - if(! $this->pdo){ |
|
| 1014 | - $this->connect(); |
|
| 1015 | - } |
|
| 1016 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 1017 | - return $this->insertId(); |
|
| 1018 | - } |
|
| 1019 | - else{ |
|
| 982 | + return $query; |
|
| 983 | + } |
|
| 984 | + else{ |
|
| 985 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 986 | + } |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Insert new record in the database |
|
| 991 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 992 | + * @param boolean $escape whether to escape or not the values |
|
| 993 | + * @return mixed the insert id of the new record or null |
|
| 994 | + */ |
|
| 995 | + public function insert($data = array(), $escape = true){ |
|
| 996 | + $column = array(); |
|
| 997 | + $val = array(); |
|
| 998 | + if(! $data && $this->getData()){ |
|
| 999 | + $columns = array_keys($this->getData()); |
|
| 1000 | + $column = implode(',', $columns); |
|
| 1001 | + $val = implode(', ', $this->getData()); |
|
| 1002 | + } |
|
| 1003 | + else{ |
|
| 1004 | + $columns = array_keys($data); |
|
| 1005 | + $column = implode(',', $columns); |
|
| 1006 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 1010 | + $query = $this->query($query); |
|
| 1011 | + |
|
| 1012 | + if ($query){ |
|
| 1013 | + if(! $this->pdo){ |
|
| 1014 | + $this->connect(); |
|
| 1015 | + } |
|
| 1016 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 1017 | + return $this->insertId(); |
|
| 1018 | + } |
|
| 1019 | + else{ |
|
| 1020 | 1020 | return false; |
| 1021 | - } |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * Update record in the database |
|
| 1026 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 1027 | - * @param boolean $escape whether to escape or not the values |
|
| 1028 | - * @return mixed the update status |
|
| 1029 | - */ |
|
| 1030 | - public function update($data = array(), $escape = true){ |
|
| 1031 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1032 | - $values = array(); |
|
| 1033 | - if(! $data && $this->getData()){ |
|
| 1034 | - foreach ($this->getData() as $column => $val){ |
|
| 1035 | - $values[] = $column . ' = ' . $val; |
|
| 1036 | - } |
|
| 1037 | - } |
|
| 1038 | - else{ |
|
| 1039 | - foreach ($data as $column => $val){ |
|
| 1040 | - $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1041 | - } |
|
| 1042 | - } |
|
| 1043 | - $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
| 1044 | - if (! is_null($this->where)){ |
|
| 1045 | - $query .= ' WHERE ' . $this->where; |
|
| 1046 | - } |
|
| 1047 | - |
|
| 1048 | - if (! is_null($this->orderBy)){ |
|
| 1049 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - if (! is_null($this->limit)){ |
|
| 1053 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1054 | - } |
|
| 1055 | - return $this->query($query); |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Delete the record in database |
|
| 1060 | - * @return mixed the delete status |
|
| 1061 | - */ |
|
| 1062 | - public function delete(){ |
|
| 1063 | - $query = 'DELETE FROM ' . $this->from; |
|
| 1064 | - |
|
| 1065 | - if (! is_null($this->where)){ |
|
| 1066 | - $query .= ' WHERE ' . $this->where; |
|
| 1067 | - } |
|
| 1068 | - |
|
| 1069 | - if (! is_null($this->orderBy)){ |
|
| 1070 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - if (! is_null($this->limit)){ |
|
| 1074 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1075 | - } |
|
| 1076 | - |
|
| 1077 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1078 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1079 | - } |
|
| 1080 | - return $this->query($query); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * Execute an SQL query |
|
| 1085 | - * @param string $query the query SQL string |
|
| 1086 | - * @param boolean $all whether to return all record or not |
|
| 1087 | - * @param boolean $array return the result as array |
|
| 1088 | - * @return mixed the query result |
|
| 1089 | - */ |
|
| 1090 | - public function query($query, $all = true, $array = false){ |
|
| 1091 | - $this->reset(); |
|
| 1092 | - if(is_array($all)){ |
|
| 1093 | - $x = explode('?', $query); |
|
| 1094 | - $q = ''; |
|
| 1095 | - foreach($x as $k => $v){ |
|
| 1096 | - if(! empty($v)){ |
|
| 1097 | - $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1098 | - } |
|
| 1099 | - } |
|
| 1100 | - $query = $q; |
|
| 1101 | - } |
|
| 1102 | - |
|
| 1103 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1104 | - $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1105 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1106 | - //cache expire time |
|
| 1021 | + } |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * Update record in the database |
|
| 1026 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 1027 | + * @param boolean $escape whether to escape or not the values |
|
| 1028 | + * @return mixed the update status |
|
| 1029 | + */ |
|
| 1030 | + public function update($data = array(), $escape = true){ |
|
| 1031 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1032 | + $values = array(); |
|
| 1033 | + if(! $data && $this->getData()){ |
|
| 1034 | + foreach ($this->getData() as $column => $val){ |
|
| 1035 | + $values[] = $column . ' = ' . $val; |
|
| 1036 | + } |
|
| 1037 | + } |
|
| 1038 | + else{ |
|
| 1039 | + foreach ($data as $column => $val){ |
|
| 1040 | + $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1041 | + } |
|
| 1042 | + } |
|
| 1043 | + $query .= (is_array($data) ? implode(', ', $values) : $data); |
|
| 1044 | + if (! is_null($this->where)){ |
|
| 1045 | + $query .= ' WHERE ' . $this->where; |
|
| 1046 | + } |
|
| 1047 | + |
|
| 1048 | + if (! is_null($this->orderBy)){ |
|
| 1049 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + if (! is_null($this->limit)){ |
|
| 1053 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1054 | + } |
|
| 1055 | + return $this->query($query); |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Delete the record in database |
|
| 1060 | + * @return mixed the delete status |
|
| 1061 | + */ |
|
| 1062 | + public function delete(){ |
|
| 1063 | + $query = 'DELETE FROM ' . $this->from; |
|
| 1064 | + |
|
| 1065 | + if (! is_null($this->where)){ |
|
| 1066 | + $query .= ' WHERE ' . $this->where; |
|
| 1067 | + } |
|
| 1068 | + |
|
| 1069 | + if (! is_null($this->orderBy)){ |
|
| 1070 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + if (! is_null($this->limit)){ |
|
| 1074 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1075 | + } |
|
| 1076 | + |
|
| 1077 | + if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1078 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1079 | + } |
|
| 1080 | + return $this->query($query); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * Execute an SQL query |
|
| 1085 | + * @param string $query the query SQL string |
|
| 1086 | + * @param boolean $all whether to return all record or not |
|
| 1087 | + * @param boolean $array return the result as array |
|
| 1088 | + * @return mixed the query result |
|
| 1089 | + */ |
|
| 1090 | + public function query($query, $all = true, $array = false){ |
|
| 1091 | + $this->reset(); |
|
| 1092 | + if(is_array($all)){ |
|
| 1093 | + $x = explode('?', $query); |
|
| 1094 | + $q = ''; |
|
| 1095 | + foreach($x as $k => $v){ |
|
| 1096 | + if(! empty($v)){ |
|
| 1097 | + $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1098 | + } |
|
| 1099 | + } |
|
| 1100 | + $query = $q; |
|
| 1101 | + } |
|
| 1102 | + |
|
| 1103 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1104 | + $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1105 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1106 | + //cache expire time |
|
| 1107 | 1107 | $cacheExpire = $this->temporaryCacheTtl; |
| 1108 | 1108 | |
| 1109 | 1109 | //return to the initial cache time |
| 1110 | 1110 | $this->temporaryCacheTtl = $this->cacheTtl; |
| 1111 | 1111 | |
| 1112 | 1112 | //config for cache |
| 1113 | - $cacheEnable = get_config('cache_enable'); |
|
| 1113 | + $cacheEnable = get_config('cache_enable'); |
|
| 1114 | 1114 | |
| 1115 | 1115 | //the database cache content |
| 1116 | - $cacheContent = null; |
|
| 1116 | + $cacheContent = null; |
|
| 1117 | 1117 | |
| 1118 | 1118 | //this database query cache key |
| 1119 | - $cacheKey = null; |
|
| 1119 | + $cacheKey = null; |
|
| 1120 | 1120 | |
| 1121 | 1121 | //the cache manager instance |
| 1122 | - $cacheInstance = null; |
|
| 1122 | + $cacheInstance = null; |
|
| 1123 | 1123 | |
| 1124 | 1124 | //if can use cache feature for this query |
| 1125 | 1125 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1126 | 1126 | |
| 1127 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1128 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1129 | - $cacheKey = md5($query . $all . $array); |
|
| 1130 | - if(is_object($this->cacheInstance)){ |
|
| 1131 | - $cacheInstance = $this->cacheInstance; |
|
| 1132 | - } |
|
| 1133 | - else{ |
|
| 1134 | - $obj = & get_instance(); |
|
| 1135 | - $cacheInstance = $obj->cache; |
|
| 1136 | - } |
|
| 1137 | - $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1138 | - } |
|
| 1139 | - else{ |
|
| 1127 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1128 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1129 | + $cacheKey = md5($query . $all . $array); |
|
| 1130 | + if(is_object($this->cacheInstance)){ |
|
| 1131 | + $cacheInstance = $this->cacheInstance; |
|
| 1132 | + } |
|
| 1133 | + else{ |
|
| 1134 | + $obj = & get_instance(); |
|
| 1135 | + $cacheInstance = $obj->cache; |
|
| 1136 | + } |
|
| 1137 | + $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1138 | + } |
|
| 1139 | + else{ |
|
| 1140 | 1140 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1141 | - } |
|
| 1141 | + } |
|
| 1142 | 1142 | |
| 1143 | - if(! $this->pdo){ |
|
| 1144 | - $this->connect(); |
|
| 1145 | - } |
|
| 1143 | + if(! $this->pdo){ |
|
| 1144 | + $this->connect(); |
|
| 1145 | + } |
|
| 1146 | 1146 | |
| 1147 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1148 | - //for database query execution time |
|
| 1149 | - $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1150 | - $bench = null; |
|
| 1151 | - if(is_object($this->benchmarkInstance)){ |
|
| 1152 | - $bench = $this->benchmarkInstance; |
|
| 1153 | - } |
|
| 1154 | - else{ |
|
| 1155 | - $obj = & get_instance(); |
|
| 1156 | - $bench = $obj->benchmark; |
|
| 1157 | - } |
|
| 1158 | - $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1159 | - //Now execute the query |
|
| 1160 | - $sqlQuery = $this->pdo->query($this->query); |
|
| 1147 | + if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1148 | + //for database query execution time |
|
| 1149 | + $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1150 | + $bench = null; |
|
| 1151 | + if(is_object($this->benchmarkInstance)){ |
|
| 1152 | + $bench = $this->benchmarkInstance; |
|
| 1153 | + } |
|
| 1154 | + else{ |
|
| 1155 | + $obj = & get_instance(); |
|
| 1156 | + $bench = $obj->benchmark; |
|
| 1157 | + } |
|
| 1158 | + $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1159 | + //Now execute the query |
|
| 1160 | + $sqlQuery = $this->pdo->query($this->query); |
|
| 1161 | 1161 | |
| 1162 | - //get response time for this query |
|
| 1163 | - $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1164 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 1165 | - if($responseTime >= 1 ){ |
|
| 1166 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1167 | - } |
|
| 1168 | - if ($sqlQuery){ |
|
| 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 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | - $this->numRows = count($this->result); |
|
| 1179 | - } |
|
| 1180 | - else{ |
|
| 1181 | - $this->numRows = $sqlQuery->rowCount(); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1185 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1186 | - $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - else{ |
|
| 1190 | - $error = $this->pdo->errorInfo(); |
|
| 1191 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1192 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1193 | - $this->error(); |
|
| 1194 | - } |
|
| 1195 | - } |
|
| 1196 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1197 | - $queryStr = $this->pdo->query($this->query); |
|
| 1198 | - if($queryStr){ |
|
| 1199 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1200 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1201 | - $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1202 | - $this->numRows = 1; |
|
| 1203 | - } |
|
| 1204 | - else{ |
|
| 1205 | - $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1206 | - $this->numRows = $queryStr->rowCount(); |
|
| 1207 | - } |
|
| 1208 | - } |
|
| 1209 | - if (! $this->result){ |
|
| 1210 | - $error = $this->pdo->errorInfo(); |
|
| 1211 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1212 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1213 | - $this->error(); |
|
| 1214 | - } |
|
| 1215 | - } |
|
| 1216 | - else{ |
|
| 1217 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1218 | - $this->result = $cacheContent; |
|
| 1219 | - $this->numRows = count($this->result); |
|
| 1220 | - } |
|
| 1221 | - $this->queryCount++; |
|
| 1222 | - if(! $this->result){ |
|
| 1223 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1224 | - } |
|
| 1225 | - return $this->result; |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - /** |
|
| 1229 | - * Set database cache time to live |
|
| 1230 | - * @param integer $ttl the cache time to live in second |
|
| 1231 | - * @return object the current Database instance |
|
| 1232 | - */ |
|
| 1233 | - public function setCache($ttl = 0){ |
|
| 1234 | - if($ttl > 0){ |
|
| 1235 | - $this->cacheTtl = $ttl; |
|
| 1236 | - $this->temporaryCacheTtl = $ttl; |
|
| 1237 | - } |
|
| 1238 | - return $this; |
|
| 1239 | - } |
|
| 1162 | + //get response time for this query |
|
| 1163 | + $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1164 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 1165 | + if($responseTime >= 1 ){ |
|
| 1166 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1167 | + } |
|
| 1168 | + if ($sqlQuery){ |
|
| 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 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | + $this->numRows = count($this->result); |
|
| 1179 | + } |
|
| 1180 | + else{ |
|
| 1181 | + $this->numRows = $sqlQuery->rowCount(); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1185 | + $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1186 | + $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + else{ |
|
| 1190 | + $error = $this->pdo->errorInfo(); |
|
| 1191 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1192 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1193 | + $this->error(); |
|
| 1194 | + } |
|
| 1195 | + } |
|
| 1196 | + else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1197 | + $queryStr = $this->pdo->query($this->query); |
|
| 1198 | + if($queryStr){ |
|
| 1199 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1200 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1201 | + $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1202 | + $this->numRows = 1; |
|
| 1203 | + } |
|
| 1204 | + else{ |
|
| 1205 | + $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1206 | + $this->numRows = $queryStr->rowCount(); |
|
| 1207 | + } |
|
| 1208 | + } |
|
| 1209 | + if (! $this->result){ |
|
| 1210 | + $error = $this->pdo->errorInfo(); |
|
| 1211 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1212 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1213 | + $this->error(); |
|
| 1214 | + } |
|
| 1215 | + } |
|
| 1216 | + else{ |
|
| 1217 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1218 | + $this->result = $cacheContent; |
|
| 1219 | + $this->numRows = count($this->result); |
|
| 1220 | + } |
|
| 1221 | + $this->queryCount++; |
|
| 1222 | + if(! $this->result){ |
|
| 1223 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1224 | + } |
|
| 1225 | + return $this->result; |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + /** |
|
| 1229 | + * Set database cache time to live |
|
| 1230 | + * @param integer $ttl the cache time to live in second |
|
| 1231 | + * @return object the current Database instance |
|
| 1232 | + */ |
|
| 1233 | + public function setCache($ttl = 0){ |
|
| 1234 | + if($ttl > 0){ |
|
| 1235 | + $this->cacheTtl = $ttl; |
|
| 1236 | + $this->temporaryCacheTtl = $ttl; |
|
| 1237 | + } |
|
| 1238 | + return $this; |
|
| 1239 | + } |
|
| 1240 | 1240 | |
| 1241 | 1241 | /** |
| 1242 | 1242 | * Enabled cache temporary for the current query not globally |
@@ -1244,186 +1244,186 @@ discard block |
||
| 1244 | 1244 | * @return object the current Database instance |
| 1245 | 1245 | */ |
| 1246 | 1246 | public function cached($ttl = 0){ |
| 1247 | - if($ttl > 0){ |
|
| 1248 | - $this->temporaryCacheTtl = $ttl; |
|
| 1249 | - } |
|
| 1247 | + if($ttl > 0){ |
|
| 1248 | + $this->temporaryCacheTtl = $ttl; |
|
| 1249 | + } |
|
| 1250 | + return $this; |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + /** |
|
| 1254 | + * Escape the data before execute query useful for security. |
|
| 1255 | + * @param mixed $data the data to be escaped |
|
| 1256 | + * @return mixed the data after escaped |
|
| 1257 | + */ |
|
| 1258 | + public function escape($data){ |
|
| 1259 | + if(is_null($data)){ |
|
| 1260 | + return null; |
|
| 1261 | + } |
|
| 1262 | + if(! $this->pdo){ |
|
| 1263 | + $this->connect(); |
|
| 1264 | + } |
|
| 1265 | + return $this->pdo->quote(trim($data)); |
|
| 1266 | + } |
|
| 1267 | + |
|
| 1268 | + /** |
|
| 1269 | + * Return the number query executed count for the current request |
|
| 1270 | + * @return int |
|
| 1271 | + */ |
|
| 1272 | + public function queryCount(){ |
|
| 1273 | + return $this->queryCount; |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + /** |
|
| 1277 | + * Return the current query SQL string |
|
| 1278 | + * @return string |
|
| 1279 | + */ |
|
| 1280 | + public function getQuery(){ |
|
| 1281 | + return $this->query; |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + /** |
|
| 1285 | + * Return the application database name |
|
| 1286 | + * @return string |
|
| 1287 | + */ |
|
| 1288 | + public function getDatabaseName(){ |
|
| 1289 | + return $this->databaseName; |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * Return the database configuration |
|
| 1294 | + * @return array |
|
| 1295 | + */ |
|
| 1296 | + public function getDatabaseConfiguration(){ |
|
| 1297 | + return $this->config; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + /** |
|
| 1301 | + * set the database configuration |
|
| 1302 | + * @param array $config the configuration |
|
| 1303 | + */ |
|
| 1304 | + public function setDatabaseConfiguration(array $config){ |
|
| 1305 | + $this->config = array_merge($this->config, $config); |
|
| 1306 | + $this->prefix = $this->config['prefix']; |
|
| 1307 | + $this->databaseName = $this->config['database']; |
|
| 1308 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1309 | + return $this; |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + /** |
|
| 1313 | + * Return the PDO instance |
|
| 1314 | + * @return PDO |
|
| 1315 | + */ |
|
| 1316 | + public function getPdo(){ |
|
| 1317 | + return $this->pdo; |
|
| 1318 | + } |
|
| 1319 | + |
|
| 1320 | + /** |
|
| 1321 | + * Set the PDO instance |
|
| 1322 | + * @param PDO $pdo the pdo object |
|
| 1323 | + */ |
|
| 1324 | + public function setPdo(PDO $pdo){ |
|
| 1325 | + $this->pdo = $pdo; |
|
| 1326 | + return $this; |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + |
|
| 1330 | + /** |
|
| 1331 | + * Return the Log instance |
|
| 1332 | + * @return Log |
|
| 1333 | + */ |
|
| 1334 | + public function getLogger(){ |
|
| 1335 | + return $this->logger; |
|
| 1336 | + } |
|
| 1337 | + |
|
| 1338 | + /** |
|
| 1339 | + * Set the log instance |
|
| 1340 | + * @param Log $logger the log object |
|
| 1341 | + */ |
|
| 1342 | + public function setLogger($logger){ |
|
| 1343 | + $this->logger = $logger; |
|
| 1344 | + return $this; |
|
| 1345 | + } |
|
| 1346 | + |
|
| 1347 | + /** |
|
| 1348 | + * Return the cache instance |
|
| 1349 | + * @return CacheInterface |
|
| 1350 | + */ |
|
| 1351 | + public function getCacheInstance(){ |
|
| 1352 | + return $this->cacheInstance; |
|
| 1353 | + } |
|
| 1354 | + |
|
| 1355 | + /** |
|
| 1356 | + * Set the cache instance |
|
| 1357 | + * @param CacheInterface $cache the cache object |
|
| 1358 | + */ |
|
| 1359 | + public function setCacheInstance($cache){ |
|
| 1360 | + $this->cacheInstance = $cache; |
|
| 1361 | + return $this; |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + /** |
|
| 1365 | + * Return the benchmark instance |
|
| 1366 | + * @return Benchmark |
|
| 1367 | + */ |
|
| 1368 | + public function getBenchmark(){ |
|
| 1369 | + return $this->benchmarkInstance; |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * Set the benchmark instance |
|
| 1374 | + * @param Benchmark $cache the cache object |
|
| 1375 | + */ |
|
| 1376 | + public function setBenchmark($benchmark){ |
|
| 1377 | + $this->benchmarkInstance = $benchmark; |
|
| 1378 | + return $this; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + /** |
|
| 1382 | + * Return the data to be used for insert, update, etc. |
|
| 1383 | + * @return array |
|
| 1384 | + */ |
|
| 1385 | + public function getData(){ |
|
| 1386 | + return $this->data; |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * Set the data to be used for insert, update, etc. |
|
| 1391 | + * @param string $key the data key identified |
|
| 1392 | + * @param mixed $value the data value |
|
| 1393 | + * @param boolean $escape whether to escape or not the $value |
|
| 1394 | + * @return object the current Database instance |
|
| 1395 | + */ |
|
| 1396 | + public function setData($key, $value, $escape = true){ |
|
| 1397 | + $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1250 | 1398 | return $this; |
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - /** |
|
| 1254 | - * Escape the data before execute query useful for security. |
|
| 1255 | - * @param mixed $data the data to be escaped |
|
| 1256 | - * @return mixed the data after escaped |
|
| 1257 | - */ |
|
| 1258 | - public function escape($data){ |
|
| 1259 | - if(is_null($data)){ |
|
| 1260 | - return null; |
|
| 1261 | - } |
|
| 1262 | - if(! $this->pdo){ |
|
| 1263 | - $this->connect(); |
|
| 1264 | - } |
|
| 1265 | - return $this->pdo->quote(trim($data)); |
|
| 1266 | - } |
|
| 1267 | - |
|
| 1268 | - /** |
|
| 1269 | - * Return the number query executed count for the current request |
|
| 1270 | - * @return int |
|
| 1271 | - */ |
|
| 1272 | - public function queryCount(){ |
|
| 1273 | - return $this->queryCount; |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - /** |
|
| 1277 | - * Return the current query SQL string |
|
| 1278 | - * @return string |
|
| 1279 | - */ |
|
| 1280 | - public function getQuery(){ |
|
| 1281 | - return $this->query; |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - /** |
|
| 1285 | - * Return the application database name |
|
| 1286 | - * @return string |
|
| 1287 | - */ |
|
| 1288 | - public function getDatabaseName(){ |
|
| 1289 | - return $this->databaseName; |
|
| 1290 | - } |
|
| 1291 | - |
|
| 1292 | - /** |
|
| 1293 | - * Return the database configuration |
|
| 1294 | - * @return array |
|
| 1295 | - */ |
|
| 1296 | - public function getDatabaseConfiguration(){ |
|
| 1297 | - return $this->config; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - /** |
|
| 1301 | - * set the database configuration |
|
| 1302 | - * @param array $config the configuration |
|
| 1303 | - */ |
|
| 1304 | - public function setDatabaseConfiguration(array $config){ |
|
| 1305 | - $this->config = array_merge($this->config, $config); |
|
| 1306 | - $this->prefix = $this->config['prefix']; |
|
| 1307 | - $this->databaseName = $this->config['database']; |
|
| 1308 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1309 | - return $this; |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - /** |
|
| 1313 | - * Return the PDO instance |
|
| 1314 | - * @return PDO |
|
| 1315 | - */ |
|
| 1316 | - public function getPdo(){ |
|
| 1317 | - return $this->pdo; |
|
| 1318 | - } |
|
| 1319 | - |
|
| 1320 | - /** |
|
| 1321 | - * Set the PDO instance |
|
| 1322 | - * @param PDO $pdo the pdo object |
|
| 1323 | - */ |
|
| 1324 | - public function setPdo(PDO $pdo){ |
|
| 1325 | - $this->pdo = $pdo; |
|
| 1326 | - return $this; |
|
| 1327 | - } |
|
| 1328 | - |
|
| 1329 | - |
|
| 1330 | - /** |
|
| 1331 | - * Return the Log instance |
|
| 1332 | - * @return Log |
|
| 1333 | - */ |
|
| 1334 | - public function getLogger(){ |
|
| 1335 | - return $this->logger; |
|
| 1336 | - } |
|
| 1337 | - |
|
| 1338 | - /** |
|
| 1339 | - * Set the log instance |
|
| 1340 | - * @param Log $logger the log object |
|
| 1341 | - */ |
|
| 1342 | - public function setLogger($logger){ |
|
| 1343 | - $this->logger = $logger; |
|
| 1344 | - return $this; |
|
| 1345 | - } |
|
| 1346 | - |
|
| 1347 | - /** |
|
| 1348 | - * Return the cache instance |
|
| 1349 | - * @return CacheInterface |
|
| 1350 | - */ |
|
| 1351 | - public function getCacheInstance(){ |
|
| 1352 | - return $this->cacheInstance; |
|
| 1353 | - } |
|
| 1354 | - |
|
| 1355 | - /** |
|
| 1356 | - * Set the cache instance |
|
| 1357 | - * @param CacheInterface $cache the cache object |
|
| 1358 | - */ |
|
| 1359 | - public function setCacheInstance($cache){ |
|
| 1360 | - $this->cacheInstance = $cache; |
|
| 1361 | - return $this; |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - /** |
|
| 1365 | - * Return the benchmark instance |
|
| 1366 | - * @return Benchmark |
|
| 1367 | - */ |
|
| 1368 | - public function getBenchmark(){ |
|
| 1369 | - return $this->benchmarkInstance; |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * Set the benchmark instance |
|
| 1374 | - * @param Benchmark $cache the cache object |
|
| 1375 | - */ |
|
| 1376 | - public function setBenchmark($benchmark){ |
|
| 1377 | - $this->benchmarkInstance = $benchmark; |
|
| 1378 | - return $this; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - /** |
|
| 1382 | - * Return the data to be used for insert, update, etc. |
|
| 1383 | - * @return array |
|
| 1384 | - */ |
|
| 1385 | - public function getData(){ |
|
| 1386 | - return $this->data; |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - /** |
|
| 1390 | - * Set the data to be used for insert, update, etc. |
|
| 1391 | - * @param string $key the data key identified |
|
| 1392 | - * @param mixed $value the data value |
|
| 1393 | - * @param boolean $escape whether to escape or not the $value |
|
| 1394 | - * @return object the current Database instance |
|
| 1395 | - */ |
|
| 1396 | - public function setData($key, $value, $escape = true){ |
|
| 1397 | - $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1398 | - return $this; |
|
| 1399 | - } |
|
| 1399 | + } |
|
| 1400 | 1400 | |
| 1401 | 1401 | |
| 1402 | 1402 | /** |
| 1403 | 1403 | * Reset the database class attributs to the initail values before each query. |
| 1404 | 1404 | */ |
| 1405 | 1405 | private function reset(){ |
| 1406 | - $this->select = '*'; |
|
| 1407 | - $this->from = null; |
|
| 1408 | - $this->where = null; |
|
| 1409 | - $this->limit = null; |
|
| 1410 | - $this->orderBy = null; |
|
| 1411 | - $this->groupBy = null; |
|
| 1412 | - $this->having = null; |
|
| 1413 | - $this->join = null; |
|
| 1414 | - $this->numRows = 0; |
|
| 1415 | - $this->insertId = null; |
|
| 1416 | - $this->query = null; |
|
| 1417 | - $this->error = null; |
|
| 1418 | - $this->result = array(); |
|
| 1419 | - $this->data = array(); |
|
| 1406 | + $this->select = '*'; |
|
| 1407 | + $this->from = null; |
|
| 1408 | + $this->where = null; |
|
| 1409 | + $this->limit = null; |
|
| 1410 | + $this->orderBy = null; |
|
| 1411 | + $this->groupBy = null; |
|
| 1412 | + $this->having = null; |
|
| 1413 | + $this->join = null; |
|
| 1414 | + $this->numRows = 0; |
|
| 1415 | + $this->insertId = null; |
|
| 1416 | + $this->query = null; |
|
| 1417 | + $this->error = null; |
|
| 1418 | + $this->result = array(); |
|
| 1419 | + $this->data = array(); |
|
| 1420 | 1420 | } |
| 1421 | 1421 | |
| 1422 | 1422 | /** |
| 1423 | 1423 | * The class destructor |
| 1424 | 1424 | */ |
| 1425 | 1425 | function __destruct(){ |
| 1426 | - $this->pdo = null; |
|
| 1426 | + $this->pdo = null; |
|
| 1427 | 1427 | } |
| 1428 | 1428 | |
| 1429 | 1429 | } |
@@ -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 = array(); |
@@ -213,12 +213,12 @@ discard block |
||
| 213 | 213 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
| 214 | 214 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
| 215 | 215 | $port = ''; |
| 216 | - if(strstr($config['hostname'], ':')){ |
|
| 216 | + if (strstr($config['hostname'], ':')) { |
|
| 217 | 217 | $p = explode(':', $config['hostname']); |
| 218 | 218 | $port = isset($p[1]) ? $p[1] : ''; |
| 219 | 219 | $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
| 220 | 220 | } |
| 221 | - $config['port'] = $port; |
|
| 221 | + $config['port'] = $port; |
|
| 222 | 222 | |
| 223 | 223 | $this->setDatabaseConfiguration($config); |
| 224 | 224 | $this->temporaryCacheTtl = $this->cacheTtl; |
@@ -228,10 +228,10 @@ discard block |
||
| 228 | 228 | * This is used to connect to database |
| 229 | 229 | * @return bool |
| 230 | 230 | */ |
| 231 | - public function connect(){ |
|
| 231 | + public function connect() { |
|
| 232 | 232 | $config = $this->getDatabaseConfiguration(); |
| 233 | - if(! empty($config)){ |
|
| 234 | - try{ |
|
| 233 | + if (!empty($config)) { |
|
| 234 | + try { |
|
| 235 | 235 | $driverDsnMap = array( |
| 236 | 236 | 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
| 237 | 237 | . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
@@ -252,13 +252,13 @@ discard block |
||
| 252 | 252 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 253 | 253 | return true; |
| 254 | 254 | } |
| 255 | - catch (PDOException $e){ |
|
| 255 | + catch (PDOException $e) { |
|
| 256 | 256 | $this->logger->fatal($e->getMessage()); |
| 257 | 257 | show_error('Cannot connect to Database.'); |
| 258 | 258 | return false; |
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | - else{ |
|
| 261 | + else { |
|
| 262 | 262 | show_error('Database configuration is not set.'); |
| 263 | 263 | return false; |
| 264 | 264 | } |
@@ -269,15 +269,15 @@ discard block |
||
| 269 | 269 | * @param string|array $table the table name or array of table list |
| 270 | 270 | * @return object the current Database instance |
| 271 | 271 | */ |
| 272 | - public function from($table){ |
|
| 273 | - if(is_array($table)){ |
|
| 272 | + public function from($table) { |
|
| 273 | + if (is_array($table)) { |
|
| 274 | 274 | $froms = ''; |
| 275 | - foreach($table as $key){ |
|
| 275 | + foreach ($table as $key) { |
|
| 276 | 276 | $froms .= $this->prefix . $key . ', '; |
| 277 | 277 | } |
| 278 | 278 | $this->from = rtrim($froms, ', '); |
| 279 | 279 | } |
| 280 | - else{ |
|
| 280 | + else { |
|
| 281 | 281 | $this->from = $this->prefix . $table; |
| 282 | 282 | } |
| 283 | 283 | return $this; |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param string|array $fields the field name or array of field list |
| 289 | 289 | * @return object the current Database instance |
| 290 | 290 | */ |
| 291 | - public function select($fields){ |
|
| 291 | + public function select($fields) { |
|
| 292 | 292 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
| 293 | 293 | $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
| 294 | 294 | return $this; |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | * @param string $field the field name to distinct |
| 300 | 300 | * @return object the current Database instance |
| 301 | 301 | */ |
| 302 | - public function distinct($field){ |
|
| 302 | + public function distinct($field) { |
|
| 303 | 303 | $distinct = ' DISTINCT ' . $field; |
| 304 | 304 | $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
| 305 | 305 | |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | * @param string $name if is not null represent the alias used for this field in the result |
| 313 | 313 | * @return object the current Database instance |
| 314 | 314 | */ |
| 315 | - public function max($field, $name = null){ |
|
| 315 | + public function max($field, $name = null) { |
|
| 316 | 316 | $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 317 | 317 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 318 | 318 | return $this; |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | * @param string $name if is not null represent the alias used for this field in the result |
| 325 | 325 | * @return object the current Database instance |
| 326 | 326 | */ |
| 327 | - public function min($field, $name = null){ |
|
| 327 | + public function min($field, $name = null) { |
|
| 328 | 328 | $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 329 | 329 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 330 | 330 | return $this; |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * @param string $name if is not null represent the alias used for this field in the result |
| 337 | 337 | * @return object the current Database instance |
| 338 | 338 | */ |
| 339 | - public function sum($field, $name = null){ |
|
| 339 | + public function sum($field, $name = null) { |
|
| 340 | 340 | $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 341 | 341 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 342 | 342 | return $this; |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | * @param string $name if is not null represent the alias used for this field in the result |
| 349 | 349 | * @return object the current Database instance |
| 350 | 350 | */ |
| 351 | - public function count($field = '*', $name = null){ |
|
| 351 | + public function count($field = '*', $name = null) { |
|
| 352 | 352 | $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 353 | 353 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 354 | 354 | return $this; |
@@ -360,7 +360,7 @@ discard block |
||
| 360 | 360 | * @param string $name if is not null represent the alias used for this field in the result |
| 361 | 361 | * @return object the current Database instance |
| 362 | 362 | */ |
| 363 | - public function avg($field, $name = null){ |
|
| 363 | + public function avg($field, $name = null) { |
|
| 364 | 364 | $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 365 | 365 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 366 | 366 | return $this; |
@@ -375,16 +375,16 @@ discard block |
||
| 375 | 375 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
| 376 | 376 | * @return object the current Database instance |
| 377 | 377 | */ |
| 378 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 378 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
| 379 | 379 | $on = $field1; |
| 380 | 380 | $table = $this->prefix . $table; |
| 381 | - if(! is_null($op)){ |
|
| 382 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 381 | + if (!is_null($op)) { |
|
| 382 | + $on = (!in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 383 | 383 | } |
| 384 | - if (is_null($this->join)){ |
|
| 384 | + if (is_null($this->join)) { |
|
| 385 | 385 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 386 | 386 | } |
| 387 | - else{ |
|
| 387 | + else { |
|
| 388 | 388 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 389 | 389 | } |
| 390 | 390 | return $this; |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * @see Database::join() |
| 396 | 396 | * @return object the current Database instance |
| 397 | 397 | */ |
| 398 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 398 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
| 399 | 399 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
| 400 | 400 | } |
| 401 | 401 | |
@@ -404,7 +404,7 @@ discard block |
||
| 404 | 404 | * @see Database::join() |
| 405 | 405 | * @return object the current Database instance |
| 406 | 406 | */ |
| 407 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 407 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
| 408 | 408 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
| 409 | 409 | } |
| 410 | 410 | |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | * @see Database::join() |
| 414 | 414 | * @return object the current Database instance |
| 415 | 415 | */ |
| 416 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 416 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
| 417 | 417 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
| 418 | 418 | } |
| 419 | 419 | |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | * @see Database::join() |
| 423 | 423 | * @return object the current Database instance |
| 424 | 424 | */ |
| 425 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 425 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 426 | 426 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
| 427 | 427 | } |
| 428 | 428 | |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | * @see Database::join() |
| 432 | 432 | * @return object the current Database instance |
| 433 | 433 | */ |
| 434 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 434 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 435 | 435 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
| 436 | 436 | } |
| 437 | 437 | |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | * @see Database::join() |
| 441 | 441 | * @return object the current Database instance |
| 442 | 442 | */ |
| 443 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 443 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 444 | 444 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
| 445 | 445 | } |
| 446 | 446 | |
@@ -450,18 +450,18 @@ discard block |
||
| 450 | 450 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 451 | 451 | * @return object the current Database instance |
| 452 | 452 | */ |
| 453 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 454 | - if(is_array($field)){ |
|
| 455 | - foreach($field as $f){ |
|
| 453 | + public function whereIsNull($field, $andOr = 'AND') { |
|
| 454 | + if (is_array($field)) { |
|
| 455 | + foreach ($field as $f) { |
|
| 456 | 456 | $this->whereIsNull($f, $andOr); |
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | - else{ |
|
| 460 | - if (! $this->where){ |
|
| 461 | - $this->where = $field.' IS NULL '; |
|
| 459 | + else { |
|
| 460 | + if (!$this->where) { |
|
| 461 | + $this->where = $field . ' IS NULL '; |
|
| 462 | 462 | } |
| 463 | - else{ |
|
| 464 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 463 | + else { |
|
| 464 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NULL '; |
|
| 465 | 465 | } |
| 466 | 466 | } |
| 467 | 467 | return $this; |
@@ -473,18 +473,18 @@ discard block |
||
| 473 | 473 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 474 | 474 | * @return object the current Database instance |
| 475 | 475 | */ |
| 476 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 477 | - if(is_array($field)){ |
|
| 478 | - foreach($field as $f){ |
|
| 476 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
| 477 | + if (is_array($field)) { |
|
| 478 | + foreach ($field as $f) { |
|
| 479 | 479 | $this->whereIsNotNull($f, $andOr); |
| 480 | 480 | } |
| 481 | 481 | } |
| 482 | - else{ |
|
| 483 | - if (! $this->where){ |
|
| 484 | - $this->where = $field.' IS NOT NULL '; |
|
| 482 | + else { |
|
| 483 | + if (!$this->where) { |
|
| 484 | + $this->where = $field . ' IS NOT NULL '; |
|
| 485 | 485 | } |
| 486 | - else{ |
|
| 487 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 486 | + else { |
|
| 487 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NOT NULL '; |
|
| 488 | 488 | } |
| 489 | 489 | } |
| 490 | 490 | return $this; |
@@ -500,24 +500,24 @@ discard block |
||
| 500 | 500 | * @param boolean $escape whether to escape or not the $val |
| 501 | 501 | * @return object the current Database instance |
| 502 | 502 | */ |
| 503 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 504 | - if (is_array($where)){ |
|
| 503 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
| 504 | + if (is_array($where)) { |
|
| 505 | 505 | $_where = array(); |
| 506 | - foreach ($where as $column => $data){ |
|
| 507 | - if(is_null($data)){ |
|
| 506 | + foreach ($where as $column => $data) { |
|
| 507 | + if (is_null($data)) { |
|
| 508 | 508 | $data = ''; |
| 509 | 509 | } |
| 510 | 510 | $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
| 511 | 511 | } |
| 512 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 512 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
| 513 | 513 | } |
| 514 | - else{ |
|
| 515 | - if(is_array($op)){ |
|
| 514 | + else { |
|
| 515 | + if (is_array($op)) { |
|
| 516 | 516 | $x = explode('?', $where); |
| 517 | 517 | $w = ''; |
| 518 | - foreach($x as $k => $v){ |
|
| 519 | - if(! empty($v)){ |
|
| 520 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 518 | + foreach ($x as $k => $v) { |
|
| 519 | + if (!empty($v)) { |
|
| 520 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 521 | 521 | $op[$k] = ''; |
| 522 | 522 | } |
| 523 | 523 | $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -525,28 +525,28 @@ discard block |
||
| 525 | 525 | } |
| 526 | 526 | $where = $w; |
| 527 | 527 | } |
| 528 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 529 | - if(is_null($op)){ |
|
| 528 | + else if (!in_array((string) $op, $this->operatorList)) { |
|
| 529 | + if (is_null($op)) { |
|
| 530 | 530 | $op = ''; |
| 531 | 531 | } |
| 532 | 532 | $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 533 | 533 | } |
| 534 | - else{ |
|
| 535 | - if(is_null($val)){ |
|
| 534 | + else { |
|
| 535 | + if (is_null($val)) { |
|
| 536 | 536 | $val = ''; |
| 537 | 537 | } |
| 538 | 538 | $where = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | - if (is_null($this->where)){ |
|
| 541 | + if (is_null($this->where)) { |
|
| 542 | 542 | $this->where = $where; |
| 543 | 543 | } |
| 544 | - else{ |
|
| 545 | - if(substr($this->where, -1) == '('){ |
|
| 544 | + else { |
|
| 545 | + if (substr($this->where, -1) == '(') { |
|
| 546 | 546 | $this->where = $this->where . ' ' . $where; |
| 547 | 547 | } |
| 548 | - else{ |
|
| 549 | - $this->where = $this->where . ' '.$andOr.' ' . $where; |
|
| 548 | + else { |
|
| 549 | + $this->where = $this->where . ' ' . $andOr . ' ' . $where; |
|
| 550 | 550 | } |
| 551 | 551 | } |
| 552 | 552 | return $this; |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | * @see Database::where() |
| 558 | 558 | * @return object the current Database instance |
| 559 | 559 | */ |
| 560 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 560 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
| 561 | 561 | return $this->where($where, $op, $val, '', 'OR', $escape); |
| 562 | 562 | } |
| 563 | 563 | |
@@ -567,7 +567,7 @@ discard block |
||
| 567 | 567 | * @see Database::where() |
| 568 | 568 | * @return object the current Database instance |
| 569 | 569 | */ |
| 570 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 570 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
| 571 | 571 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
| 572 | 572 | } |
| 573 | 573 | |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | * @see Database::where() |
| 577 | 577 | * @return object the current Database instance |
| 578 | 578 | */ |
| 579 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 579 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
| 580 | 580 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
| 581 | 581 | } |
| 582 | 582 | |
@@ -586,15 +586,15 @@ discard block |
||
| 586 | 586 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
| 587 | 587 | * @return object the current Database instance |
| 588 | 588 | */ |
| 589 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 590 | - if (is_null($this->where)){ |
|
| 589 | + public function groupStart($type = '', $andOr = ' AND') { |
|
| 590 | + if (is_null($this->where)) { |
|
| 591 | 591 | $this->where = $type . ' ('; |
| 592 | 592 | } |
| 593 | - else{ |
|
| 594 | - if(substr($this->where, -1) == '('){ |
|
| 593 | + else { |
|
| 594 | + if (substr($this->where, -1) == '(') { |
|
| 595 | 595 | $this->where .= $type . ' ('; |
| 596 | 596 | } |
| 597 | - else{ |
|
| 597 | + else { |
|
| 598 | 598 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 599 | 599 | } |
| 600 | 600 | } |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | * @see Database::groupStart() |
| 607 | 607 | * @return object the current Database instance |
| 608 | 608 | */ |
| 609 | - public function notGroupStart(){ |
|
| 609 | + public function notGroupStart() { |
|
| 610 | 610 | return $this->groupStart('NOT'); |
| 611 | 611 | } |
| 612 | 612 | |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | * @see Database::groupStart() |
| 616 | 616 | * @return object the current Database instance |
| 617 | 617 | */ |
| 618 | - public function orGroupStart(){ |
|
| 618 | + public function orGroupStart() { |
|
| 619 | 619 | return $this->groupStart('', ' OR'); |
| 620 | 620 | } |
| 621 | 621 | |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | * @see Database::groupStart() |
| 625 | 625 | * @return object the current Database instance |
| 626 | 626 | */ |
| 627 | - public function orNotGroupStart(){ |
|
| 627 | + public function orNotGroupStart() { |
|
| 628 | 628 | return $this->groupStart('NOT', ' OR'); |
| 629 | 629 | } |
| 630 | 630 | |
@@ -632,7 +632,7 @@ discard block |
||
| 632 | 632 | * Close the parenthesis for the grouped SQL |
| 633 | 633 | * @return object the current Database instance |
| 634 | 634 | */ |
| 635 | - public function groupEnd(){ |
|
| 635 | + public function groupEnd() { |
|
| 636 | 636 | $this->where .= ')'; |
| 637 | 637 | return $this; |
| 638 | 638 | } |
@@ -646,25 +646,25 @@ discard block |
||
| 646 | 646 | * @param boolean $escape whether to escape or not the values |
| 647 | 647 | * @return object the current Database instance |
| 648 | 648 | */ |
| 649 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 650 | - if (is_array($keys)){ |
|
| 649 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
| 650 | + if (is_array($keys)) { |
|
| 651 | 651 | $_keys = array(); |
| 652 | - foreach ($keys as $k => $v){ |
|
| 653 | - if(is_null($v)){ |
|
| 652 | + foreach ($keys as $k => $v) { |
|
| 653 | + if (is_null($v)) { |
|
| 654 | 654 | $v = ''; |
| 655 | 655 | } |
| 656 | 656 | $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
| 657 | 657 | } |
| 658 | 658 | $keys = implode(', ', $_keys); |
| 659 | - if (is_null($this->where)){ |
|
| 659 | + if (is_null($this->where)) { |
|
| 660 | 660 | $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
| 661 | 661 | } |
| 662 | - else{ |
|
| 663 | - if(substr($this->where, -1) == '('){ |
|
| 664 | - $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 662 | + else { |
|
| 663 | + if (substr($this->where, -1) == '(') { |
|
| 664 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 665 | 665 | } |
| 666 | - else{ |
|
| 667 | - $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
|
| 666 | + else { |
|
| 667 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'IN (' . $keys . ')'; |
|
| 668 | 668 | } |
| 669 | 669 | } |
| 670 | 670 | } |
@@ -676,7 +676,7 @@ discard block |
||
| 676 | 676 | * @see Database::in() |
| 677 | 677 | * @return object the current Database instance |
| 678 | 678 | */ |
| 679 | - public function notIn($field, array $keys, $escape = true){ |
|
| 679 | + public function notIn($field, array $keys, $escape = true) { |
|
| 680 | 680 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
| 681 | 681 | } |
| 682 | 682 | |
@@ -685,7 +685,7 @@ discard block |
||
| 685 | 685 | * @see Database::in() |
| 686 | 686 | * @return object the current Database instance |
| 687 | 687 | */ |
| 688 | - public function orIn($field, array $keys, $escape = true){ |
|
| 688 | + public function orIn($field, array $keys, $escape = true) { |
|
| 689 | 689 | return $this->in($field, $keys, '', 'OR', $escape); |
| 690 | 690 | } |
| 691 | 691 | |
@@ -694,7 +694,7 @@ discard block |
||
| 694 | 694 | * @see Database::in() |
| 695 | 695 | * @return object the current Database instance |
| 696 | 696 | */ |
| 697 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 697 | + public function orNotIn($field, array $keys, $escape = true) { |
|
| 698 | 698 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
| 699 | 699 | } |
| 700 | 700 | |
@@ -708,21 +708,21 @@ discard block |
||
| 708 | 708 | * @param boolean $escape whether to escape or not the values |
| 709 | 709 | * @return object the current Database instance |
| 710 | 710 | */ |
| 711 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 712 | - if(is_null($value1)){ |
|
| 711 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
| 712 | + if (is_null($value1)) { |
|
| 713 | 713 | $value1 = ''; |
| 714 | 714 | } |
| 715 | - if(is_null($value2)){ |
|
| 715 | + if (is_null($value2)) { |
|
| 716 | 716 | $value2 = ''; |
| 717 | 717 | } |
| 718 | - if (is_null($this->where)){ |
|
| 718 | + if (is_null($this->where)) { |
|
| 719 | 719 | $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 720 | 720 | } |
| 721 | - else{ |
|
| 722 | - if(substr($this->where, -1) == '('){ |
|
| 721 | + else { |
|
| 722 | + if (substr($this->where, -1) == '(') { |
|
| 723 | 723 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 724 | 724 | } |
| 725 | - else{ |
|
| 725 | + else { |
|
| 726 | 726 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 727 | 727 | } |
| 728 | 728 | } |
@@ -734,7 +734,7 @@ discard block |
||
| 734 | 734 | * @see Database::between() |
| 735 | 735 | * @return object the current Database instance |
| 736 | 736 | */ |
| 737 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 737 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
| 738 | 738 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
| 739 | 739 | } |
| 740 | 740 | |
@@ -743,7 +743,7 @@ discard block |
||
| 743 | 743 | * @see Database::between() |
| 744 | 744 | * @return object the current Database instance |
| 745 | 745 | */ |
| 746 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 746 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
| 747 | 747 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
| 748 | 748 | } |
| 749 | 749 | |
@@ -752,7 +752,7 @@ discard block |
||
| 752 | 752 | * @see Database::between() |
| 753 | 753 | * @return object the current Database instance |
| 754 | 754 | */ |
| 755 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 755 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
| 756 | 756 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
| 757 | 757 | } |
| 758 | 758 | |
@@ -765,20 +765,20 @@ discard block |
||
| 765 | 765 | * @param boolean $escape whether to escape or not the values |
| 766 | 766 | * @return object the current Database instance |
| 767 | 767 | */ |
| 768 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 769 | - if(is_null($data)){ |
|
| 768 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
| 769 | + if (is_null($data)) { |
|
| 770 | 770 | $data = ''; |
| 771 | 771 | } |
| 772 | 772 | $like = $escape ? $this->escape($data) : $data; |
| 773 | - if (is_null($this->where)){ |
|
| 773 | + if (is_null($this->where)) { |
|
| 774 | 774 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 775 | 775 | } |
| 776 | - else{ |
|
| 777 | - if(substr($this->where, -1) == '('){ |
|
| 776 | + else { |
|
| 777 | + if (substr($this->where, -1) == '(') { |
|
| 778 | 778 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 779 | 779 | } |
| 780 | - else{ |
|
| 781 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 780 | + else { |
|
| 781 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 782 | 782 | } |
| 783 | 783 | } |
| 784 | 784 | return $this; |
@@ -789,7 +789,7 @@ discard block |
||
| 789 | 789 | * @see Database::like() |
| 790 | 790 | * @return object the current Database instance |
| 791 | 791 | */ |
| 792 | - public function orLike($field, $data, $escape = true){ |
|
| 792 | + public function orLike($field, $data, $escape = true) { |
|
| 793 | 793 | return $this->like($field, $data, '', 'OR', $escape); |
| 794 | 794 | } |
| 795 | 795 | |
@@ -798,7 +798,7 @@ discard block |
||
| 798 | 798 | * @see Database::like() |
| 799 | 799 | * @return object the current Database instance |
| 800 | 800 | */ |
| 801 | - public function notLike($field, $data, $escape = true){ |
|
| 801 | + public function notLike($field, $data, $escape = true) { |
|
| 802 | 802 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
| 803 | 803 | } |
| 804 | 804 | |
@@ -807,7 +807,7 @@ discard block |
||
| 807 | 807 | * @see Database::like() |
| 808 | 808 | * @return object the current Database instance |
| 809 | 809 | */ |
| 810 | - public function orNotLike($field, $data, $escape = true){ |
|
| 810 | + public function orNotLike($field, $data, $escape = true) { |
|
| 811 | 811 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
| 812 | 812 | } |
| 813 | 813 | |
@@ -818,14 +818,14 @@ discard block |
||
| 818 | 818 | * @param int $limitEnd the limit count |
| 819 | 819 | * @return object the current Database instance |
| 820 | 820 | */ |
| 821 | - public function limit($limit, $limitEnd = null){ |
|
| 822 | - if(is_null($limit)){ |
|
| 821 | + public function limit($limit, $limitEnd = null) { |
|
| 822 | + if (is_null($limit)) { |
|
| 823 | 823 | return; |
| 824 | 824 | } |
| 825 | - if (! is_null($limitEnd)){ |
|
| 825 | + if (!is_null($limitEnd)) { |
|
| 826 | 826 | $this->limit = $limit . ', ' . $limitEnd; |
| 827 | 827 | } |
| 828 | - else{ |
|
| 828 | + else { |
|
| 829 | 829 | $this->limit = $limit; |
| 830 | 830 | } |
| 831 | 831 | return $this; |
@@ -837,16 +837,16 @@ discard block |
||
| 837 | 837 | * @param string $orderDir the order direction (ASC or DESC) |
| 838 | 838 | * @return object the current Database instance |
| 839 | 839 | */ |
| 840 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 841 | - if (! is_null($orderDir)){ |
|
| 842 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 840 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
| 841 | + if (!is_null($orderDir)) { |
|
| 842 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 843 | 843 | } |
| 844 | - else{ |
|
| 845 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 846 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 844 | + else { |
|
| 845 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
| 846 | + $this->orderBy = !$this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 847 | 847 | } |
| 848 | - else{ |
|
| 849 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 848 | + else { |
|
| 849 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 850 | 850 | } |
| 851 | 851 | } |
| 852 | 852 | return $this; |
@@ -857,11 +857,11 @@ discard block |
||
| 857 | 857 | * @param string|array $field the field name used or array of field list |
| 858 | 858 | * @return object the current Database instance |
| 859 | 859 | */ |
| 860 | - public function groupBy($field){ |
|
| 861 | - if(is_array($field)){ |
|
| 860 | + public function groupBy($field) { |
|
| 861 | + if (is_array($field)) { |
|
| 862 | 862 | $this->groupBy = implode(', ', $field); |
| 863 | 863 | } |
| 864 | - else{ |
|
| 864 | + else { |
|
| 865 | 865 | $this->groupBy = $field; |
| 866 | 866 | } |
| 867 | 867 | return $this; |
@@ -875,13 +875,13 @@ discard block |
||
| 875 | 875 | * @param boolean $escape whether to escape or not the values |
| 876 | 876 | * @return object the current Database instance |
| 877 | 877 | */ |
| 878 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 879 | - if(is_array($op)){ |
|
| 878 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
| 879 | + if (is_array($op)) { |
|
| 880 | 880 | $x = explode('?', $field); |
| 881 | 881 | $w = ''; |
| 882 | - foreach($x as $k => $v){ |
|
| 883 | - if(!empty($v)){ |
|
| 884 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 882 | + foreach ($x as $k => $v) { |
|
| 883 | + if (!empty($v)) { |
|
| 884 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 885 | 885 | $op[$k] = ''; |
| 886 | 886 | } |
| 887 | 887 | $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -889,14 +889,14 @@ discard block |
||
| 889 | 889 | } |
| 890 | 890 | $this->having = $w; |
| 891 | 891 | } |
| 892 | - else if (! in_array($op, $this->operatorList)){ |
|
| 893 | - if(is_null($op)){ |
|
| 892 | + else if (!in_array($op, $this->operatorList)) { |
|
| 893 | + if (is_null($op)) { |
|
| 894 | 894 | $op = ''; |
| 895 | 895 | } |
| 896 | 896 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 897 | 897 | } |
| 898 | - else{ |
|
| 899 | - if(is_null($val)){ |
|
| 898 | + else { |
|
| 899 | + if (is_null($val)) { |
|
| 900 | 900 | $val = ''; |
| 901 | 901 | } |
| 902 | 902 | $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
@@ -908,7 +908,7 @@ discard block |
||
| 908 | 908 | * Return the number of rows returned by the current query |
| 909 | 909 | * @return int |
| 910 | 910 | */ |
| 911 | - public function numRows(){ |
|
| 911 | + public function numRows() { |
|
| 912 | 912 | return $this->numRows; |
| 913 | 913 | } |
| 914 | 914 | |
@@ -916,15 +916,15 @@ discard block |
||
| 916 | 916 | * Return the last insert id value |
| 917 | 917 | * @return mixed |
| 918 | 918 | */ |
| 919 | - public function insertId(){ |
|
| 919 | + public function insertId() { |
|
| 920 | 920 | return $this->insertId; |
| 921 | 921 | } |
| 922 | 922 | |
| 923 | 923 | /** |
| 924 | 924 | * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
| 925 | 925 | */ |
| 926 | - public function error(){ |
|
| 927 | - if($this->error){ |
|
| 926 | + public function error() { |
|
| 927 | + if ($this->error) { |
|
| 928 | 928 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 929 | 929 | } |
| 930 | 930 | } |
@@ -935,14 +935,14 @@ discard block |
||
| 935 | 935 | * If is string will determine the result type "array" or "object" |
| 936 | 936 | * @return mixed the query SQL string or the record result |
| 937 | 937 | */ |
| 938 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 938 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 939 | 939 | $this->limit = 1; |
| 940 | 940 | $query = $this->getAll(true); |
| 941 | - if($returnSQLQueryOrResultType === true){ |
|
| 941 | + if ($returnSQLQueryOrResultType === true) { |
|
| 942 | 942 | return $query; |
| 943 | 943 | } |
| 944 | - else{ |
|
| 945 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 944 | + else { |
|
| 945 | + return $this->query($query, false, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 946 | 946 | } |
| 947 | 947 | } |
| 948 | 948 | |
@@ -952,37 +952,37 @@ discard block |
||
| 952 | 952 | * If is string will determine the result type "array" or "object" |
| 953 | 953 | * @return mixed the query SQL string or the record result |
| 954 | 954 | */ |
| 955 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 955 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 956 | 956 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 957 | - if (! is_null($this->join)){ |
|
| 957 | + if (!is_null($this->join)) { |
|
| 958 | 958 | $query .= $this->join; |
| 959 | 959 | } |
| 960 | 960 | |
| 961 | - if (! is_null($this->where)){ |
|
| 961 | + if (!is_null($this->where)) { |
|
| 962 | 962 | $query .= ' WHERE ' . $this->where; |
| 963 | 963 | } |
| 964 | 964 | |
| 965 | - if (! is_null($this->groupBy)){ |
|
| 965 | + if (!is_null($this->groupBy)) { |
|
| 966 | 966 | $query .= ' GROUP BY ' . $this->groupBy; |
| 967 | 967 | } |
| 968 | 968 | |
| 969 | - if (! is_null($this->having)){ |
|
| 969 | + if (!is_null($this->having)) { |
|
| 970 | 970 | $query .= ' HAVING ' . $this->having; |
| 971 | 971 | } |
| 972 | 972 | |
| 973 | - if (! is_null($this->orderBy)){ |
|
| 973 | + if (!is_null($this->orderBy)) { |
|
| 974 | 974 | $query .= ' ORDER BY ' . $this->orderBy; |
| 975 | 975 | } |
| 976 | 976 | |
| 977 | - if(! is_null($this->limit)){ |
|
| 977 | + if (!is_null($this->limit)) { |
|
| 978 | 978 | $query .= ' LIMIT ' . $this->limit; |
| 979 | 979 | } |
| 980 | 980 | |
| 981 | - if($returnSQLQueryOrResultType === true){ |
|
| 981 | + if ($returnSQLQueryOrResultType === true) { |
|
| 982 | 982 | return $query; |
| 983 | 983 | } |
| 984 | - else{ |
|
| 985 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 984 | + else { |
|
| 985 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 986 | 986 | } |
| 987 | 987 | } |
| 988 | 988 | |
@@ -992,15 +992,15 @@ discard block |
||
| 992 | 992 | * @param boolean $escape whether to escape or not the values |
| 993 | 993 | * @return mixed the insert id of the new record or null |
| 994 | 994 | */ |
| 995 | - public function insert($data = array(), $escape = true){ |
|
| 995 | + public function insert($data = array(), $escape = true) { |
|
| 996 | 996 | $column = array(); |
| 997 | 997 | $val = array(); |
| 998 | - if(! $data && $this->getData()){ |
|
| 998 | + if (!$data && $this->getData()) { |
|
| 999 | 999 | $columns = array_keys($this->getData()); |
| 1000 | 1000 | $column = implode(',', $columns); |
| 1001 | 1001 | $val = implode(', ', $this->getData()); |
| 1002 | 1002 | } |
| 1003 | - else{ |
|
| 1003 | + else { |
|
| 1004 | 1004 | $columns = array_keys($data); |
| 1005 | 1005 | $column = implode(',', $columns); |
| 1006 | 1006 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -1009,14 +1009,14 @@ discard block |
||
| 1009 | 1009 | $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
| 1010 | 1010 | $query = $this->query($query); |
| 1011 | 1011 | |
| 1012 | - if ($query){ |
|
| 1013 | - if(! $this->pdo){ |
|
| 1012 | + if ($query) { |
|
| 1013 | + if (!$this->pdo) { |
|
| 1014 | 1014 | $this->connect(); |
| 1015 | 1015 | } |
| 1016 | 1016 | $this->insertId = $this->pdo->lastInsertId(); |
| 1017 | 1017 | return $this->insertId(); |
| 1018 | 1018 | } |
| 1019 | - else{ |
|
| 1019 | + else { |
|
| 1020 | 1020 | return false; |
| 1021 | 1021 | } |
| 1022 | 1022 | } |
@@ -1027,29 +1027,29 @@ discard block |
||
| 1027 | 1027 | * @param boolean $escape whether to escape or not the values |
| 1028 | 1028 | * @return mixed the update status |
| 1029 | 1029 | */ |
| 1030 | - public function update($data = array(), $escape = true){ |
|
| 1030 | + public function update($data = array(), $escape = true) { |
|
| 1031 | 1031 | $query = 'UPDATE ' . $this->from . ' SET '; |
| 1032 | 1032 | $values = array(); |
| 1033 | - if(! $data && $this->getData()){ |
|
| 1034 | - foreach ($this->getData() as $column => $val){ |
|
| 1033 | + if (!$data && $this->getData()) { |
|
| 1034 | + foreach ($this->getData() as $column => $val) { |
|
| 1035 | 1035 | $values[] = $column . ' = ' . $val; |
| 1036 | 1036 | } |
| 1037 | 1037 | } |
| 1038 | - else{ |
|
| 1039 | - foreach ($data as $column => $val){ |
|
| 1038 | + else { |
|
| 1039 | + foreach ($data as $column => $val) { |
|
| 1040 | 1040 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1041 | 1041 | } |
| 1042 | 1042 | } |
| 1043 | 1043 | $query .= (is_array($data) ? implode(', ', $values) : $data); |
| 1044 | - if (! is_null($this->where)){ |
|
| 1044 | + if (!is_null($this->where)) { |
|
| 1045 | 1045 | $query .= ' WHERE ' . $this->where; |
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | - if (! is_null($this->orderBy)){ |
|
| 1048 | + if (!is_null($this->orderBy)) { |
|
| 1049 | 1049 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1050 | 1050 | } |
| 1051 | 1051 | |
| 1052 | - if (! is_null($this->limit)){ |
|
| 1052 | + if (!is_null($this->limit)) { |
|
| 1053 | 1053 | $query .= ' LIMIT ' . $this->limit; |
| 1054 | 1054 | } |
| 1055 | 1055 | return $this->query($query); |
@@ -1059,22 +1059,22 @@ discard block |
||
| 1059 | 1059 | * Delete the record in database |
| 1060 | 1060 | * @return mixed the delete status |
| 1061 | 1061 | */ |
| 1062 | - public function delete(){ |
|
| 1062 | + public function delete() { |
|
| 1063 | 1063 | $query = 'DELETE FROM ' . $this->from; |
| 1064 | 1064 | |
| 1065 | - if (! is_null($this->where)){ |
|
| 1065 | + if (!is_null($this->where)) { |
|
| 1066 | 1066 | $query .= ' WHERE ' . $this->where; |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | - if (! is_null($this->orderBy)){ |
|
| 1069 | + if (!is_null($this->orderBy)) { |
|
| 1070 | 1070 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | - if (! is_null($this->limit)){ |
|
| 1073 | + if (!is_null($this->limit)) { |
|
| 1074 | 1074 | $query .= ' LIMIT ' . $this->limit; |
| 1075 | 1075 | } |
| 1076 | 1076 | |
| 1077 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1077 | + if ($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite') { |
|
| 1078 | 1078 | $query = 'TRUNCATE TABLE ' . $this->from; |
| 1079 | 1079 | } |
| 1080 | 1080 | return $this->query($query); |
@@ -1087,13 +1087,13 @@ discard block |
||
| 1087 | 1087 | * @param boolean $array return the result as array |
| 1088 | 1088 | * @return mixed the query result |
| 1089 | 1089 | */ |
| 1090 | - public function query($query, $all = true, $array = false){ |
|
| 1090 | + public function query($query, $all = true, $array = false) { |
|
| 1091 | 1091 | $this->reset(); |
| 1092 | - if(is_array($all)){ |
|
| 1092 | + if (is_array($all)) { |
|
| 1093 | 1093 | $x = explode('?', $query); |
| 1094 | 1094 | $q = ''; |
| 1095 | - foreach($x as $k => $v){ |
|
| 1096 | - if(! empty($v)){ |
|
| 1095 | + foreach ($x as $k => $v) { |
|
| 1096 | + if (!empty($v)) { |
|
| 1097 | 1097 | $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
| 1098 | 1098 | } |
| 1099 | 1099 | } |
@@ -1102,7 +1102,7 @@ discard block |
||
| 1102 | 1102 | |
| 1103 | 1103 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 1104 | 1104 | $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
| 1105 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1105 | + $this->logger->info('Execute SQL query [' . $this->query . '], return type: ' . ($array ? 'ARRAY' : 'OBJECT') . ', return as list: ' . ($all ? 'YES' : 'NO')); |
|
| 1106 | 1106 | //cache expire time |
| 1107 | 1107 | $cacheExpire = $this->temporaryCacheTtl; |
| 1108 | 1108 | |
@@ -1124,34 +1124,34 @@ discard block |
||
| 1124 | 1124 | //if can use cache feature for this query |
| 1125 | 1125 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1126 | 1126 | |
| 1127 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1127 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1128 | 1128 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 1129 | 1129 | $cacheKey = md5($query . $all . $array); |
| 1130 | - if(is_object($this->cacheInstance)){ |
|
| 1130 | + if (is_object($this->cacheInstance)) { |
|
| 1131 | 1131 | $cacheInstance = $this->cacheInstance; |
| 1132 | 1132 | } |
| 1133 | - else{ |
|
| 1133 | + else { |
|
| 1134 | 1134 | $obj = & get_instance(); |
| 1135 | 1135 | $cacheInstance = $obj->cache; |
| 1136 | 1136 | } |
| 1137 | 1137 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1138 | 1138 | } |
| 1139 | - else{ |
|
| 1139 | + else { |
|
| 1140 | 1140 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1141 | 1141 | } |
| 1142 | 1142 | |
| 1143 | - if(! $this->pdo){ |
|
| 1143 | + if (!$this->pdo) { |
|
| 1144 | 1144 | $this->connect(); |
| 1145 | 1145 | } |
| 1146 | 1146 | |
| 1147 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1147 | + if (!$cacheContent && $sqlSELECTQuery) { |
|
| 1148 | 1148 | //for database query execution time |
| 1149 | 1149 | $benchmarkMarkerKey = md5($query . $all . $array); |
| 1150 | 1150 | $bench = null; |
| 1151 | - if(is_object($this->benchmarkInstance)){ |
|
| 1151 | + if (is_object($this->benchmarkInstance)) { |
|
| 1152 | 1152 | $bench = $this->benchmarkInstance; |
| 1153 | 1153 | } |
| 1154 | - else{ |
|
| 1154 | + else { |
|
| 1155 | 1155 | $obj = & get_instance(); |
| 1156 | 1156 | $bench = $obj->benchmark; |
| 1157 | 1157 | } |
@@ -1162,64 +1162,64 @@ discard block |
||
| 1162 | 1162 | //get response time for this query |
| 1163 | 1163 | $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
| 1164 | 1164 | //TODO use the configuration value for the high response time currently is 1 second |
| 1165 | - if($responseTime >= 1 ){ |
|
| 1166 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1165 | + if ($responseTime >= 1) { |
|
| 1166 | + $this->logger->warning('High response time while processing database query [' . $query . ']. The response time is [' . $responseTime . '] sec.'); |
|
| 1167 | 1167 | } |
| 1168 | - if ($sqlQuery){ |
|
| 1168 | + if ($sqlQuery) { |
|
| 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 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1177 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1178 | 1178 | $this->numRows = count($this->result); |
| 1179 | 1179 | } |
| 1180 | - else{ |
|
| 1180 | + else { |
|
| 1181 | 1181 | $this->numRows = $sqlQuery->rowCount(); |
| 1182 | 1182 | } |
| 1183 | 1183 | |
| 1184 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1185 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1184 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1185 | + $this->logger->info('Save the result for query [' . $this->query . '] into cache for future use'); |
|
| 1186 | 1186 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1187 | 1187 | } |
| 1188 | 1188 | } |
| 1189 | - else{ |
|
| 1189 | + else { |
|
| 1190 | 1190 | $error = $this->pdo->errorInfo(); |
| 1191 | 1191 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1192 | 1192 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1193 | 1193 | $this->error(); |
| 1194 | 1194 | } |
| 1195 | 1195 | } |
| 1196 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1196 | + else if ((!$cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)) { |
|
| 1197 | 1197 | $queryStr = $this->pdo->query($this->query); |
| 1198 | - if($queryStr){ |
|
| 1198 | + if ($queryStr) { |
|
| 1199 | 1199 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1200 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1200 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1201 | 1201 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1202 | 1202 | $this->numRows = 1; |
| 1203 | 1203 | } |
| 1204 | - else{ |
|
| 1204 | + else { |
|
| 1205 | 1205 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1206 | 1206 | $this->numRows = $queryStr->rowCount(); |
| 1207 | 1207 | } |
| 1208 | 1208 | } |
| 1209 | - if (! $this->result){ |
|
| 1209 | + if (!$this->result) { |
|
| 1210 | 1210 | $error = $this->pdo->errorInfo(); |
| 1211 | 1211 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1212 | 1212 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1213 | 1213 | $this->error(); |
| 1214 | 1214 | } |
| 1215 | 1215 | } |
| 1216 | - else{ |
|
| 1217 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1216 | + else { |
|
| 1217 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 1218 | 1218 | $this->result = $cacheContent; |
| 1219 | 1219 | $this->numRows = count($this->result); |
| 1220 | 1220 | } |
| 1221 | 1221 | $this->queryCount++; |
| 1222 | - if(! $this->result){ |
|
| 1222 | + if (!$this->result) { |
|
| 1223 | 1223 | $this->logger->info('No result where found for the query [' . $query . ']'); |
| 1224 | 1224 | } |
| 1225 | 1225 | return $this->result; |
@@ -1230,8 +1230,8 @@ discard block |
||
| 1230 | 1230 | * @param integer $ttl the cache time to live in second |
| 1231 | 1231 | * @return object the current Database instance |
| 1232 | 1232 | */ |
| 1233 | - public function setCache($ttl = 0){ |
|
| 1234 | - if($ttl > 0){ |
|
| 1233 | + public function setCache($ttl = 0) { |
|
| 1234 | + if ($ttl > 0) { |
|
| 1235 | 1235 | $this->cacheTtl = $ttl; |
| 1236 | 1236 | $this->temporaryCacheTtl = $ttl; |
| 1237 | 1237 | } |
@@ -1243,8 +1243,8 @@ discard block |
||
| 1243 | 1243 | * @param integer $ttl the cache time to live in second |
| 1244 | 1244 | * @return object the current Database instance |
| 1245 | 1245 | */ |
| 1246 | - public function cached($ttl = 0){ |
|
| 1247 | - if($ttl > 0){ |
|
| 1246 | + public function cached($ttl = 0) { |
|
| 1247 | + if ($ttl > 0) { |
|
| 1248 | 1248 | $this->temporaryCacheTtl = $ttl; |
| 1249 | 1249 | } |
| 1250 | 1250 | return $this; |
@@ -1255,11 +1255,11 @@ discard block |
||
| 1255 | 1255 | * @param mixed $data the data to be escaped |
| 1256 | 1256 | * @return mixed the data after escaped |
| 1257 | 1257 | */ |
| 1258 | - public function escape($data){ |
|
| 1259 | - if(is_null($data)){ |
|
| 1258 | + public function escape($data) { |
|
| 1259 | + if (is_null($data)) { |
|
| 1260 | 1260 | return null; |
| 1261 | 1261 | } |
| 1262 | - if(! $this->pdo){ |
|
| 1262 | + if (!$this->pdo) { |
|
| 1263 | 1263 | $this->connect(); |
| 1264 | 1264 | } |
| 1265 | 1265 | return $this->pdo->quote(trim($data)); |
@@ -1269,7 +1269,7 @@ discard block |
||
| 1269 | 1269 | * Return the number query executed count for the current request |
| 1270 | 1270 | * @return int |
| 1271 | 1271 | */ |
| 1272 | - public function queryCount(){ |
|
| 1272 | + public function queryCount() { |
|
| 1273 | 1273 | return $this->queryCount; |
| 1274 | 1274 | } |
| 1275 | 1275 | |
@@ -1277,7 +1277,7 @@ discard block |
||
| 1277 | 1277 | * Return the current query SQL string |
| 1278 | 1278 | * @return string |
| 1279 | 1279 | */ |
| 1280 | - public function getQuery(){ |
|
| 1280 | + public function getQuery() { |
|
| 1281 | 1281 | return $this->query; |
| 1282 | 1282 | } |
| 1283 | 1283 | |
@@ -1285,7 +1285,7 @@ discard block |
||
| 1285 | 1285 | * Return the application database name |
| 1286 | 1286 | * @return string |
| 1287 | 1287 | */ |
| 1288 | - public function getDatabaseName(){ |
|
| 1288 | + public function getDatabaseName() { |
|
| 1289 | 1289 | return $this->databaseName; |
| 1290 | 1290 | } |
| 1291 | 1291 | |
@@ -1293,7 +1293,7 @@ discard block |
||
| 1293 | 1293 | * Return the database configuration |
| 1294 | 1294 | * @return array |
| 1295 | 1295 | */ |
| 1296 | - public function getDatabaseConfiguration(){ |
|
| 1296 | + public function getDatabaseConfiguration() { |
|
| 1297 | 1297 | return $this->config; |
| 1298 | 1298 | } |
| 1299 | 1299 | |
@@ -1301,7 +1301,7 @@ discard block |
||
| 1301 | 1301 | * set the database configuration |
| 1302 | 1302 | * @param array $config the configuration |
| 1303 | 1303 | */ |
| 1304 | - public function setDatabaseConfiguration(array $config){ |
|
| 1304 | + public function setDatabaseConfiguration(array $config) { |
|
| 1305 | 1305 | $this->config = array_merge($this->config, $config); |
| 1306 | 1306 | $this->prefix = $this->config['prefix']; |
| 1307 | 1307 | $this->databaseName = $this->config['database']; |
@@ -1313,7 +1313,7 @@ discard block |
||
| 1313 | 1313 | * Return the PDO instance |
| 1314 | 1314 | * @return PDO |
| 1315 | 1315 | */ |
| 1316 | - public function getPdo(){ |
|
| 1316 | + public function getPdo() { |
|
| 1317 | 1317 | return $this->pdo; |
| 1318 | 1318 | } |
| 1319 | 1319 | |
@@ -1321,7 +1321,7 @@ discard block |
||
| 1321 | 1321 | * Set the PDO instance |
| 1322 | 1322 | * @param PDO $pdo the pdo object |
| 1323 | 1323 | */ |
| 1324 | - public function setPdo(PDO $pdo){ |
|
| 1324 | + public function setPdo(PDO $pdo) { |
|
| 1325 | 1325 | $this->pdo = $pdo; |
| 1326 | 1326 | return $this; |
| 1327 | 1327 | } |
@@ -1331,7 +1331,7 @@ discard block |
||
| 1331 | 1331 | * Return the Log instance |
| 1332 | 1332 | * @return Log |
| 1333 | 1333 | */ |
| 1334 | - public function getLogger(){ |
|
| 1334 | + public function getLogger() { |
|
| 1335 | 1335 | return $this->logger; |
| 1336 | 1336 | } |
| 1337 | 1337 | |
@@ -1339,7 +1339,7 @@ discard block |
||
| 1339 | 1339 | * Set the log instance |
| 1340 | 1340 | * @param Log $logger the log object |
| 1341 | 1341 | */ |
| 1342 | - public function setLogger($logger){ |
|
| 1342 | + public function setLogger($logger) { |
|
| 1343 | 1343 | $this->logger = $logger; |
| 1344 | 1344 | return $this; |
| 1345 | 1345 | } |
@@ -1348,7 +1348,7 @@ discard block |
||
| 1348 | 1348 | * Return the cache instance |
| 1349 | 1349 | * @return CacheInterface |
| 1350 | 1350 | */ |
| 1351 | - public function getCacheInstance(){ |
|
| 1351 | + public function getCacheInstance() { |
|
| 1352 | 1352 | return $this->cacheInstance; |
| 1353 | 1353 | } |
| 1354 | 1354 | |
@@ -1356,7 +1356,7 @@ discard block |
||
| 1356 | 1356 | * Set the cache instance |
| 1357 | 1357 | * @param CacheInterface $cache the cache object |
| 1358 | 1358 | */ |
| 1359 | - public function setCacheInstance($cache){ |
|
| 1359 | + public function setCacheInstance($cache) { |
|
| 1360 | 1360 | $this->cacheInstance = $cache; |
| 1361 | 1361 | return $this; |
| 1362 | 1362 | } |
@@ -1365,7 +1365,7 @@ discard block |
||
| 1365 | 1365 | * Return the benchmark instance |
| 1366 | 1366 | * @return Benchmark |
| 1367 | 1367 | */ |
| 1368 | - public function getBenchmark(){ |
|
| 1368 | + public function getBenchmark() { |
|
| 1369 | 1369 | return $this->benchmarkInstance; |
| 1370 | 1370 | } |
| 1371 | 1371 | |
@@ -1373,7 +1373,7 @@ discard block |
||
| 1373 | 1373 | * Set the benchmark instance |
| 1374 | 1374 | * @param Benchmark $cache the cache object |
| 1375 | 1375 | */ |
| 1376 | - public function setBenchmark($benchmark){ |
|
| 1376 | + public function setBenchmark($benchmark) { |
|
| 1377 | 1377 | $this->benchmarkInstance = $benchmark; |
| 1378 | 1378 | return $this; |
| 1379 | 1379 | } |
@@ -1382,7 +1382,7 @@ discard block |
||
| 1382 | 1382 | * Return the data to be used for insert, update, etc. |
| 1383 | 1383 | * @return array |
| 1384 | 1384 | */ |
| 1385 | - public function getData(){ |
|
| 1385 | + public function getData() { |
|
| 1386 | 1386 | return $this->data; |
| 1387 | 1387 | } |
| 1388 | 1388 | |
@@ -1393,7 +1393,7 @@ discard block |
||
| 1393 | 1393 | * @param boolean $escape whether to escape or not the $value |
| 1394 | 1394 | * @return object the current Database instance |
| 1395 | 1395 | */ |
| 1396 | - public function setData($key, $value, $escape = true){ |
|
| 1396 | + public function setData($key, $value, $escape = true) { |
|
| 1397 | 1397 | $this->data[$key] = $escape ? $this->escape($value) : $value; |
| 1398 | 1398 | return $this; |
| 1399 | 1399 | } |
@@ -1402,7 +1402,7 @@ discard block |
||
| 1402 | 1402 | /** |
| 1403 | 1403 | * Reset the database class attributs to the initail values before each query. |
| 1404 | 1404 | */ |
| 1405 | - private function reset(){ |
|
| 1405 | + private function reset() { |
|
| 1406 | 1406 | $this->select = '*'; |
| 1407 | 1407 | $this->from = null; |
| 1408 | 1408 | $this->where = null; |
@@ -1422,7 +1422,7 @@ discard block |
||
| 1422 | 1422 | /** |
| 1423 | 1423 | * The class destructor |
| 1424 | 1424 | */ |
| 1425 | - function __destruct(){ |
|
| 1425 | + function __destruct() { |
|
| 1426 | 1426 | $this->pdo = null; |
| 1427 | 1427 | } |
| 1428 | 1428 | |
@@ -188,8 +188,7 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | if(is_object($logger)){ |
| 190 | 190 | $this->logger = $logger; |
| 191 | - } |
|
| 192 | - else{ |
|
| 191 | + } else{ |
|
| 193 | 192 | $this->logger =& class_loader('Log', 'classes'); |
| 194 | 193 | $this->logger->setLogger('Library::Database'); |
| 195 | 194 | } |
@@ -251,14 +250,12 @@ discard block |
||
| 251 | 250 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 252 | 251 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 253 | 252 | return true; |
| 254 | - } |
|
| 255 | - catch (PDOException $e){ |
|
| 253 | + } catch (PDOException $e){ |
|
| 256 | 254 | $this->logger->fatal($e->getMessage()); |
| 257 | 255 | show_error('Cannot connect to Database.'); |
| 258 | 256 | return false; |
| 259 | 257 | } |
| 260 | - } |
|
| 261 | - else{ |
|
| 258 | + } else{ |
|
| 262 | 259 | show_error('Database configuration is not set.'); |
| 263 | 260 | return false; |
| 264 | 261 | } |
@@ -276,8 +273,7 @@ discard block |
||
| 276 | 273 | $froms .= $this->prefix . $key . ', '; |
| 277 | 274 | } |
| 278 | 275 | $this->from = rtrim($froms, ', '); |
| 279 | - } |
|
| 280 | - else{ |
|
| 276 | + } else{ |
|
| 281 | 277 | $this->from = $this->prefix . $table; |
| 282 | 278 | } |
| 283 | 279 | return $this; |
@@ -383,8 +379,7 @@ discard block |
||
| 383 | 379 | } |
| 384 | 380 | if (is_null($this->join)){ |
| 385 | 381 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 386 | - } |
|
| 387 | - else{ |
|
| 382 | + } else{ |
|
| 388 | 383 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 389 | 384 | } |
| 390 | 385 | return $this; |
@@ -455,12 +450,10 @@ discard block |
||
| 455 | 450 | foreach($field as $f){ |
| 456 | 451 | $this->whereIsNull($f, $andOr); |
| 457 | 452 | } |
| 458 | - } |
|
| 459 | - else{ |
|
| 453 | + } else{ |
|
| 460 | 454 | if (! $this->where){ |
| 461 | 455 | $this->where = $field.' IS NULL '; |
| 462 | - } |
|
| 463 | - else{ |
|
| 456 | + } else{ |
|
| 464 | 457 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
| 465 | 458 | } |
| 466 | 459 | } |
@@ -478,12 +471,10 @@ discard block |
||
| 478 | 471 | foreach($field as $f){ |
| 479 | 472 | $this->whereIsNotNull($f, $andOr); |
| 480 | 473 | } |
| 481 | - } |
|
| 482 | - else{ |
|
| 474 | + } else{ |
|
| 483 | 475 | if (! $this->where){ |
| 484 | 476 | $this->where = $field.' IS NOT NULL '; |
| 485 | - } |
|
| 486 | - else{ |
|
| 477 | + } else{ |
|
| 487 | 478 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
| 488 | 479 | } |
| 489 | 480 | } |
@@ -510,8 +501,7 @@ discard block |
||
| 510 | 501 | $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data); |
| 511 | 502 | } |
| 512 | 503 | $where = implode(' '.$andOr.' ', $_where); |
| 513 | - } |
|
| 514 | - else{ |
|
| 504 | + } else{ |
|
| 515 | 505 | if(is_array($op)){ |
| 516 | 506 | $x = explode('?', $where); |
| 517 | 507 | $w = ''; |
@@ -524,14 +514,12 @@ discard block |
||
| 524 | 514 | } |
| 525 | 515 | } |
| 526 | 516 | $where = $w; |
| 527 | - } |
|
| 528 | - else if (! in_array((string)$op, $this->operatorList)){ |
|
| 517 | + } else if (! in_array((string)$op, $this->operatorList)){ |
|
| 529 | 518 | if(is_null($op)){ |
| 530 | 519 | $op = ''; |
| 531 | 520 | } |
| 532 | 521 | $where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 533 | - } |
|
| 534 | - else{ |
|
| 522 | + } else{ |
|
| 535 | 523 | if(is_null($val)){ |
| 536 | 524 | $val = ''; |
| 537 | 525 | } |
@@ -540,12 +528,10 @@ discard block |
||
| 540 | 528 | } |
| 541 | 529 | if (is_null($this->where)){ |
| 542 | 530 | $this->where = $where; |
| 543 | - } |
|
| 544 | - else{ |
|
| 531 | + } else{ |
|
| 545 | 532 | if(substr($this->where, -1) == '('){ |
| 546 | 533 | $this->where = $this->where . ' ' . $where; |
| 547 | - } |
|
| 548 | - else{ |
|
| 534 | + } else{ |
|
| 549 | 535 | $this->where = $this->where . ' '.$andOr.' ' . $where; |
| 550 | 536 | } |
| 551 | 537 | } |
@@ -589,12 +575,10 @@ discard block |
||
| 589 | 575 | public function groupStart($type = '', $andOr = ' AND'){ |
| 590 | 576 | if (is_null($this->where)){ |
| 591 | 577 | $this->where = $type . ' ('; |
| 592 | - } |
|
| 593 | - else{ |
|
| 578 | + } else{ |
|
| 594 | 579 | if(substr($this->where, -1) == '('){ |
| 595 | 580 | $this->where .= $type . ' ('; |
| 596 | - } |
|
| 597 | - else{ |
|
| 581 | + } else{ |
|
| 598 | 582 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 599 | 583 | } |
| 600 | 584 | } |
@@ -658,12 +642,10 @@ discard block |
||
| 658 | 642 | $keys = implode(', ', $_keys); |
| 659 | 643 | if (is_null($this->where)){ |
| 660 | 644 | $this->where = $field . ' ' . $type . 'IN (' . $keys . ')'; |
| 661 | - } |
|
| 662 | - else{ |
|
| 645 | + } else{ |
|
| 663 | 646 | if(substr($this->where, -1) == '('){ |
| 664 | 647 | $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
| 665 | - } |
|
| 666 | - else{ |
|
| 648 | + } else{ |
|
| 667 | 649 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')'; |
| 668 | 650 | } |
| 669 | 651 | } |
@@ -717,12 +699,10 @@ discard block |
||
| 717 | 699 | } |
| 718 | 700 | if (is_null($this->where)){ |
| 719 | 701 | $this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 720 | - } |
|
| 721 | - else{ |
|
| 702 | + } else{ |
|
| 722 | 703 | if(substr($this->where, -1) == '('){ |
| 723 | 704 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 724 | - } |
|
| 725 | - else{ |
|
| 705 | + } else{ |
|
| 726 | 706 | $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
| 727 | 707 | } |
| 728 | 708 | } |
@@ -772,12 +752,10 @@ discard block |
||
| 772 | 752 | $like = $escape ? $this->escape($data) : $data; |
| 773 | 753 | if (is_null($this->where)){ |
| 774 | 754 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 775 | - } |
|
| 776 | - else{ |
|
| 755 | + } else{ |
|
| 777 | 756 | if(substr($this->where, -1) == '('){ |
| 778 | 757 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 779 | - } |
|
| 780 | - else{ |
|
| 758 | + } else{ |
|
| 781 | 759 | $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 782 | 760 | } |
| 783 | 761 | } |
@@ -824,8 +802,7 @@ discard block |
||
| 824 | 802 | } |
| 825 | 803 | if (! is_null($limitEnd)){ |
| 826 | 804 | $this->limit = $limit . ', ' . $limitEnd; |
| 827 | - } |
|
| 828 | - else{ |
|
| 805 | + } else{ |
|
| 829 | 806 | $this->limit = $limit; |
| 830 | 807 | } |
| 831 | 808 | return $this; |
@@ -840,12 +817,10 @@ discard block |
||
| 840 | 817 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 841 | 818 | if (! is_null($orderDir)){ |
| 842 | 819 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
| 843 | - } |
|
| 844 | - else{ |
|
| 820 | + } else{ |
|
| 845 | 821 | if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
| 846 | 822 | $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 847 | - } |
|
| 848 | - else{ |
|
| 823 | + } else{ |
|
| 849 | 824 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
| 850 | 825 | } |
| 851 | 826 | } |
@@ -860,8 +835,7 @@ discard block |
||
| 860 | 835 | public function groupBy($field){ |
| 861 | 836 | if(is_array($field)){ |
| 862 | 837 | $this->groupBy = implode(', ', $field); |
| 863 | - } |
|
| 864 | - else{ |
|
| 838 | + } else{ |
|
| 865 | 839 | $this->groupBy = $field; |
| 866 | 840 | } |
| 867 | 841 | return $this; |
@@ -888,14 +862,12 @@ discard block |
||
| 888 | 862 | } |
| 889 | 863 | } |
| 890 | 864 | $this->having = $w; |
| 891 | - } |
|
| 892 | - else if (! in_array($op, $this->operatorList)){ |
|
| 865 | + } else if (! in_array($op, $this->operatorList)){ |
|
| 893 | 866 | if(is_null($op)){ |
| 894 | 867 | $op = ''; |
| 895 | 868 | } |
| 896 | 869 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 897 | - } |
|
| 898 | - else{ |
|
| 870 | + } else{ |
|
| 899 | 871 | if(is_null($val)){ |
| 900 | 872 | $val = ''; |
| 901 | 873 | } |
@@ -940,8 +912,7 @@ discard block |
||
| 940 | 912 | $query = $this->getAll(true); |
| 941 | 913 | if($returnSQLQueryOrResultType === true){ |
| 942 | 914 | return $query; |
| 943 | - } |
|
| 944 | - else{ |
|
| 915 | + } else{ |
|
| 945 | 916 | return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 946 | 917 | } |
| 947 | 918 | } |
@@ -980,8 +951,7 @@ discard block |
||
| 980 | 951 | |
| 981 | 952 | if($returnSQLQueryOrResultType === true){ |
| 982 | 953 | return $query; |
| 983 | - } |
|
| 984 | - else{ |
|
| 954 | + } else{ |
|
| 985 | 955 | return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 986 | 956 | } |
| 987 | 957 | } |
@@ -999,8 +969,7 @@ discard block |
||
| 999 | 969 | $columns = array_keys($this->getData()); |
| 1000 | 970 | $column = implode(',', $columns); |
| 1001 | 971 | $val = implode(', ', $this->getData()); |
| 1002 | - } |
|
| 1003 | - else{ |
|
| 972 | + } else{ |
|
| 1004 | 973 | $columns = array_keys($data); |
| 1005 | 974 | $column = implode(',', $columns); |
| 1006 | 975 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -1015,8 +984,7 @@ discard block |
||
| 1015 | 984 | } |
| 1016 | 985 | $this->insertId = $this->pdo->lastInsertId(); |
| 1017 | 986 | return $this->insertId(); |
| 1018 | - } |
|
| 1019 | - else{ |
|
| 987 | + } else{ |
|
| 1020 | 988 | return false; |
| 1021 | 989 | } |
| 1022 | 990 | } |
@@ -1034,8 +1002,7 @@ discard block |
||
| 1034 | 1002 | foreach ($this->getData() as $column => $val){ |
| 1035 | 1003 | $values[] = $column . ' = ' . $val; |
| 1036 | 1004 | } |
| 1037 | - } |
|
| 1038 | - else{ |
|
| 1005 | + } else{ |
|
| 1039 | 1006 | foreach ($data as $column => $val){ |
| 1040 | 1007 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1041 | 1008 | } |
@@ -1129,14 +1096,12 @@ discard block |
||
| 1129 | 1096 | $cacheKey = md5($query . $all . $array); |
| 1130 | 1097 | if(is_object($this->cacheInstance)){ |
| 1131 | 1098 | $cacheInstance = $this->cacheInstance; |
| 1132 | - } |
|
| 1133 | - else{ |
|
| 1099 | + } else{ |
|
| 1134 | 1100 | $obj = & get_instance(); |
| 1135 | 1101 | $cacheInstance = $obj->cache; |
| 1136 | 1102 | } |
| 1137 | 1103 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1138 | - } |
|
| 1139 | - else{ |
|
| 1104 | + } else{ |
|
| 1140 | 1105 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1141 | 1106 | } |
| 1142 | 1107 | |
@@ -1150,8 +1115,7 @@ discard block |
||
| 1150 | 1115 | $bench = null; |
| 1151 | 1116 | if(is_object($this->benchmarkInstance)){ |
| 1152 | 1117 | $bench = $this->benchmarkInstance; |
| 1153 | - } |
|
| 1154 | - else{ |
|
| 1118 | + } else{ |
|
| 1155 | 1119 | $obj = & get_instance(); |
| 1156 | 1120 | $bench = $obj->benchmark; |
| 1157 | 1121 | } |
@@ -1169,15 +1133,13 @@ discard block |
||
| 1169 | 1133 | //if need return all result like list of record |
| 1170 | 1134 | if ($all){ |
| 1171 | 1135 | $this->result = ($array == false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
| 1172 | - } |
|
| 1173 | - else{ |
|
| 1136 | + } else{ |
|
| 1174 | 1137 | $this->result = ($array == false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
| 1175 | 1138 | } |
| 1176 | 1139 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1177 | 1140 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1178 | 1141 | $this->numRows = count($this->result); |
| 1179 | - } |
|
| 1180 | - else{ |
|
| 1142 | + } else{ |
|
| 1181 | 1143 | $this->numRows = $sqlQuery->rowCount(); |
| 1182 | 1144 | } |
| 1183 | 1145 | |
@@ -1185,23 +1147,20 @@ discard block |
||
| 1185 | 1147 | $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
| 1186 | 1148 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1187 | 1149 | } |
| 1188 | - } |
|
| 1189 | - else{ |
|
| 1150 | + } else{ |
|
| 1190 | 1151 | $error = $this->pdo->errorInfo(); |
| 1191 | 1152 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1192 | 1153 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1193 | 1154 | $this->error(); |
| 1194 | 1155 | } |
| 1195 | - } |
|
| 1196 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1156 | + } else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1197 | 1157 | $queryStr = $this->pdo->query($this->query); |
| 1198 | 1158 | if($queryStr){ |
| 1199 | 1159 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1200 | 1160 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1201 | 1161 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1202 | 1162 | $this->numRows = 1; |
| 1203 | - } |
|
| 1204 | - else{ |
|
| 1163 | + } else{ |
|
| 1205 | 1164 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1206 | 1165 | $this->numRows = $queryStr->rowCount(); |
| 1207 | 1166 | } |
@@ -1212,8 +1171,7 @@ discard block |
||
| 1212 | 1171 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1213 | 1172 | $this->error(); |
| 1214 | 1173 | } |
| 1215 | - } |
|
| 1216 | - else{ |
|
| 1174 | + } else{ |
|
| 1217 | 1175 | $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
| 1218 | 1176 | $this->result = $cacheContent; |
| 1219 | 1177 | $this->numRows = count($this->result); |