]> git.mxchange.org Git - mailer.git/blob - inc/classes/middleware/database/class_DatabaseConnection.php
Code base synced, updated
[mailer.git] / inc / classes / middleware / database / class_DatabaseConnection.php
1 <?php
2 /**
3  * Database selector class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
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 $thisInstance = 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 final static function createDatabaseConnection (DebugMiddleware $debugInstance, DatabaseFrontendInterface $dbLayer) {
53                 // Get instance
54                 $dbInstance = new DatabaseConnection();
55
56                 // Set debug output handler
57                 $dbInstance->setDebugInstance($debugInstance);
58
59                 // Set database layer
60                 $dbInstance->setDatabaseLayer($dbLayer);
61
62                 // Set db instance
63                 self::$thisInstance = $dbInstance;
64
65                 // Return instance
66                 return $dbInstance;
67         }
68
69         // Get an instance of this class
70         public final static function getInstance () {
71                 return self::$thisInstance;
72         }
73
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;
81         }
82
83         /**
84          * Getter for connection data
85          *
86          * @return      $connectData    Connection data stored with this clas
87          */
88         public final function getConnectionData () {
89                 return $this->connectData;
90         }
91
92         /**
93          * Setter for the real database layer
94          * @param       $dbLayer        An instance of the real database layer
95          * @return      void
96          */
97         public final function setDatabaseLayer (DatabaseFrontendInterface $dbLayer) {
98                 $this->dbLayer = $dbLayer;
99         }
100
101         /**
102          * Runs a "select" statement on the database layer with given table name
103          * and criteria. If this doesn't fail the result will be returned
104          *
105          * @param       $tableName                      Name of the "table" we shall query
106          * @param       $criteriaInstance       An instance of a Criteria class
107          * @return      $result                         The result as an array
108          */
109         public function doSelectByTableCriteria ($tableName, Criteria $criteriaInstance) {
110                 // Connect to the database
111                 $this->dbLayer->connectToDatabase();
112
113                 // Get result from query
114                 $result = $this->dbLayer->querySelect("array", $tableName, $criteriaInstance);
115
116                 // Return the result
117                 return $result;
118         }
119
120         /**
121          * Getter for last exception
122          *
123          * @return      $exceptionInstance      Last thrown exception
124          */
125         public final function getLastException () {
126                 $exceptionInstance = $this->dbLayer->getLastException();
127                 return $exceptionInstance;
128         }
129
130         /**
131          * "Inserts" a data set instance into a local file database folder
132          *
133          * @param       $dataSetInstance        A storeable data set
134          * @return      void
135          */
136         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
137                 // Connect to the database
138                 $this->dbLayer->connectToDatabase();
139
140                 // Ask the database layer
141                 $this->dbLayer->queryInsertDataSet($dataSetInstance);
142         }
143
144         /**
145          * "Updates" a data set instance with a database layer
146          *
147          * @param       $dataSetInstance        A storeable data set
148          * @return      void
149          */
150         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
151                 // Connect to the database
152                 $this->dbLayer->connectToDatabase();
153
154                 // Ask the database layer
155                 $this->dbLayer->queryUpdateDataSet($dataSetInstance);
156         }
157
158         /**
159          * Getter for primary key column of specified table name
160          *
161          * @param       $tableName              Name of table we need the primary key column from
162          * @return      $primaryKey             Primary key column of requested table
163          */
164         public function getPrimaryKeyOfTable ($tableName) {
165                 // Connect to the database
166                 $this->dbLayer->connectToDatabase();
167
168                 // Ask the database layer
169                 $primaryKey = $this->dbLayer->getPrimaryKeyOfTable($tableName);
170
171                 // Return the value
172                 return $primaryKey;
173         }
174 }
175
176 // [EOF]
177 ?>