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