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 \BadMethodCallException;
14 use \InvalidArgumentException;
17 * A set of data storeable in databases
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
42 private $tableName = '';
47 private $uniqueKey = '';
52 private $primaryKey = '';
57 private $primaryKeys = [];
60 * Search criteria instance
62 private $searchInstance = NULL;
65 * Protected constructor
69 private function __construct () {
70 // Call parent constructor
71 parent::__construct(__CLASS__);
75 * Creates an instance of this criteria
77 * @param $tableName Name of the table
78 * @return $criteriaInstance An instance of this criteria
79 * @throws InvalidArgumentException If a parameter is not valid
81 public static final function createDataSetCriteria (string $tableName) {
83 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: tableName=%s - CALLED!', $tableName));
84 if (empty($tableName)) {
86 throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
90 $criteriaInstance = new DataSetCriteria();
93 $criteriaInstance->setTableName($tableName);
95 // Return the instance
96 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString()));
97 return $criteriaInstance;
101 * Setter for table name
103 * @param $tableName Name of the table to set
106 public final function setTableName (string $tableName) {
107 $this->tableName = $tableName;
111 * Getter for table name
113 * @return $tableName Name of the table to set
115 public final function getTableName () {
116 return $this->tableName;
120 * Setter for unique key
122 * @param $uniqueKey Column to use as unique key
125 public final function setUniqueKey (string $uniqueKey) {
126 $this->uniqueKey = $uniqueKey;
130 * Getter for unique key
132 * @return $uniqueKey Column to use as unique key
134 public final function getUniqueKey () {
135 return $this->uniqueKey;
139 * Setter for primary key
141 * @param $primaryKey Primary key to set
144 public final function setPrimaryKey (string $primaryKey) {
145 $this->primaryKey = $primaryKey;
149 * Setter for primary key array
151 * @param $primaryKeys Primary key array to set
154 public function setPrimaryKeyCombined (array $primaryKeys) {
155 $this->primaryKeys = $primaryKeys;
159 * Getter for primary keys
161 * @return $primaryKeys Primary key array
163 public final function getPrimaryKeys () {
165 return $this->primaryKeys;
169 * Setter for search instance
171 * @param $searchInstance Searchable criteria instance
174 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
175 $this->searchInstance = $searchInstance;
179 * Getter for search instance
181 * @return $searchInstance Searchable criteria instance
183 public final function getSearchInstance () {
184 return $this->searchInstance;
188 * Getter for unique key value
190 * @return $uniqueValue Value of the unique key
191 * @throws BadMethodCallException If no primary and no unique key was found
193 public final function getUniqueValue () {
194 // Get primary key(s) first
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
196 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
197 $primaryKeys = $this->getPrimaryKeys();
200 * If this is not set, this could mean a badly written frontend as
201 * tables should always have a primary key.
203 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: this->tableName=%s,primaryKey=%s,primaryKeys()=%d', $this->getTableName(), $primaryKey, count($primaryKeys)));
204 if (count($primaryKeys) > 0) {
206 * Init return value, this can be put all together without any
207 * separator as it only aids as a "unique value" for generating the
212 // Combination set, so get all
213 foreach ($primaryKeys as $primaryKeyPart) {
215 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKeyPart=%s', $primaryKeyPart));
216 $return .= trim($this->getCriteriaElemnent($primaryKeyPart));
222 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: return=%s - EXIT!', $return));
224 } elseif (!empty($primaryKey)) {
225 // Return primary key
226 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));
230 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 ...');
233 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
236 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s', $uniqueKey));
237 if (empty($uniqueKey)) {
238 // Bad news, nothing is "unique" by design for this table
239 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);
243 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s - EXIT!', $uniqueKey));
249 * Getter for primary key or unique key if not set
251 * @return $primaryKey Primary key or unique key if not set
253 public final function getPrimaryKey () {
254 // Get primary key by default
255 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
256 $primaryKey = $this->primaryKey;
258 // Is it still empty?
259 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s', $primaryKey));
260 if (empty($primaryKey)) {
262 $primaryKey = $this->getUniqueKey();
266 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));