3 * Database selector class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
26 * Array for connection data
28 private $connectData = array(
35 // The real database layer
36 private $dbLayer = NULL;
38 // An instance of this class
39 private static $selfInstance = NULL;
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
51 // Create new database connection layer
52 public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
54 $databaseInstance = new DatabaseConnection();
57 $databaseInstance->setDatabaseLayer($dbLayer);
60 self::$selfInstance = $databaseInstance;
63 return $databaseInstance;
66 // Get an instance of this class
67 public static final function getSelfInstance () {
68 return self::$selfInstance;
71 // Public setter for database connection
72 public final function setConnectionData ($login, $pass, $dbase, $host='localhost') {
73 // Transfer connection data
74 $this->connectData['login'] = (string) $login;
75 $this->connectData['pass'] = (string) $pass;
76 $this->connectData['dbase'] = (string) $dbase;
77 $this->connectData['host'] = (string) $host;
81 * Getter for connection data
83 * @return $connectData Connection data stored with this clas
85 public final function getConnectionData () {
86 return $this->connectData;
90 * Setter for the real database layer
91 * @param $dbLayer An instance of the real database layer
94 public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
95 $this->dbLayer = $dbLayer;
99 * Getter for index key
101 * @return $indexKey Index key
103 public final function getIndexKey () {
104 return $this->dbLayer->getIndexKey();
108 * Runs a 'select' statement on the database layer with given table name
109 * and criteria. If this doesn't fail the result will be returned
111 * @param $tableName Name of the 'table' we shall query
112 * @param $criteriaInstance An instance of a Criteria class
113 * @return $result The result as an array
115 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
116 // Connect to the database
117 $this->dbLayer->connectToDatabase();
119 // Get result from query
120 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
127 * Getter for last exception
129 * @return $exceptionInstance Last thrown exception
131 public final function getLastException () {
132 $exceptionInstance = $this->dbLayer->getLastException();
133 return $exceptionInstance;
137 * 'Inserts' a data set instance into a local file database folder
139 * @param $dataSetInstance A storeable data set
142 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
143 // Connect to the database
144 $this->dbLayer->connectToDatabase();
146 // Ask the database layer
147 $this->dbLayer->queryInsertDataSet($dataSetInstance);
151 * 'Updates' a data set instance with a database layer
153 * @param $dataSetInstance A storeable data set
156 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
157 // Connect to the database
158 $this->dbLayer->connectToDatabase();
160 // Ask the database layer
161 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
165 * Getter for primary key column of specified table name
167 * @param $tableName Name of table we need the primary key column from
168 * @return $primaryKey Primary key column of requested table
170 public function getPrimaryKeyOfTable ($tableName) {
171 // Connect to the database
172 $this->dbLayer->connectToDatabase();
174 // Ask the database layer
175 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
182 * Removes non-public data from given array.
184 * @param $data An array with possible non-public data that needs to be removed.
185 * @return $data A cleaned up array with only public data.
187 public function removeNonPublicDataFromArray (array $data) {
188 // Call database backend
189 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
190 $data = $this->dbLayer->removeNonPublicDataFromArray($data);
192 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));