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 * Database selector class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 <<<<<<< HEAD:framework/main/middleware/database/class_DatabaseConnection.php
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
23 >>>>>>> Some updates::inc/main/middleware/database/class_DatabaseConnection.php
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
42 * Array for connection data
44 private $connectData = array(
52 * The real database layer
54 private $dbLayer = NULL;
57 * An instance of this class
59 private static $selfInstance = NULL;
62 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates a new database connection layer
72 * @param $debugInstance An instance of a DebugMiddleware class
73 * @param $dbLayer An instance of a DatabaseBackend class
74 * @todo $debugInstance is currently not used
76 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
78 $databaseInstance = new DatabaseConnection();
81 $databaseInstance->setDatabaseLayer($dbLayer);
84 self::$selfInstance = $databaseInstance;
87 return $databaseInstance;
91 * Getter for this class
93 * @return $selfInstance An instance of this class
95 public static final function getSelfInstance () {
96 return self::$selfInstance;
100 * Setter for all database connection data. All these parameters may be
101 * supported by the underlaying backend.
103 * @param $login Login name to database
104 * @param $pass Passwort for above login
105 * @param $dbase Name of used database
106 * @param $host Host to connect to (default: 127.0.0.1)
109 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
110 // Transfer connection data
111 $this->connectData['login'] = (string) $login;
112 $this->connectData['pass'] = (string) $pass;
113 $this->connectData['dbase'] = (string) $dbase;
114 $this->connectData['host'] = (string) $host;
118 * Getter for connection data
120 * @return $connectData Connection data stored with this clas
122 public final function getConnectionData () {
123 return $this->connectData;
127 * Setter for the real database layer
128 * @param $dbLayer An instance of the real database layer
131 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
132 $this->dbLayer = $dbLayer;
136 * Getter for index key
138 * @return $indexKey Index key
140 public final function getIndexKey () {
141 return $this->dbLayer->getIndexKey();
145 * Runs a 'select' statement on the database layer with given table name
146 * and criteria. If this doesn't fail the result will be returned
148 * @param $tableName Name of the 'table' we shall query
149 * @param $criteriaInstance An instance of a Criteria class
150 * @return $result The result as an array
152 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
153 // Connect to the database
154 $this->dbLayer->connectToDatabase();
156 // Get result from query
157 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
164 * Getter for last exception
166 * @return $exceptionInstance Last thrown exception
168 public final function getLastException () {
169 $exceptionInstance = $this->dbLayer->getLastException();
170 return $exceptionInstance;
174 * 'Inserts' a data set instance into a local file database folder
176 * @param $dataSetInstance A storeable data set
179 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
180 // Connect to the database
181 $this->dbLayer->connectToDatabase();
183 // Ask the database layer
184 $this->dbLayer->queryInsertDataSet($dataSetInstance);
188 * 'Updates' a data set instance with a database layer
190 * @param $dataSetInstance A storeable data set
193 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
194 // Connect to the database
195 $this->dbLayer->connectToDatabase();
197 // Ask the database layer
198 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
202 * Getter for primary key column of specified table name
204 * @param $tableName Name of table we need the primary key column from
205 * @return $primaryKey Primary key column of requested table
207 public function getPrimaryKeyOfTable ($tableName) {
208 // Connect to the database
209 $this->dbLayer->connectToDatabase();
211 // Ask the database layer
212 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
219 * Removes non-public data from given array.
221 * @param $data An array with possible non-public data that needs to be removed.
222 * @return $data A cleaned up array with only public data.
224 public function removeNonPublicDataFromArray (array $data) {
225 // Connect to the database
226 $this->dbLayer->connectToDatabase();
228 // Call database backend
229 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
230 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
232 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
237 * Count total table rows
239 * @param $tableName Table name
240 * @return $count Total row count
242 public function countTotalRows ($tableName) {
243 // Connect to the database
244 $this->dbLayer->connectToDatabase();
246 // Ask the database layer
247 $count = $this->dbLayer->countTotalRows($tableName);