3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Criteria\Criteria;
7 use CoreFramework\Registry\Registerable;
8 use CoreFramework\Middleware\BaseMiddleware;
9 use CoreFramework\Middleware\Debug\DebugMiddleware;
12 * Database selector class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
35 * Array for connection data
37 private $connectData = array(
45 * The real database layer
47 private $dbLayer = NULL;
50 * An instance of this class
52 private static $selfInstance = NULL;
55 * Protected constructor
57 protected function __construct () {
58 // Call parent constructor
59 parent::__construct(__CLASS__);
63 * Creates a new database connection layer
65 * @param $debugInstance An instance of a DebugMiddleware class
66 * @param $dbLayer An instance of a DatabaseBackend class
68 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
70 $databaseInstance = new DatabaseConnection();
73 $databaseInstance->setDatabaseLayer($dbLayer);
76 self::$selfInstance = $databaseInstance;
79 return $databaseInstance;
83 * Getter for this class
85 * @return $selfInstance An instance of this class
87 public static final function getSelfInstance () {
88 return self::$selfInstance;
92 * Setter for all database connection data. All these parameters may be
93 * supported by the underlaying backend.
95 * @param $login Login name to database
96 * @param $pass Passwort for above login
97 * @param $dbase Name of used database
98 * @param $host Host to connect to (default: 127.0.0.1)
101 public final function setConnectionData ($login, $pass, $dbase, $host = '127.0.0.1') {
102 // Transfer connection data
103 $this->connectData['login'] = (string) $login;
104 $this->connectData['pass'] = (string) $pass;
105 $this->connectData['dbase'] = (string) $dbase;
106 $this->connectData['host'] = (string) $host;
110 * Getter for connection data
112 * @return $connectData Connection data stored with this clas
114 public final function getConnectionData () {
115 return $this->connectData;
119 * Setter for the real database layer
120 * @param $dbLayer An instance of the real database layer
123 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
124 $this->dbLayer = $dbLayer;
128 * Getter for index key
130 * @return $indexKey Index key
132 public final function getIndexKey () {
133 return $this->dbLayer->getIndexKey();
137 * Runs a 'select' statement on the database layer with given table name
138 * and criteria. If this doesn't fail the result will be returned
140 * @param $tableName Name of the 'table' we shall query
141 * @param $criteriaInstance An instance of a Criteria class
142 * @return $result The result as an array
144 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
145 // Connect to the database
146 $this->dbLayer->connectToDatabase();
148 // Get result from query
149 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
156 * Getter for last exception
158 * @return $exceptionInstance Last thrown exception
160 public final function getLastException () {
161 $exceptionInstance = $this->dbLayer->getLastException();
162 return $exceptionInstance;
166 * 'Inserts' a data set instance into a local file database folder
168 * @param $dataSetInstance A storeable data set
171 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
172 // Connect to the database
173 $this->dbLayer->connectToDatabase();
175 // Ask the database layer
176 $this->dbLayer->queryInsertDataSet($dataSetInstance);
180 * 'Updates' a data set instance with a database layer
182 * @param $dataSetInstance A storeable data set
185 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
186 // Connect to the database
187 $this->dbLayer->connectToDatabase();
189 // Ask the database layer
190 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
194 * Getter for primary key column of specified table name
196 * @param $tableName Name of table we need the primary key column from
197 * @return $primaryKey Primary key column of requested table
199 public function getPrimaryKeyOfTable ($tableName) {
200 // Connect to the database
201 $this->dbLayer->connectToDatabase();
203 // Ask the database layer
204 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
211 * Removes non-public data from given array.
213 * @param $data An array with possible non-public data that needs to be removed.
214 * @return $data A cleaned up array with only public data.
216 public function removeNonPublicDataFromArray (array $data) {
217 // Connect to the database
218 $this->dbLayer->connectToDatabase();
220 // Call database backend
221 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
222 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
224 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
229 * Count total table rows
231 * @param $tableName Table name
232 * @return $count Total row count
234 public function countTotalRows ($tableName) {
235 // Connect to the database
236 $this->dbLayer->connectToDatabase();
238 // Ask the database layer
239 $count = $this->dbLayer->countTotalRows($tableName);