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 ***************************************************************************/
34 private $is_connected;
39 * PHPFSPOT_DB class constructor
41 * This constructor initially connect to the database.
43 public function __construct($parent, $db_path)
47 $this->parent = $phpfspot;
48 $this->db_path = $db_path;
50 /* We are starting disconnected */
51 $this->setConnStatus(false);
53 /* Connect to database */
59 * PHPFSPOT_DB class deconstructor
61 * This destructor will close the current database connection.
63 public function __destruct()
65 $this->db_disconnect();
70 * PHPFSPOT_DB database connect
72 * This function will connect to the database
74 private function db_connect()
76 switch($this->parent->cfg->db_access) {
78 if(($this->db = sqlite3_open($this->db_path)) === false) {
79 $this->throwError("Unable to connect to database:" . $this->getLastError());
80 $this->setConnStatus(false);
83 sqlite3_create_function($this->db, 'basename', 1, 'basename');
84 $this->setConnStatus(true);
89 $this->db = new PDO("sqlite:".$this->db_path);
90 $this->setConnStatus(true);
92 catch (Exception $e) {
93 $this->throwError("Unable to connect to database:" . $e->getMessage());
94 $this->setConnStatus(false);
103 * PHPFSPOT_DB database disconnect
105 * This function will disconnected an established database connection.
107 private function db_disconnect()
109 switch($this->parent->cfg->db_access) {
111 if($this->getConnStatus())
112 sqlite3_close($this->db);
122 * PHPFSPOT_DB database query
124 * This function will execute a SQL query and return the result as
127 public function db_query($query = "")
129 if($this->getConnStatus()) {
131 $this->last_query = $query;
133 switch($this->parent->cfg->db_access) {
135 if(($result = sqlite3_query($this->db, $query)) === false)
136 $this->ThrowError($this->getLastError());
140 $result = $this->db->query($query);
143 catch (Exception $e) {
144 $this->ThrowError($e->getMessage());
154 $this->ThrowError("Can't execute query - we are not connected!");
159 * PHPFSPOT_DB database query & execute
161 * This function will execute a SQL query and return nothing.
163 public function db_exec($query = "")
165 if($this->getConnStatus()) {
167 $this->last_query = $query;
169 switch($this->parent->cfg->db_access) {
171 if(($result = sqlite3_exec($this->db, $query)) === false)
172 $this->ThrowError($this->getLastError());
176 $result = $this->db->query($query);
178 catch (Exception $e){
179 $this->ThrowError($e->getLMessage());
185 $this->ThrowError("Can't execute query - we are not connected!");
189 public function db_fetch_object($resource)
191 switch($this->parent->cfg->db_access) {
193 return sqlite3_fetch_array($resource);
195 return $resource->fetch();
201 * PHPFSPOT_DB fetch ONE row
203 * This function will execute the given but only return the
206 public function db_fetchSingleRow($query = "")
208 if($this->getConnStatus()) {
210 $result = $this->db_query($query);
211 switch($this->parent->cfg->db_access) {
213 $row = $this->db_fetch_object($result);
216 $row = $result->fetch();
222 $this->ThrowError("Can't fetch row - we are not connected!");
224 } // db_fetchSingleRow()
227 * PHPFSPOT_DB number of affected rows
229 * This functions returns the number of affected rows but the
232 public function db_getNumRows($query = "")
235 $result = $this->db_query($query);
238 if(PEAR::isError($result))
239 $this->throwError($result->getMessage());
241 return $result->numRows();
246 * PHPFSPOT_DB check table exists
248 * This function checks if the given table exists in the
250 * @param string, table name
251 * @return true if table found otherwise false
253 public function db_check_table_exists($table_name = "")
255 if($this->getConnStatus()) {
257 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
258 switch($this->parent->cfg->db_access) {
260 while($table = $this->db_fetch_object($result)) {
261 if($table['name'] == $table_name)
266 foreach($result as $table ){
267 if($table['NAME'] == $table_name)
276 $this->ThrowError("Can't check table - we are not connected!");
278 } // db_check_table_exists()
281 * PHPFSPOT_DB get connection status
283 * This function checks the internal state variable if already
284 * connected to database.
286 private function setConnStatus($status)
288 $this->is_connected = $status;
293 * PHPFSPOT_DB set connection status
294 * This function sets the internal state variable to indicate
295 * current database connection status.
297 private function getConnStatus()
299 return $this->is_connected;
304 * PHPFSPOT_DB throw error
306 * This function shows up error messages and afterwards through exceptions.
308 private function ThrowError($string)
310 if(!defined('DB_NOERROR')) {
311 print "Error during query: ". $this->last_query ."<br /><br />\n";
312 print "<br /><br />". $string ."<br /><br />\n";
316 catch(Exectpion $e) {
320 $this->last_error = $string;
324 private function getLastError()
326 switch($this->parent->cfg->db_access) {
328 return sqlite3_error($this->db);
330 return "". $this->db->errorInfo();