* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class BaseDatabaseWrapper extends BaseFrameworkSystem { /** * Cache instance */ private $cacheInstance = NULL; /** * Current table name to use */ private $tableName = 'unknown'; /** * Protected constructor * * @return void */ protected function __construct ($class) { // Call parent constructor parent::__construct($class); // Initialize the cache instance $this->initCacheInstance(); } /** * Initializes the cache instance with a new object * * @return void */ private final function initCacheInstance () { // Set the new instance $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(); } /** * 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, array $onlyKeys = array()) { // First get a key suitable for our cache and extend it with this class name $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 = $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); // Cache the result if not null if (!is_null($result)) { // A valid result has returned from the database layer $this->cacheInstance->offsetSet($cacheKey, $result); } else { // This invalid result must be wrapped $result = array( BaseDatabaseFrontend::RESULT_INDEX_STATUS => 'invalid', BaseDatabaseFrontend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException() ); } } // Create an instance of a DatabaseResult class with the given result $resultInstance = DatabaseResult::createDatabaseResult($result); // And return the instance return $resultInstance; } /** * 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, $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, $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; } /** * Getter for primary key used in wrapped table * * @return $primaryKey Primary key used in wrapped table */ public final function getPrimaryKeyValue () { // Get the table name and a database instance and ask for it $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName()); // Return value return $primaryKey; } } // [EOF] ?>