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
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, LimitableObject {
25 // Array for connection data
26 private $connectData = array();
28 // The real database layer
29 private $dbLayer = null;
31 // An instance of this class
32 private static $thisInstance = null;
34 // Private constructor
35 protected function __construct() {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
40 $this->setObjectDescription("Datenbank-Mittelschicht");
42 // Create an unique ID
43 $this->createUniqueID();
46 $this->removeSystemArray();
49 // Create new database connection layer
50 public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
52 $dbInstance = new DatabaseConnection();
54 // Set debug output handler
55 $dbInstance->setDebugInstance($debugInstance);
58 $dbInstance->setDatabaseLayer($dbLayer);
61 self::$thisInstance = $dbInstance;
67 // Get an instance of this class
68 public final static function getInstance () {
69 return self::$thisInstance;
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;
82 * Save a whole object or parts of it to the database or local file
84 * @param $object The object we shall save
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
91 public final function saveObject ($object) {
93 if (is_null($this->dbLayer)) {
95 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96 } elseif (!is_object($this->dbLayer)) {
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);
104 // For now just pipe it through to the database layer
105 $this->dbLayer->saveObject($object);
109 * Set a limitation for the saving process. This shall be done before
110 * saveObject() is called else saveObject() shall save the whole object.
112 * @param $limitInstance An instance of ObjectLimits which contains
113 * elements we shall exclusivly include in
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
121 public final function limitObject (ObjectLimits $limitInstance) {
122 // Get real database connection
123 $this->dbLayer = $this->getDatabaseInstance();
125 // Some sanity checks
126 if (is_null($this->dbLayer)) {
128 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
129 } elseif (!is_object($this->dbLayer)) {
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);
137 // For now we pipe this through to the real database instance
138 $this->dbLayer->limitObject($limitInstance);
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.
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
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
156 public final function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
157 // Some sanity checks
158 if (is_null($this->dbLayer)) {
160 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
161 } elseif (!is_object($this->dbLayer)) {
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);
169 // Pass the returning result through
170 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
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.
178 * @param $idNumber The ID number which we need for looking up
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
186 public final function getObjectFromCachedData ($idNumber) {
187 // Some sanity checks
188 if (is_null($this->dbLayer)) {
190 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
191 } elseif (!is_object($this->dbLayer)) {
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);
199 // Pass the returning result through
200 return $this->dbLayer->getObjectFromCachedData($idNumber);
204 * Setter for the real database layer
205 * @param $dbLayer An instance of the real database layer
208 public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
209 $this->dbLayer = $dbLayer;