Continued:
[core.git] / inc / main / middleware / database / class_DatabaseConnection.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Connection\Database;
4
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Registry\Registerable;
8 use CoreFramework\Middleware\BaseMiddleware;
9 use CoreFramework\Middleware\Debug\DebugMiddleware;
10
11 /**
12  * Database selector class
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
34         /**
35          * Array for connection data
36          */
37         private $connectData = array(
38                 'login' => '',
39                 'pass'  => '',
40                 'dbase' => '',
41                 'host'  => ''
42         );
43
44         /**
45          * The real database layer
46          */
47         private $dbLayer = NULL;
48
49         /**
50          * An instance of this class
51          */
52         private static $selfInstance = NULL;
53
54         /**
55          * Protected constructor
56          */
57         protected function __construct () {
58                 // Call parent constructor
59                 parent::__construct(__CLASS__);
60         }
61
62         /**
63          * Creates a new database connection layer
64          *
65          * @param       $debugInstance  An instance of a DebugMiddleware class
66          * @param       $dbLayer                An instance of a DatabaseBackend class
67          */
68         public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
69                 // Get instance
70                 $databaseInstance = new DatabaseConnection();
71
72                 // Set database layer
73                 $databaseInstance->setDatabaseLayer($dbLayer);
74
75                 // Set db instance
76                 self::$selfInstance = $databaseInstance;
77
78                 // Return instance
79                 return $databaseInstance;
80         }
81
82         /**
83          * Getter for this class
84          *
85          * @return      $selfInstance   An instance of this class
86          */
87         public static final function getSelfInstance () {
88                 return self::$selfInstance;
89         }
90
91         /**
92          * Setter for all database connection data. All these parameters may be
93          * supported by the underlaying backend.
94          *
95          * @param       $login  Login name to database
96          * @param       $pass   Passwort for above login
97          * @param       $dbase  Name of used database
98          * @param       $host   Host to connect to (default: 127.0.0.1)
99          * @return      void
100          */
101         public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
102                 // Transfer connection data
103                 $this->connectData['login'] = (string) $login;
104                 $this->connectData['pass']  = (string) $pass;
105                 $this->connectData['dbase'] = (string) $dbase;
106                 $this->connectData['host']  = (string) $host;
107         }
108
109         /**
110          * Getter for connection data
111          *
112          * @return      $connectData    Connection data stored with this clas
113          */
114         public final function getConnectionData () {
115                 return $this->connectData;
116         }
117
118         /**
119          * Setter for the real database layer
120          * @param       $dbLayer        An instance of the real database layer
121          * @return      void
122          */
123         public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
124                 $this->dbLayer = $dbLayer;
125         }
126
127         /**
128          * Getter for index key
129          *
130          * @return      $indexKey       Index key
131          */
132         public final function getIndexKey () {
133                 return $this->dbLayer->getIndexKey();
134         }
135
136         /**
137          * Runs a 'select' statement on the database layer with given table name
138          * and criteria. If this doesn't fail the result will be returned
139          *
140          * @param       $tableName                      Name of the 'table' we shall query
141          * @param       $criteriaInstance       An instance of a Criteria class
142          * @return      $result                         The result as an array
143          */
144         public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
145                 // Connect to the database
146                 $this->dbLayer->connectToDatabase();
147
148                 // Get result from query
149                 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
150
151                 // Return the result
152                 return $result;
153         }
154
155         /**
156          * Getter for last exception
157          *
158          * @return      $exceptionInstance      Last thrown exception
159          */
160         public final function getLastException () {
161                 $exceptionInstance = $this->dbLayer->getLastException();
162                 return $exceptionInstance;
163         }
164
165         /**
166          * 'Inserts' a data set instance into a local file database folder
167          *
168          * @param       $dataSetInstance        A storeable data set
169          * @return      void
170          */
171         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
172                 // Connect to the database
173                 $this->dbLayer->connectToDatabase();
174
175                 // Ask the database layer
176                 $this->dbLayer->queryInsertDataSet($dataSetInstance);
177         }
178
179         /**
180          * 'Updates' a data set instance with a database layer
181          *
182          * @param       $dataSetInstance        A storeable data set
183          * @return      void
184          */
185         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
186                 // Connect to the database
187                 $this->dbLayer->connectToDatabase();
188
189                 // Ask the database layer
190                 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
191         }
192
193         /**
194          * Getter for primary key column of specified table name
195          *
196          * @param       $tableName              Name of table we need the primary key column from
197          * @return      $primaryKey             Primary key column of requested table
198          */
199         public function getPrimaryKeyOfTable ($tableName) {
200                 // Connect to the database
201                 $this->dbLayer->connectToDatabase();
202
203                 // Ask the database layer
204                 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
205
206                 // Return the value
207                 return $primaryKey;
208         }
209
210         /**
211          * Removes non-public data from given array.
212          *
213          * @param       $data   An array with possible non-public data that needs to be removed.
214          * @return      $data   A cleaned up array with only public data.
215          */
216         public function removeNonPublicDataFromArray (array $data) {
217                 // Connect to the database
218                 $this->dbLayer->connectToDatabase();
219
220                 // Call database backend
221                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
222                 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
223
224                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
225                 return $data;
226         }
227
228         /**
229          * Count total table rows
230          *
231          * @param       $tableName      Table name
232          * @return      $count          Total row count
233          */
234         public function countTotalRows ($tableName) {
235                 // Connect to the database
236                 $this->dbLayer->connectToDatabase();
237
238                 // Ask the database layer
239                 $count = $this->dbLayer->countTotalRows($tableName);
240
241                 // Return the value
242                 return $count;
243         }
244
245 }