Renamed to DatabaseBackend to get rid of word 'Interface' in an interface name as...
[core.git] / inc / classes / middleware / database / class_DatabaseConnection.php
1 <?php
2 /**
3  * Database selector class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Registerable {
25         /**
26          * Array for connection data
27          */
28         private $connectData = array(
29                 'login' => '',
30                 'pass'  => '',
31                 'dbase' => '',
32                 'host'  => ''
33         );
34
35         // The real database layer
36         private $dbLayer = NULL;
37
38         // An instance of this class
39         private static $selfInstance = NULL;
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49         }
50
51         // Create new database connection layer
52         public static final function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseBackend $dbLayer) {
53                 // Get instance
54                 $databaseInstance = new DatabaseConnection();
55
56                 // Set database layer
57                 $databaseInstance->setDatabaseLayer($dbLayer);
58
59                 // Set db instance
60                 self::$selfInstance = $databaseInstance;
61
62                 // Return instance
63                 return $databaseInstance;
64         }
65
66         // Get an instance of this class
67         public static final function getSelfInstance () {
68                 return self::$selfInstance;
69         }
70
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;
78         }
79
80         /**
81          * Getter for connection data
82          *
83          * @return      $connectData    Connection data stored with this clas
84          */
85         public final function getConnectionData () {
86                 return $this->connectData;
87         }
88
89         /**
90          * Setter for the real database layer
91          * @param       $dbLayer        An instance of the real database layer
92          * @return      void
93          */
94         public final function setDatabaseLayer (DatabaseBackend $dbLayer) {
95                 $this->dbLayer = $dbLayer;
96         }
97
98         /**
99          * Getter for index key
100          *
101          * @return      $indexKey       Index key
102          */
103         public final function getIndexKey () {
104                 return $this->dbLayer->getIndexKey();
105         }
106
107         /**
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
110          *
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
114          */
115         public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
116                 // Connect to the database
117                 $this->dbLayer->connectToDatabase();
118
119                 // Get result from query
120                 $result = $this->dbLayer->querySelect($tableName, $criteriaInstance);
121
122                 // Return the result
123                 return $result;
124         }
125
126         /**
127          * Getter for last exception
128          *
129          * @return      $exceptionInstance      Last thrown exception
130          */
131         public final function getLastException () {
132                 $exceptionInstance = $this->dbLayer->getLastException();
133                 return $exceptionInstance;
134         }
135
136         /**
137          * 'Inserts' a data set instance into a local file database folder
138          *
139          * @param       $dataSetInstance        A storeable data set
140          * @return      void
141          */
142         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
143                 // Connect to the database
144                 $this->dbLayer->connectToDatabase();
145
146                 // Ask the database layer
147                 $this->dbLayer->queryInsertDataSet($dataSetInstance);
148         }
149
150         /**
151          * 'Updates' a data set instance with a database layer
152          *
153          * @param       $dataSetInstance        A storeable data set
154          * @return      void
155          */
156         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
157                 // Connect to the database
158                 $this->dbLayer->connectToDatabase();
159
160                 // Ask the database layer
161                 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
162         }
163
164         /**
165          * Getter for primary key column of specified table name
166          *
167          * @param       $tableName              Name of table we need the primary key column from
168          * @return      $primaryKey             Primary key column of requested table
169          */
170         public function getPrimaryKeyOfTable ($tableName) {
171                 // Connect to the database
172                 $this->dbLayer->connectToDatabase();
173
174                 // Ask the database layer
175                 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
176
177                 // Return the value
178                 return $primaryKey;
179         }
180 }
181
182 // [EOF]
183 ?>