X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcriteria%2Fdataset%2Fclass_DataSetCriteria.php;h=2745239433474cf09ec96c9caced5bdb72b26e6b;hp=da22ede83488672ab502bffcab6ab4af24b21395;hb=5203f9bd014ad46fbc7ee54e7223dcd46e14e3b4;hpb=84e2207412d3c6ea9f940a83b2cdd4503509808a diff --git a/inc/classes/main/criteria/dataset/class_DataSetCriteria.php b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php index da22ede8..27452394 100644 --- a/inc/classes/main/criteria/dataset/class_DataSetCriteria.php +++ b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php @@ -2,12 +2,11 @@ /** * A set of data storeable in databases * - * @see DatabaseFrontendInterface - An interface for database frontends (front-end to the application) - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @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 @@ -28,11 +27,6 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { */ private $tableName = ''; - /** - * Table columns (criteria) to store - */ - private $tableColumns = array(); - /** * Unique key */ @@ -43,12 +37,17 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { */ private $primaryKey = ''; + /** + * Primary keys + */ + private $primaryKeys = array(); + /** * Protected constructor * * @return void */ - protected function __construct() { + protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } @@ -59,7 +58,7 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { * @param $tableName Name of the table * @return $criteriaInstance An instance of this criteria */ - public final static function createDataSetCriteria ($tableName) { + public static final function createDataSetCriteria ($tableName) { // Get a new instance $criteriaInstance = new DataSetCriteria(); @@ -70,30 +69,6 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { return $criteriaInstance; } - /** - * Add criteria - * - * @param $criteriaKey Criteria key - * @param $criteriaValue Criteria value - * @return void - */ - public function addCriteria ($criteriaKey, $criteriaValue) { - $this->tableColumns[(string) $criteriaKey] = $criteriaValue; - } - - /** - * Add configured criteria - * - * @param $criteriaKey Criteria key - * @param $configEntry Configuration entry - * @return void - */ - public function addConfiguredCriteria ($criteriaKey, $configEntry) { - // Add configuration entry as criteria - $value = $this->getConfigInstance()->getConfigEntry($configEntry); - $this->addCriteria($criteriaKey, $value); - } - /** * Setter for table name * @@ -138,16 +113,58 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { * @return $uniqueValue Value of the unique key */ public final function getUniqueValue () { - return $this->tableColumns[$this->getUniqueKey()]; - } - - /** - * Getter for criteria array - * - * @return $tableColumns - */ - public final function getCriteriaArray () { - return $this->tableColumns; + // Get primary key(s) first + $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey())); + $primaryKeys = $this->getPrimaryKeys(); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->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__)->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__)->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__)->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::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.'); + } else { + // Return unique key + return $uniqueKey; + } + } } /** @@ -177,6 +194,26 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria { 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; + } } // [EOF]