X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fdatabase%2Fclass_BaseDatabaseWrapper.php;h=c2a18b864e59f43df7d31fc98563e8469c75f1fc;hb=5203f9bd014ad46fbc7ee54e7223dcd46e14e3b4;hp=63d121c90af517c30ac50756b04071d1fee63c78;hpb=0aa26cbbe1dcfcd42beee694b29fec8c994ce253;p=core.git diff --git a/inc/classes/main/database/class_BaseDatabaseWrapper.php b/inc/classes/main/database/class_BaseDatabaseWrapper.php index 63d121c9..c2a18b86 100644 --- a/inc/classes/main/database/class_BaseDatabaseWrapper.php +++ b/inc/classes/main/database/class_BaseDatabaseWrapper.php @@ -2,11 +2,11 @@ /** * A generic database wrapper * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -51,8 +51,11 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return void */ private final function initCacheInstance () { - // Set the new instance - $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); + // Is the cache enabled? + if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) { + // Set the new instance + $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); + } // END - if } /** @@ -82,12 +85,18 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return void */ protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) { - // First get a key suitable for our cache and extend it with this class name - $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys); - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + // Default cache key is NULL + $cacheKey = NULL; + + // Is cache enabled? + if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) { + // First get a key suitable for our cache and extend it with this class name + $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + } // END - if // Does this key exists in cache? - if ($this->cacheInstance->offsetExists($cacheKey)) { + if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) { // Purge the cache $this->cacheInstance->purgeOffset($cacheKey); } // END - if @@ -104,12 +113,18 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return void */ protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) { - // First get a key suitable for our cache and extend it with this class name - $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys); - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + // Init cache key + $cacheKey = NULL; + + // Is cache enabled? + if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) { + // First get a key suitable for our cache and extend it with this class name + $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + } // END - if // Does this key exists in cache? - if ($this->cacheInstance->offsetExists($cacheKey)) { + if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) { // Purge the cache $this->cacheInstance->purgeOffset($cacheKey); } // END - if @@ -145,27 +160,37 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return $resultInstance An instance of a database result class */ public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) { - // First get a key suitable for our cache and extend it with this class name - $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys); + // Default cache key if cache is not enabled + $cacheKey = NULL; + + // Is the cache enabled? + if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) { + // First get a key suitable for our cache and extend it with this class name + $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys); + } // END - if // Does this key exists in cache? - if ($this->cacheInstance->offsetExists($cacheKey)) { + if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) { // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE)); // Then use this result $result = $this->cacheInstance->offsetGet($cacheKey); } else { // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey); // Now it's time to ask the database layer for this select statement $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result)); // Cache the result if not null if (!is_null($result)) { - // A valid result has returned from the database layer - $this->cacheInstance->offsetSet($cacheKey, $result); + // Is cache enabled? + if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) { + // A valid result has returned from the database layer + $this->cacheInstance->offsetSet($cacheKey, $result); + } // END - if } else { // This invalid result must be wrapped $result = array( @@ -175,7 +200,8 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { } } - // Create an instance of a DatabaseResult class with the given result + // Create an instance of a CachedDatabaseResult class with the given result + // @TODO Minor: Update above comment to e.g. BaseDatabaseResult $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result)); // And return the instance @@ -189,7 +215,7 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * @param $onlyKeys Only use these keys for a cache key * @return $numRows Numbers of rows of database entries */ - public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) { + public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) { // Total numbers is -1 so we can distinglish between failed and valid queries $numRows = 0; @@ -221,6 +247,33 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { // Return value return $primaryKey; } + + /** + * Count rows of this table + * + * @return $count Count of total rows in this table + */ + public final function countTotalRows () { + // Get the table name and a database instance and ask for it + $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName()); + + // Return value + return $count; + } + + /** + * Removes non-public data from given array. + * + * @param $data An array with possible non-public data that needs to be removed. + * @return $data A cleaned up array with only public data. + */ + public function removeNonPublicDataFromArray (array $data) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...'); + $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data)); + return $data; + } } // [EOF]