3 namespace CoreFramework\Criteria\DataSet;
5 // Import framework stuff
6 use CoreFramework\Criteria\BaseCriteria;
9 * A set of data storeable in databases
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
34 private $tableName = '';
39 private $uniqueKey = '';
44 private $primaryKey = '';
49 private $primaryKeys = array();
52 * Protected constructor
56 protected function __construct () {
57 // Call parent constructor
58 parent::__construct(__CLASS__);
62 * Creates an instance of this criteria
64 * @param $tableName Name of the table
65 * @return $criteriaInstance An instance of this criteria
67 public static final function createDataSetCriteria ($tableName) {
69 $criteriaInstance = new DataSetCriteria();
72 $criteriaInstance->setTableName($tableName);
74 // Return the instance
75 return $criteriaInstance;
79 * Setter for table name
81 * @param $tableName Name of the table to set
84 public final function setTableName ($tableName) {
85 $this->tableName = (string) $tableName;
89 * Getter for table name
91 * @return $tableName Name of the table to set
93 public final function getTableName () {
94 return $this->tableName;
98 * Setter for unique key
100 * @param $uniqueKey Column to use as unique key
103 public final function setUniqueKey ($uniqueKey) {
104 $this->uniqueKey = (string) $uniqueKey;
108 * Getter for unique key
110 * @return $uniqueKey Column to use as unique key
112 public final function getUniqueKey () {
113 return $this->uniqueKey;
117 * Getter for unique key value
119 * @return $uniqueValue Value of the unique key
121 public final function getUniqueValue () {
122 // Get primary key(s) first
123 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
124 $primaryKeys = $this->getPrimaryKeys();
127 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
130 * If this is not set, this could mean a badly written frontend as
131 * tables should always have a primary key.
133 if (count($primaryKeys) > 0) {
135 * Init return value, this can be put all together without any
136 * separator as it only aids as a "unique value" for generating the
141 // Combination set, so get all
142 foreach ($primaryKeys as $primaryKey) {
144 $return .= trim($this->getCriteriaElemnent($primaryKey));
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
152 } elseif (!empty($primaryKey)) {
153 // Return primary key
156 // @TODO Issue a warning
157 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
160 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
166 if (empty($uniqueKey)) {
167 // Bad news, nothing is "unique" by design for this table
168 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
177 * Getter for primary key or unique key if not set
179 * @return $primaryKey Primary key or unique key if not set
181 public final function getPrimaryKey () {
182 // Get primary key by default
183 $primaryKey = $this->primaryKey;
185 if (empty($primaryKey)) {
187 $primaryKey = $this->getUniqueKey();
195 * Setter for primary key
197 * @param $primaryKey Primary key to set
200 public final function setPrimaryKey ($primaryKey) {
201 $this->primaryKey = (string) $primaryKey;
205 * Setter for primary key array
207 * @param $primaryKeys Primary key array to set
210 public function setPrimaryKeyCombined (array $primaryKeys) {
211 $this->primaryKeys = $primaryKeys;
215 * Getter for primary keys
217 * @return $primaryKeys Primary key array
219 public final function getPrimaryKeys () {
221 return $this->primaryKeys;