3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7 use CoreFramework\Middleware\BaseMiddleware;
8 use CoreFramework\Middleware\Debug\DebugMiddleware;
11 * Database selector class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
34 * Array for connection data
36 private $connectData = array(
44 * The real database layer
46 private $dbLayer = NULL;
49 * An instance of this class
51 private static $selfInstance = NULL;
54 * Protected constructor
56 protected function __construct () {
57 // Call parent constructor
58 parent::__construct(__CLASS__);
62 * Creates a new database connection layer
64 * @param $debugInstance An instance of a DebugMiddleware class
65 * @param $dbLayer An instance of a DatabaseBackend class
67 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
69 $databaseInstance = new DatabaseConnection();
72 $databaseInstance->setDatabaseLayer($dbLayer);
75 self::$selfInstance = $databaseInstance;
78 return $databaseInstance;
82 * Getter for this class
84 * @return $selfInstance An instance of this class
86 public static final function getSelfInstance () {
87 return self::$selfInstance;
91 * Setter for all database connection data. All these parameters may be
92 * supported by the underlaying backend.
94 * @param $login Login name to database
95 * @param $pass Passwort for above login
96 * @param $dbase Name of used database
97 * @param $host Host to connect to (default: 127.0.0.1)
100 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
101 // Transfer connection data
102 $this->connectData['login'] = (string) $login;
103 $this->connectData['pass'] = (string) $pass;
104 $this->connectData['dbase'] = (string) $dbase;
105 $this->connectData['host'] = (string) $host;
109 * Getter for connection data
111 * @return $connectData Connection data stored with this clas
113 public final function getConnectionData () {
114 return $this->connectData;
118 * Setter for the real database layer
119 * @param $dbLayer An instance of the real database layer
122 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
123 $this->dbLayer = $dbLayer;
127 * Getter for index key
129 * @return $indexKey Index key
131 public final function getIndexKey () {
132 return $this->dbLayer->getIndexKey();
136 * Runs a 'select' statement on the database layer with given table name
137 * and criteria. If this doesn't fail the result will be returned
139 * @param $tableName Name of the 'table' we shall query
140 * @param $criteriaInstance An instance of a Criteria class
141 * @return $result The result as an array
143 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
144 // Connect to the database
145 $this->dbLayer->connectToDatabase();
147 // Get result from query
148 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
155 * Getter for last exception
157 * @return $exceptionInstance Last thrown exception
159 public final function getLastException () {
160 $exceptionInstance = $this->dbLayer->getLastException();
161 return $exceptionInstance;
165 * 'Inserts' a data set instance into a local file database folder
167 * @param $dataSetInstance A storeable data set
170 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
171 // Connect to the database
172 $this->dbLayer->connectToDatabase();
174 // Ask the database layer
175 $this->dbLayer->queryInsertDataSet($dataSetInstance);
179 * 'Updates' a data set instance with a database layer
181 * @param $dataSetInstance A storeable data set
184 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
185 // Connect to the database
186 $this->dbLayer->connectToDatabase();
188 // Ask the database layer
189 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
193 * Getter for primary key column of specified table name
195 * @param $tableName Name of table we need the primary key column from
196 * @return $primaryKey Primary key column of requested table
198 public function getPrimaryKeyOfTable ($tableName) {
199 // Connect to the database
200 $this->dbLayer->connectToDatabase();
202 // Ask the database layer
203 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
210 * Removes non-public data from given array.
212 * @param $data An array with possible non-public data that needs to be removed.
213 * @return $data A cleaned up array with only public data.
215 public function removeNonPublicDataFromArray (array $data) {
216 // Connect to the database
217 $this->dbLayer->connectToDatabase();
219 // Call database backend
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
221 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
228 * Count total table rows
230 * @param $tableName Table name
231 * @return $count Total row count
233 public function countTotalRows ($tableName) {
234 // Connect to the database
235 $this->dbLayer->connectToDatabase();
237 // Ask the database layer
238 $count = $this->dbLayer->countTotalRows($tableName);