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