3 namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
8 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
11 * A set of data storeable in databases
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
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.
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.
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/>.
32 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
36 private $tableName = '';
41 private $uniqueKey = '';
46 private $primaryKey = '';
51 private $primaryKeys = array();
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Creates an instance of this criteria
66 * @param $tableName Name of the table
67 * @return $criteriaInstance An instance of this criteria
69 public static final function createDataSetCriteria ($tableName) {
71 $criteriaInstance = new DataSetCriteria();
74 $criteriaInstance->setTableName($tableName);
76 // Return the instance
77 return $criteriaInstance;
81 * Setter for table name
83 * @param $tableName Name of the table to set
86 public final function setTableName ($tableName) {
87 $this->tableName = (string) $tableName;
91 * Getter for table name
93 * @return $tableName Name of the table to set
95 public final function getTableName () {
96 return $this->tableName;
100 * Setter for unique key
102 * @param $uniqueKey Column to use as unique key
105 public final function setUniqueKey ($uniqueKey) {
106 $this->uniqueKey = (string) $uniqueKey;
110 * Getter for unique key
112 * @return $uniqueKey Column to use as unique key
114 public final function getUniqueKey () {
115 return $this->uniqueKey;
119 * Getter for unique key value
121 * @return $uniqueValue Value of the unique key
123 public final function getUniqueValue () {
124 // Get primary key(s) first
125 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
126 $primaryKeys = $this->getPrimaryKeys();
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
132 * If this is not set, this could mean a badly written frontend as
133 * tables should always have a primary key.
135 if (count($primaryKeys) > 0) {
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
143 // Combination set, so get all
144 foreach ($primaryKeys as $primaryKey) {
146 $return .= trim($this->getCriteriaElemnent($primaryKey));
150 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
154 } elseif (!empty($primaryKey)) {
155 // Return primary key
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 ...');
162 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
165 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
168 if (empty($uniqueKey)) {
169 // Bad news, nothing is "unique" by design for this table
170 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
179 * Getter for primary key or unique key if not set
181 * @return $primaryKey Primary key or unique key if not set
183 public final function getPrimaryKey () {
184 // Get primary key by default
185 $primaryKey = $this->primaryKey;
187 if (empty($primaryKey)) {
189 $primaryKey = $this->getUniqueKey();
197 * Setter for primary key
199 * @param $primaryKey Primary key to set
202 public final function setPrimaryKey ($primaryKey) {
203 $this->primaryKey = (string) $primaryKey;
207 * Setter for primary key array
209 * @param $primaryKeys Primary key array to set
212 public function setPrimaryKeyCombined (array $primaryKeys) {
213 $this->primaryKeys = $primaryKeys;
217 * Getter for primary keys
219 * @return $primaryKeys Primary key array
221 public final function getPrimaryKeys () {
223 return $this->primaryKeys;