3 namespace CoreFramework\Database\Wrapper;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * A generic database wrapper
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseDatabaseWrapper extends BaseFrameworkSystem {
35 private $cacheInstance = NULL;
38 * Current table name to use
40 private $tableName = 'unknown';
43 * Protected constructor
47 protected function __construct ($class) {
48 // Call parent constructor
49 parent::__construct($class);
51 // Initialize the cache instance
52 $this->initCacheInstance();
56 * Initializes the cache instance with a new object
60 private final function initCacheInstance () {
61 // Is the cache enabled?
62 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
63 // Set the new instance
64 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
69 * Setter for table name
71 * @param $tableName Name of table name to set
74 protected final function setTableName ($tableName) {
75 $this->tableName = (string) $tableName;
79 * Getter for table name
81 * @return $tableName Name of table name to set
83 protected final function getTableName () {
84 return $this->tableName;
88 * 'Inserts' a data set instance into a local file database folder
90 * @param $dataSetInstance A storeable data set
91 * @param $onlyKeys Only use these keys for a cache key
94 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
95 // Default cache key is NULL
99 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
100 // First get a key suitable for our cache and extend it with this class name
101 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
102 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
105 // Does this key exists in cache?
106 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
108 $this->cacheInstance->purgeOffset($cacheKey);
111 // Handle it over to the middleware
112 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
116 * 'Updates' a data set instance with a database layer
118 * @param $dataSetInstance A storeable data set
119 * @param $onlyKeys Only use these keys for a cache key
122 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
127 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
128 // First get a key suitable for our cache and extend it with this class name
129 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
130 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
133 // Does this key exists in cache?
134 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
136 $this->cacheInstance->purgeOffset($cacheKey);
139 // Handle it over to the middleware
140 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
144 * Getter for index key
146 * @return $indexKey Index key
148 public final function getIndexKey () {
149 return $this->getDatabaseInstance()->getIndexKey();
153 * Getter for last exception
155 * @return $lastException Last exception or NULL if none occured
157 public final function getLastException () {
158 return $this->getDatabaseInstance()->getLastException();
162 * Do a "select" query on the current table with the given search criteria and
163 * store it in cache for later usage
165 * @param $criteriaInstance An instance of a Criteria class
166 * @param $onlyKeys Only use these keys for a cache key
167 * @return $resultInstance An instance of a database result class
169 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
170 // Default cache key if cache is not enabled
173 // Is the cache enabled?
174 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
175 // First get a key suitable for our cache and extend it with this class name
176 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
179 // Does this key exists in cache?
180 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
184 // Then use this result
185 $result = $this->cacheInstance->offsetGet($cacheKey);
188 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
190 // Now it's time to ask the database layer for this select statement
191 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result));
194 // Cache the result if not null
195 if (!is_null($result)) {
197 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
198 // A valid result has returned from the database layer
199 $this->cacheInstance->offsetSet($cacheKey, $result);
202 // This invalid result must be wrapped
204 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
205 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
210 // Create an instance of a CachedDatabaseResult class with the given result
211 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
212 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
214 // And return the instance
215 return $resultInstance;
219 * Count the numbers of rows we shall receive
221 * @param $criteriaInstance An instance of a Criteria class
222 * @param $onlyKeys Only use these keys for a cache key
223 * @return $numRows Numbers of rows of database entries
225 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
226 // Total numbers is -1 so we can distinglish between failed and valid queries
229 // Get the result from above method
230 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
232 // Was that query fine?
233 if ($resultInstance->ifStatusIsOkay()) {
234 // Then get the number of rows
235 $numRows = $resultInstance->getAffectedRows();
238 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
246 * Getter for primary key used in wrapped table
248 * @return $primaryKey Primary key used in wrapped table
250 public final function getPrimaryKeyValue () {
251 // Get the table name and a database instance and ask for it
252 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
259 * Count rows of this table
261 * @return $count Count of total rows in this table
263 public final function countTotalRows () {
264 // Get the table name and a database instance and ask for it
265 $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName());
272 * Removes non-public data from given array.
274 * @param $data An array with possible non-public data that needs to be removed.
275 * @return $data A cleaned up array with only public data.
277 public function removeNonPublicDataFromArray (array $data) {
278 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
279 $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data);
281 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));