X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=inline;f=inc%2Fclasses%2Fmain%2Fdatabase%2Fclass_BaseDatabaseWrapper.php;h=31a63a39d3d162a36db6066965bafbde4a5cb4cb;hb=4cfe82256d609b7d6cdffb7baa2286bd026040d5;hp=8d999f13b3ffb6fd6824655562fb2480646abf76;hpb=0cd57c3885f00ad77fc599e53ed2f2d5e7ac267f;p=core.git diff --git a/inc/classes/main/database/class_BaseDatabaseWrapper.php b/inc/classes/main/database/class_BaseDatabaseWrapper.php index 8d999f13..31a63a39 100644 --- a/inc/classes/main/database/class_BaseDatabaseWrapper.php +++ b/inc/classes/main/database/class_BaseDatabaseWrapper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -25,12 +25,12 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { /** * Cache instance */ - private $cacheInstance = null; + private $cacheInstance = NULL; /** * Current table name to use */ - private $tableName = "unknown"; + private $tableName = 'unknown'; /** * Protected constructor @@ -43,10 +43,6 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { // Initialize the cache instance $this->initCacheInstance(); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** @@ -59,25 +55,110 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache(); } + /** + * Setter for table name + * + * @param $tableName Name of table name to set + * @return void + */ + protected final function setTableName ($tableName) { + $this->tableName = (string) $tableName; + } + + /** + * Getter for table name + * + * @return $tableName Name of table name to set + */ + protected final function getTableName () { + return $this->tableName; + } + + /** + * 'Inserts' a data set instance into a local file database folder + * + * @param $dataSetInstance A storeable data set + * @param $onlyKeys Only use these keys for a cache key + * @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: */ $this->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + + // Does this key exists in cache? + if ($this->cacheInstance->offsetExists($cacheKey)) { + // Purge the cache + $this->cacheInstance->purgeOffset($cacheKey); + } // END - if + + // Handle it over to the middleware + $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance); + } + + /** + * 'Updates' a data set instance with a database layer + * + * @param $dataSetInstance A storeable data set + * @param $onlyKeys Only use these keys for a cache key + * @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: */ $this->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...'); + + // Does this key exists in cache? + if ($this->cacheInstance->offsetExists($cacheKey)) { + // Purge the cache + $this->cacheInstance->purgeOffset($cacheKey); + } // END - if + + // Handle it over to the middleware + $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); + } + + /** + * Getter for index key + * + * @return $indexKey Index key + */ + public final function getIndexKey () { + return $this->getDatabaseInstance()->getIndexKey(); + } + + /** + * Getter for last exception + * + * @return $lastException Last exception or NULL if none occured + */ + public final function getLastException () { + return $this->getDatabaseInstance()->getLastException(); + } + /** * Do a "select" query on the current table with the given search criteria and * store it in cache for later usage * * @param $criteriaInstance An instance of a Criteria class + * @param $onlyKeys Only use these keys for a cache key * @return $resultInstance An instance of a database result class */ - public function doSelectByCriteria (Criteria $criteriaInstance) { + public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) { // First get a key suitable for our cache and extend it with this class name - $cacheKey = sprintf("%s@%s", - $this->__toString(), - $criteriaInstance->getCacheKey() - ); + $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys); // Does this key exists in cache? if ($this->cacheInstance->offsetExists($cacheKey)) { + // Debug message + //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey); + // Then use this result - $result = $cacheInstance->offsetGet($cacheKey); + $result = $this->cacheInstance->offsetGet($cacheKey); } else { + // Debug message + //* DEBUG: */ $this->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); @@ -88,8 +169,8 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { } else { // This invalid result must be wrapped $result = array( - 'status' => "invalid", - 'exception' => $this->getDatabaseInstance()->getLastException() + BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid', + BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException() ); } } @@ -105,44 +186,29 @@ class BaseDatabaseWrapper extends BaseFrameworkSystem { * Count the numbers of rows we shall receive * * @param $criteriaInstance An instance of a Criteria class + * @param $onlyKeys Only use these keys for a cache key * @return $numRows Numbers of rows of database entries */ - public function doSelectCountByCriteria (Criteria $criteriaInstance) { - // Total numbers is zero by default + public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) { + // Total numbers is -1 so we can distinglish between failed and valid queries $numRows = 0; // Get the result from above method - $resultInstance = $this->doSelectByCriteria($criteriaInstance); + $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys); // Was that query fine? if ($resultInstance->ifStatusIsOkay()) { // Then get the number of rows $numRows = $resultInstance->getAffectedRows(); + + // Debug message + //* DEBUG: */ $this->debugOutput('BASE-WRAPPER: numRows=' . $numRows); } // END - if // Return the result return $numRows; } - /** - * Setter for table name - * - * @param $tableName Name of table name to set - * @return void - */ - protected final function setTableName ($tableName) { - $this->tableName = (string) $tableName; - } - - /** - * Getter for table name - * - * @return $tableName Name of table name to set - */ - protected final function getTableName () { - return $this->tableName; - } - /** * Getter for primary key used in wrapped table *