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 ***************************************************************************/
35 private $is_connected;
40 * PHPFSPOT_DB class constructor
42 * This constructor initially connect to the database.
44 public function __construct($parent, $db_path)
48 $this->parent = $phpfspot;
49 $this->db_path = $db_path;
51 /* We are starting disconnected */
52 $this->setConnStatus(false);
54 /* Connect to database */
60 * PHPFSPOT_DB class deconstructor
62 * This destructor will close the current database connection.
64 public function __destruct()
66 $this->db_disconnect();
71 * PHPFSPOT_DB database connect
73 * This function will connect to the database
75 private function db_connect()
77 switch($this->parent->cfg->db_access) {
79 if(($this->db = sqlite3_open($this->db_path)) === false) {
80 $this->throwError("Unable to connect to database:" . $this->getLastError());
81 $this->setConnStatus(false);
84 sqlite3_create_function($this->db, 'basename', 1, 'basename');
85 $this->setConnStatus(true);
90 $this->db = new PDO("sqlite:".$this->db_path);
91 $this->setConnStatus(true);
93 catch (Exception $e) {
94 $this->throwError("Unable to connect to database:" . $e->getMessage());
95 $this->setConnStatus(false);
104 * PHPFSPOT_DB database disconnect
106 * This function will disconnected an established database connection.
108 private function db_disconnect()
110 switch($this->parent->cfg->db_access) {
112 if($this->getConnStatus())
113 sqlite3_close($this->db);
123 * PHPFSPOT_DB database query
125 * This function will execute a SQL query and return the result as
128 public function db_query($query = "")
130 if($this->getConnStatus()) {
132 $this->last_query = $query;
134 switch($this->parent->cfg->db_access) {
136 if(($result = sqlite3_query($this->db, $query)) === false)
137 $this->ThrowError($this->getLastError());
141 $result = $this->db->query($query);
144 catch (Exception $e) {
145 $this->ThrowError($e->getMessage());
155 $this->ThrowError("Can't execute query - we are not connected!");
160 * PHPFSPOT_DB database query & execute
162 * This function will execute a SQL query and return nothing.
164 public function db_exec($query = "")
166 if($this->getConnStatus()) {
168 $this->last_query = $query;
170 switch($this->parent->cfg->db_access) {
172 if(($result = sqlite3_exec($this->db, $query)) === false)
173 $this->ThrowError($this->getLastError());
177 $result = $this->db->query($query);
179 catch (Exception $e){
180 $this->ThrowError($e->getLMessage());
186 $this->ThrowError("Can't execute query - we are not connected!");
190 public function db_fetch_object($resource)
192 switch($this->parent->cfg->db_access) {
194 return sqlite3_fetch_array($resource);
196 return $resource->fetch();
202 * PHPFSPOT_DB fetch ONE row
204 * This function will execute the given but only return the
207 public function db_fetchSingleRow($query = "")
209 if($this->getConnStatus()) {
211 $result = $this->db_query($query);
212 switch($this->parent->cfg->db_access) {
214 $row = $this->db_fetch_object($result);
217 $row = $result->fetch();
223 $this->ThrowError("Can't fetch row - we are not connected!");
225 } // db_fetchSingleRow()
228 * PHPFSPOT_DB number of affected rows
230 * This functions returns the number of affected rows but the
233 public function db_getNumRows($query = "")
236 $result = $this->db_query($query);
239 if(PEAR::isError($result))
240 $this->throwError($result->getMessage());
242 return $result->numRows();
247 * PHPFSPOT_DB check table exists
249 * This function checks if the given table exists in the
251 * @param string, table name
252 * @return true if table found otherwise false
254 public function db_check_table_exists($table_name = "")
256 if($this->getConnStatus()) {
258 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
259 switch($this->parent->cfg->db_access) {
261 while($table = $this->db_fetch_object($result)) {
262 if($table['name'] == $table_name)
267 foreach($result as $table ){
268 if($table['NAME'] == $table_name)
277 $this->ThrowError("Can't check table - we are not connected!");
279 } // db_check_table_exists()
282 * PHPFSPOT_DB get connection status
284 * This function checks the internal state variable if already
285 * connected to database.
287 private function setConnStatus($status)
289 $this->is_connected = $status;
294 * PHPFSPOT_DB set connection status
295 * This function sets the internal state variable to indicate
296 * current database connection status.
298 private function getConnStatus()
300 return $this->is_connected;
305 * PHPFSPOT_DB throw error
307 * This function shows up error messages and afterwards through exceptions.
309 private function ThrowError($string)
311 if(!defined('DB_NOERROR')) {
312 print "Error during query: ". $this->last_query ."<br /><br />\n";
313 print "<br /><br />". $string ."<br /><br />\n";
317 catch(Exectpion $e) {
321 $this->last_error = $string;
325 private function getLastError()
327 switch($this->parent->cfg->db_access) {
329 return sqlite3_error($this->db);
331 return "". $this->db->errorInfo();