]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/middleware/database/class_DatabaseConnection.php
More conventions than code added:
[shipsimu.git] / inc / classes / middleware / database / class_DatabaseConnection.php
index d7db9d86975c88bc86b2c58d08df5c065ed9d18c..34091867e66f88d2d0de74568b38fbaf3089453f 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * Database selector class
  *
- * @author             Roland Haeder <webmaster@mxchange.org>
- * @version            0.3.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.mxchange.org
+ * @link               http://www.ship-simu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
-       // Array for connection data
-       private $connectData = array();
+class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
+       /**
+        * Array for connection data
+        */
+       private $connectData = array(
+               'login' => "",
+               'pass'  => "",
+               'dbase' => "",
+               'host'  => ""
+       );
 
        // The real database layer
        private $dbLayer = null;
@@ -31,19 +38,20 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li
        // An instance of this class
        private static $thisInstance = null;
 
-       // Private constructor
-       private final function __construct() {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct() {
                // Call parent constructor
-               parent::constructor(__CLASS__);
+               parent::__construct(__CLASS__);
 
                // Set description
                $this->setObjectDescription("Datenbank-Mittelschicht");
 
                // Create an unique ID
-               $this->createUniqueID();
-
-               // Clean up a little
-               $this->removeSystemArray();
+               $this->generateUniqueId();
        }
 
        // Create new database connection layer
@@ -70,7 +78,7 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li
        }
 
        // Public setter for database connection
-       public final function setConnectionData ($login, $pass, $dbase, $host) {
+       public final function setConnectionData ($login, $pass, $dbase, $host="localhost") {
                // Transfer connection data
                $this->connectData['login'] = (string) $login;
                $this->connectData['pass']  = (string) $pass;
@@ -79,134 +87,97 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Li
        }
 
        /**
-        * Save a whole object or parts of it to the database or local file
+        * Getter for connection data
         *
-        * @param               $object The object we shall save
-        * @return      void
-        * @throws      NullPointerException    If $limitInstance is null
-        * @throws      NoObjectException               If $limitInstance is not an object
-        * @throws      MissingMethodException  If the required method
-        *                                                              saveObject() was not found
+        * @return      $connectData    Connection data stored with this clas
         */
-       public final function saveObject ($object) {
-               // Some sanity checks
-               if (is_null($this->dbLayer)) {
-                       // Is null
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_object($this->dbLayer)) {
-                       // Is not an object
-                       throw new NoObjectException($this->dbLayer, self::EXCEPTION_IS_NO_OBJECT);
-               } elseif (!method_exists($this->dbLayer, 'saveObject')) {
-                       // Does not have the required instance
-                       throw new MissingMethodException(array($this->dbLayer, 'saveObject'), self::EXCEPTION_MISSING_METHOD);
-               }
-
-               // For now just pipe it through to the database layer
-               $this->dbLayer->saveObject($object);
+       public final function getConnectionData () {
+               return $this->connectData;
        }
 
        /**
-        * Set a limitation for the saving process. This shall be done before
-        * saveObject() is called else saveObject() shall save the whole object.
-        *
-        * @param               $limitInstance  An instance of ObjectLimits which contains
-        *                                              elements we shall exclusivly include in
-        *                                              saving process
+        * Setter for the real database layer
+        * @param       $dbLayer        An instance of the real database layer
         * @return      void
-        * @throws      NullPointerException    If $limitInstance is null
-        * @throws      NoObjectException               If $limitInstance is not an object
-        * @throws      MissingMethodException  If the required method
-        *                                                              limitObject() was not found
         */
-       public final function limitObject (ObjectLimits $limitInstance) {
-               // Get real database connection
-               $this->dbLayer = $this->getDatabaseInstance();
-
-               // Some sanity checks
-               if (is_null($this->dbLayer)) {
-                       // Is null
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_object($this->dbLayer)) {
-                       // Is not an object
-                       throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
-               } elseif (!method_exists($this->dbLayer, 'limitObject')) {
-                       // Does not have the required instance
-                       throw new MissingMethodException(array($this->dbLayer, 'limitObject'), self::EXCEPTION_MISSING_METHOD);
-               }
-
-               // For now we pipe this through to the real database instance
-               $this->dbLayer->limitObject($limitInstance);
+       public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
+               $this->dbLayer = $dbLayer;
        }
 
        /**
         * Analyses if a unique ID has already been used or not. This method does
         * only pass the given ID through to the "real" database layer.
         *
-        * @param               $uniqueID               A unique ID number which shall be checked
-        *                                              before it will be used
-        * @param               $inConstructor  If called from a constructor or from
-        *                                              somewhere else
+        * @param       $uniqueID               A unique ID number which shall be checked
+        *                                                      before it will be used
+        * @param       $inConstructor  If called from a constructor 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      NullPointerException    If $this->dbLayer is null
-        * @throws      NoObjectException               If $this->dbLayer is not an object
-        * @throws      MissingMethodException  If the required method
-        *                                                              isUniqueIdUsed() was not found
+        *                                                      false = It is already in use by an other object
         */
-       public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
-               // Some sanity checks
-               if (is_null($this->dbLayer)) {
-                       // Is null
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_object($this->dbLayer)) {
-                       // Is not an object
-                       throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
-               } elseif (!method_exists($this->dbLayer, 'isUniqueIdUsed')) {
-                       // Does not have the required instance
-                       throw new MissingMethodException(array($this->dbLayer, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
-               }
+       public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
 
                // Pass the returning result through
                return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
        }
 
        /**
-        * Gets cached data from the database layer and if not found fetch it from
-        * the database again. This method does not return the header stuff because
-        * The underlaying database class will return only the requested content.
+        * Runs a "select" statement on the database layer with given table name
+        * and criteria. If this doesn't fail the result will be returned
         *
-        * @param               $idNumber               The ID number which we need for looking up
-        *                                              the requested data
-        * @return      $cachedArray    The maybe cached data from the database
-        * @throws      NullPointerException    If $this->dbLayer is null
-        * @throws      NoObjectException               If $this->dbLayer is not an object
-        * @throws      MissingMethodException  If the required method
-        *                                                              isUniqueIdUsed() was not found
+        * @param       $tableName                      Name of the "table" we shall query
+        * @param       $criteriaInstance       An instance of a Criteria class
+        * @return      $result                         The result as an array
         */
-       public final function getObjectFromCachedData ($idNumber) {
-               // Some sanity checks
-               if (is_null($this->dbLayer)) {
-                       // Is null
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_object($this->dbLayer)) {
-                       // Is not an object
-                       throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
-               } elseif (!method_exists($this->dbLayer, 'getObjectFromCachedData')) {
-                       // Does not have the required instance
-                       throw new MissingMethodException(array($this->dbLayer, 'getObjectFromCachedData'), self::EXCEPTION_MISSING_METHOD);
-               }
+       public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
 
-               // Pass the returning result through
-               return $this->dbLayer->getObjectFromCachedData($idNumber);
+               // Get result from query
+               $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);
+
+               // Return the result
+               return $result;
        }
 
        /**
-        * Setter for the real database layer
-        * @param               $dbLayer                An instance of the real database layer
+        * Getter for last exception
+        *
+        * @return      $exceptionInstance      Last thrown exception
+        */
+       public final function getLastException () {
+               $exceptionInstance = $this->dbLayer->getLastException();
+               return $exceptionInstance;
+       }
+
+       /**
+        * "Inserts" a data set instance into a local file database folder
+        *
+        * @param       $dataSetInstance        A storeable data set
         * @return      void
         */
-       public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
-               $this->dbLayer = $dbLayer;
+       public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Ask the database layer
+               $this->dbLayer->queryInsertDataSet($dataSetInstance);
+       }
+
+       /**
+        * "Updates" a data set instance with a database layer
+        *
+        * @param       $dataSetInstance        A storeable data set
+        * @return      void
+        */
+       public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Ask the database layer
+               $this->dbLayer->queryUpdateDataSet($dataSetInstance);
        }
 }