3 * Database selector class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
24 // Array for connection data
25 private $connectData = array();
27 // The real database layer
28 private $dbLayer = null;
30 // An instance of this class
31 private static $thisInstance = null;
33 // Private constructor
34 private final function __construct() {
35 // Call parent constructor
36 parent::constructor(__CLASS__);
39 $this->setPartDescr("Datenbank-Mittelschicht");
41 // Create an unique ID
42 $this->createUniqueID();
45 $this->removeSystemArray();
48 // Create new database connection layer
49 public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
51 $dbInstance = new DatabaseConnection();
53 // Set debug output handler
54 $dbInstance->setDebugInstance($debugInstance);
57 $dbInstance->setDatabaseLayer($dbLayer);
60 self::$thisInstance = $dbInstance;
66 // Get an instance of this class
67 public final static function getInstance () {
68 return self::$thisInstance;
71 // Public setter for database connection
72 public final function setConnectionData ($login, $pass, $dbase, $host) {
73 // Transfer connection data
74 $this->connectData['login'] = (string) $login;
75 $this->connectData['pass'] = (string) $pass;
76 $this->connectData['dbase'] = (string) $dbase;
77 $this->connectData['host'] = (string) $host;
81 * Save a whole object or parts of it to the database or local file
83 * @param $object The object we shall save
85 * @throws NullPointerException If $limitInstance is null
86 * @throws NoObjectException If $limitInstance is not an object
87 * @throws MissingMethodException If the required method
88 * saveObject() was not found
90 public final function saveObject ($object) {
92 if (is_null($this->dbLayer)) {
94 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
95 } elseif (!is_object($this->dbLayer)) {
97 throw new NoObjectException($this->dbLayer, self::EXCEPTION_IS_NO_OBJECT);
98 } elseif (!method_exists($this->dbLayer, 'saveObject')) {
99 // Does not have the required instance
100 throw new MissingMethodException(array($this->dbLayer, 'saveObject'), self::EXCEPTION_MISSING_METHOD);
103 // For now just pipe it through to the database layer
104 $this->dbLayer->saveObject($object);
108 * Set a limitation for the saving process. This shall be done before
109 * saveObject() is called else saveObject() shall save the whole object.
111 * @param $limitInstance An instance of ObjectLimits which contains
112 * elements we shall exclusivly include in
115 * @throws NullPointerException If $limitInstance is null
116 * @throws NoObjectException If $limitInstance is not an object
117 * @throws MissingMethodException If the required method
118 * limitObject() was not found
120 public final function limitObject (ObjectLimits $limitInstance) {
121 // Get real database connection
122 $this->dbLayer = $this->getDatabaseInstance();
124 // Some sanity checks
125 if (is_null($this->dbLayer)) {
127 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
128 } elseif (!is_object($this->dbLayer)) {
130 throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
131 } elseif (!method_exists($this->dbLayer, 'limitObject')) {
132 // Does not have the required instance
133 throw new MissingMethodException(array($this->dbLayer, 'limitObject'), self::EXCEPTION_MISSING_METHOD);
136 // For now we pipe this through to the real database instance
137 $this->dbLayer->limitObject($limitInstance);
141 * Analyses if a unique ID has already been used or not. This method does
142 * only pass the given ID through to the "real" database layer.
144 * @param $uniqueID A unique ID number which shall be checked
145 * before it will be used
146 * @param $inConstructor If called from a constructor or from
148 * @return $isUnused true = The unique ID was not found in the database,
149 * false = It is already in use by an other object
150 * @throws NullPointerException If $this->dbLayer is null
151 * @throws NoObjectException If $this->dbLayer is not an object
152 * @throws MissingMethodException If the required method
153 * isUniqueIdUsed() was not found
155 public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
156 // Some sanity checks
157 if (is_null($this->dbLayer)) {
159 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
160 } elseif (!is_object($this->dbLayer)) {
162 throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
163 } elseif (!method_exists($this->dbLayer, 'isUniqueIdUsed')) {
164 // Does not have the required instance
165 throw new MissingMethodException(array($this->dbLayer, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
168 // Pass the returning result through
169 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
173 * Gets cached data from the database layer and if not found fetch it from
174 * the database again. This method does not return the header stuff because
175 * The underlaying database class will return only the requested content.
177 * @param $idNumber The ID number which we need for looking up
179 * @return $cachedArray The maybe cached data from the database
180 * @throws NullPointerException If $this->dbLayer is null
181 * @throws NoObjectException If $this->dbLayer is not an object
182 * @throws MissingMethodException If the required method
183 * isUniqueIdUsed() was not found
185 public final function getObjectFromCachedData ($idNumber) {
186 // Some sanity checks
187 if (is_null($this->dbLayer)) {
189 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
190 } elseif (!is_object($this->dbLayer)) {
192 throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
193 } elseif (!method_exists($this->dbLayer, 'getObjectFromCachedData')) {
194 // Does not have the required instance
195 throw new MissingMethodException(array($this->dbLayer, 'getObjectFromCachedData'), self::EXCEPTION_MISSING_METHOD);
198 // Pass the returning result through
199 return $this->dbLayer->getObjectFromCachedData($idNumber);
203 * Setter for the real database layer
204 * @param $dbLayer An instance of the real database layer
207 public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
208 $this->dbLayer = $dbLayer;