further pimped code for phpDocumentor
[phpfspot.git] / phpfspot_db.php
1 <?php
2
3 /***************************************************************************
4  *
5  * phpfspot, presents your F-Spot photo collection in Web browsers.
6  *
7  * Copyright (c) by Andreas Unterkircher
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  ***************************************************************************/
24
25 /**
26  * PHPFSPOT_DB class
27  * @package phpfspot
28  */
29 class PHPFSPOT_DB {
30
31    private $db;
32    private $db_path;
33    private $parent;
34    private $is_connected;
35    private $last_error;
36    private $last_query;
37
38    /**
39     * PHPFSPOT_DB class constructor
40     *
41     * This constructor initially connect to the database.
42     */
43    public function __construct($parent, $db_path)
44    {
45       global $phpfspot;
46
47       $this->parent = $phpfspot;
48       $this->db_path = $db_path;
49
50       /* We are starting disconnected */
51       $this->setConnStatus(false);
52
53       /* Connect to database */
54       $this->db_connect();
55
56    } // __construct()
57          
58    /**
59     * PHPFSPOT_DB class deconstructor
60     *
61     * This destructor will close the current database connection.
62     */ 
63    public function __destruct()
64    {
65       $this->db_disconnect();
66
67    } // _destruct()
68
69    /**
70     * PHPFSPOT_DB database connect
71     *
72     * This function will connect to the database
73     */
74    private function db_connect()
75    {
76       switch($this->parent->cfg->db_access) {
77          case 'native':
78             if(($this->db = sqlite3_open($this->db_path)) === false) {
79                $this->throwError("Unable to connect to database:" . $this->getLastError());
80                $this->setConnStatus(false);
81             }
82             else {
83                sqlite3_create_function($this->db, 'basename', 1, 'basename');
84                $this->setConnStatus(true);
85             }
86             break;
87          case 'pdo':
88             try {
89                $this->db =  new PDO("sqlite:".$this->db_path);
90                $this->setConnStatus(true);
91             }
92             catch (Exception $e) {
93                $this->throwError("Unable to connect to database:" . $e->getMessage());
94                $this->setConnStatus(false);
95             }
96             break;
97
98       }
99
100    } // db_connect()
101
102    /**
103     * PHPFSPOT_DB database disconnect
104     *
105     * This function will disconnected an established database connection.
106     */
107    private function db_disconnect()
108    {
109       switch($this->parent->cfg->db_access) {
110          case 'native':
111             if($this->getConnStatus())
112                sqlite3_close($this->db);
113             break;
114          case 'pdo':
115             $this->db = NULL;
116             break;
117       }
118
119    } // db_disconnect()
120
121    /**
122     * PHPFSPOT_DB database query
123     *
124     * This function will execute a SQL query and return the result as
125     * object.
126     */
127    public function db_query($query = "")
128    {
129       if($this->getConnStatus()) {
130    
131          $this->last_query = $query;
132
133          switch($this->parent->cfg->db_access) {
134             case 'native':
135                if(($result = sqlite3_query($this->db, $query)) === false)
136                   $this->ThrowError($this->getLastError());
137                break;
138             case 'pdo':
139                try{
140                   $result = $this->db->query($query);
141                   return $result;
142                }
143                catch (Exception $e) {
144                   $this->ThrowError($e->getMessage());
145                   $result= NULL;
146                }
147                break;
148
149          }
150         
151          return $result;
152       }
153       else 
154          $this->ThrowError("Can't execute query - we are not connected!");
155
156    } // db_query()
157
158    /**
159     * PHPFSPOT_DB database query & execute
160     *
161     * This function will execute a SQL query and return nothing.
162     */
163    public function db_exec($query = "")
164    {
165       if($this->getConnStatus()) {
166
167          $this->last_query = $query;
168
169          switch($this->parent->cfg->db_access) {
170             case 'native':
171                if(($result = sqlite3_exec($this->db, $query)) === false)
172                   $this->ThrowError($this->getLastError());
173                break;
174             case 'pdo':
175                try {
176                   $result = $this->db->query($query);
177                }
178                catch (Exception $e){
179                   $this->ThrowError($e->getLMessage());
180                }
181                break;
182          }
183       }
184       else 
185          $this->ThrowError("Can't execute query - we are not connected!");
186
187    } // db_exec()
188
189    public function db_fetch_object($resource)
190    {
191       switch($this->parent->cfg->db_access) {
192          case 'native':
193             return sqlite3_fetch_array($resource);
194          case 'pdo':
195             return $resource->fetch();
196       }
197
198    } // db_fetch_object
199
200    /**
201     * PHPFSPOT_DB fetch ONE row
202     *
203     * This function will execute the given but only return the
204     * first result.
205     */
206    public function db_fetchSingleRow($query = "") 
207    {
208       if($this->getConnStatus()) {
209
210          $result = $this->db_query($query);
211          switch($this->parent->cfg->db_access) {
212             case 'native':
213                $row = $this->db_fetch_object($result);
214                break;
215             case 'pdo':
216                $row = $result->fetch();
217                break;
218          }
219          return $row;
220       }
221       else 
222          $this->ThrowError("Can't fetch row - we are not connected!");
223       
224    } // db_fetchSingleRow()
225
226    /**
227     * PHPFSPOT_DB number of affected rows
228     *
229     * This functions returns the number of affected rows but the
230     * given SQL query.
231     */
232    public function db_getNumRows($query = "")
233    {
234       /* Execute query */
235       $result = $this->db_query($query);
236
237       /* Errors? */
238       if(PEAR::isError($result)) 
239          $this->throwError($result->getMessage());
240
241       return $result->numRows();
242
243    } // db_getNumRows()
244
245    /**
246     * PHPFSPOT_DB check table exists
247     *
248     * This function checks if the given table exists in the
249     * database
250     * @param string, table name
251     * @return true if table found otherwise false
252     */
253    public function db_check_table_exists($table_name = "")
254    {
255       if($this->getConnStatus()) {
256
257          $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
258          switch($this->parent->cfg->db_access) {
259             case 'native':
260                while($table = $this->db_fetch_object($result)) {
261                   if($table['name'] == $table_name)
262                      return true;
263                }
264                break;
265             case 'pdo':
266                foreach($result as $table ){
267                   if($table['NAME'] == $table_name)
268                      return true;
269                }
270                break;
271           }
272
273          return false;
274       }
275       else
276          $this->ThrowError("Can't check table - we are not connected!");
277          
278    } // db_check_table_exists()
279
280    /**
281     * PHPFSPOT_DB get connection status
282     *
283     * This function checks the internal state variable if already
284     * connected to database.
285     */
286    private function setConnStatus($status)
287    {
288       $this->is_connected = $status;
289       
290    } // setConnStatus()
291
292    /**
293     * PHPFSPOT_DB set connection status
294     * This function sets the internal state variable to indicate
295     * current database connection status.
296     */
297    private function getConnStatus()
298    {
299       return $this->is_connected;
300
301    } // getConnStatus()
302
303    /**
304     * PHPFSPOT_DB throw error
305     *
306     * This function shows up error messages and afterwards through exceptions.
307     */
308    private function ThrowError($string)
309    {
310       if(!defined('DB_NOERROR'))  {
311          print "Error during query: ". $this->last_query ."<br /><br />\n";
312          print "<br /><br />". $string ."<br /><br />\n";
313          try {
314             throw new Exception;
315          }
316          catch(Exectpion $e) {
317          }
318       }
319
320       $this->last_error = $string;
321          
322    } // ThrowError()
323
324    private function getLastError()
325    {
326       switch($this->parent->cfg->db_access) {
327          case 'native':
328             return sqlite3_error($this->db);
329          case 'pdo':
330             return "". $this->db->errorInfo();
331       }
332
333    } // getLastError()
334
335 }
336
337 ?>