From d9fe7cf9bba173a486238d80c22cf9ad1bc77373 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 3 Dec 2020 23:02:41 +0100 Subject: [PATCH] Continued: - fixes for currentPos calculation, needs to be done in factory method (or private method being invoked there) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../result/class_CachedDatabaseResult.php | 51 +++++++++++++++---- .../result/class_SearchableResult.php | 2 +- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/framework/main/classes/database/result/class_CachedDatabaseResult.php b/framework/main/classes/database/result/class_CachedDatabaseResult.php index 2d869894..80fe8d05 100644 --- a/framework/main/classes/database/result/class_CachedDatabaseResult.php +++ b/framework/main/classes/database/result/class_CachedDatabaseResult.php @@ -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}
\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; diff --git a/framework/main/interfaces/result/class_SearchableResult.php b/framework/main/interfaces/result/class_SearchableResult.php index d0925cc5..caad0998 100644 --- a/framework/main/interfaces/result/class_SearchableResult.php +++ b/framework/main/interfaces/result/class_SearchableResult.php @@ -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); } -- 2.39.2