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