+++ /dev/null
-<?php
-/**
- * Database selector class
- *
- * @author Roland Haeder <webmaster@ship-simu.org>
- * @version 0.0
- * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
- * @license GNU GPL 3.0 or any newer version
- *
- * 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, LimitableObject {
- // Array for connection data
- private $connectData = array();
-
- // The real database layer
- private $dbLayer = null;
-
- // An instance of this class
- private static $thisInstance = null;
-
- // Private constructor
- private final 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();
- }
-
- // Create new database connection layer
- public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
- // Get instance
- $dbInstance = new DatabaseConnection();
-
- // Set debug output handler
- $dbInstance->setDebugInstance($debugInstance);
-
- // Set database layer
- $dbInstance->setDatabaseLayer($dbLayer);
-
- // Set db instance
- self::$thisInstance = $dbInstance;
-
- // Return instance
- return $dbInstance;
- }
-
- // Get an instance of this class
- public final static function getInstance () {
- return self::$thisInstance;
- }
-
- // Public setter for database connection
- public final function setConnectionData ($login, $pass, $dbase, $host) {
- // Transfer connection data
- $this->connectData['login'] = (string) $login;
- $this->connectData['pass'] = (string) $pass;
- $this->connectData['dbase'] = (string) $dbase;
- $this->connectData['host'] = (string) $host;
- }
-
- /**
- * Save a whole object or parts of it to the database or local file
- *
- * @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
- */
- 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);
- }
-
- /**
- * 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
- * @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);
- }
-
- /**
- * 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
- * @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
- */
- 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);
- }
-
- /**
- * 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.
- *
- * @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
- */
- 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);
- }
-
- /**
- * Setter for the real database layer
- * @param $dbLayer An instance of the real database layer
- * @return void
- */
- public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
- $this->dbLayer = $dbLayer;
- }
-}
-
-// [EOF]
-?>