3 /***************************************************************************
5 * phpfspot, presents your F-Spot photo collection in Web browsers.
7 * Copyright (c) by Andreas Unterkircher
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
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.
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.
23 ***************************************************************************/
30 private $is_connected;
35 * PHPFSPOT_DB class constructor
37 * This constructor initially connect to the database.
39 public function __construct($parent, $db_path)
41 $this->parent = $parent;
42 $this->db_path = $db_path;
44 /* We are starting disconnected */
45 $this->setConnStatus(false);
47 /* Connect to database */
53 * PHPFSPOT_DB class deconstructor
55 * This destructor will close the current database connection.
57 public function __destruct()
59 $this->db_disconnect();
64 * PHPFSPOT_DB database connect
66 * This function will connect to the database
68 private function db_connect()
70 switch($this->parent->cfg->db_access) {
72 if(($this->db = sqlite3_open($this->db_path)) === false) {
73 $this->throwError("Unable to connect to database:" . $this->getLastError());
74 $this->setConnStatus(false);
77 sqlite3_create_function($this->db, 'basename', 1, 'basename');
78 $this->setConnStatus(true);
83 $this->db = new PDO("sqlite:".$this->db_path);
84 $this->setConnStatus(true);
86 catch (Exception $e) {
87 $this->throwError("Unable to connect to database:" . $e->getMessage());
88 $this->setConnStatus(false);
97 * PHPFSPOT_DB database disconnect
99 * This function will disconnected an established database connection.
101 private function db_disconnect()
103 switch($this->parent->cfg->db_access) {
105 if($this->getConnStatus())
106 sqlite3_close($this->db);
116 * PHPFSPOT_DB database query
118 * This function will execute a SQL query and return the result as
121 public function db_query($query = "")
123 if($this->getConnStatus()) {
125 $this->last_query = $query;
127 switch($this->parent->cfg->db_access) {
129 if(($result = sqlite3_query($this->db, $query)) === false)
130 $this->ThrowError($this->getLastError());
134 $result = $this->db->query($query);
137 catch (Exception $e) {
138 $this->ThrowError($e->getMessage());
148 $this->ThrowError("Can't execute query - we are not connected!");
153 * PHPFSPOT_DB database query & execute
155 * This function will execute a SQL query and return nothing.
157 public function db_exec($query = "")
159 if($this->getConnStatus()) {
161 $this->last_query = $query;
163 switch($this->parent->cfg->db_access) {
165 if(($result = sqlite3_exec($this->db, $query)) === false)
166 $this->ThrowError($this->getLastError());
170 $result = $this->db->query($query);
172 catch (Exception $e){
173 $this->ThrowError($e->getLMessage());
179 $this->ThrowError("Can't execute query - we are not connected!");
183 public function db_fetch_object($resource)
185 switch($this->parent->cfg->db_access) {
187 return sqlite3_fetch_array($resource);
189 return $resource->fetch();
195 * PHPFSPOT_DB fetch ONE row
197 * This function will execute the given but only return the
200 public function db_fetchSingleRow($query = "")
202 if($this->getConnStatus()) {
204 $result = $this->db_query($query);
205 switch($this->parent->cfg->db_access) {
207 $row = $this->db_fetch_object($result);
210 $row = $result->fetch();
216 $this->ThrowError("Can't fetch row - we are not connected!");
218 } // db_fetchSingleRow()
221 * PHPFSPOT_DB number of affected rows
223 * This functions returns the number of affected rows but the
226 public function db_getNumRows($query = "")
229 $result = $this->db_query($query);
232 if(PEAR::isError($result))
233 $this->throwError($result->getMessage());
235 return $result->numRows();
240 * PHPFSPOT_DB check table exists
242 * This function checks if the given table exists in the
244 * @param string, table name
245 * @return true if table found otherwise false
247 public function db_check_table_exists($table_name = "")
249 if($this->getConnStatus()) {
251 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
252 switch($this->parent->cfg->db_access) {
254 while($table = $this->db_fetch_object($result)) {
255 if($table['name'] == $table_name)
260 foreach($result as $table ){
261 if($table['NAME'] == $table_name)
270 $this->ThrowError("Can't check table - we are not connected!");
272 } // db_check_table_exists()
275 * PHPFSPOT_DB get connection status
277 * This function checks the internal state variable if already
278 * connected to database.
280 private function setConnStatus($status)
282 $this->is_connected = $status;
287 * PHPFSPOT_DB set connection status
288 * This function sets the internal state variable to indicate
289 * current database connection status.
291 private function getConnStatus()
293 return $this->is_connected;
298 * PHPFSPOT_DB throw error
300 * This function shows up error messages and afterwards through exceptions.
302 private function ThrowError($string)
304 if(!defined('DB_NOERROR')) {
305 print "Error during query: ". $this->last_query ."<br /><br />\n";
306 print "<br /><br />". $string ."<br /><br />\n";
310 catch(Exectpion $e) {
314 $this->last_error = $string;
318 private function getLastError()
320 switch($this->parent->cfg->db_access) {
322 return sqlite3_error($this->db);
324 return "". $this->db->errorInfo();