]> git.mxchange.org Git - core.git/blob - framework/main/classes/criteria/dataset/class_DataSetCriteria.php
Continued:
[core.git] / framework / main / classes / criteria / dataset / class_DataSetCriteria.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
10
11 /**
12  * A set of data storeable in databases
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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 DataSetCriteria extends BaseCriteria implements StoreableCriteria {
34         /**
35          * Table name
36          */
37         private $tableName = '';
38
39         /**
40          * Unique key
41          */
42         private $uniqueKey = '';
43
44         /**
45          * Primary key
46          */
47         private $primaryKey = '';
48
49         /**
50          * Primary keys
51          */
52         private $primaryKeys = [];
53
54         /**
55          * Search criteria instance
56          */
57         private $searchInstance = NULL;
58
59         /**
60          * Protected constructor
61          *
62          * @return      void
63          */
64         protected function __construct () {
65                 // Call parent constructor
66                 parent::__construct(__CLASS__);
67         }
68
69         /**
70          * Creates an instance of this criteria
71          *
72          * @param       $tableName                      Name of the table
73          * @return      $criteriaInstance       An instance of this criteria
74          */
75         public static final function createDataSetCriteria (string $tableName) {
76                 // Get a new instance
77                 $criteriaInstance = new DataSetCriteria();
78
79                 // Set table name
80                 $criteriaInstance->setTableName($tableName);
81
82                 // Return the instance
83                 return $criteriaInstance;
84         }
85
86         /**
87          * Setter for table name
88          *
89          * @param       $tableName      Name of the table to set
90          * @return      void
91          */
92         public final function setTableName (string $tableName) {
93                 $this->tableName = $tableName;
94         }
95
96         /**
97          * Getter for table name
98          *
99          * @return      $tableName      Name of the table to set
100          */
101         public final function getTableName () {
102                 return $this->tableName;
103         }
104
105         /**
106          * Setter for unique key
107          *
108          * @param       $uniqueKey      Column to use as unique key
109          * @return      void
110          */
111         public final function setUniqueKey (string $uniqueKey) {
112                 $this->uniqueKey = $uniqueKey;
113         }
114
115         /**
116          * Getter for unique key
117          *
118          * @return      $uniqueKey      Column to use as unique key
119          */
120         public final function getUniqueKey () {
121                 return $this->uniqueKey;
122         }
123
124         /**
125          * Setter for primary key
126          *
127          * @param       $primaryKey             Primary key to set
128          * @return      void
129          */
130         public final function setPrimaryKey (string $primaryKey) {
131                 $this->primaryKey = $primaryKey;
132         }
133
134         /**
135          * Setter for primary key array
136          *
137          * @param       $primaryKeys    Primary key array to set
138          * @return      void
139          */
140         public function setPrimaryKeyCombined (array $primaryKeys) {
141                 $this->primaryKeys = $primaryKeys;
142         }
143
144         /**
145          * Getter for primary keys
146          *
147          * @return      $primaryKeys    Primary key array
148          */
149         public final function getPrimaryKeys () {
150                 // Return it
151                 return $this->primaryKeys;
152         }
153
154         /**
155          * Setter for search instance
156          *
157          * @param       $searchInstance         Searchable criteria instance
158          * @return      void
159          */
160         public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
161                 $this->searchInstance = $searchInstance;
162         }
163
164         /**
165          * Getter for search instance
166          *
167          * @return      $searchInstance         Searchable criteria instance
168          */
169         public final function getSearchInstance () {
170                 return $this->searchInstance;
171         }
172
173         /**
174          * Getter for unique key value
175          *
176          * @return      $uniqueValue    Value of the unique key
177          */
178         public final function getUniqueValue () {
179                 // Get primary key(s) first
180                 $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
181                 $primaryKeys = $this->getPrimaryKeys();
182
183                 // Debug message
184                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
185
186                 /*
187                  * If this is not set, this could mean a badly written frontend as
188                  * tables should always have a primary key.
189                  */
190                 if (count($primaryKeys) > 0) {
191                         /*
192                          * Init return value, this can be put all together without any
193                          * separator as it only aids as a "unique value" for generating the
194                          * "row file name".
195                          */
196                         $return = '';
197
198                         // Combination set, so get all
199                         foreach ($primaryKeys as $primaryKey) {
200                                 // Add it
201                                 $return .= trim($this->getCriteriaElemnent($primaryKey));
202                         } // END - foreach
203
204                         // Debug message
205                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
206
207                         // Return it
208                         return $return;
209                 } elseif (!empty($primaryKey)) {
210                         // Return primary key
211                         return $primaryKey;
212                 } else {
213                         // @TODO Issue a warning
214                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
215
216                         // Get unique key
217                         $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
218
219                         // Debug message
220                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
221
222                         // Is it empty, too?
223                         if (empty($uniqueKey)) {
224                                 // Bad news, nothing is "unique" by design for this table
225                                 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
226                         } else {
227                                 // Return unique key
228                                 return $uniqueKey;
229                         }
230                 }
231         }
232
233         /**
234          * Getter for primary key or unique key if not set
235          *
236          * @return      $primaryKey             Primary key or unique key if not set
237          */
238         public final function getPrimaryKey () {
239                 // Get primary key by default
240                 $primaryKey = $this->primaryKey;
241
242                 if (empty($primaryKey)) {
243                         // Get uniqueKey
244                         $primaryKey = $this->getUniqueKey();
245                 } // END - if
246
247                 // Return it
248                 return $primaryKey;
249         }
250
251 }