3 * A generic database wrapper
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 // Is the cache enabled?
55 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
56 // Set the new instance
57 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
62 * Setter for table name
64 * @param $tableName Name of table name to set
67 protected final function setTableName ($tableName) {
68 $this->tableName = (string) $tableName;
72 * Getter for table name
74 * @return $tableName Name of table name to set
76 protected final function getTableName () {
77 return $this->tableName;
81 * 'Inserts' a data set instance into a local file database folder
83 * @param $dataSetInstance A storeable data set
84 * @param $onlyKeys Only use these keys for a cache key
87 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
88 // Default cache key is NULL
92 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
93 // First get a key suitable for our cache and extend it with this class name
94 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
95 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
98 // Does this key exists in cache?
99 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
101 $this->cacheInstance->purgeOffset($cacheKey);
104 // Handle it over to the middleware
105 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
109 * 'Updates' a data set instance with a database layer
111 * @param $dataSetInstance A storeable data set
112 * @param $onlyKeys Only use these keys for a cache key
115 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
120 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
121 // First get a key suitable for our cache and extend it with this class name
122 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
123 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
126 // Does this key exists in cache?
127 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
129 $this->cacheInstance->purgeOffset($cacheKey);
132 // Handle it over to the middleware
133 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
137 * Getter for index key
139 * @return $indexKey Index key
141 public final function getIndexKey () {
142 return $this->getDatabaseInstance()->getIndexKey();
146 * Getter for last exception
148 * @return $lastException Last exception or NULL if none occured
150 public final function getLastException () {
151 return $this->getDatabaseInstance()->getLastException();
155 * Do a "select" query on the current table with the given search criteria and
156 * store it in cache for later usage
158 * @param $criteriaInstance An instance of a Criteria class
159 * @param $onlyKeys Only use these keys for a cache key
160 * @return $resultInstance An instance of a database result class
162 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
163 // Default cache key if cache is not enabled
166 // Is the cache enabled?
167 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
168 // First get a key suitable for our cache and extend it with this class name
169 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
172 // Does this key exists in cache?
173 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
177 // Then use this result
178 $result = $this->cacheInstance->offsetGet($cacheKey);
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
183 // Now it's time to ask the database layer for this select statement
184 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result));
187 // Cache the result if not null
188 if (!is_null($result)) {
190 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
191 // A valid result has returned from the database layer
192 $this->cacheInstance->offsetSet($cacheKey, $result);
195 // This invalid result must be wrapped
197 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
198 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
203 // Create an instance of a CachedDatabaseResult class with the given result
204 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
205 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
207 // And return the instance
208 return $resultInstance;
212 * Count the numbers of rows we shall receive
214 * @param $criteriaInstance An instance of a Criteria class
215 * @param $onlyKeys Only use these keys for a cache key
216 * @return $numRows Numbers of rows of database entries
218 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
219 // Total numbers is -1 so we can distinglish between failed and valid queries
222 // Get the result from above method
223 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
225 // Was that query fine?
226 if ($resultInstance->ifStatusIsOkay()) {
227 // Then get the number of rows
228 $numRows = $resultInstance->getAffectedRows();
231 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
239 * Getter for primary key used in wrapped table
241 * @return $primaryKey Primary key used in wrapped table
243 public final function getPrimaryKeyValue () {
244 // Get the table name and a database instance and ask for it
245 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
252 * Count rows of this table
254 * @return $count Count of total rows in this table
256 public final function countTotalRows () {
257 // Get the table name and a database instance and ask for it
258 $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName());
265 * Removes non-public data from given array.
267 * @param $data An array with possible non-public data that needs to be removed.
268 * @return $data A cleaned up array with only public data.
270 public function removeNonPublicDataFromArray (array $data) {
271 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
272 $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data);
274 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));