3 * A generic database wrapper
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseDatabaseWrapper extends BaseFrameworkSystem {
28 private $cacheInstance = NULL;
31 * Current table name to use
33 private $tableName = 'unknown';
36 * Protected constructor
40 protected function __construct ($class) {
41 // Call parent constructor
42 parent::__construct($class);
44 // Initialize the cache instance
45 $this->initCacheInstance();
49 * Initializes the cache instance with a new object
53 private final function initCacheInstance () {
54 // Set the new instance
55 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
59 * Setter for table name
61 * @param $tableName Name of table name to set
64 protected final function setTableName ($tableName) {
65 $this->tableName = (string) $tableName;
69 * Getter for table name
71 * @return $tableName Name of table name to set
73 protected final function getTableName () {
74 return $this->tableName;
78 * 'Inserts' a data set instance into a local file database folder
80 * @param $dataSetInstance A storeable data set
81 * @param $onlyKeys Only use these keys for a cache key
84 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
85 // Default cache key is NULL
89 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
90 // First get a key suitable for our cache and extend it with this class name
91 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
92 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
95 // Does this key exists in cache?
96 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
98 $this->cacheInstance->purgeOffset($cacheKey);
101 // Handle it over to the middleware
102 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
106 * 'Updates' a data set instance with a database layer
108 * @param $dataSetInstance A storeable data set
109 * @param $onlyKeys Only use these keys for a cache key
112 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
117 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
118 // First get a key suitable for our cache and extend it with this class name
119 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
120 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
123 // Does this key exists in cache?
124 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
126 $this->cacheInstance->purgeOffset($cacheKey);
129 // Handle it over to the middleware
130 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
134 * Getter for index key
136 * @return $indexKey Index key
138 public final function getIndexKey () {
139 return $this->getDatabaseInstance()->getIndexKey();
143 * Getter for last exception
145 * @return $lastException Last exception or NULL if none occured
147 public final function getLastException () {
148 return $this->getDatabaseInstance()->getLastException();
152 * Do a "select" query on the current table with the given search criteria and
153 * store it in cache for later usage
155 * @param $criteriaInstance An instance of a Criteria class
156 * @param $onlyKeys Only use these keys for a cache key
157 * @return $resultInstance An instance of a database result class
159 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
160 // Default cache key if cache is not enabled
163 // Is the cache enabled?
164 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
165 // First get a key suitable for our cache and extend it with this class name
166 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
169 // Does this key exists in cache?
170 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
174 // Then use this result
175 $result = $this->cacheInstance->offsetGet($cacheKey);
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
180 // Now it's time to ask the database layer for this select statement
181 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result));
184 // Cache the result if not null
185 if (!is_null($result)) {
186 // A valid result has returned from the database layer
187 $this->cacheInstance->offsetSet($cacheKey, $result);
189 // This invalid result must be wrapped
191 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
192 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
197 // Create an instance of a CachedDatabaseResult class with the given result
198 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
199 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
201 // And return the instance
202 return $resultInstance;
206 * Count the numbers of rows we shall receive
208 * @param $criteriaInstance An instance of a Criteria class
209 * @param $onlyKeys Only use these keys for a cache key
210 * @return $numRows Numbers of rows of database entries
212 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
213 // Total numbers is -1 so we can distinglish between failed and valid queries
216 // Get the result from above method
217 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
219 // Was that query fine?
220 if ($resultInstance->ifStatusIsOkay()) {
221 // Then get the number of rows
222 $numRows = $resultInstance->getAffectedRows();
225 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
233 * Getter for primary key used in wrapped table
235 * @return $primaryKey Primary key used in wrapped table
237 public final function getPrimaryKeyValue () {
238 // Get the table name and a database instance and ask for it
239 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
246 * Removes non-public data from given array.
248 * @param $data An array with possible non-public data that needs to be removed.
249 * @return $data A cleaned up array with only public data.
251 public function removeNonPublicDataFromArray (array $data) {
252 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
253 $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data);
255 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));