3 * A generic database wrapper
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseDatabaseWrapper extends BaseFrameworkSystem {
28 private $cacheInstance = NULL;
31 * Current table name to use
33 private $tableName = 'unknown';
36 * Protected constructor
40 protected function __construct ($class) {
41 // Call parent constructor
42 parent::__construct($class);
44 // Initialize the cache instance
45 $this->initCacheInstance();
49 * Initializes the cache instance with a new object
53 private final function initCacheInstance () {
54 // Set the new instance
55 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
59 * Setter for table name
61 * @param $tableName Name of table name to set
64 protected final function setTableName ($tableName) {
65 $this->tableName = (string) $tableName;
69 * Getter for table name
71 * @return $tableName Name of table name to set
73 protected final function getTableName () {
74 return $this->tableName;
78 * 'Inserts' a data set instance into a local file database folder
80 * @param $dataSetInstance A storeable data set
81 * @param $onlyKeys Only use these keys for a cache key
84 protected function queryInsertDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
85 // First get a key suitable for our cache and extend it with this class name
86 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
87 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
89 // Does this key exists in cache?
90 if ($this->cacheInstance->offsetExists($cacheKey)) {
92 $this->cacheInstance->purgeOffset($cacheKey);
95 // Handle it over to the middleware
96 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
100 * 'Updates' a data set instance with a database layer
102 * @param $dataSetInstance A storeable data set
103 * @param $onlyKeys Only use these keys for a cache key
106 protected function queryUpdateDataSet (StoreableCriteria $dataSetInstance, array $onlyKeys = array()) {
107 // First get a key suitable for our cache and extend it with this class name
108 $cacheKey = $this->getCacheKeyByCriteria($dataSetInstance, $onlyKeys);
109 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Using cache key ' . $cacheKey . ' for purging ...');
111 // Does this key exists in cache?
112 if ($this->cacheInstance->offsetExists($cacheKey)) {
114 $this->cacheInstance->purgeOffset($cacheKey);
117 // Handle it over to the middleware
118 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
122 * Getter for index key
124 * @return $indexKey Index key
126 public final function getIndexKey () {
127 return $this->getDatabaseInstance()->getIndexKey();
131 * Getter for last exception
133 * @return $lastException Last exception or NULL if none occured
135 public final function getLastException () {
136 return $this->getDatabaseInstance()->getLastException();
140 * Do a "select" query on the current table with the given search criteria and
141 * store it in cache for later usage
143 * @param $criteriaInstance An instance of a Criteria class
144 * @param $onlyKeys Only use these keys for a cache key
145 * @return $resultInstance An instance of a database result class
147 public function doSelectByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
148 // First get a key suitable for our cache and extend it with this class name
149 $cacheKey = $this->getCacheKeyByCriteria($criteriaInstance, $onlyKeys);
151 // Does this key exists in cache?
152 if ($this->cacheInstance->offsetExists($cacheKey)) {
154 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Cache used for cacheKey=' . $cacheKey);
156 // Then use this result
157 $result = $this->cacheInstance->offsetGet($cacheKey);
160 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey);
162 // Now it's time to ask the database layer for this select statement
163 $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance);
165 // Cache the result if not null
166 if (!is_null($result)) {
167 // A valid result has returned from the database layer
168 $this->cacheInstance->offsetSet($cacheKey, $result);
170 // This invalid result must be wrapped
172 BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid',
173 BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException()
178 // Create an instance of a DatabaseResult class with the given result
179 $resultInstance = DatabaseResult::createDatabaseResult($result);
181 // And return the instance
182 return $resultInstance;
186 * Count the numbers of rows we shall receive
188 * @param $criteriaInstance An instance of a Criteria class
189 * @param $onlyKeys Only use these keys for a cache key
190 * @return $numRows Numbers of rows of database entries
192 public function doSelectCountByCriteria (Criteria $criteriaInstance, $onlyKeys = array()) {
193 // Total numbers is -1 so we can distinglish between failed and valid queries
196 // Get the result from above method
197 $resultInstance = $this->doSelectByCriteria($criteriaInstance, $onlyKeys);
199 // Was that query fine?
200 if ($resultInstance->ifStatusIsOkay()) {
201 // Then get the number of rows
202 $numRows = $resultInstance->getAffectedRows();
205 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-WRAPPER: numRows=' . $numRows);
213 * Getter for primary key used in wrapped table
215 * @return $primaryKey Primary key used in wrapped table
217 public final function getPrimaryKeyValue () {
218 // Get the table name and a database instance and ask for it
219 $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName());