3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Connector\Database\DatabaseConnector;
7 use CoreFramework\Criteria\Criteria;
8 use CoreFramework\Criteria\Storing\StoreableCriteria;
9 use CoreFramework\Database\Backend\DatabaseBackend;
10 use CoreFramework\Registry\Registerable;
11 use CoreFramework\Middleware\BaseMiddleware;
12 use CoreFramework\Middleware\Debug\DebugMiddleware;
15 * Database selector class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
38 * Array for connection data
40 private $connectData = array(
48 * The real database layer
50 private $dbLayer = NULL;
53 * An instance of this class
55 private static $selfInstance = NULL;
58 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
66 * Creates a new database connection layer
68 * @param $debugInstance An instance of a DebugMiddleware class
69 * @param $dbLayer An instance of a DatabaseBackend class
71 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
73 $databaseInstance = new DatabaseConnection();
76 $databaseInstance->setDatabaseLayer($dbLayer);
79 self::$selfInstance = $databaseInstance;
82 return $databaseInstance;
86 * Getter for this class
88 * @return $selfInstance An instance of this class
90 public static final function getSelfInstance () {
91 return self::$selfInstance;
95 * Setter for all database connection data. All these parameters may be
96 * supported by the underlaying backend.
98 * @param $login Login name to database
99 * @param $pass Passwort for above login
100 * @param $dbase Name of used database
101 * @param $host Host to connect to (default: 127.0.0.1)
104 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
105 // Transfer connection data
106 $this->connectData['login'] = (string) $login;
107 $this->connectData['pass'] = (string) $pass;
108 $this->connectData['dbase'] = (string) $dbase;
109 $this->connectData['host'] = (string) $host;
113 * Getter for connection data
115 * @return $connectData Connection data stored with this clas
117 public final function getConnectionData () {
118 return $this->connectData;
122 * Setter for the real database layer
123 * @param $dbLayer An instance of the real database layer
126 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
127 $this->dbLayer = $dbLayer;
131 * Getter for index key
133 * @return $indexKey Index key
135 public final function getIndexKey () {
136 return $this->dbLayer->getIndexKey();
140 * Runs a 'select' statement on the database layer with given table name
141 * and criteria. If this doesn't fail the result will be returned
143 * @param $tableName Name of the 'table' we shall query
144 * @param $criteriaInstance An instance of a Criteria class
145 * @return $result The result as an array
147 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
148 // Connect to the database
149 $this->dbLayer->connectToDatabase();
151 // Get result from query
152 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
159 * Getter for last exception
161 * @return $exceptionInstance Last thrown exception
163 public final function getLastException () {
164 $exceptionInstance = $this->dbLayer->getLastException();
165 return $exceptionInstance;
169 * 'Inserts' a data set instance into a local file database folder
171 * @param $dataSetInstance A storeable data set
174 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
175 // Connect to the database
176 $this->dbLayer->connectToDatabase();
178 // Ask the database layer
179 $this->dbLayer->queryInsertDataSet($dataSetInstance);
183 * 'Updates' a data set instance with a database layer
185 * @param $dataSetInstance A storeable data set
188 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
189 // Connect to the database
190 $this->dbLayer->connectToDatabase();
192 // Ask the database layer
193 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
197 * Getter for primary key column of specified table name
199 * @param $tableName Name of table we need the primary key column from
200 * @return $primaryKey Primary key column of requested table
202 public function getPrimaryKeyOfTable ($tableName) {
203 // Connect to the database
204 $this->dbLayer->connectToDatabase();
206 // Ask the database layer
207 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
214 * Removes non-public data from given array.
216 * @param $data An array with possible non-public data that needs to be removed.
217 * @return $data A cleaned up array with only public data.
219 public function removeNonPublicDataFromArray (array $data) {
220 // Connect to the database
221 $this->dbLayer->connectToDatabase();
223 // Call database backend
224 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
225 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
227 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
232 * Count total table rows
234 * @param $tableName Table name
235 * @return $count Total row count
237 public function countTotalRows ($tableName) {
238 // Connect to the database
239 $this->dbLayer->connectToDatabase();
241 // Ask the database layer
242 $count = $this->dbLayer->countTotalRows($tableName);