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