]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/database/databases/class_LocalFileDatabase.php
Database result added, SqlException added
[shipsimu.git] / inc / classes / main / database / databases / class_LocalFileDatabase.php
index 16314e1164e3557e7cd55f68bad8657b87340aea..56034fa0b62087794511527ca856538c8286b05a 100644 (file)
@@ -24,6 +24,9 @@
  * 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;
+
        /**
         * Save path for "file database"
         */
@@ -49,6 +52,16 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         */
        private $alreadyConnected = false;
 
+       /**
+        * Last error message
+        */
+       private $lastError = "";
+
+       /**
+        * Last exception
+        */
+       private $lastException = null;
+
        /**
         * The private constructor. Do never instance from outside!
         * You need to set a local file path. The class will then validate it.
@@ -116,6 +129,24 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                return $this->savePath;
        }
 
+       /**
+        * Getter for last error message
+        *
+        * @return      $lastError      Last error message
+        */
+       public final function getLastError () {
+               return $this->lastError;
+       }
+
+       /**
+        * Getter for last exception
+        *
+        * @return      $lastException  Last thrown exception
+        */
+       public final function getLastException () {
+               return $this->lastException;
+       }
+
        /**
         * Saves a given object to the local file system by serializing and
         * transparently compressing it
@@ -284,6 +315,17 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                $this->lastFile = $fqfn;
        }
 
+       /**
+        * Reset the last error and exception instance. This should be done after
+        * a successfull "query"
+        *
+        * @return      void
+        */
+       private final function resetLastError () {
+               $this->lastError = "";
+               $this->lastException = null;
+       }
+
        /**
         * Getter for last read file
         *
@@ -504,16 +546,45 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         * @param       $criteria               Local search criteria class
         * @return      $resultData             Result data of the query
         * @throws      UnsupportedCriteriaException    If the criteria is unsupported
+        * @throws      SqlException                                    If an "SQL error" occurs
         */
        public function querySelect ($resultType, $tableName, Criteria $criteriaInstance) {
+               // The result is null by any errors
+               $resultData = null;
+
                // Is this criteria supported?
                if (!$criteriaInstance instanceof LocalCriteria) {
                        // Not supported by this database layer
                        throw new UnsupportedCriteriaException(array($this, $criteriaInstance), self::EXCEPTION_REQUIRED_INTERFACE_MISSING);
                }
 
-               // A "select" query on local files is not that easy, so begin slowly with it...
-               $this->partialStub(sprintf("type=%s,table=%s,criteria=%s", $resultType, $tableName, $criteriaInstance));
+               // Create full path name
+               $pathName = $this->getSavePath() . $tableName . '/';
+
+               // A "select" query is not that easy on local files, so first try to
+               // find the "table" which is in fact a directory on the server
+               try {
+                       // Get a directory pointer instance
+                       $directoryPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
+                       $this->partialStub("Finish handling found &quot;tables&quot;");
+
+                       // Reset last error message and exception
+                       $this->resetLastError();
+               } catch (PathIsNoDirectoryException $e) {
+                       // Path not found means "table not found" for real databases...
+                       $this->lastException = $e;
+                       $this->lastError = $e->getMessage();
+
+                       // So throw an SqlException here with faked error message
+                       throw new SqlException (array($this, sprintf("Table &#39;%s&#39; not found", $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
+               } catch (FrameworkException $e) {
+                       // Catch all exceptions and store them in last error
+                       $this->lastException = $e;
+                       $this->lastError = $e->getMessage();
+               }
+
+               // Return the gathered result
+               return $resultData;
        }
 }