3 * A database wrapper for user points classes
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper {
26 * Constants for database table names
28 const DB_TABLE_USER_POINTS = 'user_points';
31 * Name of the user->points column
33 const DB_COLUMN_POINTS_UID = 'points_uid';
36 * Name of the points column
38 const DB_COLUMN_POINTS = 'points';
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this database wrapper by a provided user class
53 * @return $wrapperInstance An instance of the created wrapper class
55 public static final function createUserPointsDatabaseWrapper () {
57 $wrapperInstance = new UserPointsDatabaseWrapper();
59 // Set (primary!) table name
60 $wrapperInstance->setTableName(self::DB_TABLE_USER_POINTS);
62 // Return the instance
63 return $wrapperInstance;
67 * Inserts the given points for the given user in the database
69 * @param $pointsInstance An instance of a user class
72 public function insertUserPoints (BookablePoints $pointsInstance) {
73 // Generate a data set for the request
74 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
76 // Set the primary key
77 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
79 // Add registration elements to the dataset
80 $pointsInstance->addElementsToDataSet($dataSetInstance, NULL);
82 // "Insert" this request instance completely into the database
83 $this->queryInsertDataSet($dataSetInstance);
87 * Updates an user database entry with given result
89 * @param $resultInstance An instance of a Updateable database result
92 public function doUpdateByResult (UpdateableResult $resultInstance) {
93 // Generate a data set object
94 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
96 // Add all update criteria to the database set
97 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
100 $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance());
102 // Set the primary key
103 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
105 // "Update" this request with the database
106 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);