3 namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
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;
12 use \InvalidArgumentException;
15 * A set of data storeable in databases
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
40 private $tableName = '';
45 private $uniqueKey = '';
50 private $primaryKey = '';
55 private $primaryKeys = [];
58 * Search criteria instance
60 private $searchInstance = NULL;
63 * Protected constructor
67 private function __construct () {
68 // Call parent constructor
69 parent::__construct(__CLASS__);
73 * Creates an instance of this criteria
75 * @param $tableName Name of the table
76 * @return $criteriaInstance An instance of this criteria
77 * @throws InvalidArgumentException If a parameter is not valid
79 public static final function createDataSetCriteria (string $tableName) {
81 if (empty($tableName)) {
83 throw new InvalidArgumentException('Parameter "tableName" is empty');
87 $criteriaInstance = new DataSetCriteria();
90 $criteriaInstance->setTableName($tableName);
92 // Return the instance
93 return $criteriaInstance;
97 * Setter for table name
99 * @param $tableName Name of the table to set
102 public final function setTableName (string $tableName) {
103 $this->tableName = $tableName;
107 * Getter for table name
109 * @return $tableName Name of the table to set
111 public final function getTableName () {
112 return $this->tableName;
116 * Setter for unique key
118 * @param $uniqueKey Column to use as unique key
121 public final function setUniqueKey (string $uniqueKey) {
122 $this->uniqueKey = $uniqueKey;
126 * Getter for unique key
128 * @return $uniqueKey Column to use as unique key
130 public final function getUniqueKey () {
131 return $this->uniqueKey;
135 * Setter for primary key
137 * @param $primaryKey Primary key to set
140 public final function setPrimaryKey (string $primaryKey) {
141 $this->primaryKey = $primaryKey;
145 * Setter for primary key array
147 * @param $primaryKeys Primary key array to set
150 public function setPrimaryKeyCombined (array $primaryKeys) {
151 $this->primaryKeys = $primaryKeys;
155 * Getter for primary keys
157 * @return $primaryKeys Primary key array
159 public final function getPrimaryKeys () {
161 return $this->primaryKeys;
165 * Setter for search instance
167 * @param $searchInstance Searchable criteria instance
170 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
171 $this->searchInstance = $searchInstance;
175 * Getter for search instance
177 * @return $searchInstance Searchable criteria instance
179 public final function getSearchInstance () {
180 return $this->searchInstance;
184 * Getter for unique key value
186 * @return $uniqueValue Value of the unique key
188 public final function getUniqueValue () {
189 // Get primary key(s) first
190 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
191 $primaryKeys = $this->getPrimaryKeys();
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
197 * If this is not set, this could mean a badly written frontend as
198 * tables should always have a primary key.
200 if (count($primaryKeys) > 0) {
202 * Init return value, this can be put all together without any
203 * separator as it only aids as a "unique value" for generating the
208 // Combination set, so get all
209 foreach ($primaryKeys as $primaryKey) {
211 $return .= trim($this->getCriteriaElemnent($primaryKey));
215 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
219 } elseif (!empty($primaryKey)) {
220 // Return primary key
223 // @TODO Issue a warning
224 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 ...');
227 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
230 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
233 if (empty($uniqueKey)) {
234 // Bad news, nothing is "unique" by design for this table
235 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
244 * Getter for primary key or unique key if not set
246 * @return $primaryKey Primary key or unique key if not set
248 public final function getPrimaryKey () {
249 // Get primary key by default
250 $primaryKey = $this->primaryKey;
252 if (empty($primaryKey)) {
254 $primaryKey = $this->getUniqueKey();