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";
36 const DB_COLUMN_EMAIL = "email";
38 // Constants for database table names
39 const DB_TABLE_USER = "user";
42 * Protected constructor
46 protected function __construct() {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
50 // Set part description
51 $this->setObjectDescription("Database wrapper for user objects");
53 // Create unique ID number
54 $this->generateUniqueId();
58 * Creates an instance of this database wrapper by a provided user class
60 * @return $wrapperInstance An instance of the created wrapper class
61 * @throws WrapperUserNameNotFoundException If the supplied username
64 public final static function createUserDatabaseWrapper () {
66 $wrapperInstance = new UserDatabaseWrapper();
68 // Initialize the cache instance
69 $wrapperInstance->initCacheInstance();
71 // Return the instance
72 return $wrapperInstance;
76 * Initializes the cache instance with a new object
80 protected function initCacheInstance () {
81 // Set the new instance
82 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
86 * Do a "select" query on the user table with the given search criteria and
87 * store it in cache for later usage
89 * @param $criteriaInstance An instance of a Criteria class
90 * @return $resultInstance An instance of a database result class
92 public function doSelectByCriteria (Criteria $criteriaInstance) {
93 // First get a key suitable for our cache and extend it with this class name
94 $cacheKey = sprintf("%s@%s",
96 $criteriaInstance->getCacheKey()
99 // Does this key exists in cache?
100 if ($this->cacheInstance->offsetExists($cacheKey)) {
101 // Then use this result
102 $result = $cacheInstance->offsetGet($cacheKey);
104 // Now it's time to ask the database layer for this select statement
105 $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance);
107 // Cache the result if not null
108 if (!is_null($result)) {
109 // A valid result has returned from the database layer
110 $this->cacheInstance->offsetSet($cacheKey, $result);
112 // This invalid result must be wrapped
114 'status' => "invalid",
115 'exception' => $this->getDatabaseInstance()->getLastException()
120 // Create an instance of a DatabaseResult class with the given result
121 $resultInstance = DatabaseResult::createDatabaseResult($result);
123 // And return the instance
124 return $resultInstance;
128 * Handles inserting the registration data from a registration instance into the database
130 * @param $registrationInstance An instance of a registration class
133 public function insertRegistrationObject (UserRegister $registrationInstance) {
134 // Generate a data set for the request
135 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria');
136 $dataSetInstance->setTableName(self::DB_TABLE_USER);
138 // Add registration elements to the dataset
139 $registrationInstance->addElementsToDataSet($dataSetInstance);
141 // "Insert" this request instance completely into the database
142 $this->getDatabaseInstance()->insertDataSet($dataSetInstance);