3 /***************************************************************************
5 * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 ***************************************************************************/
29 private $is_connected;
34 * PHPFSPOT_DB class constructor
36 * This constructor initially connect to the database.
38 public function __construct($parent, $db_path)
40 $this->parent = $parent;
41 $this->db_path = $db_path;
43 /* We are starting disconnected */
44 $this->setConnStatus(false);
46 /* Connect to database */
52 * PHPFSPOT_DB class deconstructor
54 * This destructor will close the current database connection.
56 public function __destruct()
58 $this->db_disconnect();
63 * PHPFSPOT_DB database connect
65 * This function will connect to the database
67 private function db_connect()
69 switch($this->parent->cfg->db_access) {
71 if(($this->db = sqlite3_open($this->db_path)) === false) {
72 $this->throwError("Unable to connect to database:" . $this->getLastError());
73 $this->setConnStatus(false);
76 sqlite3_create_function($this->db, 'basename', 1, 'basename');
77 $this->setConnStatus(true);
82 $this->db = new PDO("sqlite:".$this->db_path);
83 $this->setConnStatus(true);
85 catch (Exception $e) {
86 $this->throwError("Unable to connect to database:" . $e->getMessage());
87 $this->setConnStatus(false);
96 * PHPFSPOT_DB database disconnect
98 * This function will disconnected an established database connection.
100 private function db_disconnect()
102 switch($this->parent->cfg->db_access) {
104 if($this->getConnStatus())
105 sqlite3_close($this->db);
115 * PHPFSPOT_DB database query
117 * This function will execute a SQL query and return the result as
120 public function db_query($query = "")
122 if($this->getConnStatus()) {
124 $this->last_query = $query;
126 switch($this->parent->cfg->db_access) {
128 if(($result = sqlite3_query($this->db, $query)) === false)
129 $this->ThrowError($this->getLastError());
133 $result = $this->db->query($query);
136 catch (Exception $e) {
137 $this->ThrowError($e->getMessage());
147 $this->ThrowError("Can't execute query - we are not connected!");
152 * PHPFSPOT_DB database query & execute
154 * This function will execute a SQL query and return nothing.
156 public function db_exec($query = "")
158 if($this->getConnStatus()) {
160 $this->last_query = $query;
162 switch($this->parent->cfg->db_access) {
164 if(($result = sqlite3_exec($this->db, $query)) === false)
165 $this->ThrowError($this->getLastError());
169 $result = $this->db->query($query);
171 catch (Exception $e){
172 $this->ThrowError($e->getLMessage());
178 $this->ThrowError("Can't execute query - we are not connected!");
182 public function db_fetch_object($resource)
184 switch($this->parent->cfg->db_access) {
186 return sqlite3_fetch_array($resource);
188 return $resource->fetch();
194 * PHPFSPOT_DB fetch ONE row
196 * This function will execute the given but only return the
199 public function db_fetchSingleRow($query = "")
201 if($this->getConnStatus()) {
203 $result = $this->db_query($query);
204 switch($this->parent->cfg->db_access) {
206 $row = $this->db_fetch_object($result);
209 $row = $result->fetch();
215 $this->ThrowError("Can't fetch row - we are not connected!");
217 } // db_fetchSingleRow()
220 * PHPFSPOT_DB number of affected rows
222 * This functions returns the number of affected rows but the
225 public function db_getNumRows($query = "")
228 $result = $this->db_query($query);
231 if(PEAR::isError($result))
232 $this->throwError($result->getMessage());
234 return $result->numRows();
239 * PHPFSPOT_DB check table exists
241 * This function checks if the given table exists in the
243 * @param string, table name
244 * @return true if table found otherwise false
246 public function db_check_table_exists($table_name = "")
248 if($this->getConnStatus()) {
250 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
251 switch($this->parent->cfg->db_access) {
253 while($table = $this->db_fetch_object($result)) {
254 if($table['name'] == $table_name)
259 foreach($result as $table ){
260 if($table['NAME'] == $table_name)
269 $this->ThrowError("Can't check table - we are not connected!");
271 } // db_check_table_exists()
274 * PHPFSPOT_DB get connection status
276 * This function checks the internal state variable if already
277 * connected to database.
279 private function setConnStatus($status)
281 $this->is_connected = $status;
286 * PHPFSPOT_DB set connection status
287 * This function sets the internal state variable to indicate
288 * current database connection status.
290 private function getConnStatus()
292 return $this->is_connected;
297 * PHPFSPOT_DB throw error
299 * This function shows up error messages and afterwards through exceptions.
301 private function ThrowError($string)
303 if(!defined('DB_NOERROR')) {
304 print "Error during query: ". $this->last_query ."<br /><br />\n";
305 print "<br /><br />". $string ."<br /><br />\n";
309 catch(Exectpion $e) {
313 $this->last_error = $string;
317 private function getLastError()
319 switch($this->parent->cfg->db_access) {
321 return sqlite3_error($this->db);
323 return "". $this->db->errorInfo();