3 namespace CoreFramework\Database\Wrapper;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A generic database wrapper
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseDatabaseWrapper extends BaseFrameworkSystem {
34 private $cacheInstance = NULL;
37 * Current table name to use
39 private $tableName = 'unknown';
42 * Protected constructor
46 protected function __construct ($class) {
47 // Call parent constructor
48 parent::__construct($class);
50 // Initialize the cache instance
51 $this->initCacheInstance();
55 * Initializes the cache instance with a new object
59 private final function initCacheInstance () {
60 // Is the cache enabled?
61 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
62 // Set the new instance
63 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
68 * Setter for table name
70 * @param $tableName Name of table name to set
73 protected final function setTableName ($tableName) {
74 $this->tableName = (string) $tableName;
78 * Getter for table name
80 * @return $tableName Name of table name to set
82 protected final function getTableName () {
83 return $this->tableName;
87 * 'Inserts' a data set instance into a local file database folder
89 * @param $dataSetInstance A storeable data set
90 * @param $onlyKeys Only use these keys for a cache key
93 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
94 // Default cache key is NULL
98 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
99 // First get a key suitable for our cache and extend it with this class name
100 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
101 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
104 // Does this key exists in cache?
105 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
107 $this->cacheInstance->purgeOffset($cacheKey);
110 // Handle it over to the middleware
111 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
115 * 'Updates' a data set instance with a database layer
117 * @param $dataSetInstance A storeable data set
118 * @param $onlyKeys Only use these keys for a cache key
121 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
126 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
127 // First get a key suitable for our cache and extend it with this class name
128 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
129 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
132 // Does this key exists in cache?
133 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
135 $this->cacheInstance->purgeOffset($cacheKey);
138 // Handle it over to the middleware
139 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
143 * Getter for index key
145 * @return $indexKey Index key
147 public final function getIndexKey () {
148 return $this->getDatabaseInstance()->getIndexKey();
152 * Getter for last exception
154 * @return $lastException Last exception or NULL if none occured
156 public final function getLastException () {
157 return $this->getDatabaseInstance()->getLastException();
161 * Do a "select" query on the current table with the given search criteria and
162 * store it in cache for later usage
164 * @param $criteriaInstance An instance of a Criteria class
165 * @param $onlyKeys Only use these keys for a cache key
166 * @return $resultInstance An instance of a database result class
168 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
169 // Default cache key if cache is not enabled
172 // Is the cache enabled?
173 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
174 // First get a key suitable for our cache and extend it with this class name
175 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
178 // Does this key exists in cache?
179 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
183 // Then use this result
184 $result = $this->cacheInstance->offsetGet($cacheKey);
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
189 // Now it's time to ask the database layer for this select statement
190 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result));
193 // Cache the result if not null
194 if (!is_null($result)) {
196 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
197 // A valid result has returned from the database layer
198 $this->cacheInstance->offsetSet($cacheKey, $result);
201 // This invalid result must be wrapped
203 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
204 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
209 // Create an instance of a CachedDatabaseResult class with the given result
210 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
211 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
213 // And return the instance
214 return $resultInstance;
218 * Count the numbers of rows we shall receive
220 * @param $criteriaInstance An instance of a Criteria class
221 * @param $onlyKeys Only use these keys for a cache key
222 * @return $numRows Numbers of rows of database entries
224 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
225 // Total numbers is -1 so we can distinglish between failed and valid queries
228 // Get the result from above method
229 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
231 // Was that query fine?
232 if ($resultInstance->ifStatusIsOkay()) {
233 // Then get the number of rows
234 $numRows = $resultInstance->getAffectedRows();
237 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
245 * Getter for primary key used in wrapped table
247 * @return $primaryKey Primary key used in wrapped table
249 public final function getPrimaryKeyValue () {
250 // Get the table name and a database instance and ask for it
251 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
258 * Count rows of this table
260 * @return $count Count of total rows in this table
262 public final function countTotalRows () {
263 // Get the table name and a database instance and ask for it
264 $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName());
271 * Removes non-public data from given array.
273 * @param $data An array with possible non-public data that needs to be removed.
274 * @return $data A cleaned up array with only public data.
276 public function removeNonPublicDataFromArray (array $data) {
277 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
278 $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data);
280 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));