3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7 use CoreFramework\Middleware\BaseMiddleware;
10 * Database selector class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
33 * Array for connection data
35 private $connectData = array(
43 * The real database layer
45 private $dbLayer = NULL;
48 * An instance of this class
50 private static $selfInstance = NULL;
53 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Creates a new database connection layer
63 * @param $debugInstance An instance of a DebugMiddleware class
64 * @param $dbLayer An instance of a DatabaseBackend class
66 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
68 $databaseInstance = new DatabaseConnection();
71 $databaseInstance->setDatabaseLayer($dbLayer);
74 self::$selfInstance = $databaseInstance;
77 return $databaseInstance;
81 * Getter for this class
83 * @return $selfInstance An instance of this class
85 public static final function getSelfInstance () {
86 return self::$selfInstance;
90 * Setter for all database connection data. All these parameters may be
91 * supported by the underlaying backend.
93 * @param $login Login name to database
94 * @param $pass Passwort for above login
95 * @param $dbase Name of used database
96 * @param $host Host to connect to (default: 127.0.0.1)
99 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
100 // Transfer connection data
101 $this->connectData['login'] = (string) $login;
102 $this->connectData['pass'] = (string) $pass;
103 $this->connectData['dbase'] = (string) $dbase;
104 $this->connectData['host'] = (string) $host;
108 * Getter for connection data
110 * @return $connectData Connection data stored with this clas
112 public final function getConnectionData () {
113 return $this->connectData;
117 * Setter for the real database layer
118 * @param $dbLayer An instance of the real database layer
121 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
122 $this->dbLayer = $dbLayer;
126 * Getter for index key
128 * @return $indexKey Index key
130 public final function getIndexKey () {
131 return $this->dbLayer->getIndexKey();
135 * Runs a 'select' statement on the database layer with given table name
136 * and criteria. If this doesn't fail the result will be returned
138 * @param $tableName Name of the 'table' we shall query
139 * @param $criteriaInstance An instance of a Criteria class
140 * @return $result The result as an array
142 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
143 // Connect to the database
144 $this->dbLayer->connectToDatabase();
146 // Get result from query
147 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
154 * Getter for last exception
156 * @return $exceptionInstance Last thrown exception
158 public final function getLastException () {
159 $exceptionInstance = $this->dbLayer->getLastException();
160 return $exceptionInstance;
164 * 'Inserts' a data set instance into a local file database folder
166 * @param $dataSetInstance A storeable data set
169 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
170 // Connect to the database
171 $this->dbLayer->connectToDatabase();
173 // Ask the database layer
174 $this->dbLayer->queryInsertDataSet($dataSetInstance);
178 * 'Updates' a data set instance with a database layer
180 * @param $dataSetInstance A storeable data set
183 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
184 // Connect to the database
185 $this->dbLayer->connectToDatabase();
187 // Ask the database layer
188 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
192 * Getter for primary key column of specified table name
194 * @param $tableName Name of table we need the primary key column from
195 * @return $primaryKey Primary key column of requested table
197 public function getPrimaryKeyOfTable ($tableName) {
198 // Connect to the database
199 $this->dbLayer->connectToDatabase();
201 // Ask the database layer
202 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
209 * Removes non-public data from given array.
211 * @param $data An array with possible non-public data that needs to be removed.
212 * @return $data A cleaned up array with only public data.
214 public function removeNonPublicDataFromArray (array $data) {
215 // Connect to the database
216 $this->dbLayer->connectToDatabase();
218 // Call database backend
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
220 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
222 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
227 * Count total table rows
229 * @param $tableName Table name
230 * @return $count Total row count
232 public function countTotalRows ($tableName) {
233 // Connect to the database
234 $this->dbLayer->connectToDatabase();
236 // Ask the database layer
237 $count = $this->dbLayer->countTotalRows($tableName);