d9242d8aa8b60e547b6da819533a2191726fc8ee
[core.git] / framework / main / classes / database / frontend / class_UserPointsDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Database\Frontend\Points;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult;
10
11 /**
12  * A database wrapper for user points classes
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper, Registerable {
34         /**
35          * Constants for database table names
36          */
37         const DB_TABLE_USER_POINTS = 'user_points';
38
39         /**
40          * Name of the user->points column
41          */
42         const DB_COLUMN_POINTS_UID = 'points_uid';
43
44         /**
45          * Name of the points column
46          */
47         const DB_COLUMN_POINTS = 'points';
48
49         /**
50          * Protected constructor
51          *
52          * @return      void
53          */
54         protected function __construct () {
55                 // Call parent constructor
56                 parent::__construct(__CLASS__);
57         }
58
59         /**
60          * Creates an instance of this database wrapper by a provided user class
61          *
62          * @return      $wrapperInstance        An instance of the created wrapper class
63          */
64         public static final function createUserPointsDatabaseWrapper () {
65                 // Get a new instance
66                 $wrapperInstance = new UserPointsDatabaseWrapper();
67
68                 // Set (primary!) table name
69                 $wrapperInstance->setTableName(self::DB_TABLE_USER_POINTS);
70
71                 // Return the instance
72                 return $wrapperInstance;
73         }
74
75         /**
76          * Inserts the given points for the given user in the database
77          *
78          * @param       $pointsInstance         An instance of a user class
79          * @return      void
80          */
81         public function insertUserPoints (BookablePoints $pointsInstance) {
82                 // Generate a data set for the request
83                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
84
85                 // Set the primary key
86                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
87
88                 // Add registration elements to the dataset
89                 $pointsInstance->addElementsToDataSet($dataSetInstance);
90
91                 // "Insert" this request instance completely into the database
92                 $this->queryInsertDataSet($dataSetInstance);
93         }
94
95         /**
96          * Updates an user database entry with given result
97          *
98          * @param       $resultInstance         An instance of a Updateable database result
99          * @return      void
100          */
101         public function doUpdateByResult (UpdateableResult $resultInstance) {
102                 // Generate a data set object
103                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
104
105                 // Add all update criteria to the database set
106                 $resultInstance->addElementsToDataSet($dataSetInstance);
107
108                 // Add seach criteria
109                 $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance());
110
111                 // Set the primary key
112                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
113
114                 // "Update" this request with the database
115                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
116         }
117
118 }