* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { /** * Table name */ private $tableName = ''; /** * Unique key */ private $uniqueKey = ''; /** * Primary key */ private $primaryKey = ''; /** * Primary keys */ private $primaryKeys = array(); /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this criteria * * @param $tableName Name of the table * @return $criteriaInstance An instance of this criteria */ public static final function createDataSetCriteria ($tableName) { // Get a new instance $criteriaInstance = new DataSetCriteria(); // Set table name $criteriaInstance->setTableName($tableName); // Return the instance return $criteriaInstance; } /** * Setter for table name * * @param $tableName Name of the table to set * @return void */ public final function setTableName ($tableName) { $this->tableName = (string) $tableName; } /** * Getter for table name * * @return $tableName Name of the table to set */ public final function getTableName () { return $this->tableName; } /** * Setter for unique key * * @param $uniqueKey Column to use as unique key * @return void */ public final function setUniqueKey ($uniqueKey) { $this->uniqueKey = (string) $uniqueKey; } /** * Getter for unique key * * @return $uniqueKey Column to use as unique key */ public final function getUniqueKey () { return $this->uniqueKey; } /** * Getter for unique key value * * @return $uniqueValue Value of the unique key */ public final function getUniqueValue () { // Get primary key(s) first $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey())); $primaryKeys = $this->getPrimaryKeys(); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys)); /* * If this is not set, this could mean a badly written frontend as * tables should always have a primary key. */ if (count($primaryKeys) > 0) { /* * Init return value, this can be put all together without any * separator as it only aids as a "unique value" for generating the * "row file name". */ $return = ''; // Combination set, so get all foreach ($primaryKeys as $primaryKey) { // Add it $return .= trim($this->getCriteriaElemnent($primaryKey)); } // END - foreach // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!'); // Return it return $return; } elseif (!empty($primaryKey)) { // Return primary key return $primaryKey; } else { // @TODO Issue a warning self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...'); // Get unique key $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey())); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey); // Is it empty, too? if (empty($uniqueKey)) { // Bad news, nothing is "unique" by design for this table ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.'); } else { // Return unique key return $uniqueKey; } } } /** * Getter for primary key or unique key if not set * * @return $primaryKey Primary key or unique key if not set */ public final function getPrimaryKey () { // Get primary key by default $primaryKey = $this->primaryKey; if (empty($primaryKey)) { // Get uniqueKey $primaryKey = $this->getUniqueKey(); } // END - if // Return it return $primaryKey; } /** * Setter for primary key * * @param $primaryKey Primary key to set * @return void */ public final function setPrimaryKey ($primaryKey) { $this->primaryKey = (string) $primaryKey; } /** * Setter for primary key array * * @param $primaryKeys Primary key array to set * @return void */ public function setPrimaryKeyCombined (array $primaryKeys) { $this->primaryKeys = $primaryKeys; } /** * Getter for primary keys * * @return $primaryKeys Primary key array */ public final function getPrimaryKeys () { // Return it return $this->primaryKeys; } }