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;
10 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
13 use \InvalidArgumentException;
16 * A set of data storeable in databases
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
41 private $tableName = '';
46 private $uniqueKey = '';
51 private $primaryKey = '';
56 private $primaryKeys = [];
59 * Search criteria instance
61 private $searchInstance = NULL;
64 * Protected constructor
68 private function __construct () {
69 // Call parent constructor
70 parent::__construct(__CLASS__);
74 * Creates an instance of this criteria
76 * @param $tableName Name of the table
77 * @return $criteriaInstance An instance of this criteria
78 * @throws InvalidArgumentException If a parameter is not valid
80 public static final function createDataSetCriteria (string $tableName) {
82 if (empty($tableName)) {
84 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
88 $criteriaInstance = new DataSetCriteria();
91 $criteriaInstance->setTableName($tableName);
93 // Return the instance
94 return $criteriaInstance;
98 * Setter for table name
100 * @param $tableName Name of the table to set
103 public final function setTableName (string $tableName) {
104 $this->tableName = $tableName;
108 * Getter for table name
110 * @return $tableName Name of the table to set
112 public final function getTableName () {
113 return $this->tableName;
117 * Setter for unique key
119 * @param $uniqueKey Column to use as unique key
122 public final function setUniqueKey (string $uniqueKey) {
123 $this->uniqueKey = $uniqueKey;
127 * Getter for unique key
129 * @return $uniqueKey Column to use as unique key
131 public final function getUniqueKey () {
132 return $this->uniqueKey;
136 * Setter for primary key
138 * @param $primaryKey Primary key to set
141 public final function setPrimaryKey (string $primaryKey) {
142 $this->primaryKey = $primaryKey;
146 * Setter for primary key array
148 * @param $primaryKeys Primary key array to set
151 public function setPrimaryKeyCombined (array $primaryKeys) {
152 $this->primaryKeys = $primaryKeys;
156 * Getter for primary keys
158 * @return $primaryKeys Primary key array
160 public final function getPrimaryKeys () {
162 return $this->primaryKeys;
166 * Setter for search instance
168 * @param $searchInstance Searchable criteria instance
171 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
172 $this->searchInstance = $searchInstance;
176 * Getter for search instance
178 * @return $searchInstance Searchable criteria instance
180 public final function getSearchInstance () {
181 return $this->searchInstance;
185 * Getter for unique key value
187 * @return $uniqueValue Value of the unique key
189 public final function getUniqueValue () {
190 // Get primary key(s) first
191 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
192 $primaryKeys = $this->getPrimaryKeys();
195 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
198 * If this is not set, this could mean a badly written frontend as
199 * tables should always have a primary key.
201 if (count($primaryKeys) > 0) {
203 * Init return value, this can be put all together without any
204 * separator as it only aids as a "unique value" for generating the
209 // Combination set, so get all
210 foreach ($primaryKeys as $primaryKey) {
212 $return .= trim($this->getCriteriaElemnent($primaryKey));
216 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
220 } elseif (!empty($primaryKey)) {
221 // Return primary key
224 // @TODO Issue a warning
225 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 ...');
228 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
231 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
234 if (empty($uniqueKey)) {
235 // Bad news, nothing is "unique" by design for this table
236 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
245 * Getter for primary key or unique key if not set
247 * @return $primaryKey Primary key or unique key if not set
249 public final function getPrimaryKey () {
250 // Get primary key by default
251 $primaryKey = $this->primaryKey;
253 if (empty($primaryKey)) {
255 $primaryKey = $this->getUniqueKey();