]> git.mxchange.org Git - mailer.git/blobdiff - inc/classes/middleware/database/class_DatabaseConnection.php
Launcher scripts updated
[mailer.git] / inc / classes / middleware / database / class_DatabaseConnection.php
index bb67ebc8ef5da6be2d55218793878d9426cdc706..6f0a4e1ef2660a5b0664689a5aaf11feb749b591 100644 (file)
@@ -1,9 +1,36 @@
 <?php
-
-// Database selector class
-class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
-       // Array for connection data
-       private $connectData = array();
+/**
+ * Database selector class
+ *
+ * @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.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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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, Registerable {
+       /**
+        * Array for connection data
+        */
+       private $connectData = array(
+               'login' => "",
+               'pass'  => "",
+               'dbase' => "",
+               'host'  => ""
+       );
 
        // The real database layer
        private $dbLayer = null;
@@ -11,19 +38,14 @@ 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__);
-
-               // Set description
-               $this->setPartDescr("Datenbank-Mittelschicht");
-
-               // Create an unique ID
-               $this->createUniqueID();
-
-               // Clean up a little
-               $this->removeSystemArray();
+               parent::__construct(__CLASS__);
        }
 
        // Create new database connection layer
@@ -50,7 +72,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;
@@ -59,134 +81,104 @@ 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      $connectData    Connection data stored with this clas
+        */
+       public final function getConnectionData () {
+               return $this->connectData;
+       }
+
+       /**
+        * 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
-        *                                                              saveObject() was not found
         */
-       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 setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
+               $this->dbLayer = $dbLayer;
        }
 
        /**
-        * Set a limitation for the saving process. This shall be done before
-        * saveObject() is called else saveObject() shall save the whole object.
+        * Getter for index key
         *
-        * @param               $limitInstance  An instance of ObjectLimits which contains
-        *                                              elements we shall exclusivly include in
-        *                                              saving process
-        * @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
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->dbLayer->getIndexKey();
+       }
+
+       /**
+        * 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       $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 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 function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Get result from query
+               $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);
+
+               // Return the result
+               return $result;
        }
 
        /**
-        * 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.
+        * Getter for last exception
         *
-        * @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
+        * @return      $exceptionInstance      Last thrown exception
         */
-       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);
-               }
-
-               // Pass the returning result through
-               return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
-       }       
+       public final function getLastException () {
+               $exceptionInstance = $this->dbLayer->getLastException();
+               return $exceptionInstance;
+       }
 
        /**
-        * 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.
+        * "Inserts" a data set instance into a local file database folder
         *
-        * @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       $dataSetInstance        A storeable data set
+        * @return      void
         */
-       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);
-               }
-
-               // Pass the returning result through
-               return $this->dbLayer->getObjectFromCachedData($idNumber);
+       public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Ask the database layer
+               $this->dbLayer->queryInsertDataSet($dataSetInstance);
        }
 
        /**
-        * Setter for the real database layer
-        * @param               $dbLayer                An instance of the real database layer
+        * "Updates" a data set instance with a database layer
+        *
+        * @param       $dataSetInstance        A storeable data set
         * @return      void
         */
-       public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
-               $this->dbLayer = $dbLayer;
+       public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Ask the database layer
+               $this->dbLayer->queryUpdateDataSet($dataSetInstance);
+       }
+
+       /**
+        * Getter for primary key column of specified table name
+        *
+        * @param       $tableName              Name of table we need the primary key column from
+        * @return      $primaryKey             Primary key column of requested table
+        */
+       public function getPrimaryKeyOfTable ($tableName) {
+               // Connect to the database
+               $this->dbLayer->connectToDatabase();
+
+               // Ask the database layer
+               $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
+
+               // Return the value
+               return $primaryKey;
        }
 }