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