]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/middleware/database/class_DatabaseConnection.php
87e89f2753122d1d8bbc43f057cdab1fc09f536a
[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.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         /**
26          * Array for connection data
27          */
28         private $connectData = array(
29                 'login' => "",
30                 'pass'  => "",
31                 'dbase' => "",
32                 'host'  => ""
33         );
34
35         // The real database layer
36         private $dbLayer = null;
37
38         // An instance of this class
39         private static $thisInstance = null;
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct() {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Set description
51                 $this->setObjectDescription("Datenbank-Mittelschicht");
52
53                 // Create an unique ID
54                 $this->createUniqueID();
55         }
56
57         // Create new database connection layer
58         public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
59                 // Get instance
60                 $dbInstance = new DatabaseConnection();
61
62                 // Set debug output handler
63                 $dbInstance->setDebugInstance($debugInstance);
64
65                 // Set database layer
66                 $dbInstance->setDatabaseLayer($dbLayer);
67
68                 // Set db instance
69                 self::$thisInstance = $dbInstance;
70
71                 // Return instance
72                 return $dbInstance;
73         }
74
75         // Get an instance of this class
76         public final static function getInstance () {
77                 return self::$thisInstance;
78         }
79
80         // Public setter for database connection
81         public final function setConnectionData ($login, $pass, $dbase, $host="localhost") {
82                 // Transfer connection data
83                 $this->connectData['login'] = (string) $login;
84                 $this->connectData['pass']  = (string) $pass;
85                 $this->connectData['dbase'] = (string) $dbase;
86                 $this->connectData['host']  = (string) $host;
87         }
88
89         /**
90          * Getter for connection data
91          *
92          * @return      $connectData    Connection data stored with this clas
93          */
94         public final function getConnectionData () {
95                 return $this->connectData;
96         }
97
98         /**
99          * Save a whole object or parts of it to the database or local file
100          *
101          * @param       $object         The object we shall save
102          * @return      void
103          */
104         public function saveObject (FrameworkInterface $object) {
105                 // Connect to the database
106                 $this->dbLayer->connectToDatabase();
107
108                 // For now just pipe it through to the database layer
109                 $this->dbLayer->saveObject($object);
110         }
111
112         /**
113          * Set a limitation for the saving process. This shall be done before
114          * saveObject() is called else saveObject() shall save the whole object.
115          *
116          * @param       $limitInstance  An instance of ObjectLimits which contains
117          *                                                      elements we shall exclusivly include in
118          *                                                      saving process
119          * @return      void
120          */
121         public function limitObject (ObjectLimits $limitInstance) {
122                 // Connect to the database
123                 $this->dbLayer->connectToDatabase();
124
125                 // For now we pipe this through to the real database instance
126                 $this->dbLayer->limitObject($limitInstance);
127         }
128
129         /**
130          * Analyses if a unique ID has already been used or not. This method does
131          * only pass the given ID through to the "real" database layer.
132          *
133          * @param       $uniqueID               A unique ID number which shall be checked
134          *                                                      before it will be used
135          * @param       $inConstructor  If called from a constructor or from
136          *                                                      somewhere else
137          * @return      $isUnused               true    = The unique ID was not found in the database,
138          *                                                      false = It is already in use by an other object
139          */
140         public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
141                 // Connect to the database
142                 $this->dbLayer->connectToDatabase();
143
144                 // Pass the returning result through
145                 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
146         }
147
148         /**
149          * Gets cached data from the database layer and if not found fetch it from
150          * the database again. This method does not return the header stuff because
151          * the underlaying database class will return only the requested content.
152          *
153          * @param       $idNumber               The ID number which we need for looking up
154          *                                                      the requested data
155          * @return      $cachedArray    The maybe cached data from the database
156          */
157         public function getObjectFromCachedData ($idNumber) {
158                 // Connect to the database
159                 $this->dbLayer->connectToDatabase();
160
161                 // Pass the returning result through
162                 return $this->dbLayer->getObjectFromCachedData($idNumber);
163         }
164
165         /**
166          * Setter for the real database layer
167          * @param       $dbLayer        An instance of the real database layer
168          * @return      void
169          */
170         public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
171                 $this->dbLayer = $dbLayer;
172         }
173
174         /**
175          * Runs a "select" statement on the database layer with given table name
176          * and criteria. If this doesn't fail the result will be returned
177          *
178          * @param       $tableName                      Name of the "table" we shall query
179          * @param       $criteriaInstance       An instance of a Criteria class
180          * @return      $result                         The result as an array
181          */
182         public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
183                 // Connect to the database
184                 $this->dbLayer->connectToDatabase();
185
186                 // Get result from query
187                 $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);
188
189                 // Return the result
190                 return $result;
191         }
192
193         /**
194          * Getter for last exception
195          *
196          * @return      $exceptionInstance      Last thrown exception
197          */
198         public final function getLastException () {
199                 $exceptionInstance = $this->dbLayer->getLastException();
200                 return $exceptionInstance;
201         }
202
203         /**
204          * "Inserts" a data set instance into a local file database folder
205          *
206          * @param       $dataSetInstance        A storeable data set
207          * @return      void
208          */
209         public function insertDataSet (StoreableCriteria $dataSetInstance) {
210                 // Connect to the database
211                 $this->dbLayer->connectToDatabase();
212
213                 // Ask the database layer
214                 $this->dbLayer->insertDataSet($requestInstance);
215         }
216 }
217
218 // [EOF]
219 ?>