* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable { /** * Array for connection data */ private $connectData = array( 'login' => '', 'pass' => '', 'dbase' => '', 'host' => '' ); // The real database layer private $dbLayer = null; // An instance of this class private static $thisInstance = null; /** * Protected constructor * * @return void */ protected function __construct() { // Call parent constructor parent::__construct(__CLASS__); } // Create new database connection layer public final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) { // Get instance $dbInstance = new DatabaseConnection(); // Set debug output handler $dbInstance->setDebugInstance($debugInstance); // Set database layer $dbInstance->setDatabaseLayer($dbLayer); // Set db instance self::$thisInstance = $dbInstance; // Return instance return $dbInstance; } // Get an instance of this class public final static function getInstance () { return self::$thisInstance; } // Public setter for database connection public final function setConnectionData ($login, $pass, $dbase, $host='localhost') { // Transfer connection data $this->connectData['login'] = (string) $login; $this->connectData['pass'] = (string) $pass; $this->connectData['dbase'] = (string) $dbase; $this->connectData['host'] = (string) $host; } /** * Getter for connection data * * @return $connectData Connection data stored with this clas */ public final function getConnectionData () { return $this->connectData; } /** * Setter for the real database layer * @param $dbLayer An instance of the real database layer * @return void */ public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) { $this->dbLayer = $dbLayer; } /** * Getter for index key * * @return $indexKey Index key */ public final function getIndexKey () { return $this->dbLayer->getIndexKey(); } /** * Runs a 'select' statement on the database layer with given table name * and criteria. If this doesn't fail the result will be returned * * @param $tableName Name of the 'table' we shall query * @param $criteriaInstance An instance of a Criteria class * @return $result The result as an array */ public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) { // Connect to the database $this->dbLayer->connectToDatabase(); // Get result from query $result = $this->dbLayer->querySelect('array', $tableName, $criteriaInstance); // Return the result return $result; } /** * Getter for last exception * * @return $exceptionInstance Last thrown exception */ public final function getLastException () { $exceptionInstance = $this->dbLayer->getLastException(); return $exceptionInstance; } /** * 'Inserts' a data set instance into a local file database folder * * @param $dataSetInstance A storeable data set * @return void */ public function queryInsertDataSet (StoreableCriteria $dataSetInstance) { // Connect to the database $this->dbLayer->connectToDatabase(); // Ask the database layer $this->dbLayer->queryInsertDataSet($dataSetInstance); } /** * 'Updates' a data set instance with a database layer * * @param $dataSetInstance A storeable data set * @return void */ public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) { // Connect to the database $this->dbLayer->connectToDatabase(); // Ask the database layer $this->dbLayer->queryUpdateDataSet($dataSetInstance); } /** * Getter for primary key column of specified table name * * @param $tableName Name of table we need the primary key column from * @return $primaryKey Primary key column of requested table */ public function getPrimaryKeyOfTable ($tableName) { // Connect to the database $this->dbLayer->connectToDatabase(); // Ask the database layer $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName); // Return the value return $primaryKey; } } // [EOF] ?>