3 namespace CoreFramework\Connection\Database;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
9 * Database selector class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
32 * Array for connection data
34 private $connectData = array(
41 // The real database layer
42 private $dbLayer = NULL;
44 // An instance of this class
45 private static $selfInstance = NULL;
48 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
57 // Create new database connection layer
58 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
60 $databaseInstance = new DatabaseConnection();
63 $databaseInstance->setDatabaseLayer($dbLayer);
66 self::$selfInstance = $databaseInstance;
69 return $databaseInstance;
72 // Get an instance of this class
73 public static final function getSelfInstance () {
74 return self::$selfInstance;
77 // Public setter for database connection
78 public final function setConnectionData ($login, $pass, $dbase, $host='localhost') {
79 // Transfer connection data
80 $this->connectData['login'] = (string) $login;
81 $this->connectData['pass'] = (string) $pass;
82 $this->connectData['dbase'] = (string) $dbase;
83 $this->connectData['host'] = (string) $host;
87 * Getter for connection data
89 * @return $connectData Connection data stored with this clas
91 public final function getConnectionData () {
92 return $this->connectData;
96 * Setter for the real database layer
97 * @param $dbLayer An instance of the real database layer
100 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
101 $this->dbLayer = $dbLayer;
105 * Getter for index key
107 * @return $indexKey Index key
109 public final function getIndexKey () {
110 return $this->dbLayer->getIndexKey();
114 * Runs a 'select' statement on the database layer with given table name
115 * and criteria. If this doesn't fail the result will be returned
117 * @param $tableName Name of the 'table' we shall query
118 * @param $criteriaInstance An instance of a Criteria class
119 * @return $result The result as an array
121 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
122 // Connect to the database
123 $this->dbLayer->connectToDatabase();
125 // Get result from query
126 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
133 * Getter for last exception
135 * @return $exceptionInstance Last thrown exception
137 public final function getLastException () {
138 $exceptionInstance = $this->dbLayer->getLastException();
139 return $exceptionInstance;
143 * 'Inserts' a data set instance into a local file database folder
145 * @param $dataSetInstance A storeable data set
148 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
149 // Connect to the database
150 $this->dbLayer->connectToDatabase();
152 // Ask the database layer
153 $this->dbLayer->queryInsertDataSet($dataSetInstance);
157 * 'Updates' a data set instance with a database layer
159 * @param $dataSetInstance A storeable data set
162 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
163 // Connect to the database
164 $this->dbLayer->connectToDatabase();
166 // Ask the database layer
167 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
171 * Getter for primary key column of specified table name
173 * @param $tableName Name of table we need the primary key column from
174 * @return $primaryKey Primary key column of requested table
176 public function getPrimaryKeyOfTable ($tableName) {
177 // Connect to the database
178 $this->dbLayer->connectToDatabase();
180 // Ask the database layer
181 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
188 * Removes non-public data from given array.
190 * @param $data An array with possible non-public data that needs to be removed.
191 * @return $data A cleaned up array with only public data.
193 public function removeNonPublicDataFromArray (array $data) {
194 // Connect to the database
195 $this->dbLayer->connectToDatabase();
197 // Call database backend
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
199 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
201 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
206 * Count total table rows
208 * @param $tableName Table name
209 * @return $count Total row count
211 public function countTotalRows ($tableName) {
212 // Connect to the database
213 $this->dbLayer->connectToDatabase();
215 // Ask the database layer
216 $count = $this->dbLayer->countTotalRows($tableName);