X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fresult%2Fclass_DatabaseResult.php;h=45412dc90f34ea45191e114f2bc7c906dca755c9;hb=4f8c6dfcdc9b0a11ef041d0658f98d10a85e87fa;hp=c6a0b1149a9d3e1907e019f51711ab0c68e5c74e;hpb=fd80d47afc96ae0c0759530800051a0f07eb9c92;p=shipsimu.git diff --git a/inc/classes/main/result/class_DatabaseResult.php b/inc/classes/main/result/class_DatabaseResult.php index c6a0b11..45412dc 100644 --- a/inc/classes/main/result/class_DatabaseResult.php +++ b/inc/classes/main/result/class_DatabaseResult.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -19,9 +19,13 @@ * 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 . + * along with this program. If not, see . */ -class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, SeekableIterator { +class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, UpdateableResult, SeekableIterator { + // Exception constants + const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0; + const EXCEPTION_RESULT_UPDATE_FAILED = 0x1c1; + /** * Current position in array */ @@ -37,6 +41,16 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Se */ private $resultArray = array(); + /** + * Array of out-dated entries + */ + private $outDated = array(); + + /** + * Affected rows + */ + private $affectedRows = 0; + /** * Protected constructor * @@ -84,6 +98,26 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Se $this->resultArray = $resultArray; } + /** + * Updates the current entry by given update criteria + * + * @param $updateInstance An instance of an Updateable criteria + * @return void + */ + private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) { + // Get the current entry key + $entryKey = $this->key(); + + // Now get the update criteria array and update all entries + foreach ($updateInstance->getUpdateCriteria() as $criteriaKey=>$criteriaValue) { + // Update data + $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue; + + // Mark it as out-dated + $this->outDated[$criteriaKey] = 1; + } // END - foreach + } + /** * "Iterator" method next() to advance to the next valid entry. This method * does also check if the result is invalid @@ -179,7 +213,7 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Se * @return void */ public function rewind () { - $this->currentPos = 0; + $this->currentPos = -1; } /** @@ -189,7 +223,109 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Se * @return $result Found result entry */ public function searchEntry (LocalSearchCriteria $criteriaInstance) { - die("OK"); + die(__METHOD__.": Unfinished!"); + } + + /** + * Adds an update request to the database result for writing it to the + * database layer + * + * @param $criteriaInstance An instance of a updateable criteria + * @return void + * @throws ResultUpdateException If no result was updated + */ + public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) { + // Rewind the pointer + $this->rewind(); + + // Get search criteria + $searchInstance = $criteriaInstance->getSearchInstance(); + + // And start looking for the result + $foundEntries = 0; + while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) { + // Get next entry + $this->next(); + $currentEntry = $this->current(); + + // Is this entry found? + if ($searchInstance->ifEntryMatches($currentEntry)) { + // Update this entry + $this->updateCurrentEntryByCriteria($criteriaInstance); + + // Count one up + $foundEntries++; + } // END - if + } // END - while + + // Set affected rows + $this->setAffectedRows($foundEntries); + + // If no entry is found/updated throw an exception + if ($foundEntries == 0) { + // Throw an exception here + throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED); + } // END - if + + // Set search instance + $this->setSearchInstance($searchInstance); + } + + /** + * Setter for affected rows + * + * @param $rows Number of affected rows + * @return void + */ + public final function setAffectedRows ($rows) { + $this->affectedRows = $rows; + } + + /** + * Getter for affected rows + * + * @return $rows Number of affected rows + */ + public final function getAffectedRows () { + return $this->affectedRows; + } + + /** + * Checks wether we have out-dated entries or not + * + * @return $needsUpdate Wether we have out-dated entries + */ + public function ifDataNeedsFlush () { + $needsUpdate = (count($this->outDated) > 0); + return $needsUpdate; + } + + /** + * Adds registration elements to a given dataset instance + * + * @param $criteriaInstance An instance of a storeable criteria + * @return void + */ + public function addElementsToDataSet (StoreableCriteria $criteriaInstance) { + // Rewind the pointer + $this->rewind(); + + // Walk through all entries + while ($this->valid()) { + // Get next entry + $this->next(); + $currentEntry = $this->current(); + + // Walk only through out-dated columns + foreach ($this->outDated as $key=>$dummy) { + // Does this key exist? + //* DEBUG: */ echo "outDated: {$key}
\n"; + if (isset($currentEntry[$key])) { + // Then update it + $criteriaInstance->addCriteria($key, $currentEntry[$key]); + } // END - foreach + } // END - foreach + } // END - while } }