3 namespace Org\Mxchange\CoreFramework\Database\Frontend;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Criteria\Criteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
10 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 use Org\Mxchange\CoreFramework\Result\Database\BaseDatabaseResult;
13 use Org\Mxchange\CoreFramework\Traits\Cache\CacheableTrait;
16 * A generic database frontend
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 abstract class BaseDatabaseFrontend extends BaseFrameworkSystem {
42 * Current table name to use
44 private $tableName = 'unknown';
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct (string $className) {
53 // Call parent constructor
54 parent::__construct($className);
56 // Initialize the cache instance
57 $this->initCacheInstance();
61 * Initializes the cache instance with a new object
65 private final function initCacheInstance () {
66 // Is the cache enabled?
67 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
68 // Set the new instance
69 $this->setCacheInstance(ObjectFactory::createObjectByConfiguredName('cache_class'));
74 * Setter for table name
76 * @param $tableName Name of table name to set
79 protected final function setTableName (string $tableName) {
80 $this->tableName = $tableName;
84 * Getter for table name
86 * @return $tableName Name of table name to set
88 protected final function getTableName () {
89 return $this->tableName;
93 * 'Inserts' a data set instance into a local file database folder
95 * @param $dataSetInstance A storeable data set
96 * @param $onlyKeys Only use these keys for a cache key
99 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = []) {
100 // Default cache key is NULL
101 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: dataSetInstance=%s,onlyKeys()=%d - CALLED!', $dataSetInstance->__toString(), count($onlyKeys)));
105 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
106 // First get a key suitable for our cache and extend it with this class name
107 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
108 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
111 // Does this key exists in cache?
112 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
113 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
115 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Calling this->cacheInstance->purgeOffset(%s) ...', $cacheKey));
116 $this->getCacheInstance()->purgeOffset($cacheKey);
119 // Handle it over to the middleware
120 FrameworkBootstrap::getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
123 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: EXIT!');
127 * 'Updates' a data set instance with a database layer
129 * @param $dataSetInstance A storeable data set
130 * @param $onlyKeys Only use these keys for a cache key
133 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = []) {
138 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
139 // First get a key suitable for our cache and extend it with this class name
140 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
141 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: Using cache key ' . $cacheKey . ' for purging ...');
144 // Does this key exists in cache?
145 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey))) {
147 $this->getCacheInstance()->purgeOffset($cacheKey);
150 // Handle it over to the middleware
151 FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
155 * Getter for index key
157 * @return $indexKey Index key
159 public final function getIndexKey () {
160 return FrameworkBootstrap::getDatabaseInstance()->getIndexKey();
164 * Getter for last exception
166 * @return $lastException Last exception or NULL if none occured
168 public final function getLastException () {
169 return FrameworkBootstrap::getDatabaseInstance()->getLastException();
173 * Do a "select" query on the current table with the given search criteria and
174 * store it in cache for later usage
176 * @param $criteriaInstance An instance of a Criteria class
177 * @param $onlyKeys Only use these keys for a cache key
178 * @return $resultInstance An instance of a database result class
180 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = []) {
181 // Default cache key if cache is not enabled
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
186 // Is the cache enabled?
187 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
188 // First get a key suitable for our cache and extend it with this class name
189 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
192 // Does this key exists in cache?
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
194 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->getCacheInstance()->offsetExists($cacheKey, BaseDatabaseResult::RESULT_NAME_ROWS, 1))) {
195 // Then use this result
196 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Cache used for cacheKey=%s', $cacheKey));
197 $result = $this->getCacheInstance()->offsetGet($cacheKey);
199 // Now it's time to ask the database layer for this select statement
200 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Quering database, cacheKey=%s ...', $cacheKey));
201 $result = FrameworkBootstrap::getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
203 // Cache the result if not null
204 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: result[]=%s', gettype($result)));
205 if (!is_null($result)) {
207 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
208 // A valid result has returned from the database layer
209 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: Setting cacheKey=%s with result()=%d entries', $cacheKey, count($result)));
210 $this->getCacheInstance()->offsetSet($cacheKey, $result);
213 // This invalid result must be wrapped
215 BaseDatabaseResult::RESULT_NAME_STATUS => 'invalid',
216 BaseDatabaseResult::RESULT_NAME_EXCEPTION => FrameworkBootstrap::getDatabaseInstance()->getLastException()
221 // Create an instance of a CachedDatabaseResult class with the given result
222 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: result[%s]=%s,result[%s]?=%d,result[%s]?=%d', BaseDatabaseResult::RESULT_NAME_STATUS, $result[BaseDatabaseResult::RESULT_NAME_STATUS], BaseDatabaseResult::RESULT_NAME_ROWS, isset($result[BaseDatabaseResult::RESULT_NAME_ROWS]), BaseDatabaseResult::RESULT_NAME_EXCEPTION, isset($result[BaseDatabaseResult::RESULT_NAME_EXCEPTION])));
224 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
226 // And return the instance
227 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FRONTEND: resultInstance=%s - EXIT!', $resultInstance->__toString()));
228 return $resultInstance;
232 * Count the numbers of rows we shall receive
234 * @param $criteriaInstance An instance of a Criteria class
235 * @param $onlyKeys Only use these keys for a cache key
236 * @return $numRows Numbers of rows of database entries
238 public function doSelectCountByCriteria (Criteria $criteriaInstance, array $onlyKeys = []) {
239 // Total numbers is -1 so we can distinglish between failed and valid queries
242 // Get the result from above method
243 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
245 // Was that query fine?
246 if ($resultInstance->ifStatusIsOkay()) {
247 // Then get the number of rows
248 $numRows = $resultInstance->getAffectedRows();
251 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRONTEND: numRows=' . $numRows);
259 * Getter for primary key used in wrapped table
261 * @return $primaryKey Primary key used in wrapped table
263 public final function getPrimaryKeyValue () {
264 // Get the table name and a database instance and ask for it
265 $primaryKey = FrameworkBootstrap::getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
272 * Count rows of this table
274 * @return $count Count of total rows in this table
276 public final function countTotalRows () {
277 // Get the table name and a database instance and ask for it
278 $count = FrameworkBootstrap::getDatabaseInstance()->countTotalRows($this->getTableName());
285 * Removes non-public data from given array.
287 * @param $data An array with possible non-public data that needs to be removed.
288 * @return $data A cleaned up array with only public data.
290 public function removeNonPublicDataFromArray (array $data) {
291 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
292 $data = FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray($data);
294 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));