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)
45 $this->parent = $parent;
46 $this->db_path = $db_path;
48 /* We are starting disconnected */
49 $this->setConnStatus(false);
51 /* Connect to database */
57 * PHPFSPOT_DB class deconstructor
59 * This destructor will close the current database connection.
61 public function __destruct()
63 $this->db_disconnect();
68 * PHPFSPOT_DB database connect
70 * This function will connect to the database
72 private function db_connect()
74 switch($this->parent->cfg->db_access) {
76 if(($this->db = sqlite3_open($this->db_path)) === false) {
77 $this->throwError("Unable to connect to database:" . $this->getLastError());
78 $this->setConnStatus(false);
81 sqlite3_create_function($this->db, 'basename', 1, 'basename');
82 $this->setConnStatus(true);
87 $this->db = new PDO("sqlite:".$this->db_path);
88 $this->setConnStatus(true);
90 catch (Exception $e) {
91 $this->throwError("Unable to connect to database:" . $e->getMessage());
92 $this->setConnStatus(false);
101 * PHPFSPOT_DB database disconnect
103 * This function will disconnected an established database connection.
105 private function db_disconnect()
107 switch($this->parent->cfg->db_access) {
109 if($this->getConnStatus())
110 sqlite3_close($this->db);
120 * PHPFSPOT_DB database query
122 * This function will execute a SQL query and return the result as
125 public function db_query($query = "")
127 if($this->getConnStatus()) {
129 $this->last_query = $query;
131 switch($this->parent->cfg->db_access) {
133 if(($result = sqlite3_query($this->db, $query)) === false)
134 $this->ThrowError($this->getLastError());
138 $result = $this->db->query($query);
141 catch (Exception $e) {
142 $this->ThrowError($e->getMessage());
152 $this->ThrowError("Can't execute query - we are not connected!");
157 * PHPFSPOT_DB database query & execute
159 * This function will execute a SQL query and return nothing.
161 public function db_exec($query = "")
163 if($this->getConnStatus()) {
165 $this->last_query = $query;
167 switch($this->parent->cfg->db_access) {
169 if(($result = sqlite3_exec($this->db, $query)) === false)
170 $this->ThrowError($this->getLastError());
174 $result = $this->db->query($query);
176 catch (Exception $e){
177 $this->ThrowError($e->getLMessage());
183 $this->ThrowError("Can't execute query - we are not connected!");
187 public function db_fetch_object($resource)
189 switch($this->parent->cfg->db_access) {
191 return sqlite3_fetch_array($resource);
193 return $resource->fetch();
199 * PHPFSPOT_DB fetch ONE row
201 * This function will execute the given but only return the
204 public function db_fetchSingleRow($query = "")
206 if($this->getConnStatus()) {
208 $result = $this->db_query($query);
209 switch($this->parent->cfg->db_access) {
211 $row = $this->db_fetch_object($result);
214 $row = $result->fetch();
220 $this->ThrowError("Can't fetch row - we are not connected!");
222 } // db_fetchSingleRow()
225 * PHPFSPOT_DB number of affected rows
227 * This functions returns the number of affected rows but the
230 public function db_getNumRows($query = "")
233 $result = $this->db_query($query);
236 if(PEAR::isError($result))
237 $this->throwError($result->getMessage());
239 return $result->numRows();
244 * PHPFSPOT_DB check table exists
246 * This function checks if the given table exists in the
248 * @param string, table name
249 * @return true if table found otherwise false
251 public function db_check_table_exists($table_name = "")
253 if($this->getConnStatus()) {
255 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
256 switch($this->parent->cfg->db_access) {
258 while($table = $this->db_fetch_object($result)) {
259 if($table['name'] == $table_name)
264 foreach($result as $table ){
265 if($table['NAME'] == $table_name)
274 $this->ThrowError("Can't check table - we are not connected!");
276 } // db_check_table_exists()
279 * PHPFSPOT_DB get connection status
281 * This function checks the internal state variable if already
282 * connected to database.
284 private function setConnStatus($status)
286 $this->is_connected = $status;
291 * PHPFSPOT_DB set connection status
292 * This function sets the internal state variable to indicate
293 * current database connection status.
295 private function getConnStatus()
297 return $this->is_connected;
302 * PHPFSPOT_DB throw error
304 * This function shows up error messages and afterwards through exceptions.
306 private function ThrowError($string)
308 if(!defined('DB_NOERROR')) {
309 print "Error during query: ". $this->last_query ."<br /><br />\n";
310 print "<br /><br />". $string ."<br /><br />\n";
314 catch(Exectpion $e) {
318 $this->last_error = $string;
322 private function getLastError()
324 switch($this->parent->cfg->db_access) {
326 return sqlite3_error($this->db);
328 return "". $this->db->errorInfo();