3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Criteria\Storing\StoreableCriteria;
8 use CoreFramework\Database\Backend\DatabaseBackend;
9 use CoreFramework\Registry\Registerable;
10 use CoreFramework\Middleware\BaseMiddleware;
11 use CoreFramework\Middleware\Debug\DebugMiddleware;
14 * Database selector class
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
37 * Array for connection data
39 private $connectData = array(
47 * The real database layer
49 private $dbLayer = NULL;
52 * An instance of this class
54 private static $selfInstance = NULL;
57 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
65 * Creates a new database connection layer
67 * @param $debugInstance An instance of a DebugMiddleware class
68 * @param $dbLayer An instance of a DatabaseBackend class
70 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
72 $databaseInstance = new DatabaseConnection();
75 $databaseInstance->setDatabaseLayer($dbLayer);
78 self::$selfInstance = $databaseInstance;
81 return $databaseInstance;
85 * Getter for this class
87 * @return $selfInstance An instance of this class
89 public static final function getSelfInstance () {
90 return self::$selfInstance;
94 * Setter for all database connection data. All these parameters may be
95 * supported by the underlaying backend.
97 * @param $login Login name to database
98 * @param $pass Passwort for above login
99 * @param $dbase Name of used database
100 * @param $host Host to connect to (default: 127.0.0.1)
103 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
104 // Transfer connection data
105 $this->connectData['login'] = (string) $login;
106 $this->connectData['pass'] = (string) $pass;
107 $this->connectData['dbase'] = (string) $dbase;
108 $this->connectData['host'] = (string) $host;
112 * Getter for connection data
114 * @return $connectData Connection data stored with this clas
116 public final function getConnectionData () {
117 return $this->connectData;
121 * Setter for the real database layer
122 * @param $dbLayer An instance of the real database layer
125 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
126 $this->dbLayer = $dbLayer;
130 * Getter for index key
132 * @return $indexKey Index key
134 public final function getIndexKey () {
135 return $this->dbLayer->getIndexKey();
139 * Runs a 'select' statement on the database layer with given table name
140 * and criteria. If this doesn't fail the result will be returned
142 * @param $tableName Name of the 'table' we shall query
143 * @param $criteriaInstance An instance of a Criteria class
144 * @return $result The result as an array
146 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
147 // Connect to the database
148 $this->dbLayer->connectToDatabase();
150 // Get result from query
151 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
158 * Getter for last exception
160 * @return $exceptionInstance Last thrown exception
162 public final function getLastException () {
163 $exceptionInstance = $this->dbLayer->getLastException();
164 return $exceptionInstance;
168 * 'Inserts' a data set instance into a local file database folder
170 * @param $dataSetInstance A storeable data set
173 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
174 // Connect to the database
175 $this->dbLayer->connectToDatabase();
177 // Ask the database layer
178 $this->dbLayer->queryInsertDataSet($dataSetInstance);
182 * 'Updates' a data set instance with a database layer
184 * @param $dataSetInstance A storeable data set
187 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
188 // Connect to the database
189 $this->dbLayer->connectToDatabase();
191 // Ask the database layer
192 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
196 * Getter for primary key column of specified table name
198 * @param $tableName Name of table we need the primary key column from
199 * @return $primaryKey Primary key column of requested table
201 public function getPrimaryKeyOfTable ($tableName) {
202 // Connect to the database
203 $this->dbLayer->connectToDatabase();
205 // Ask the database layer
206 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
213 * Removes non-public data from given array.
215 * @param $data An array with possible non-public data that needs to be removed.
216 * @return $data A cleaned up array with only public data.
218 public function removeNonPublicDataFromArray (array $data) {
219 // Connect to the database
220 $this->dbLayer->connectToDatabase();
222 // Call database backend
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
224 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
226 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
231 * Count total table rows
233 * @param $tableName Table name
234 * @return $count Total row count
236 public function countTotalRows ($tableName) {
237 // Connect to the database
238 $this->dbLayer->connectToDatabase();
240 // Ask the database layer
241 $count = $this->dbLayer->countTotalRows($tableName);