--- /dev/null
+<?php
+/**
+ * Database selector class
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.shipsimu.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 <http://www.gnu.org/licenses/>.
+ */
+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 $selfInstance = NULL;
+
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
+ protected function __construct () {
+ // Call parent constructor
+ parent::__construct(__CLASS__);
+ }
+
+ // Create new database connection layer
+ public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
+ // Get instance
+ $databaseInstance = new DatabaseConnection();
+
+ // Set database layer
+ $databaseInstance->setDatabaseLayer($dbLayer);
+
+ // Set db instance
+ self::$selfInstance = $databaseInstance;
+
+ // Return instance
+ return $databaseInstance;
+ }
+
+ // Get an instance of this class
+ public static final function getSelfInstance () {
+ return self::$selfInstance;
+ }
+
+ // 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 (DatabaseBackend $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($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;
+ }
+
+ /**
+ * Removes non-public data from given array.
+ *
+ * @param $data An array with possible non-public data that needs to be removed.
+ * @return $data A cleaned up array with only public data.
+ */
+ public function removeNonPublicDataFromArray (array $data) {
+ // Connect to the database
+ $this->dbLayer->connectToDatabase();
+
+ // Call database backend
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: Calling this->dbLayer->removeNonPublicDataFromArray(data) ...');
+ $data = $this->dbLayer->removeNonPublicDataFromArray($data);
+
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DB-CONNECTION[' . $this->__toString() . ']: data[]=' . gettype($data));
+ return $data;
+ }
+
+ /**
+ * Count total table rows
+ *
+ * @param $tableName Table name
+ * @return $count Total row count
+ */
+ public function countTotalRows ($tableName) {
+ // Connect to the database
+ $this->dbLayer->connectToDatabase();
+
+ // Ask the database layer
+ $count = $this->dbLayer->countTotalRows($tableName);
+
+ // Return the value
+ return $count;
+ }
+}
+
+// [EOF]
+?>