3 namespace Org\Mxchange\CoreFramework\Connection\Database;
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;
15 use \InvalidArgumentException;
18 * Database selector class
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
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.
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.
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/>.
39 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
41 * Array for connection data
43 private $connectData = array(
51 * The real database layer
53 private $backendInstance = NULL;
56 * An instance of this class
58 private static $selfInstance = NULL;
61 * Protected constructor
63 protected function __construct () {
64 // Call parent constructor
65 parent::__construct(__CLASS__);
69 * Creates a new database connection layer
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
75 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $backendInstance) {
77 $databaseInstance = new DatabaseConnection();
80 $databaseInstance->setDatabaseLayer($backendInstance);
83 self::$selfInstance = $databaseInstance;
86 return $databaseInstance;
90 * Getter for this class
92 * @return $selfInstance An instance of this class
94 public static final function getSelfInstance () {
95 return self::$selfInstance;
99 * Setter for all database connection data. All these parameters may be
100 * supported by the underlaying backend.
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)
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;
117 * Getter for connection data
119 * @return $connectData Connection data stored with this clas
121 public final function getConnectionData () {
122 return $this->connectData;
126 * Setter for the real database layer
127 * @param $backendInstance An instance of a DatabaseBackend class
130 public final function setDatabaseLayer (DatabaseBackend $backendInstance) {
131 $this->backendInstance = $backendInstance;
135 * Getter for index key
137 * @return $indexKey Index key
139 public final function getIndexKey () {
140 return $this->backendInstance->getIndexKey();
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
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
152 public function doSelectByTableCriteria (string $tableName, Criteria $criteriaInstance) {
153 // Validate parameter
154 if (empty($tableName)) {
156 throw new InvalidArgumentException('Parameter "tableName" is empty');
159 // Connect to the database
160 $this->backendInstance->connectToDatabase();
162 // Get result from query
163 $result = $this->backendInstance->querySelect($tableName, $criteriaInstance);
170 * Getter for last exception
172 * @return $exceptionInstance Last thrown exception
174 public final function getLastException () {
175 $exceptionInstance = $this->backendInstance->getLastException();
176 return $exceptionInstance;
180 * 'Inserts' a data set instance into a local file database folder
182 * @param $dataSetInstance A storeable data set
185 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
186 // Connect to the database
187 $this->backendInstance->connectToDatabase();
189 // Ask the database layer
190 $this->backendInstance->queryInsertDataSet($dataSetInstance);
194 * 'Updates' a data set instance with a database layer
196 * @param $dataSetInstance A storeable data set
199 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
200 // Connect to the database
201 $this->backendInstance->connectToDatabase();
203 // Ask the database layer
204 $this->backendInstance->queryUpdateDataSet($dataSetInstance);
208 * Getter for primary key column of specified table name
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
214 public function getPrimaryKeyOfTable (string $tableName) {
215 // Validate parameter
216 if (empty($tableName)) {
218 throw new InvalidArgumentException('Parameter "tableName" is empty');
221 // Connect to the database
222 $this->backendInstance->connectToDatabase();
224 // Ask the database layer
225 $primaryKey = $this->backendInstance->getPrimaryKeyOfTable($tableName);
232 * Removes non-public data from given array.
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.
237 public function removeNonPublicDataFromArray (array $data) {
238 // Connect to the database
239 $this->backendInstance->connectToDatabase();
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);
245 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
250 * Count total table rows
252 * @param $tableName Table name
253 * @return $count Total row count
254 * @throws InvalidArgumentException If a parameter is empty
256 public function countTotalRows (string $tableName) {
257 // Validate parameter
258 if (empty($tableName)) {
260 throw new InvalidArgumentException('Parameter "tableName" is empty');
263 // Connect to the database
264 $this->backendInstance->connectToDatabase();
266 // Ask the database layer
267 $count = $this->backendInstance->countTotalRows($tableName);