3 // Database selector class
4 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
5 // Array for connection data
6 private $connectData = array();
8 // The real database layer
9 private $dbLayer = null;
11 // An instance of this class
12 private static $thisInstance = null;
14 // Private constructor
15 private final function __construct() {
16 // Call parent constructor
17 parent::constructor(__CLASS__);
20 $this->setPartDescr("Datenbank-Mittelschicht");
22 // Create an unique ID
23 $this->createUniqueID();
26 $this->removeSystemArray();
29 // Create new database connection layer
30 public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
32 $dbInstance = new DatabaseConnection();
34 // Set debug output handler
35 $dbInstance->setDebugInstance($debugInstance);
38 $dbInstance->setDatabaseLayer($dbLayer);
41 self::$thisInstance = $dbInstance;
47 // Get an instance of this class
48 public final static function getInstance () {
49 return self::$thisInstance;
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;
62 * Save a whole object or parts of it to the database or local file
64 * @param $object The object we shall save
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
71 public final function saveObject ($object) {
73 if (is_null($this->dbLayer)) {
75 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
76 } elseif (!is_object($this->dbLayer)) {
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);
84 // For now just pipe it through to the database layer
85 $this->dbLayer->saveObject($object);
89 * Set a limitation for the saving process. This shall be done before
90 * saveObject() is called else saveObject() shall save the whole object.
92 * @param $limitInstance An instance of ObjectLimits which contains
93 * elements we shall exclusivly include in
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
101 public final function limitObject (ObjectLimits $limitInstance) {
102 // Get real database connection
103 $this->dbLayer = $this->getDatabaseInstance();
105 // Some sanity checks
106 if (is_null($this->dbLayer)) {
108 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
109 } elseif (!is_object($this->dbLayer)) {
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);
117 // For now we pipe this through to the real database instance
118 $this->dbLayer->limitObject($limitInstance);
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.
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
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
136 public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
137 // Some sanity checks
138 if (is_null($this->dbLayer)) {
140 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
141 } elseif (!is_object($this->dbLayer)) {
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);
149 // Pass the returning result through
150 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
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.
158 * @param $idNumber The ID number which we need for looking up
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
166 public final function getObjectFromCachedData ($idNumber) {
167 // Some sanity checks
168 if (is_null($this->dbLayer)) {
170 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
171 } elseif (!is_object($this->dbLayer)) {
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);
179 // Pass the returning result through
180 return $this->dbLayer->getObjectFromCachedData($idNumber);
184 * Setter for the real database layer
185 * @param $dbLayer An instance of the real database layer
188 public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
189 $this->dbLayer = $dbLayer;