3 * Database selector class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 $thisInstance = null;
42 * Protected constructor
46 protected function __construct() {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
51 // Create new database connection layer
52 public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
54 $dbInstance = new DatabaseConnection();
56 // Set debug output handler
57 $dbInstance->setDebugInstance($debugInstance);
60 $dbInstance->setDatabaseLayer($dbLayer);
63 self::$thisInstance = $dbInstance;
69 // Get an instance of this class
70 public final static function getInstance () {
71 return self::$thisInstance;
74 // Public setter for database connection
75 public final function setConnectionData ($login, $pass, $dbase, $host="localhost") {
76 // Transfer connection data
77 $this->connectData['login'] = (string) $login;
78 $this->connectData['pass'] = (string) $pass;
79 $this->connectData['dbase'] = (string) $dbase;
80 $this->connectData['host'] = (string) $host;
84 * Getter for connection data
86 * @return $connectData Connection data stored with this clas
88 public final function getConnectionData () {
89 return $this->connectData;
93 * Setter for the real database layer
94 * @param $dbLayer An instance of the real database layer
97 public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
98 $this->dbLayer = $dbLayer;
102 * Analyses if a unique ID has already been used or not. This method does
103 * only pass the given ID through to the "real" database layer.
105 * @param $uniqueID A unique ID number which shall be checked
106 * before it will be used
107 * @param $inConstructor If called from a constructor or from
109 * @return $isUnused true = The unique ID was not found in the database,
110 * false = It is already in use by an other object
112 public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
113 // Connect to the database
114 $this->dbLayer->connectToDatabase();
116 // Pass the returning result through
117 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
121 * Runs a "select" statement on the database layer with given table name
122 * and criteria. If this doesn't fail the result will be returned
124 * @param $tableName Name of the "table" we shall query
125 * @param $criteriaInstance An instance of a Criteria class
126 * @return $result The result as an array
128 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
129 // Connect to the database
130 $this->dbLayer->connectToDatabase();
132 // Get result from query
133 $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);
140 * Getter for last exception
142 * @return $exceptionInstance Last thrown exception
144 public final function getLastException () {
145 $exceptionInstance = $this->dbLayer->getLastException();
146 return $exceptionInstance;
150 * "Inserts" a data set instance into a local file database folder
152 * @param $dataSetInstance A storeable data set
155 public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
156 // Connect to the database
157 $this->dbLayer->connectToDatabase();
159 // Ask the database layer
160 $this->dbLayer->queryInsertDataSet($dataSetInstance);
164 * "Updates" a data set instance with a database layer
166 * @param $dataSetInstance A storeable data set
169 public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
170 // Connect to the database
171 $this->dbLayer->connectToDatabase();
173 // Ask the database layer
174 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
178 * Getter for primary key column of specified table name
180 * @param $tableName Name of table we need the primary key column from
181 * @return $primaryKey Primary key column of requested table
183 public function getPrimaryKeyOfTable ($tableName) {
184 // Connect to the database
185 $this->dbLayer->connectToDatabase();
187 // Ask the database layer
188 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);