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