]> git.mxchange.org Git - core.git/blob - framework/main/middleware/database/class_DatabaseConnection.php
Continued:
[core.git] / framework / main / middleware / database / class_DatabaseConnection.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Connection\Database;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Connector\Database\DatabaseConnector;
7 use Org\Mxchange\CoreFramework\Criteria\Criteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\Database\Backend\DatabaseBackend;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
12 use Org\Mxchange\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 - 2020 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          * @todo        $debugInstance is currently not used
71          */
72         public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
73                 // Get instance
74                 $databaseInstance = new DatabaseConnection();
75
76                 // Set database layer
77                 $databaseInstance->setDatabaseLayer($dbLayer);
78
79                 // Set db instance
80                 self::$selfInstance = $databaseInstance;
81
82                 // Return instance
83                 return $databaseInstance;
84         }
85
86         /**
87          * Getter for this class
88          *
89          * @return      $selfInstance   An instance of this class
90          */
91         public static final function getSelfInstance () {
92                 return self::$selfInstance;
93         }
94
95         /**
96          * Setter for all database connection data. All these parameters may be
97          * supported by the underlaying backend.
98          *
99          * @param       $login  Login name to database
100          * @param       $pass   Passwort for above login
101          * @param       $dbase  Name of used database
102          * @param       $host   Host to connect to (default: 127.0.0.1)
103          * @return      void
104          */
105         public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
106                 // Transfer connection data
107                 $this->connectData['login'] = (string) $login;
108                 $this->connectData['pass']  = (string) $pass;
109                 $this->connectData['dbase'] = (string) $dbase;
110                 $this->connectData['host']  = (string) $host;
111         }
112
113         /**
114          * Getter for connection data
115          *
116          * @return      $connectData    Connection data stored with this clas
117          */
118         public final function getConnectionData () {
119                 return $this->connectData;
120         }
121
122         /**
123          * Setter for the real database layer
124          * @param       $dbLayer        An instance of the real database layer
125          * @return      void
126          */
127         public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
128                 $this->dbLayer = $dbLayer;
129         }
130
131         /**
132          * Getter for index key
133          *
134          * @return      $indexKey       Index key
135          */
136         public final function getIndexKey () {
137                 return $this->dbLayer->getIndexKey();
138         }
139
140         /**
141          * Runs a 'select' statement on the database layer with given table name
142          * and criteria. If this doesn't fail the result will be returned
143          *
144          * @param       $tableName                      Name of the 'table' we shall query
145          * @param       $criteriaInstance       An instance of a Criteria class
146          * @return      $result                         The result as an array
147          */
148         public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
149                 // Connect to the database
150                 $this->dbLayer->connectToDatabase();
151
152                 // Get result from query
153                 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
154
155                 // Return the result
156                 return $result;
157         }
158
159         /**
160          * Getter for last exception
161          *
162          * @return      $exceptionInstance      Last thrown exception
163          */
164         public final function getLastException () {
165                 $exceptionInstance = $this->dbLayer->getLastException();
166                 return $exceptionInstance;
167         }
168
169         /**
170          * 'Inserts' a data set instance into a local file database folder
171          *
172          * @param       $dataSetInstance        A storeable data set
173          * @return      void
174          */
175         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
176                 // Connect to the database
177                 $this->dbLayer->connectToDatabase();
178
179                 // Ask the database layer
180                 $this->dbLayer->queryInsertDataSet($dataSetInstance);
181         }
182
183         /**
184          * 'Updates' a data set instance with a database layer
185          *
186          * @param       $dataSetInstance        A storeable data set
187          * @return      void
188          */
189         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
190                 // Connect to the database
191                 $this->dbLayer->connectToDatabase();
192
193                 // Ask the database layer
194                 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
195         }
196
197         /**
198          * Getter for primary key column of specified table name
199          *
200          * @param       $tableName              Name of table we need the primary key column from
201          * @return      $primaryKey             Primary key column of requested table
202          */
203         public function getPrimaryKeyOfTable ($tableName) {
204                 // Connect to the database
205                 $this->dbLayer->connectToDatabase();
206
207                 // Ask the database layer
208                 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
209
210                 // Return the value
211                 return $primaryKey;
212         }
213
214         /**
215          * Removes non-public data from given array.
216          *
217          * @param       $data   An array with possible non-public data that needs to be removed.
218          * @return      $data   A cleaned up array with only public data.
219          */
220         public function removeNonPublicDataFromArray (array $data) {
221                 // Connect to the database
222                 $this->dbLayer->connectToDatabase();
223
224                 // Call database backend
225                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
226                 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
227
228                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
229                 return $data;
230         }
231
232         /**
233          * Count total table rows
234          *
235          * @param       $tableName      Table name
236          * @return      $count          Total row count
237          */
238         public function countTotalRows ($tableName) {
239                 // Connect to the database
240                 $this->dbLayer->connectToDatabase();
241
242                 // Ask the database layer
243                 $count = $this->dbLayer->countTotalRows($tableName);
244
245                 // Return the value
246                 return $count;
247         }
248
249 }