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