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