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 * A set of data storeable in databases
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
37 private $tableName = '';
42 private $uniqueKey = '';
47 private $primaryKey = '';
52 private $primaryKeys = [];
55 * Search criteria instance
57 private $searchInstance = NULL;
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates an instance of this criteria
72 * @param $tableName Name of the table
73 * @return $criteriaInstance An instance of this criteria
75 public static final function createDataSetCriteria (string $tableName) {
77 $criteriaInstance = new DataSetCriteria();
80 $criteriaInstance->setTableName($tableName);
82 // Return the instance
83 return $criteriaInstance;
87 * Setter for table name
89 * @param $tableName Name of the table to set
92 public final function setTableName (string $tableName) {
93 $this->tableName = $tableName;
97 * Getter for table name
99 * @return $tableName Name of the table to set
101 public final function getTableName () {
102 return $this->tableName;
106 * Setter for unique key
108 * @param $uniqueKey Column to use as unique key
111 public final function setUniqueKey (string $uniqueKey) {
112 $this->uniqueKey = $uniqueKey;
116 * Getter for unique key
118 * @return $uniqueKey Column to use as unique key
120 public final function getUniqueKey () {
121 return $this->uniqueKey;
125 * Setter for primary key
127 * @param $primaryKey Primary key to set
130 public final function setPrimaryKey (string $primaryKey) {
131 $this->primaryKey = $primaryKey;
135 * Setter for primary key array
137 * @param $primaryKeys Primary key array to set
140 public function setPrimaryKeyCombined (array $primaryKeys) {
141 $this->primaryKeys = $primaryKeys;
145 * Getter for primary keys
147 * @return $primaryKeys Primary key array
149 public final function getPrimaryKeys () {
151 return $this->primaryKeys;
155 * Setter for search instance
157 * @param $searchInstance Searchable criteria instance
160 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
161 $this->searchInstance = $searchInstance;
165 * Getter for search instance
167 * @return $searchInstance Searchable criteria instance
169 public final function getSearchInstance () {
170 return $this->searchInstance;
174 * Getter for unique key value
176 * @return $uniqueValue Value of the unique key
178 public final function getUniqueValue () {
179 // Get primary key(s) first
180 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
181 $primaryKeys = $this->getPrimaryKeys();
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
187 * If this is not set, this could mean a badly written frontend as
188 * tables should always have a primary key.
190 if (count($primaryKeys) > 0) {
192 * Init return value, this can be put all together without any
193 * separator as it only aids as a "unique value" for generating the
198 // Combination set, so get all
199 foreach ($primaryKeys as $primaryKey) {
201 $return .= trim($this->getCriteriaElemnent($primaryKey));
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
209 } elseif (!empty($primaryKey)) {
210 // Return primary key
213 // @TODO Issue a warning
214 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 ...');
217 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
223 if (empty($uniqueKey)) {
224 // Bad news, nothing is "unique" by design for this table
225 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
234 * Getter for primary key or unique key if not set
236 * @return $primaryKey Primary key or unique key if not set
238 public final function getPrimaryKey () {
239 // Get primary key by default
240 $primaryKey = $this->primaryKey;
242 if (empty($primaryKey)) {
244 $primaryKey = $this->getUniqueKey();