3 namespace CoreFramework\Criteria\DataSet;
5 // Import framework stuff
6 use CoreFramework\Criteria\BaseCriteria;
7 use CoreFramework\Criteria\Storing\StoreableCriteria;
10 * A set of data storeable in databases
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
35 private $tableName = '';
40 private $uniqueKey = '';
45 private $primaryKey = '';
50 private $primaryKeys = array();
53 * Protected constructor
57 protected function __construct () {
58 // Call parent constructor
59 parent::__construct(__CLASS__);
63 * Creates an instance of this criteria
65 * @param $tableName Name of the table
66 * @return $criteriaInstance An instance of this criteria
68 public static final function createDataSetCriteria ($tableName) {
70 $criteriaInstance = new DataSetCriteria();
73 $criteriaInstance->setTableName($tableName);
75 // Return the instance
76 return $criteriaInstance;
80 * Setter for table name
82 * @param $tableName Name of the table to set
85 public final function setTableName ($tableName) {
86 $this->tableName = (string) $tableName;
90 * Getter for table name
92 * @return $tableName Name of the table to set
94 public final function getTableName () {
95 return $this->tableName;
99 * Setter for unique key
101 * @param $uniqueKey Column to use as unique key
104 public final function setUniqueKey ($uniqueKey) {
105 $this->uniqueKey = (string) $uniqueKey;
109 * Getter for unique key
111 * @return $uniqueKey Column to use as unique key
113 public final function getUniqueKey () {
114 return $this->uniqueKey;
118 * Getter for unique key value
120 * @return $uniqueValue Value of the unique key
122 public final function getUniqueValue () {
123 // Get primary key(s) first
124 $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
125 $primaryKeys = $this->getPrimaryKeys();
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
131 * If this is not set, this could mean a badly written frontend as
132 * tables should always have a primary key.
134 if (count($primaryKeys) > 0) {
136 * Init return value, this can be put all together without any
137 * separator as it only aids as a "unique value" for generating the
142 // Combination set, so get all
143 foreach ($primaryKeys as $primaryKey) {
145 $return .= trim($this->getCriteriaElemnent($primaryKey));
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
153 } elseif (!empty($primaryKey)) {
154 // Return primary key
157 // @TODO Issue a warning
158 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
161 $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
167 if (empty($uniqueKey)) {
168 // Bad news, nothing is "unique" by design for this table
169 ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
178 * Getter for primary key or unique key if not set
180 * @return $primaryKey Primary key or unique key if not set
182 public final function getPrimaryKey () {
183 // Get primary key by default
184 $primaryKey = $this->primaryKey;
186 if (empty($primaryKey)) {
188 $primaryKey = $this->getUniqueKey();
196 * Setter for primary key
198 * @param $primaryKey Primary key to set
201 public final function setPrimaryKey ($primaryKey) {
202 $this->primaryKey = (string) $primaryKey;
206 * Setter for primary key array
208 * @param $primaryKeys Primary key array to set
211 public function setPrimaryKeyCombined (array $primaryKeys) {
212 $this->primaryKeys = $primaryKeys;
216 * Getter for primary keys
218 * @return $primaryKeys Primary key array
220 public final function getPrimaryKeys () {
222 return $this->primaryKeys;