]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/database/databases/class_LocalFileDatabase.php
Setting index is now much nicer (more abstract!)
[shipsimu.git] / inc / classes / main / database / databases / class_LocalFileDatabase.php
index 69ce662f9e4055ae034a006259ff6bafaa027007..29e2e5e0f397f3dab498d8dc80ce70565a1598ce 100644 (file)
@@ -70,6 +70,11 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         */
        private $tableInfo = array();
 
+       /**
+        * Element for index
+        */
+       private $indexKey = "__idx";
+
        /**
         * The protected constructor. Do never instance from outside! You need to
         * set a local file path. The class will then validate it.
@@ -150,106 +155,15 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                return $this->lastException;
        }
 
-       /**
-        * Analyses if a unique ID has already been used or not by search in the
-        * local database folder.
-        *
-        * @param       $uniqueID               A unique ID number which shall be checked
-        *                                                      before it will be used
-        * @param       $inConstructor  If we got called in a de/con-structor or
-        *                                                      from somewhere else
-        * @return      $isUnused               true    = The unique ID was not found in the database,
-        *                                                      false = It is already in use by an other object
-        * @throws      NoArrayCreatedException         If explode() fails to create an array
-        * @throws      InvalidArrayCountException      If the array contains less or
-        *                                                                      more than two elements
-        * @deprecated
-        */
-       public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
-               // Currently not used... ;-)
-               $isUsed = false;
-
-               // Split the unique ID up in path and file name
-               $pathFile = explode("@", $uniqueID);
-
-               // Are there two elements? Index 0 is the path, 1 the file name + global extension
-               if (!is_array($pathFile)) {
-                       // No array found
-                       if ($inConstructor) {
-                               return false;
-                       } else {
-                               throw new NoArrayCreatedException(array($this, 'pathFile'), self::EXCEPTION_ARRAY_EXPECTED);
-                       }
-               } elseif (count($pathFile) != 2) {
-                       // Invalid ID returned!
-                       if ($inConstructor) {
-                               return false;
-                       } else {
-                               throw new InvalidArrayCountException(array($this, 'pathFile', count($pathFile), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
-                       }
-               }
-
-               // Create full path name
-               $pathName = $this->getSavePath() . $pathFile[0];
-
-               // Check if the file is there with a file handler
-               if ($inConstructor) {
-                       // No exceptions in constructors and destructors!
-                       $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName, true);
-
-                       // Has an object being created?
-                       if (!is_object($dirInstance)) return false;
-               } else {
-                       // Outside a constructor
-                       try {
-                               $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
-                       } catch (PathIsNoDirectoryException $e) {
-                               // Okay, path not found
-                               return false;
-                       }
-               }
-
-               // Initialize the search loop
-               $isValid = false;
-               while ($dataFile = $dirInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn", "info." . $this->getFileExtension()))) {
-                       // Generate FQFN for testing
-                       $fqfn = sprintf("%s/%s", $pathName, $dataFile);
-                       $this->setLastFile($fqfn);
-
-                       // Get instance for file handler
-                       $inputHandler = $this->getFileIoInstance();
-
-                       // Try to read from it. This makes it sure that the file is
-                       // readable and a valid database file
-                       $this->setLastFileContents($inputHandler->loadFileContents($fqfn));
-
-                       // Extract filename (= unique ID) from it
-                       $ID = substr(basename($fqfn), 0, -(strlen($this->getFileExtension()) + 1));
-
-                       // Is this the required unique ID?
-                       if ($ID == $pathFile[1]) {
-                               // Okay, already in use!
-                               $isUsed = true;
-                       }
-               }
-
-               // Close the directory handler
-               $dirInstance->closeDirectory();
-
-               // Now the same for the file...
-               return $isUsed;
-       }
-
        /**
         * Setter for the last read file
         *
-        * @param               $fqfn   The FQFN of the last read file
+        * @param       $fqfn   The FQFN of the last read file
         * @return      void
         */
        private final function setLastFile ($fqfn) {
-               // Cast string
-               $fqfn = (string) $fqfn;
-               $this->lastFile = $fqfn;
+               // Cast string and set it
+               $this->lastFile = (string) $fqfn;
        }
 
        /**
@@ -278,9 +192,8 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         * @param               $contents               An array with header and data elements
         * @return      void
         */
-       private final function setLastFileContents ($contents) {
-               // Cast array
-               $contents = (array) $contents;
+       private final function setLastFileContents (array $contents) {
+               // Set array
                $this->lastContents = $contents;
        }
 
@@ -302,6 +215,15 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                return $this->fileExtension;
        }
 
+       /**
+        * Getter for index key
+        *
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->indexKey;
+       }
+
        /**
         * Reads a local data file  and returns it's contents in an array
         *
@@ -460,6 +382,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                        // Initialize limit/skip
                        $limitFound = 0;
                        $skipFound = 0;
+                       $idx = 1;
 
                        // Read the directory with some exceptions
                        while (($dataFile = $directoryInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn", "info." . $this->getFileExtension()))) && ($limitFound < $criteriaInstance->getLimit())) {
@@ -467,7 +390,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
                                        // Skip this file!
                                        continue;
-                               }
+                               } // END - if
 
                                // Read the file
                                $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
@@ -475,7 +398,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                // Is this an array?
                                if (is_array($dataArray)) {
                                        // Search in the criteria with FMFW (First Matches, First Wins)
-                                       foreach ($dataArray as $key=>$value) {
+                                       foreach ($dataArray as $key => $value) {
                                                // Get criteria element
                                                $criteria = $criteriaInstance->getCriteriaElemnent($key);
 
@@ -492,8 +415,13 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                                                } // END - if
                                                        } // END - if
 
+                                                       // Set id number
+                                                       $dataArray[$this->getIndexKey()] = $idx;
+
                                                        // Entry found!
                                                        $resultData['rows'][] = $dataArray;
+
+                                                       // Count found entries up
                                                        $limitFound++;
                                                        break;
                                                } // END - if
@@ -502,6 +430,9 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                        // Throw an exception here
                                        throw new SqlException(array($this, sprintf("File &#39;%s&#39; contains invalid data.", $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
                                }
+
+                               // Count entry up
+                               $idx++;
                        } // END - while
 
                        // Close directory and throw the instance away
@@ -603,7 +534,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                // Is this an array?
                                if (is_array($dataArray)) {
                                        // Search in the criteria with FMFW (First Matches, First Wins)
-                                       foreach ($dataArray as $key=>$value) {
+                                       foreach ($dataArray as $key => $value) {
                                                // Get criteria element
                                                $criteria = $searchInstance->getCriteriaElemnent($key);
 
@@ -621,7 +552,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                                        } // END - if
 
                                                        // Entry found, so update it
-                                                       foreach ($criteriaArray as $criteriaKey=>$criteriaValue) {
+                                                       foreach ($criteriaArray as $criteriaKey => $criteriaValue) {
                                                                $dataArray[$criteriaKey] = $criteriaValue;
                                                        } // END - foreach