]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/result/class_DatabaseResult.php
Typos fixed and special command resolver are now possible
[shipsimu.git] / inc / classes / main / result / class_DatabaseResult.php
index 45412dc90f34ea45191e114f2bc7c906dca755c9..94fe3b5deff55a2f96fac4aaba5bde59197b32a0 100644 (file)
@@ -51,6 +51,11 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
         */
        private $affectedRows = 0;
 
+       /**
+        * Found value
+        */
+       private $foundValue = "";
+
        /**
         * Protected constructor
         *
@@ -60,12 +65,6 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                // Call parent constructor
                parent::__construct(__CLASS__);
 
-               // Set part description
-               $this->setObjectDescription("Database result");
-
-               // Create unique ID number
-               $this->generateUniqueId();
-
                // Clean up a little
                $this->removeNumberFormaters();
                $this->removeSystemArray();
@@ -109,7 +108,7 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                $entryKey = $this->key();
 
                // Now get the update criteria array and update all entries
-               foreach ($updateInstance->getUpdateCriteria() as $criteriaKey=>$criteriaValue) {
+               foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
                        // Update data
                        $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
 
@@ -120,7 +119,7 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
 
        /**
         * "Iterator" method next() to advance to the next valid entry. This method
-        * does also check if the result is invalid
+        * does also check if result is invalid
         *
         * @return      $nextValid      Wether the next entry is valid
         */
@@ -129,14 +128,11 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                $nextValid = false;
 
                // Is the result valid?
-               if ($this->resultArray['status'] === "ok") {
-                       // The status is fine so let's have a look for the next entry
-                       if ($this->valid()) {
-                               // Next entry found, so count one up and cache it
-                               $this->currentPos++;
-                               $this->currentRow = $this->resultArray['rows'][$this->currentPos];
-                               $nextValid = true;
-                       } // END - if
+               if ($this->valid()) {
+                       // Next entry found, so count one up and cache it
+                       $this->currentPos++;
+                       $this->currentRow = $this->resultArray['rows'][$this->currentPos];
+                       $nextValid = true;
                } // END - if
 
                // Return the result
@@ -189,7 +185,7 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                $isValid = false;
 
                // Check if 
-               if ((isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
+               if (($this->ifStatusIsOkay()) && (isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
                        // All fine!
                        $isValid = true;
                } // END - if
@@ -198,6 +194,15 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                return $isValid;
        }
 
+       /**
+        * Determines wether the status of the query was fine ("ok")
+        *
+        * @return      $ifStatusOkay   Wether the status of the query was okay
+        */
+       public function ifStatusIsOkay () {
+               return ((isset($this->resultArray['status'])) && ($this->resultArray['status'] === "ok"));
+       }
+
        /**
         * Gets the current key of iteration
         *
@@ -208,19 +213,21 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
        }
 
        /**
-        * Rewind to the beginning
+        * Rewind to the beginning and clear array $currentRow
         *
         * @return      void
         */
        public function rewind () {
                $this->currentPos = -1;
+               $this->currentRow = array();
        }
 
        /**
-        * Searches for an entry in the data result and returns it
+        * Searches for an entry in data result and returns it
         *
         * @param       $criteriaInstance       The criteria to look inside the data set
         * @return      $result                         Found result entry
+        * @todo        0% done
         */
        public function searchEntry (LocalSearchCriteria $criteriaInstance) {
                die(__METHOD__.": Unfinished!");
@@ -290,6 +297,15 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                return $this->affectedRows;
        }
 
+       /**
+        * Getter for found value of previous found() call
+        *
+        * @return      $foundValue             Found value of previous found() call
+        */
+       public final function getFoundValue () {
+               return $this->foundValue;
+       }
+
        /**
         * Checks wether we have out-dated entries or not
         *
@@ -307,25 +323,80 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
         * @return      void
         */
        public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
+               // Walk only through out-dated columns
+               foreach ($this->outDated as $key => $dummy) {
+                       // Does this key exist?
+                       //* DEBUG: */ echo "outDated: {$key}<br />\n";
+                       if ($this->find($key)) {
+                               // Then update it
+                               $criteriaInstance->addCriteria($key, $this->getFoundValue());
+                       } // END - if
+               } // END - foreach
+       }
+
+       /**
+        * Find a key inside the result array
+        *
+        * @param       $key    The key we shall find
+        * @return      $found  Wether the key was found or not
+        */
+       public function find ($key) {
+               // By default nothing is found
+               $found = false;
+
                // Rewind the pointer
                $this->rewind();
 
                // Walk through all entries
                while ($this->valid()) {
-                       // Get next entry
+                       // Advance to next entry
                        $this->next();
+
+                       // Get the whole array
                        $currentEntry = $this->current();
 
-                       // Walk only through out-dated columns
-                       foreach ($this->outDated as $key=>$dummy) {
-                               // Does this key exist?
-                               //* DEBUG: */ echo "outDated: {$key}<br />\n";
-                               if (isset($currentEntry[$key])) {
-                                       // Then update it
-                                       $criteriaInstance->addCriteria($key, $currentEntry[$key]);
-                               } // END - foreach
-                       } // END - foreach
+                       // Is the element there?
+                       if (isset($currentEntry[$key])) {
+                               // Okay, found!
+                               $found = true;
+
+                               // So "cache" it
+                               $this->foundValue = $currentEntry[$key];
+
+                               // And stop searching
+                               break;
+                       } // END - if
                } // END - while
+
+               // Return the result
+               return $found;
+       }
+
+       /**
+        * Solver for result index value with call-back method
+        *
+        * @param       $databaseColumn         Database column where the index might be found
+        * @param       $wrapperInstance        The wrapper instance to ask for array element
+        * @para        $callBack                       Call-back object for setting the index;
+        *                                                              0=object instance,1=method name
+        * @return      void
+4       * @todo        Find a caching way without modifying the result array
+        */
+       public function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack) {
+               // By default nothing is found
+               $indexValue = 0;
+
+               // Is the element in result itself found?
+               if ($this->find($databaseColumn)) {
+                       // Use this value
+                       $indexValue = $this->getFoundValue();
+               } elseif ($this->find($wrapperInstance->getIndexKey())) {
+                       // Use this value
+                       $indexValue = $this->getFoundValue();
+               }
+
+               // Set the index
+               call_user_func_array($callBack, array($indexValue));
        }
 }