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, LimitableObject {
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 $this->setObjectDescription("Datenbank-Mittelschicht");
53 // Create an unique ID
54 $this->createUniqueID();
57 // Create new database connection layer
58 public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
60 $dbInstance = new DatabaseConnection();
62 // Set debug output handler
63 $dbInstance->setDebugInstance($debugInstance);
66 $dbInstance->setDatabaseLayer($dbLayer);
69 self::$thisInstance = $dbInstance;
75 // Get an instance of this class
76 public final static function getInstance () {
77 return self::$thisInstance;
80 // Public setter for database connection
81 public final function setConnectionData ($login, $pass, $dbase, $host="localhost") {
82 // Transfer connection data
83 $this->connectData['login'] = (string) $login;
84 $this->connectData['pass'] = (string) $pass;
85 $this->connectData['dbase'] = (string) $dbase;
86 $this->connectData['host'] = (string) $host;
90 * Save a whole object or parts of it to the database or local file
92 * @param $object The object we shall save
95 public function saveObject ($object) {
96 // Connect to the database
97 $this->dbLayer->connectToDatabase();
99 // For now just pipe it through to the database layer
100 $this->dbLayer->saveObject($object);
104 * Set a limitation for the saving process. This shall be done before
105 * saveObject() is called else saveObject() shall save the whole object.
107 * @param $limitInstance An instance of ObjectLimits which contains
108 * elements we shall exclusivly include in
112 public function limitObject (ObjectLimits $limitInstance) {
113 // Connect to the database
114 $this->dbLayer->connectToDatabase();
116 // For now we pipe this through to the real database instance
117 $this->dbLayer->limitObject($limitInstance);
121 * Analyses if a unique ID has already been used or not. This method does
122 * only pass the given ID through to the "real" database layer.
124 * @param $uniqueID A unique ID number which shall be checked
125 * before it will be used
126 * @param $inConstructor If called from a constructor or from
128 * @return $isUnused true = The unique ID was not found in the database,
129 * false = It is already in use by an other object
131 public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
132 // Connect to the database
133 $this->dbLayer->connectToDatabase();
135 // Pass the returning result through
136 return $this->dbLayer->isUniqueIdUsed($uniqueID, $inConstructor);
140 * Gets cached data from the database layer and if not found fetch it from
141 * the database again. This method does not return the header stuff because
142 * the underlaying database class will return only the requested content.
144 * @param $idNumber The ID number which we need for looking up
146 * @return $cachedArray The maybe cached data from the database
148 public function getObjectFromCachedData ($idNumber) {
149 // Connect to the database
150 $this->dbLayer->connectToDatabase();
152 // Pass the returning result through
153 return $this->dbLayer->getObjectFromCachedData($idNumber);
157 * Setter for the real database layer
158 * @param $dbLayer An instance of the real database layer
161 public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
162 $this->dbLayer = $dbLayer;
166 * Runs a "select" statement on the database layer with given table name
167 * and criteria. If this doesn't fail the result will be returned
169 * @param $tableName Name of the "table" we shall query
170 * @param $criteriaInstance An instance of a Criteria class
171 * @return $result The result as an array
173 public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
174 // Connect to the database
175 $this->dbLayer->connectToDatabase();
177 // Get result from query
178 $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);