Comment header fixed
[mailer.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.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
25         // Array for connection data
26         private $connectData = array();
27
28         // The real database layer
29         private $dbLayer = null;
30
31         // An instance of this class
32         private static $thisInstance = null;
33
34         // Private constructor
35         protected function __construct() {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Set description
40                 $this->setObjectDescription("Datenbank-Mittelschicht");
41
42                 // Create an unique ID
43                 $this->createUniqueID();
44
45                 // Clean up a little
46                 $this->removeSystemArray();
47         }
48
49         // Create new database connection layer
50         public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
51                 // Get instance
52                 $dbInstance = new DatabaseConnection();
53
54                 // Set debug output handler
55                 $dbInstance->setDebugInstance($debugInstance);
56
57                 // Set database layer
58                 $dbInstance->setDatabaseLayer($dbLayer);
59
60                 // Set db instance
61                 self::$thisInstance = $dbInstance;
62
63                 // Return instance
64                 return $dbInstance;
65         }
66
67         // Get an instance of this class
68         public final static function getInstance () {
69                 return self::$thisInstance;
70         }
71
72         // Public setter for database connection
73         public final function setConnectionData ($login, $pass, $dbase, $host) {
74                 // Transfer connection data
75                 $this->connectData['login'] = (string) $login;
76                 $this->connectData['pass']  = (string) $pass;
77                 $this->connectData['dbase'] = (string) $dbase;
78                 $this->connectData['host']  = (string) $host;
79         }
80
81         /**
82          * Save a whole object or parts of it to the database or local file
83          *
84          * @param               $object The object we shall save
85          * @return      void
86          * @throws      NullPointerException    If $limitInstance is null
87          * @throws      NoObjectException               If $limitInstance is not an object
88          * @throws      MissingMethodException  If the required method
89          *                                                              saveObject() was not found
90          */
91         public final function saveObject ($object) {
92                 // Some sanity checks
93                 if (is_null($this->dbLayer)) {
94                         // Is null
95                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96                 } elseif (!is_object($this->dbLayer)) {
97                         // Is not an object
98                         throw new NoObjectException($this->dbLayer, self::EXCEPTION_IS_NO_OBJECT);
99                 } elseif (!method_exists($this->dbLayer, 'saveObject')) {
100                         // Does not have the required instance
101                         throw new MissingMethodException(array($this->dbLayer, 'saveObject'), self::EXCEPTION_MISSING_METHOD);
102                 }
103
104                 // For now just pipe it through to the database layer
105                 $this->dbLayer->saveObject($object);
106         }
107
108         /**
109          * Set a limitation for the saving process. This shall be done before
110          * saveObject() is called else saveObject() shall save the whole object.
111          *
112          * @param               $limitInstance  An instance of ObjectLimits which contains
113          *                                              elements we shall exclusivly include in
114          *                                              saving process
115          * @return      void
116          * @throws      NullPointerException    If $limitInstance is null
117          * @throws      NoObjectException               If $limitInstance is not an object
118          * @throws      MissingMethodException  If the required method
119          *                                                              limitObject() was not found
120          */
121         public final function limitObject (ObjectLimits $limitInstance) {
122                 // Get real database connection
123                 $this->dbLayer = $this->getDatabaseInstance();
124
125                 // Some sanity checks
126                 if (is_null($this->dbLayer)) {
127                         // Is null
128                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
129                 } elseif (!is_object($this->dbLayer)) {
130                         // Is not an object
131                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
132                 } elseif (!method_exists($this->dbLayer, 'limitObject')) {
133                         // Does not have the required instance
134                         throw new MissingMethodException(array($this->dbLayer, 'limitObject'), self::EXCEPTION_MISSING_METHOD);
135                 }
136
137                 // For now we pipe this through to the real database instance
138                 $this->dbLayer->limitObject($limitInstance);
139         }
140
141         /**
142          * Analyses if a unique ID has already been used or not. This method does
143          * only pass the given ID through to the "real" database layer.
144          *
145          * @param               $uniqueID               A unique ID number which shall be checked
146          *                                              before it will be used
147          * @param               $inConstructor  If called from a constructor or from
148          *                                              somewhere else
149          * @return      $isUnused               true    = The unique ID was not found in the database,
150          *                                              false = It is already in use by an other object
151          * @throws      NullPointerException    If $this->dbLayer is null
152          * @throws      NoObjectException               If $this->dbLayer is not an object
153          * @throws      MissingMethodException  If the required method
154          *                                                              isUniqueIdUsed() was not found
155          */
156         public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
157                 // Some sanity checks
158                 if (is_null($this->dbLayer)) {
159                         // Is null
160                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
161                 } elseif (!is_object($this->dbLayer)) {
162                         // Is not an object
163                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
164                 } elseif (!method_exists($this->dbLayer, 'isUniqueIdUsed')) {
165                         // Does not have the required instance
166                         throw new MissingMethodException(array($this->dbLayer, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
167                 }
168
169                 // Pass the returning result through
170                 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
171         }
172
173         /**
174          * Gets cached data from the database layer and if not found fetch it from
175          * the database again. This method does not return the header stuff because
176          * The underlaying database class will return only the requested content.
177          *
178          * @param               $idNumber               The ID number which we need for looking up
179          *                                              the requested data
180          * @return      $cachedArray    The maybe cached data from the database
181          * @throws      NullPointerException    If $this->dbLayer is null
182          * @throws      NoObjectException               If $this->dbLayer is not an object
183          * @throws      MissingMethodException  If the required method
184          *                                                              isUniqueIdUsed() was not found
185          */
186         public final function getObjectFromCachedData ($idNumber) {
187                 // Some sanity checks
188                 if (is_null($this->dbLayer)) {
189                         // Is null
190                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
191                 } elseif (!is_object($this->dbLayer)) {
192                         // Is not an object
193                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
194                 } elseif (!method_exists($this->dbLayer, 'getObjectFromCachedData')) {
195                         // Does not have the required instance
196                         throw new MissingMethodException(array($this->dbLayer, 'getObjectFromCachedData'), self::EXCEPTION_MISSING_METHOD);
197                 }
198
199                 // Pass the returning result through
200                 return $this->dbLayer->getObjectFromCachedData($idNumber);
201         }
202
203         /**
204          * Setter for the real database layer
205          * @param               $dbLayer                An instance of the real database layer
206          * @return      void
207          */
208         public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
209                 $this->dbLayer = $dbLayer;
210         }
211 }
212
213 // [EOF]
214 ?>