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