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())
133 $this->last_query = $query;
135 switch($this->parent->cfg->db_access) {
137 if(($result = sqlite3_query($this->db, $query)) === false)
138 $this->ThrowError($this->getLastError());
142 $result = $this->db->query($query);
145 catch (Exception $e) {
146 $this->ThrowError($e->getMessage());
158 * PHPFSPOT_DB database query & execute
160 * This function will execute a SQL query and return nothing.
162 public function db_exec($query = "")
164 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());
178 $result = $this->db->query($query);
180 catch (Exception $e){
181 $this->ThrowError($e->getLMessage());
191 public function db_fetch_object($resource)
193 switch($this->parent->cfg->db_access) {
195 return sqlite3_fetch_array($resource);
197 return $resource->fetch();
203 * PHPFSPOT_DB fetch ONE row
205 * This function will execute the given but only return the
208 public function db_fetchSingleRow($query = "")
210 if(!$this->getConnStatus())
213 $result = $this->db_query($query);
214 switch($this->parent->cfg->db_access) {
216 $row = $this->db_fetch_object($result);
219 $row = $result->fetch();
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())
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)
276 } // db_check_table_exists()
279 * PHPFSPOT_DB check column exist
281 * This function checks if the given column exists within
282 * the specified table.
284 public function db_check_column_exists($table_name, $column)
286 if(!$this->getConnStatus())
289 $result = $this->db_query("
292 (SELECT * FROM sqlite_master UNION ALL
293 SELECT * FROM sqlite_temp_master)
295 tbl_name LIKE '". $table_name ."'
298 AND name NOT LIKE 'sqlite_%'
299 ORDER BY substr(type,2,1), name
302 while($row = $this->db_fetch_object($result)) {
303 /* CREATE TABLE xx ( col1 int, col2 bool, col3 ...) */
304 if(strstr($row['sql'], " ". $column ." ") !== false)
310 } // db_check_column_exists()
313 * PHPFSPOT_DB get connection status
315 * This function checks the internal state variable if already
316 * connected to database.
318 private function setConnStatus($status)
320 $this->is_connected = $status;
325 * PHPFSPOT_DB set connection status
326 * This function sets the internal state variable to indicate
327 * current database connection status.
329 private function getConnStatus()
331 return $this->is_connected;
336 * PHPFSPOT_DB throw error
338 * This function shows up error messages and afterwards through exceptions.
340 private function ThrowError($string)
342 if(!defined('DB_NOERROR')) {
343 print "Error during query: ". $this->last_query ."<br /><br />\n";
344 print "<br /><br />". $string ."<br /><br />\n";
348 catch(Exectpion $e) {
352 $this->last_error = $string;
356 private function getLastError()
358 switch($this->parent->cfg->db_access) {
360 return sqlite3_error($this->db);
362 return "". $this->db->errorInfo();
370 * this will start a transaction on ACID-supporting database
375 public function db_start_transaction()
377 if(!$this->getConnStatus())
380 $result = $this->db_exec("BEGIN");
383 if(PEAR::isError($result))
384 $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
388 } // db_start_transaction()
393 * this will commit an ongoing transaction on ACID-supporting
398 public function db_commit_transaction()
400 if(!$this->getConnStatus())
403 $result = $this->db_exec("COMMIT");
406 if(PEAR::isError($result))
407 $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
411 } // db_commit_transaction()
414 * rollback transaction()
416 * this function aborts a on going transaction
420 public function db_rollback_transaction()
422 if(!$this->getConnStatus())
425 $result = $this->db_exec("ROLLBACK");
428 if(PEAR::isError($result))
429 $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
433 } // db_rollback_transaction()