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