Continued:
[core.git] / inc / main / classes / database / class_BaseDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Database\Wrapper;
4
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Object\BaseFrameworkSystem;
9
10 /**
11  * A generic database wrapper
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class BaseDatabaseWrapper extends BaseFrameworkSystem {
33         /**
34          * Cache instance
35          */
36         private $cacheInstance = NULL;
37
38         /**
39          * Current table name to use
40          */
41         private $tableName = 'unknown';
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct ($class) {
49                 // Call parent constructor
50                 parent::__construct($class);
51
52                 // Initialize the cache instance
53                 $this->initCacheInstance();
54         }
55
56         /**
57          * Initializes the cache instance with a new object
58          *
59          * @return      void
60          */
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();
66                 } // END - if
67         }
68
69         /**
70          * Setter for table name
71          *
72          * @param       $tableName      Name of table name to set
73          * @return      void
74          */
75         protected final function setTableName ($tableName) {
76                 $this->tableName = (string) $tableName;
77         }
78
79         /**
80          * Getter for table name
81          *
82          * @return      $tableName      Name of table name to set
83          */
84         protected final function getTableName () {
85                 return $this->tableName;
86         }
87
88         /**
89          * 'Inserts' a data set instance into a local file database folder
90          *
91          * @param       $dataSetInstance        A storeable data set
92          * @param       $onlyKeys                       Only use these keys for a cache key
93          * @return      void
94          */
95         protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
96                 // Default cache key is NULL
97                 $cacheKey = NULL;
98
99                 // Is cache enabled?
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 ...');
104                 } // END - if
105
106                 // Does this key exists in cache?
107                 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
108                         // Purge the cache
109                         $this->cacheInstance->purgeOffset($cacheKey);
110                 } // END - if
111
112                 // Handle it over to the middleware
113                 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
114         }
115
116         /**
117          * 'Updates' a data set instance with a database layer
118          *
119          * @param       $dataSetInstance        A storeable data set
120          * @param       $onlyKeys                       Only use these keys for a cache key
121          * @return      void
122          */
123         protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
124                 // Init cache key
125                 $cacheKey = NULL;
126
127                 // Is cache enabled?
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 ...');
132                 } // END - if
133
134                 // Does this key exists in cache?
135                 if (($this->getConfigInstance()->getConfigEntry('database_cache_enabled') === TRUE) && ($this->cacheInstance->offsetExists($cacheKey))) {
136                         // Purge the cache
137                         $this->cacheInstance->purgeOffset($cacheKey);
138                 } // END - if
139
140                 // Handle it over to the middleware
141                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
142         }
143
144         /**
145          * Getter for index key
146          *
147          * @return      $indexKey       Index key
148          */
149         public final function getIndexKey () {
150                 return $this->getDatabaseInstance()->getIndexKey();
151         }
152
153         /**
154          * Getter for last exception
155          *
156          * @return      $lastException  Last exception or NULL if none occured
157          */
158         public final function getLastException () {
159                 return $this->getDatabaseInstance()->getLastException();
160         }
161
162         /**
163          * Do a "select" query on the current table with the given search criteria and
164          * store it in cache for later usage
165          *
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
169          */
170         public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
171                 // Default cache key if cache is not enabled
172                 $cacheKey = NULL;
173
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);
178                 } // END - if
179
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))) {
182                         // Debug message
183                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey . ':' . print_r($this->cacheInstance->offsetGet($cacheKey), TRUE));
184
185                         // Then use this result
186                         $result = $this->cacheInstance->offsetGet($cacheKey);
187                 } else {
188                         // Debug message
189                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
190
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));
194
195                         // Cache the result if not null
196                         if (!is_null($result)) {
197                                 // Is cache enabled?
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);
201                                 } // END - if
202                         } else {
203                                 // This invalid result must be wrapped
204                                 $result = array(
205                                         BaseDatabaseBackend::RESULT_INDEX_STATUS    => 'invalid',
206                                         BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
207                                 );
208                         }
209                 }
210
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));
214
215                 // And return the instance
216                 return $resultInstance;
217         }
218
219         /**
220          * Count the numbers of rows we shall receive
221          *
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
225          */
226         public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
227                 // Total numbers is -1 so we can distinglish between failed and valid queries
228                 $numRows = 0;
229
230                 // Get the result from above method
231                 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
232
233                 // Was that query fine?
234                 if ($resultInstance->ifStatusIsOkay()) {
235                         // Then get the number of rows
236                         $numRows = $resultInstance->getAffectedRows();
237
238                         // Debug message
239                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
240                 } // END - if
241
242                 // Return the result
243                 return $numRows;
244         }
245
246         /**
247          * Getter for primary key used in wrapped table
248          *
249          * @return      $primaryKey             Primary key used in wrapped table
250          */
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());
254
255                 // Return value
256                 return $primaryKey;
257         }
258
259         /**
260          * Count rows of this table
261          *
262          * @return      $count  Count of total rows in this table
263          */
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());
267
268                 // Return value
269                 return $count;
270         }
271
272         /**
273          * Removes non-public data from given array.
274          *
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.
277          */
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);
281
282                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data));
283                 return $data;
284         }
285
286 }