]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/database/backend/class_CachedLocalFileDatabase.php
Introduced FileNotFoundException
[core.git] / inc / classes / main / database / backend / class_CachedLocalFileDatabase.php
index 1b25719c4618423638686c1fa086cefc0227cf76..935cf27fe1a53ea87c033e8bd3ec4a9f67508bfe 100644 (file)
@@ -177,7 +177,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
 
                // Unserialize it
-               $dataArray = unserialize($serializedData);
+               $dataArray = json_decode($serializedData, TRUE);
 
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
@@ -200,7 +200,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
 
                // Serialize and compress it
-               $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($dataArray));
+               $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(json_encode($dataArray));
 
                // Write data
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
@@ -228,7 +228,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                // Get the file contents
                try {
                        $infoArray = $this->getDataArrayFromFile($fqfn);
-               } catch (FileIoException $e) {
+               } catch (FileNotFoundException $e) {
                        // Not found, so ignore it here
                }
 
@@ -279,9 +279,6 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
         * @return      void
         */
        private function updateTableInfoFile (StoreableCriteria $dataSetInstance) {
-               // "Cache" table name
-               $tableName = $dataSetInstance->getTableName();
-
                // Create FQFN for creating the table information file
                $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
 
@@ -290,7 +287,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                $this->tableInfo[$tableName]['last_updated'] = time();
 
                // Write the data to the file
-               $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
+               $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
        }
 
        /**
@@ -583,7 +580,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                        $this->setLastException($e);
 
                        // Throw an SQL exception
-                       throw new SqlException(array($this, sprintf("Cannot write data to table '%s', is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
+                       throw new SqlException(array($this, sprintf('Cannot write data to table '%s', is the table created? Exception: %s, message:%s', $dataSetInstance->getTableName(), $e->__toString(), $e->getMessage()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
                }
        }
 
@@ -623,6 +620,53 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: data[' . gettype($data) . ']='.print_r($data, TRUE));
                return $data;
        }
+
+       /**
+        * Counts total rows of given table
+        *
+        * @param       $tableName      Table name
+        * @return      $count          Total rows of given table
+        */
+       public function countTotalRows($tableName) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableName=' . $tableName . ' - CALLED!');
+
+               // Create full path name
+               $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
+
+               // Try all the requests
+               try {
+                       // Get a file pointer instance
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+
+                       // Initialize counter
+                       $count = 0;
+
+                       // Read the directory with some exceptions
+                       while ($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) {
+                               // Does the extension match?
+                               if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
+                                       // Debug message
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
+                                       // Skip this file!
+                                       continue;
+                               } // END - if
+
+                               // Count this row up
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - COUNTED!');
+                               $count++;
+                       } // END - while
+               } catch (FrameworkException $e) {
+                       // Catch all exceptions and store them in last error
+                       $this->setLastException($e);
+
+                       // Throw an SQL exception
+                       throw new SqlException(array($this, sprintf('Cannot count on table '%s', is the table created?', $dataSetInstance->getTableName()), self::DB_CODE_TABLE_NOT_FOUND), self::EXCEPTION_SQL_QUERY);
+               }
+
+               // Return count
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: tableName=' . $tableName . ',count=' . $count . ' - EXIT!');
+               return $count;
+       }
 }
 
 // [EOF]