3 * A database wrapper for the User class
5 * @see DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class UserDatabaseWrapper extends BaseDatabaseWrapper {
29 private $cacheInstance = null;
31 // Constants for exceptions
32 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00;
34 // Constants for database columns
35 const DB_COLUMN_USERNAME = "username";
37 // Constants for database table names
38 const DB_TABLE_USER = "user";
41 * Protected constructor
45 protected function __construct() {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
49 // Set part description
50 $this->setObjectDescription("Database wrapper for user objects");
52 // Create unique ID number
53 $this->createUniqueID();
57 * Creates an instance of this database wrapper by a provided user class
59 * @return $wrapperInstance An instance of the created wrapper class
60 * @throws WrapperUserNameNotFoundException If the supplied username
63 public final static function createUserDatabaseWrapper () {
65 $wrapperInstance = new UserDatabaseWrapper();
67 // Initialize the cache instance
68 $wrapperInstance->initCacheInstance();
70 // Return the instance
71 return $wrapperInstance;
75 * Initializes the cache instance with a new object
79 protected function initCacheInstance () {
80 // Set the new instance
81 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
85 * Do a "select" query on the user table with the given search criteria and
86 * store it in cache for later usage
88 * @param $criteriaInstance An instance of a Criteria class
89 * @return $resultInstance An instance of a database result class
91 public function doSelectByCriteria (Criteria $criteriaInstance) {
92 // First get a key suitable for our cache and extend it with this class name
93 $cacheKey = sprintf("%s@%s",
95 $criteriaInstance->getCacheKey()
98 // Does this key exists in cache?
99 if ($this->cacheInstance->offsetExists($cacheKey)) {
100 // Then use this result
101 $result = $cacheInstance->offsetGet($cacheKey);
103 // Now it's time to ask the database layer for this select statement
104 $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance);
107 $this->cacheInstance->offsetSet($cacheKey, $result);
110 // Create an instance of a DatabaseResult class with the given result
111 $resultInstance = DatabaseResult::createDatabaseResult($result);
113 // And return the instance
114 return $resultInstance;