3 * A set of data storeable in databases
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
28 private $tableName = '';
33 private $uniqueKey = '';
38 private $primaryKey = '';
43 private $primaryKeys = array();
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this criteria
58 * @param $tableName Name of the table
59 * @return $criteriaInstance An instance of this criteria
61 public static final function createDataSetCriteria ($tableName) {
63 $criteriaInstance = new DataSetCriteria();
66 $criteriaInstance->setTableName($tableName);
68 // Return the instance
69 return $criteriaInstance;
73 * Setter for table name
75 * @param $tableName Name of the table to set
78 public final function setTableName ($tableName) {
79 $this->tableName = (string) $tableName;
83 * Getter for table name
85 * @return $tableName Name of the table to set
87 public final function getTableName () {
88 return $this->tableName;
92 * Setter for unique key
94 * @param $uniqueKey Column to use as unique key
97 public final function setUniqueKey ($uniqueKey) {
98 $this->uniqueKey = (string) $uniqueKey;
102 * Getter for unique key
104 * @return $uniqueKey Column to use as unique key
106 public final function getUniqueKey () {
107 return $this->uniqueKey;
111 * Getter for unique key value
113 * @return $uniqueValue Value of the unique key
115 public final function getUniqueValue () {
116 // Get primary key(s) first
117 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
118 $primaryKeys = $this->getPrimaryKeys();
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
124 * If this is not set, this could mean a badly written frontend as
125 * tables should always have a primary key.
127 if (count($primaryKeys) > 0) {
129 * Init return value, this can be put all together without any
130 * separator as it only aids as a "unique value" for generating the
135 // Combination set, so get all
136 foreach ($primaryKeys as $primaryKey) {
138 $return .= trim($this->getCriteriaElemnent($primaryKey));
142 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
146 } elseif (!empty($primaryKey)) {
147 // Return primary key
150 // @TODO Issue a warning
151 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
154 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
157 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
160 if (empty($uniqueKey)) {
161 // Bad news, nothing is "unique" by design for this table
162 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
171 * Getter for primary key or unique key if not set
173 * @return $primaryKey Primary key or unique key if not set
175 public final function getPrimaryKey () {
176 // Get primary key by default
177 $primaryKey = $this->primaryKey;
179 if (empty($primaryKey)) {
181 $primaryKey = $this->getUniqueKey();
189 * Setter for primary key
191 * @param $primaryKey Primary key to set
194 public final function setPrimaryKey ($primaryKey) {
195 $this->primaryKey = (string) $primaryKey;
199 * Setter for primary key array
201 * @param $primaryKeys Primary key array to set
204 public function setPrimaryKeyCombined (array $primaryKeys) {
205 $this->primaryKeys = $primaryKeys;
209 * Getter for primary keys
211 * @return $primaryKeys Primary key array
213 public final function getPrimaryKeys () {
215 return $this->primaryKeys;