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\Generic\FrameworkInterface;
12 use \BadMethodCallException;
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 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: tableName=%s - CALLED!', $tableName));
83 if (empty($tableName)) {
85 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
89 $criteriaInstance = new DataSetCriteria();
92 $criteriaInstance->setTableName($tableName);
94 // Return the instance
95 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString()));
96 return $criteriaInstance;
100 * Setter for table name
102 * @param $tableName Name of the table to set
105 public final function setTableName (string $tableName) {
106 $this->tableName = $tableName;
110 * Getter for table name
112 * @return $tableName Name of the table to set
114 public final function getTableName () {
115 return $this->tableName;
119 * Setter for unique key
121 * @param $uniqueKey Column to use as unique key
124 public final function setUniqueKey (string $uniqueKey) {
125 $this->uniqueKey = $uniqueKey;
129 * Getter for unique key
131 * @return $uniqueKey Column to use as unique key
133 public final function getUniqueKey () {
134 return $this->uniqueKey;
138 * Setter for primary key
140 * @param $primaryKey Primary key to set
143 public final function setPrimaryKey (string $primaryKey) {
144 $this->primaryKey = $primaryKey;
148 * Setter for primary key array
150 * @param $primaryKeys Primary key array to set
153 public function setPrimaryKeyCombined (array $primaryKeys) {
154 $this->primaryKeys = $primaryKeys;
158 * Getter for primary keys
160 * @return $primaryKeys Primary key array
162 public final function getPrimaryKeys () {
164 return $this->primaryKeys;
168 * Setter for search instance
170 * @param $searchInstance Searchable criteria instance
173 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
174 $this->searchInstance = $searchInstance;
178 * Getter for search instance
180 * @return $searchInstance Searchable criteria instance
182 public final function getSearchInstance () {
183 return $this->searchInstance;
187 * Getter for unique key value
189 * @return $uniqueValue Value of the unique key
190 * @throws BadMethodCallException If no primary and no unique key was found
192 public final function getUniqueValue () {
193 // Get primary key(s) first
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
195 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
196 $primaryKeys = $this->getPrimaryKeys();
199 * If this is not set, this could mean a badly written frontend as
200 * tables should always have a primary key.
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: this->tableName=%s,primaryKey=%s,primaryKeys()=%d', $this->getTableName(), $primaryKey, count($primaryKeys)));
203 if (count($primaryKeys) > 0) {
205 * Init return value, this can be put all together without any
206 * separator as it only aids as a "unique value" for generating the
211 // Combination set, so get all
212 foreach ($primaryKeys as $primaryKeyPart) {
214 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKeyPart=%s', $primaryKeyPart));
215 $return .= trim($this->getCriteriaElemnent($primaryKeyPart));
221 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: return=%s - EXIT!', $return));
223 } elseif (!empty($primaryKey)) {
224 // Return primary key
225 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));
229 self::createDebugInstance(__CLASS__, __LINE__)->warningMessage('DATA-SET-CRITERIA: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
232 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
235 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s', $uniqueKey));
236 if (empty($uniqueKey)) {
237 // Bad news, nothing is "unique" by design for this table
238 throw new BadMethodCallException(sprintf('Table %s has both no primary and unique key, but %s was called. Please fix your table.', $this->getTableName(), __METHOD__), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
242 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s - EXIT!', $uniqueKey));
248 * Getter for primary key or unique key if not set
250 * @return $primaryKey Primary key or unique key if not set
252 public final function getPrimaryKey () {
253 // Get primary key by default
254 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
255 $primaryKey = $this->primaryKey;
257 // Is it still empty?
258 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s', $primaryKey));
259 if (empty($primaryKey)) {
261 $primaryKey = $this->getUniqueKey();
265 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));