0.3.0 inital import
[mailer.git] / inc / classes / middleware / database / class_DatabaseConnection.php
1 <?php
2
3 // Database selector class
4 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
5         // Array for connection data
6         private $connectData = array();
7
8         // The real database layer
9         private $dbLayer = null;
10
11         // An instance of this class
12         private static $thisInstance = null;
13
14         // Private constructor
15         private final function __construct() {
16                 // Call parent constructor
17                 parent::constructor(__CLASS__);
18
19                 // Set description
20                 $this->setPartDescr("Datenbank-Mittelschicht");
21
22                 // Create an unique ID
23                 $this->createUniqueID();
24
25                 // Clean up a little
26                 $this->removeSystemArray();
27         }
28
29         // Create new database connection layer
30         public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
31                 // Get instance
32                 $dbInstance = new DatabaseConnection();
33
34                 // Set debug output handler
35                 $dbInstance->setDebugInstance($debugInstance);
36
37                 // Set database layer
38                 $dbInstance->setDatabaseLayer($dbLayer);
39
40                 // Set db instance
41                 self::$thisInstance = $dbInstance;
42
43                 // Return instance
44                 return $dbInstance;
45         }
46
47         // Get an instance of this class
48         public final static function getInstance () {
49                 return self::$thisInstance;
50         }
51
52         // Public setter for database connection
53         public final function setConnectionData ($login, $pass, $dbase, $host) {
54                 // Transfer connection data
55                 $this->connectData['login'] = (string) $login;
56                 $this->connectData['pass']  = (string) $pass;
57                 $this->connectData['dbase'] = (string) $dbase;
58                 $this->connectData['host']  = (string) $host;
59         }
60
61         /**
62          * Save a whole object or parts of it to the database or local file
63          *
64          * @param               $object The object we shall save
65          * @return      void
66          * @throws      NullPointerException    If $limitInstance is null
67          * @throws      NoObjectException               If $limitInstance is not an object
68          * @throws      MissingMethodException  If the required method
69          *                                                              saveObject() was not found
70          */
71         public final function saveObject ($object) {
72                 // Some sanity checks
73                 if (is_null($this->dbLayer)) {
74                         // Is null
75                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
76                 } elseif (!is_object($this->dbLayer)) {
77                         // Is not an object
78                         throw new NoObjectException($this->dbLayer, self::EXCEPTION_IS_NO_OBJECT);
79                 } elseif (!method_exists($this->dbLayer, 'saveObject')) {
80                         // Does not have the required instance
81                         throw new MissingMethodException(array($this->dbLayer, 'saveObject'), self::EXCEPTION_MISSING_METHOD);
82                 }
83
84                 // For now just pipe it through to the database layer
85                 $this->dbLayer->saveObject($object);
86         }
87
88         /**
89          * Set a limitation for the saving process. This shall be done before
90          * saveObject() is called else saveObject() shall save the whole object.
91          *
92          * @param               $limitInstance  An instance of ObjectLimits which contains
93          *                                              elements we shall exclusivly include in
94          *                                              saving process
95          * @return      void
96          * @throws      NullPointerException    If $limitInstance is null
97          * @throws      NoObjectException               If $limitInstance is not an object
98          * @throws      MissingMethodException  If the required method
99          *                                                              limitObject() was not found
100          */
101         public final function limitObject (ObjectLimits $limitInstance) {
102                 // Get real database connection
103                 $this->dbLayer = $this->getDatabaseInstance();
104
105                 // Some sanity checks
106                 if (is_null($this->dbLayer)) {
107                         // Is null
108                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
109                 } elseif (!is_object($this->dbLayer)) {
110                         // Is not an object
111                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
112                 } elseif (!method_exists($this->dbLayer, 'limitObject')) {
113                         // Does not have the required instance
114                         throw new MissingMethodException(array($this->dbLayer, 'limitObject'), self::EXCEPTION_MISSING_METHOD);
115                 }
116
117                 // For now we pipe this through to the real database instance
118                 $this->dbLayer->limitObject($limitInstance);
119         }
120
121         /**
122          * Analyses if a unique ID has already been used or not. This method does
123          * only pass the given ID through to the "real" database layer.
124          *
125          * @param               $uniqueID               A unique ID number which shall be checked
126          *                                              before it will be used
127          * @param               $inConstructor  If called from a constructor or from
128          *                                              somewhere else
129          * @return      $isUnused               true    = The unique ID was not found in the database,
130          *                                              false = It is already in use by an other object
131          * @throws      NullPointerException    If $this->dbLayer is null
132          * @throws      NoObjectException               If $this->dbLayer is not an object
133          * @throws      MissingMethodException  If the required method
134          *                                                              isUniqueIdUsed() was not found
135          */
136         public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
137                 // Some sanity checks
138                 if (is_null($this->dbLayer)) {
139                         // Is null
140                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
141                 } elseif (!is_object($this->dbLayer)) {
142                         // Is not an object
143                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
144                 } elseif (!method_exists($this->dbLayer, 'isUniqueIdUsed')) {
145                         // Does not have the required instance
146                         throw new MissingMethodException(array($this->dbLayer, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
147                 }
148
149                 // Pass the returning result through
150                 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
151         }       
152
153         /**
154          * Gets cached data from the database layer and if not found fetch it from
155          * the database again. This method does not return the header stuff because
156          * The underlaying database class will return only the requested content.
157          *
158          * @param               $idNumber               The ID number which we need for looking up
159          *                                              the requested data
160          * @return      $cachedArray    The maybe cached data from the database
161          * @throws      NullPointerException    If $this->dbLayer is null
162          * @throws      NoObjectException               If $this->dbLayer is not an object
163          * @throws      MissingMethodException  If the required method
164          *                                                              isUniqueIdUsed() was not found
165          */
166         public final function getObjectFromCachedData ($idNumber) {
167                 // Some sanity checks
168                 if (is_null($this->dbLayer)) {
169                         // Is null
170                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
171                 } elseif (!is_object($this->dbLayer)) {
172                         // Is not an object
173                         throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
174                 } elseif (!method_exists($this->dbLayer, 'getObjectFromCachedData')) {
175                         // Does not have the required instance
176                         throw new MissingMethodException(array($this->dbLayer, 'getObjectFromCachedData'), self::EXCEPTION_MISSING_METHOD);
177                 }
178
179                 // Pass the returning result through
180                 return $this->dbLayer->getObjectFromCachedData($idNumber);
181         }
182
183         /**
184          * Setter for the real database layer
185          * @param               $dbLayer                An instance of the real database layer
186          * @return      void
187          */
188         public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
189                 $this->dbLayer = $dbLayer;
190         }
191 }
192
193 // [EOF]
194 ?>