8499c0f190416eee778fd99fb21e7456a0a21d1e
[shipsimu.git] / inc / classes / middleware / database / class_DatabaseConnection.php
1 <?php
2 /**
3  * Database selector class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  *
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.
14  *
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.
19  *
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/>.
22  */
23 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
24         // Array for connection data
25         private $connectData = array();
26
27         // The real database layer
28         private $dbLayer = null;
29
30         // An instance of this class
31         private static $thisInstance = null;
32
33         // Private constructor
34         private final function __construct() {
35                 // Call parent constructor
36                 parent::constructor(__CLASS__);
37
38                 // Set description
39                 $this->setPartDescr("Datenbank-Mittelschicht");
40
41                 // Create an unique ID
42                 $this->createUniqueID();
43
44                 // Clean up a little
45                 $this->removeSystemArray();
46         }
47
48         // Create new database connection layer
49         public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
50                 // Get instance
51                 $dbInstance = new DatabaseConnection();
52
53                 // Set debug output handler
54                 $dbInstance->setDebugInstance($debugInstance);
55
56                 // Set database layer
57                 $dbInstance->setDatabaseLayer($dbLayer);
58
59                 // Set db instance
60                 self::$thisInstance = $dbInstance;
61
62                 // Return instance
63                 return $dbInstance;
64         }
65
66         // Get an instance of this class
67         public final static function getInstance () {
68                 return self::$thisInstance;
69         }
70
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;
78         }
79
80         /**
81          * Save a whole object or parts of it to the database or local file
82          *
83          * @param               $object The object we shall save
84          * @return      void
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
89          */
90         public final function saveObject ($object) {
91                 // Some sanity checks
92                 if (is_null($this->dbLayer)) {
93                         // Is null
94                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
95                 } elseif (!is_object($this->dbLayer)) {
96                         // Is not an object
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);
101                 }
102
103                 // For now just pipe it through to the database layer
104                 $this->dbLayer->saveObject($object);
105         }
106
107         /**
108          * Set a limitation for the saving process. This shall be done before
109          * saveObject() is called else saveObject() shall save the whole object.
110          *
111          * @param               $limitInstance  An instance of ObjectLimits which contains
112          *                                              elements we shall exclusivly include in
113          *                                              saving process
114          * @return      void
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
119          */
120         public final function limitObject (ObjectLimits $limitInstance) {
121                 // Get real database connection
122                 $this->dbLayer = $this->getDatabaseInstance();
123
124                 // Some sanity checks
125                 if (is_null($this->dbLayer)) {
126                         // Is null
127                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
128                 } elseif (!is_object($this->dbLayer)) {
129                         // Is not an object
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);
134                 }
135
136                 // For now we pipe this through to the real database instance
137                 $this->dbLayer->limitObject($limitInstance);
138         }
139
140         /**
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.
143          *
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
147          *                                              somewhere else
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
154          */
155         public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
156                 // Some sanity checks
157                 if (is_null($this->dbLayer)) {
158                         // Is null
159                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
160                 } elseif (!is_object($this->dbLayer)) {
161                         // Is not an object
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);
166                 }
167
168                 // Pass the returning result through
169                 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
170         }
171
172         /**
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.
176          *
177          * @param               $idNumber               The ID number which we need for looking up
178          *                                              the requested data
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
184          */
185         public final function getObjectFromCachedData ($idNumber) {
186                 // Some sanity checks
187                 if (is_null($this->dbLayer)) {
188                         // Is null
189                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
190                 } elseif (!is_object($this->dbLayer)) {
191                         // Is not an object
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);
196                 }
197
198                 // Pass the returning result through
199                 return $this->dbLayer->getObjectFromCachedData($idNumber);
200         }
201
202         /**
203          * Setter for the real database layer
204          * @param               $dbLayer                An instance of the real database layer
205          * @return      void
206          */
207         public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
208                 $this->dbLayer = $dbLayer;
209         }
210 }
211
212 // [EOF]
213 ?>