imported ApplicationEntryPoint
[core.git] / framework / main / classes / criteria / dataset / class_DataSetCriteria.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Criteria\DataSet;
4
5 // Import framework stuff
6 use CoreFramework\Criteria\BaseCriteria;
7 use CoreFramework\Criteria\Storing\StoreableCriteria;
8 use CoreFramework\EntryPoint\ApplicationEntryPoint;
9
10 /**
11  * A set of data storeable in databases
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
33         /**
34          * Table name
35          */
36         private $tableName = '';
37
38         /**
39          * Unique key
40          */
41         private $uniqueKey = '';
42
43         /**
44          * Primary key
45          */
46         private $primaryKey = '';
47
48         /**
49          * Primary keys
50          */
51         private $primaryKeys = array();
52
53         /**
54          * Protected constructor
55          *
56          * @return      void
57          */
58         protected function __construct () {
59                 // Call parent constructor
60                 parent::__construct(__CLASS__);
61         }
62
63         /**
64          * Creates an instance of this criteria
65          *
66          * @param       $tableName                      Name of the table
67          * @return      $criteriaInstance       An instance of this criteria
68          */
69         public static final function createDataSetCriteria ($tableName) {
70                 // Get a new instance
71                 $criteriaInstance = new DataSetCriteria();
72
73                 // Set table name
74                 $criteriaInstance->setTableName($tableName);
75
76                 // Return the instance
77                 return $criteriaInstance;
78         }
79
80         /**
81          * Setter for table name
82          *
83          * @param       $tableName      Name of the table to set
84          * @return      void
85          */
86         public final function setTableName ($tableName) {
87                 $this->tableName = (string) $tableName;
88         }
89
90         /**
91          * Getter for table name
92          *
93          * @return      $tableName      Name of the table to set
94          */
95         public final function getTableName () {
96                 return $this->tableName;
97         }
98
99         /**
100          * Setter for unique key
101          *
102          * @param       $uniqueKey      Column to use as unique key
103          * @return      void
104          */
105         public final function setUniqueKey ($uniqueKey) {
106                 $this->uniqueKey = (string) $uniqueKey;
107         }
108
109         /**
110          * Getter for unique key
111          *
112          * @return      $uniqueKey      Column to use as unique key
113          */
114         public final function getUniqueKey () {
115                 return $this->uniqueKey;
116         }
117
118         /**
119          * Getter for unique key value
120          *
121          * @return      $uniqueValue    Value of the unique key
122          */
123         public final function getUniqueValue () {
124                 // Get primary key(s) first
125                 $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
126                 $primaryKeys = $this->getPrimaryKeys();
127
128                 // Debug message
129                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
130
131                 /*
132                  * If this is not set, this could mean a badly written frontend as
133                  * tables should always have a primary key.
134                  */
135                 if (count($primaryKeys) > 0) {
136                         /*
137                          * Init return value, this can be put all together without any
138                          * separator as it only aids as a "unique value" for generating the
139                          * "row file name".
140                          */
141                         $return = '';
142
143                         // Combination set, so get all
144                         foreach ($primaryKeys as $primaryKey) {
145                                 // Add it
146                                 $return .= trim($this->getCriteriaElemnent($primaryKey));
147                         } // END - foreach
148
149                         // Debug message
150                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
151
152                         // Return it
153                         return $return;
154                 } elseif (!empty($primaryKey)) {
155                         // Return primary key
156                         return $primaryKey;
157                 } else {
158                         // @TODO Issue a warning
159                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
160
161                         // Get unique key
162                         $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
163
164                         // Debug message
165                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
166
167                         // Is it empty, too?
168                         if (empty($uniqueKey)) {
169                                 // Bad news, nothing is "unique" by design for this table
170                                 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
171                         } else {
172                                 // Return unique key
173                                 return $uniqueKey;
174                         }
175                 }
176         }
177
178         /**
179          * Getter for primary key or unique key if not set
180          *
181          * @return      $primaryKey             Primary key or unique key if not set
182          */
183         public final function getPrimaryKey () {
184                 // Get primary key by default
185                 $primaryKey = $this->primaryKey;
186
187                 if (empty($primaryKey)) {
188                         // Get uniqueKey
189                         $primaryKey = $this->getUniqueKey();
190                 } // END - if
191
192                 // Return it
193                 return $primaryKey;
194         }
195
196         /**
197          * Setter for primary key
198          *
199          * @param       $primaryKey             Primary key to set
200          * @return      void
201          */
202         public final function setPrimaryKey ($primaryKey) {
203                 $this->primaryKey = (string) $primaryKey;
204         }
205
206         /**
207          * Setter for primary key array
208          *
209          * @param       $primaryKeys    Primary key array to set
210          * @return      void
211          */
212         public function setPrimaryKeyCombined (array $primaryKeys) {
213                 $this->primaryKeys = $primaryKeys;
214         }
215
216         /**
217          * Getter for primary keys
218          *
219          * @return      $primaryKeys    Primary key array
220          */
221         public final function getPrimaryKeys () {
222                 // Return it
223                 return $this->primaryKeys;
224         }
225
226 }