]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 3 Dec 2020 22:02:41 +0000 (23:02 +0100)
committerRoland Häder <roland@mxchange.org>
Thu, 3 Dec 2020 22:02:41 +0000 (23:02 +0100)
- fixes for currentPos calculation, needs to be done in factory method (or
  private method being invoked there)

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/database/result/class_CachedDatabaseResult.php
framework/main/interfaces/result/class_SearchableResult.php

index 2d869894a6d7e3c08ef855654dd13d7ff5bf2db0..80fe8d05df7a3904c56145727c795af655071b37 100644 (file)
@@ -102,8 +102,9 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                // Get a new instance
                $resultInstance = new CachedDatabaseResult();
 
-               // Set the result array
+               // Set the result array and reset current position
                $resultInstance->setResultArray($resultArray);
+               $resultInstance->resetCurrentPosition();
 
                // Set affected rows
                $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]));
@@ -207,10 +208,11 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         */
        public function valid () {
                // Check if all is fine ...
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: this->currentPos=' . $this->currentPos);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
                $isValid = ($this->ifStatusIsOkay() && isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos]) && isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][0]));
 
                // Return the result
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: isValid=%d - EXIT!', intval($isValid)));
                return $isValid;
        }
 
@@ -220,8 +222,13 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @return      $isValid Whether the next/rewind entry is valid
         */
        public function count () {
+               // Count rows
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: CALLED!');
+               $count = count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
+
                // Return it
-               return count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: count=%d - EXIT!', $count));
+               return $count;
        }
 
        /**
@@ -230,8 +237,12 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @return      $ifStatusOkay   Whether the status of the query was okay
         */
        public function ifStatusIsOkay () {
+               // Check all conditions
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
                $ifStatusOkay = (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS]) && $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS] === BaseDatabaseBackend::RESULT_OKAY);
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: ifStatusOkay=' . intval($ifStatusOkay));
+
+               // Return status
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: ifStatusOkay=%s - EXIT!', intval($ifStatusOkay)));
                return $ifStatusOkay;
        }
 
@@ -241,6 +252,8 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @return      $currentPos     Key from iterator
         */
        public function key () {
+               // Return current array position
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
                return $this->currentPos;
        }
 
@@ -250,8 +263,28 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @return      void
         */
        public function rewind () {
-               $this->currentPos = 0;
+               // Reset both current array position and current row
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos));
+               $this->resetCurrentPosition();
                $this->currentRow = [];
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: EXIT!');
+       }
+
+       /**
+        * Resets current array position to 0 if at least one record is there or -1
+        * if no record is there.
+        *
+        * @return      void
+        */
+       private function resetCurrentPosition () {
+               // Reset position
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: CALLED!');
+               $this->currentPos = ($this->count() > 0 ? 0 : -1);
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: EXIT!');
        }
 
        /**
@@ -262,7 +295,8 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @todo        0% done
         */
        public function searchEntry (LocalSearchCriteria $criteriaInstance) {
-               $this->debugBackTrace('[' . '[' . __METHOD__ . ':' . __LINE__ . ']:  Unfinished!');
+               // Unfinished code
+               $this->debugBackTrace(sprintf('[%s:%d]: criteriaInstance=%s', __METHOD__, __LINE__, print_r($criteriaInstance, TRUE)));
        }
 
        /**
@@ -358,7 +392,6 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
                // 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());
@@ -372,7 +405,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @param       $key    The key we shall find
         * @return      $found  Whether the key was found or not
         */
-       public function find ($key) {
+       public function find (string $key) {
                // By default nothing is found
                $found = false;
 
@@ -414,7 +447,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul
         * @return      void
         * @todo        Find a caching way without modifying the result array
         */
-       public function solveResultIndex ($databaseColumn, DatabaseFrontend $frontendInstance, array $callBack) {
+       public function solveResultIndex (string $databaseColumn, DatabaseFrontend $frontendInstance, array $callBack) {
                // By default nothing is found
                $indexValue = 0;
 
index d0925cc5203aab8aa1d007ba58d13b6c1a23e994..caad0998773a30557366fdb53027e91d9b402cfa 100644 (file)
@@ -45,6 +45,6 @@ interface SearchableResult extends FrameworkInterface {
         * @para        $callBack                       Call-back object for setting the index
         * @return      void
         */
-       function solveResultIndex ($databaseColumn, DatabaseFrontend $frontendInstance, array $callBack);
+       function solveResultIndex (string $databaseColumn, DatabaseFrontend $frontendInstance, array $callBack);
 
 }