PrimeraApi improved, some code cosmetics:
[core.git] / inc / classes / main / database / databases / class_LocalFileDatabase.php
index 78375f6612f579962883e85cc4c86a5945f848e7..9e7e3f42b7effda605629a42ffd91684c5583db9 100644 (file)
@@ -6,7 +6,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -75,7 +75,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
        /**
         * Element for index
         */
-       private $indexKey = "__idx";
+       private $indexKey = '__idx';
 
        /**
         * The protected constructor. Do never instance from outside! You need to
@@ -83,37 +83,36 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         *
         * @return      void
         */
-       protected function __construct() {
+       protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
         * Create an object of LocalFileDatabase and set the save path for local files.
         * This method also validates the given file path.
         *
-        * @param               $savePath                                       The local file path string
-        * @param               $ioInstance                             The input/output handler. This
-        *                                                                      should be FileIoHandler
-        * @return      $dbInstance                             An instance of LocalFileDatabase
+        * @param       $savePath               The local file path string
+        * @return      $dbInstance             An instance of LocalFileDatabase
         */
-       public final static function createLocalFileDatabase ($savePath, FileIoHandler $ioInstance) {
+       public static final function createLocalFileDatabase ($savePath) {
                // Get an instance
-               $dbInstance = new LocalFileDatabase();
+               $databaseInstance = new LocalFileDatabase();
 
                // Set save path and IO instance
-               $dbInstance->setSavePath($savePath);
-               $dbInstance->setFileIoInstance($ioInstance);
+               $databaseInstance->setSavePath($savePath);
+
+               // Set the compressor channel
+               $databaseInstance->setCompressorChannel(CompressorChannel::createCompressorChannel(
+                       $databaseInstance->getConfigInstance()->getConfigEntry('base_path').
+                       $databaseInstance->getConfigInstance()->getConfigEntry('compressor_base_path')
+               ));
 
                // "Connect" to the database
-               $dbInstance->connectToDatabase();
+               $databaseInstance->connectToDatabase();
 
                // Return database instance
-               return $dbInstance;
+               return $databaseInstance;
        }
 
        /**
@@ -123,11 +122,8 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         * @return      void
         */
        public final function setSavePath ($savePath) {
-               // Secure string
-               $savePath = (string) $savePath;
-
                // Set save path
-               $this->savePath = $savePath;
+               $this->savePath = (string) $savePath;
        }
 
        /**
@@ -285,12 +281,12 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                $infoArray = array();
 
                // Create FQFN for getting the table information file
-               $fqfn = $this->getSavePath() . $dataSetInstance->getTableName() . '/info.' . $this->getFileExtension();
+               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
 
                // Get the file contents
                try {
                        $infoArray = $this->getDataArrayFromFile($fqfn);
-               } catch (FileNotFoundException $e) {
+               } catch (FileIoException $e) {
                        // Not found, so ignore it here
                }
 
@@ -298,6 +294,21 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                return $infoArray;
        }
 
+       /**
+        * Generates an FQFN from given dataset instance and string
+        *
+        * @param       $dataSetInstance        An instance of a database set class
+        * @param       $rowName                        Name of the row
+        * @return      $fqfn                           The FQFN for this row
+        */
+       private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
+               // This is the FQFN
+               $fqfn = $this->getSavePath() . $dataSetInstance->getTableName() . '/' . $rowName . '.' . $this->getFileExtension();
+
+               // Return it
+               return $fqfn;
+       }
+
        /**
         * Creates the table info file from given dataset instance
         *
@@ -306,7 +317,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         */
        private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
                // Create FQFN for creating the table information file
-               $fqfn = $this->getSavePath() . $dataSetInstance->getTableName() . '/info.' . $this->getFileExtension();
+               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
 
                // Get the data out from dataset in a local array
                $this->tableInfo[$dataSetInstance->getTableName()] = array(
@@ -469,12 +480,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         */
        public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
                // Create full path name
-               $fqfn = sprintf("%s%s/%s.%s",
-                       $this->getSavePath(),
-                       $dataSetInstance->getTableName(),
-                       md5($dataSetInstance->getUniqueValue()),
-                       $this->getFileExtension()
-               );
+               $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
 
                // Try to save the request away
                try {
@@ -489,7 +495,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                } catch (FrameworkException $e) {
                        // Catch all exceptions and store them in last error
                        $this->lastException = $e;
-                       $this->lastError = $e->getMessage();
+                       $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);