3 namespace CoreFramework\Database\Wrapper;
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Object\BaseFrameworkSystem;
11 * A generic database wrapper
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseDatabaseWrapper extends BaseFrameworkSystem {
36 private $cacheInstance = NULL;
39 * Current table name to use
41 private $tableName = 'unknown';
44 * Protected constructor
48 protected function __construct ($class) {
49 // Call parent constructor
50 parent::__construct($class);
52 // Initialize the cache instance
53 $this->initCacheInstance();
57 * Initializes the cache instance with a new object
61 private final function initCacheInstance () {
62 // Is the cache enabled?
63 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
64 // Set the new instance
65 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
70 * Setter for table name
72 * @param $tableName Name of table name to set
75 protected final function setTableName ($tableName) {
76 $this->tableName = (string) $tableName;
80 * Getter for table name
82 * @return $tableName Name of table name to set
84 protected final function getTableName () {
85 return $this->tableName;
89 * 'Inserts' a data set instance into a local file database folder
91 * @param $dataSetInstance A storeable data set
92 * @param $onlyKeys Only use these keys for a cache key
95 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
96 // Default cache key is NULL
100 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
101 // First get a key suitable for our cache and extend it with this class name
102 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
103 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
106 // Does this key exists in cache?
107 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
109 $this->cacheInstance->purgeOffset($cacheKey);
112 // Handle it over to the middleware
113 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
117 * 'Updates' a data set instance with a database layer
119 * @param $dataSetInstance A storeable data set
120 * @param $onlyKeys Only use these keys for a cache key
123 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
128 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
129 // First get a key suitable for our cache and extend it with this class name
130 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
131 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
134 // Does this key exists in cache?
135 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
137 $this->cacheInstance->purgeOffset($cacheKey);
140 // Handle it over to the middleware
141 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
145 * Getter for index key
147 * @return $indexKey Index key
149 public final function getIndexKey () {
150 return $this->getDatabaseInstance()->getIndexKey();
154 * Getter for last exception
156 * @return $lastException Last exception or NULL if none occured
158 public final function getLastException () {
159 return $this->getDatabaseInstance()->getLastException();
163 * Do a "select" query on the current table with the given search criteria and
164 * store it in cache for later usage
166 * @param $criteriaInstance An instance of a Criteria class
167 * @param $onlyKeys Only use these keys for a cache key
168 * @return $resultInstance An instance of a database result class
170 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
171 // Default cache key if cache is not enabled
174 // Is the cache enabled?
175 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
176 // First get a key suitable for our cache and extend it with this class name
177 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
180 // Does this key exists in cache?
181 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
185 // Then use this result
186 $result = $this->cacheInstance->offsetGet($cacheKey);
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
191 // Now it's time to ask the database layer for this select statement
192 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result));
195 // Cache the result if not null
196 if (!is_null($result)) {
198 if ($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) {
199 // A valid result has returned from the database layer
200 $this->cacheInstance->offsetSet($cacheKey, $result);
203 // This invalid result must be wrapped
205 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
206 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
211 // Create an instance of a CachedDatabaseResult class with the given result
212 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
213 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
215 // And return the instance
216 return $resultInstance;
220 * Count the numbers of rows we shall receive
222 * @param $criteriaInstance An instance of a Criteria class
223 * @param $onlyKeys Only use these keys for a cache key
224 * @return $numRows Numbers of rows of database entries
226 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
227 // Total numbers is -1 so we can distinglish between failed and valid queries
230 // Get the result from above method
231 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
233 // Was that query fine?
234 if ($resultInstance->ifStatusIsOkay()) {
235 // Then get the number of rows
236 $numRows = $resultInstance->getAffectedRows();
239 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
247 * Getter for primary key used in wrapped table
249 * @return $primaryKey Primary key used in wrapped table
251 public final function getPrimaryKeyValue () {
252 // Get the table name and a database instance and ask for it
253 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
260 * Count rows of this table
262 * @return $count Count of total rows in this table
264 public final function countTotalRows () {
265 // Get the table name and a database instance and ask for it
266 $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName());
273 * Removes non-public data from given array.
275 * @param $data An array with possible non-public data that needs to be removed.
276 * @return $data A cleaned up array with only public data.
278 public function removeNonPublicDataFromArray (array $data) {
279 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
280 $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data);
282 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));