3 namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
8 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
11 * A set of data storeable in databases
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 <<<<<<< HEAD:framework/main/classes/criteria/dataset/class_DataSetCriteria.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/criteria/dataset/class_DataSetCriteria.php
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 = array();
58 * Protected constructor
62 protected function __construct () {
63 // Call parent constructor
64 parent::__construct(__CLASS__);
68 * Creates an instance of this criteria
70 * @param $tableName Name of the table
71 * @return $criteriaInstance An instance of this criteria
73 public static final function createDataSetCriteria ($tableName) {
75 $criteriaInstance = new DataSetCriteria();
78 $criteriaInstance->setTableName($tableName);
80 // Return the instance
81 return $criteriaInstance;
85 * Setter for table name
87 * @param $tableName Name of the table to set
90 public final function setTableName ($tableName) {
91 $this->tableName = (string) $tableName;
95 * Getter for table name
97 * @return $tableName Name of the table to set
99 public final function getTableName () {
100 return $this->tableName;
104 * Setter for unique key
106 * @param $uniqueKey Column to use as unique key
109 public final function setUniqueKey ($uniqueKey) {
110 $this->uniqueKey = (string) $uniqueKey;
114 * Getter for unique key
116 * @return $uniqueKey Column to use as unique key
118 public final function getUniqueKey () {
119 return $this->uniqueKey;
123 * Getter for unique key value
125 * @return $uniqueValue Value of the unique key
127 public final function getUniqueValue () {
128 // Get primary key(s) first
129 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
130 $primaryKeys = $this->getPrimaryKeys();
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
136 * If this is not set, this could mean a badly written frontend as
137 * tables should always have a primary key.
139 if (count($primaryKeys) > 0) {
141 * Init return value, this can be put all together without any
142 * separator as it only aids as a "unique value" for generating the
147 // Combination set, so get all
148 foreach ($primaryKeys as $primaryKey) {
150 $return .= trim($this->getCriteriaElemnent($primaryKey));
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
158 } elseif (!empty($primaryKey)) {
159 // Return primary key
162 // @TODO Issue a warning
163 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
166 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
172 if (empty($uniqueKey)) {
173 // Bad news, nothing is "unique" by design for this table
174 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
183 * Getter for primary key or unique key if not set
185 * @return $primaryKey Primary key or unique key if not set
187 public final function getPrimaryKey () {
188 // Get primary key by default
189 $primaryKey = $this->primaryKey;
191 if (empty($primaryKey)) {
193 $primaryKey = $this->getUniqueKey();
201 * Setter for primary key
203 * @param $primaryKey Primary key to set
206 public final function setPrimaryKey ($primaryKey) {
207 $this->primaryKey = (string) $primaryKey;
211 * Setter for primary key array
213 * @param $primaryKeys Primary key array to set
216 public function setPrimaryKeyCombined (array $primaryKeys) {
217 $this->primaryKeys = $primaryKeys;
221 * Getter for primary keys
223 * @return $primaryKeys Primary key array
225 public final function getPrimaryKeys () {
227 return $this->primaryKeys;