]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/database/databases/class_LocalFileDatabase.php
createUniqueID -> generateUniqueId renamed, dataset criteria added, registration...
[shipsimu.git] / inc / classes / main / database / databases / class_LocalFileDatabase.php
index db1d87e90aa7ccac0db9deaf5a22a26bbf733f8d..c40fbf9490c0ec93118685ce2d772395898e066c 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontendInterface {
+
        // Constants for MySQL backward-compatiblity (PLEASE FIX THEM!)
-       const DB_CODE_TABLE_MISSING = 0x000;
+       const DB_CODE_TABLE_MISSING     = 0x000;
+       const DB_CODE_TABLE_UNWRITEABLE = 0x001;
 
        /**
         * Save path for "file database"
@@ -76,7 +78,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                $this->setObjectDescription("Class for local file databases");
 
                // Create unique ID
-               $this->createUniqueID();
+               $this->generateUniqueId();
 
                // Clean up a little
                $this->removeSystemArray();
@@ -558,9 +560,54 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                'rows'          => array()
                        );
 
+                       // Initialize limit/skip
+                       $limitFound = 0; $skipFound = 0;
+
                        // Read the directory with some exceptions
-                       while ($dataFile = $directoryInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn"))) {
-                               $this->partialStub(sprintf("File %s found.", $dataFile));
+                       while (($dataFile = $directoryInstance->readDirectoryExcept(array(".", "..", ".htaccess", ".svn"))) && ($limitFound < $criteriaInstance->getLimit())) {
+                               // Open this file for reading
+                               $filePointer = FrameworkFileInputPointer::createFrameworkFileInputPointer($pathName . $dataFile);
+
+                               // Get the raw data and BASE64-decode it
+                               $compressedData = base64_decode($filePointer->readLinesFromFile());
+
+                               // Close the file and throw the instance away
+                               $filePointer->closeFile();
+                               unset($filePointer);
+
+                               // Decompress it
+                               $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
+
+                               // Unserialize it
+                               $dataArray = unserialize($serializedData);
+
+                               // Is this an array?
+                               if (is_array($dataArray)) {
+                                       // Search in the criteria with FMFW (First Matches, First Wins)
+                                       foreach ($dataArray as $key=>$value) {
+                                               // Get criteria element
+                                               $criteria = $criteriaInstance->getCriteriaElemnent($key);
+
+                                               // Is the criteria met?
+                                               if ((!is_null($criteria)) && ($criteria == $value))  {
+
+                                                       // Shall we skip this entry?
+                                                       if ($criteriaInstance->getSkip() > 0) {
+                                                               // We shall skip some entries
+                                                               if ($skipFound < $criteriaInstance->getSkip()) {
+                                                                       // Skip this entry
+                                                                       $skipFound++;
+                                                                       break;
+                                                               } // END - if
+                                                       } // END - if
+
+                                                       // Entry found!
+                                                       $resultData['rows'][]    = $dataArray;
+                                                       $limitFound++;
+                                                       break;
+                                               } // END - if
+                                       } // END - foreach
+                               } // END - if
                        } // END - while
 
                        // Close directory and throw the instance away
@@ -591,20 +638,36 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         *
         * @param       $dataSetInstance        A storeable data set
         * @return      void
+        * @throws      SqlException    If an SQL error occurs
         */
        public function insertDataSet (StoreableCriteria $dataSetInstance) {
                // Create full path name
                $fqfn = sprintf("%s%s/%s.%s",
                        $this->getSavePath(),
                        $dataSetInstance->getTableName(),
-                       $dataSetInstance->getCombinedUniqueKey(),
+                       md5($dataSetInstance->getUniqueValue()),
                        $this->getFileExtension()
                );
-               die($fqfn);
+
+               // Skip here while developing
+               return true;
 
                // Try to save the request away
                try {
                        // Get a file pointer instance
+                       $filePointer = FrameworkFileOutputPointer::createFrameworkFileOutputPointer($fqfn, 'w');
+
+                       // Get the criteria array from the dataset
+                       $criteriaArray = $dataSetInstance->getCriteriaArray();
+
+                       // Serialize and compress it
+                       $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($criteriaArray));
+
+                       // Write this data BASE64 encoded to the file
+                       $filePointer->writeToFile(base64_encode($compressedData));
+
+                       // Close the file pointer
+                       $filePointer->closeFile();
 
                        // Reset last error message and exception
                        $this->resetLastError();
@@ -612,6 +675,9 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                        // Catch all exceptions and store them in last error
                        $this->lastException = $e;
                        $this->lastError = $e->getMessage();
+
+                       // Throw an SQL exception
+                       throw new SqlException (array($this, sprintf("Cannot write data to table &#39;%s&#39;", $tableName), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
                }
        }
 }