3 namespace CoreFramework\Criteria\DataSet;
6 * A set of data storeable in databases
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
31 private $tableName = '';
36 private $uniqueKey = '';
41 private $primaryKey = '';
46 private $primaryKeys = array();
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
59 * Creates an instance of this criteria
61 * @param $tableName Name of the table
62 * @return $criteriaInstance An instance of this criteria
64 public static final function createDataSetCriteria ($tableName) {
66 $criteriaInstance = new DataSetCriteria();
69 $criteriaInstance->setTableName($tableName);
71 // Return the instance
72 return $criteriaInstance;
76 * Setter for table name
78 * @param $tableName Name of the table to set
81 public final function setTableName ($tableName) {
82 $this->tableName = (string) $tableName;
86 * Getter for table name
88 * @return $tableName Name of the table to set
90 public final function getTableName () {
91 return $this->tableName;
95 * Setter for unique key
97 * @param $uniqueKey Column to use as unique key
100 public final function setUniqueKey ($uniqueKey) {
101 $this->uniqueKey = (string) $uniqueKey;
105 * Getter for unique key
107 * @return $uniqueKey Column to use as unique key
109 public final function getUniqueKey () {
110 return $this->uniqueKey;
114 * Getter for unique key value
116 * @return $uniqueValue Value of the unique key
118 public final function getUniqueValue () {
119 // Get primary key(s) first
120 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
121 $primaryKeys = $this->getPrimaryKeys();
124 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
127 * If this is not set, this could mean a badly written frontend as
128 * tables should always have a primary key.
130 if (count($primaryKeys) > 0) {
132 * Init return value, this can be put all together without any
133 * separator as it only aids as a "unique value" for generating the
138 // Combination set, so get all
139 foreach ($primaryKeys as $primaryKey) {
141 $return .= trim($this->getCriteriaElemnent($primaryKey));
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
149 } elseif (!empty($primaryKey)) {
150 // Return primary key
153 // @TODO Issue a warning
154 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
157 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
163 if (empty($uniqueKey)) {
164 // Bad news, nothing is "unique" by design for this table
165 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
174 * Getter for primary key or unique key if not set
176 * @return $primaryKey Primary key or unique key if not set
178 public final function getPrimaryKey () {
179 // Get primary key by default
180 $primaryKey = $this->primaryKey;
182 if (empty($primaryKey)) {
184 $primaryKey = $this->getUniqueKey();
192 * Setter for primary key
194 * @param $primaryKey Primary key to set
197 public final function setPrimaryKey ($primaryKey) {
198 $this->primaryKey = (string) $primaryKey;
202 * Setter for primary key array
204 * @param $primaryKeys Primary key array to set
207 public function setPrimaryKeyCombined (array $primaryKeys) {
208 $this->primaryKeys = $primaryKeys;
212 * Getter for primary keys
214 * @return $primaryKeys Primary key array
216 public final function getPrimaryKeys () {
218 return $this->primaryKeys;