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