class_DatabaseResult.php

Go to the documentation of this file.
00001 <?php
00024 class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, UpdateableResult, SeekableIterator {
00025         // Exception constants
00026         const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0;
00027         const EXCEPTION_RESULT_UPDATE_FAILED    = 0x1c1;
00028 
00032         private $currentPos = -1;
00033 
00037         private $currentRow = null;
00038 
00042         private $resultArray = array();
00043 
00047         private $outDated = array();
00048 
00052         private $affectedRows = 0;
00053 
00057         private $foundValue = "";
00058 
00064         protected function __construct () {
00065                 // Call parent constructor
00066                 parent::__construct(__CLASS__);
00067 
00068                 // Clean up a little
00069                 $this->removeNumberFormaters();
00070                 $this->removeSystemArray();
00071         }
00072 
00079         public final static function createDatabaseResult (array $resultArray) {
00080                 // Get a new instance
00081                 $resultInstance = new DatabaseResult();
00082 
00083                 // Set the result array
00084                 $resultInstance->setResultArray($resultArray);
00085 
00086                 // Return the instance
00087                 return $resultInstance;
00088         }
00089 
00096         protected final function setResultArray (array $resultArray) {
00097                 $this->resultArray = $resultArray;
00098         }
00099 
00106         private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
00107                 // Get the current entry key
00108                 $entryKey = $this->key();
00109 
00110                 // Now get the update criteria array and update all entries
00111                 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
00112                         // Update data
00113                         $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
00114 
00115                         // Mark it as out-dated
00116                         $this->outDated[$criteriaKey] = 1;
00117                 } // END - foreach
00118         }
00119 
00126         public function next () {
00127                 // Default is not valid
00128                 $nextValid = false;
00129 
00130                 // Is the result valid?
00131                 if ($this->valid()) {
00132                         // Next entry found, so count one up and cache it
00133                         $this->currentPos++;
00134                         $this->currentRow = $this->resultArray['rows'][$this->currentPos];
00135                         $nextValid = true;
00136                 } // END - if
00137 
00138                 // Return the result
00139                 return $nextValid;
00140         }
00141 
00148         public function seek ($index) {
00149                 // Rewind to beginning
00150                 $this->rewind();
00151 
00152                 // Search for the entry
00153                 while ($this->currentPos < $index && ($this->valid())) {
00154                         // Continue on
00155                         $this->next();
00156                 } // END - while
00157         }
00158 
00164         public function current () {
00165                 // Default is not found
00166                 $current = null;
00167 
00168                 // Does the current enty exist?
00169                 if (isset($this->resultArray['rows'][$this->currentPos])) {
00170                         // Then get it
00171                         $current = $this->resultArray['rows'][$this->currentPos];
00172                 } // END - if
00173 
00174                 // Return the result
00175                 return $current;
00176         }
00177 
00183         public function valid () {
00184                 // By default nothing is valid
00185                 $isValid = false;
00186 
00187                 // Check if 
00188                 if (($this->ifStatusIsOkay()) && (isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
00189                         // All fine!
00190                         $isValid = true;
00191                 } // END - if
00192 
00193                 // Return the result
00194                 return $isValid;
00195         }
00196 
00202         public function ifStatusIsOkay () {
00203                 return ((isset($this->resultArray['status'])) && ($this->resultArray['status'] === "ok"));
00204         }
00205 
00211         public function key () {
00212                 return $this->currentPos;
00213         }
00214 
00220         public function rewind () {
00221                 $this->currentPos = -1;
00222                 $this->currentRow = array();
00223         }
00224 
00232         public function searchEntry (LocalSearchCriteria $criteriaInstance) {
00233                 die(__METHOD__.": Unfinished!");
00234         }
00235 
00244         public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
00245                 // Rewind the pointer
00246                 $this->rewind();
00247 
00248                 // Get search criteria
00249                 $searchInstance = $criteriaInstance->getSearchInstance();
00250 
00251                 // And start looking for the result
00252                 $foundEntries = 0;
00253                 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
00254                         // Get next entry
00255                         $this->next();
00256                         $currentEntry = $this->current();
00257 
00258                         // Is this entry found?
00259                         if ($searchInstance->ifEntryMatches($currentEntry)) {
00260                                 // Update this entry
00261                                 $this->updateCurrentEntryByCriteria($criteriaInstance);
00262 
00263                                 // Count one up
00264                                 $foundEntries++;
00265                         } // END - if
00266                 } // END - while
00267 
00268                 // Set affected rows
00269                 $this->setAffectedRows($foundEntries);
00270 
00271                 // If no entry is found/updated throw an exception
00272                 if ($foundEntries == 0) {
00273                         // Throw an exception here
00274                         throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
00275                 } // END - if
00276 
00277                 // Set search instance
00278                 $this->setSearchInstance($searchInstance);
00279         }
00280 
00287         public final function setAffectedRows ($rows) {
00288                 $this->affectedRows = $rows;
00289         }
00290 
00296         public final function getAffectedRows () {
00297                 return $this->affectedRows;
00298         }
00299 
00305         public final function getFoundValue () {
00306                 return $this->foundValue;
00307         }
00308 
00314         public function ifDataNeedsFlush () {
00315                 $needsUpdate = (count($this->outDated) > 0);
00316                 return $needsUpdate;
00317         }
00318 
00325         public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
00326                 // Walk only through out-dated columns
00327                 foreach ($this->outDated as $key => $dummy) {
00328                         // Does this key exist?
00329                         //* DEBUG: */ echo "outDated: {$key}<br />\n";
00330                         if ($this->find($key)) {
00331                                 // Then update it
00332                                 $criteriaInstance->addCriteria($key, $this->getFoundValue());
00333                         } // END - if
00334                 } // END - foreach
00335         }
00336 
00343         public function find ($key) {
00344                 // By default nothing is found
00345                 $found = false;
00346 
00347                 // Rewind the pointer
00348                 $this->rewind();
00349 
00350                 // Walk through all entries
00351                 while ($this->valid()) {
00352                         // Advance to next entry
00353                         $this->next();
00354 
00355                         // Get the whole array
00356                         $currentEntry = $this->current();
00357 
00358                         // Is the element there?
00359                         if (isset($currentEntry[$key])) {
00360                                 // Okay, found!
00361                                 $found = true;
00362 
00363                                 // So "cache" it
00364                                 $this->foundValue = $currentEntry[$key];
00365 
00366                                 // And stop searching
00367                                 break;
00368                         } // END - if
00369                 } // END - while
00370 
00371                 // Return the result
00372                 return $found;
00373         }
00374 
00385         public function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack) {
00386                 // By default nothing is found
00387                 $indexValue = 0;
00388 
00389                 // Is the element in result itself found?
00390                 if ($this->find($databaseColumn)) {
00391                         // Use this value
00392                         $indexValue = $this->getFoundValue();
00393                 } elseif ($this->find($wrapperInstance->getIndexKey())) {
00394                         // Use this value
00395                         $indexValue = $this->getFoundValue();
00396                 }
00397 
00398                 // Set the index
00399                 call_user_func_array($callBack, array($indexValue));
00400         }
00401 }
00402 
00403 // [EOF]
00404 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6