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;
14 * A generic database wrapper
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 abstract class BaseDatabaseWrapper extends BaseFrameworkSystem {
39 private $cacheInstance = NULL;
42 * Current table name to use
44 private $tableName = 'unknown';
47 * Protected constructor
51 protected function __construct ($class) {
52 // Call parent constructor
53 parent::__construct($class);
55 // Initialize the cache instance
56 $this->initCacheInstance();
60 * Initializes the cache instance with a new object
64 private final function initCacheInstance () {
65 // Is the cache enabled?
66 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
67 // Set the new instance
68 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
73 * Setter for table name
75 * @param $tableName Name of table name to set
78 protected final function setTableName ($tableName) {
79 $this->tableName = (string) $tableName;
83 * Getter for table name
85 * @return $tableName Name of table name to set
87 protected final function getTableName () {
88 return $this->tableName;
92 * 'Inserts' a data set instance into a local file database folder
94 * @param $dataSetInstance A storeable data set
95 * @param $onlyKeys Only use these keys for a cache key
98 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
99 // Default cache key is NULL
100 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: dataSetInstance=%s,onlyKeys()=%d - CALLED!', $dataSetInstance->__toString(), count($onlyKeys)));
104 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
105 // First get a key suitable for our cache and extend it with this class name
106 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
107 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
110 // Does this key exists in cache?
111 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
112 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->cacheInstance->offsetExists($cacheKey))) {
114 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: Calling this->cacheInstance->purgeOffset(%s) ...', $cacheKey));
115 $this->cacheInstance->purgeOffset($cacheKey);
118 // Handle it over to the middleware
119 FrameworkBootstrap::getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
122 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: EXIT!');
126 * 'Updates' a data set instance with a database layer
128 * @param $dataSetInstance A storeable data set
129 * @param $onlyKeys Only use these keys for a cache key
132 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
137 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
138 // First get a key suitable for our cache and extend it with this class name
139 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
140 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
143 // Does this key exists in cache?
144 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->cacheInstance->offsetExists($cacheKey))) {
146 $this->cacheInstance->purgeOffset($cacheKey);
149 // Handle it over to the middleware
150 FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
154 * Getter for index key
156 * @return $indexKey Index key
158 public final function getIndexKey () {
159 return FrameworkBootstrap::getDatabaseInstance()->getIndexKey();
163 * Getter for last exception
165 * @return $lastException Last exception or NULL if none occured
167 public final function getLastException () {
168 return FrameworkBootstrap::getDatabaseInstance()->getLastException();
172 * Do a "select" query on the current table with the given search criteria and
173 * store it in cache for later usage
175 * @param $criteriaInstance An instance of a Criteria class
176 * @param $onlyKeys Only use these keys for a cache key
177 * @return $resultInstance An instance of a database result class
179 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
180 // Default cache key if cache is not enabled
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: criteriaInstance=%s,onlyKeys()=%d - CALLED!', $criteriaInstance->__toString(), count($onlyKeys)));
185 // Is the cache enabled?
186 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
187 // First get a key suitable for our cache and extend it with this class name
188 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
191 // Does this key exists in cache?
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: cacheKey[%s]=%s', gettype($cacheKey), $cacheKey));
193 if ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) && ($this->cacheInstance->offsetExists($cacheKey, BaseDatabaseBackend::RESULT_INDEX_ROWS, 1))) {
194 // Then use this result
195 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: Cache used for cacheKey=%s', $cacheKey));
196 $result = $this->cacheInstance->offsetGet($cacheKey);
198 // Now it's time to ask the database layer for this select statement
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: Quering database, cacheKey=%s ...', $cacheKey));
200 $result = FrameworkBootstrap::getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
202 // Cache the result if not null
203 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: result[]=%s', gettype($result)));
204 if (!is_null($result)) {
206 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('database_cache_enabled') === true) {
207 // A valid result has returned from the database layer
208 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: Setting cacheKey=%s with result()=%d entries', $cacheKey, count($result)));
209 $this->cacheInstance->offsetSet($cacheKey, $result);
212 // This invalid result must be wrapped
214 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
215 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => FrameworkBootstrap::getDatabaseInstance()->getLastException()
220 // Create an instance of a CachedDatabaseResult class with the given result
221 // @TODO Minor: Update above comment to e.g. BaseDatabaseResult
222 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: result[%s]=%s,result[%s]?=%d,result[%s]?=%d', BaseDatabaseBackend::RESULT_INDEX_STATUS, $result[BaseDatabaseBackend::RESULT_INDEX_STATUS], BaseDatabaseBackend::RESULT_INDEX_ROWS, isset($result[BaseDatabaseBackend::RESULT_INDEX_ROWS]), BaseDatabaseBackend::RESULT_INDEX_EXCEPTION, isset($result[BaseDatabaseBackend::RESULT_INDEX_EXCEPTION])));
223 $resultInstance = ObjectFactory::createObjectByConfiguredName('database_result_class', array($result));
225 // And return the instance
226 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-WRAPPER: resultInstance=%s - EXIT!', $resultInstance->__toString()));
227 return $resultInstance;
231 * Count the numbers of rows we shall receive
233 * @param $criteriaInstance An instance of a Criteria class
234 * @param $onlyKeys Only use these keys for a cache key
235 * @return $numRows Numbers of rows of database entries
237 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
238 // Total numbers is -1 so we can distinglish between failed and valid queries
241 // Get the result from above method
242 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
244 // Was that query fine?
245 if ($resultInstance->ifStatusIsOkay()) {
246 // Then get the number of rows
247 $numRows = $resultInstance->getAffectedRows();
250 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
258 * Getter for primary key used in wrapped table
260 * @return $primaryKey Primary key used in wrapped table
262 public final function getPrimaryKeyValue () {
263 // Get the table name and a database instance and ask for it
264 $primaryKey = FrameworkBootstrap::getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());
271 * Count rows of this table
273 * @return $count Count of total rows in this table
275 public final function countTotalRows () {
276 // Get the table name and a database instance and ask for it
277 $count = FrameworkBootstrap::getDatabaseInstance()->countTotalRows($this->getTableName());
284 * Removes non-public data from given array.
286 * @param $data An array with possible non-public data that needs to be removed.
287 * @return $data A cleaned up array with only public data.
289 public function removeNonPublicDataFromArray (array $data) {
290 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...');
291 $data = FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray($data);
293 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));