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 $this->setConnStatus(true);
81 $this->db = new PDO("sqlite:".$this->db_path);
82 $this->setConnStatus(true);
84 catch (Exception $e) {
85 $this->throwError("Unable to connect to database:" . $e->getMessage());
86 $this->setConnStatus(false);
95 * PHPFSPOT_DB database disconnect
97 * This function will disconnected an established database connection.
99 private function db_disconnect()
101 switch($this->parent->cfg->db_access) {
103 if($this->getConnStatus())
104 sqlite3_close($this->db);
114 * PHPFSPOT_DB database query
116 * This function will execute a SQL query and return the result as
119 public function db_query($query = "")
121 if($this->getConnStatus()) {
123 $this->last_query = $query;
125 switch($this->parent->cfg->db_access) {
127 if(($result = sqlite3_query($this->db, $query)) === false)
128 $this->ThrowError($this->getLastError());
132 $result = $this->db->query($query);
135 catch (Exception $e) {
136 $this->ThrowError($e->getMessage());
146 $this->ThrowError("Can't execute query - we are not connected!");
151 * PHPFSPOT_DB database query & execute
153 * This function will execute a SQL query and return nothing.
155 public function db_exec($query = "")
157 if($this->getConnStatus()) {
159 $this->last_query = $query;
161 switch($this->parent->cfg->db_access) {
163 if(($result = sqlite3_exec($this->db, $query)) === false)
164 $this->ThrowError($this->getLastError());
168 $result = $this->db->query($query);
170 catch (Exception $e){
171 $this->ThrowError($e->getLMessage());
177 $this->ThrowError("Can't execute query - we are not connected!");
181 public function db_fetch_object($resource)
183 switch($this->parent->cfg->db_access) {
185 return sqlite3_fetch_array($resource);
187 return $resource->fetch();
193 * PHPFSPOT_DB fetch ONE row
195 * This function will execute the given but only return the
198 public function db_fetchSingleRow($query = "")
200 if($this->getConnStatus()) {
202 $result = $this->db_query($query);
203 switch($this->parent->cfg->db_access) {
205 $row = $this->db_fetch_object($result);
214 $this->ThrowError("Can't fetch row - we are not connected!");
216 } // db_fetchSingleRow()
219 * PHPFSPOT_DB number of affected rows
221 * This functions returns the number of affected rows but the
224 public function db_getNumRows($query = "")
227 $result = $this->db_query($query);
230 if(PEAR::isError($result))
231 $this->throwError($result->getMessage());
233 return $result->numRows();
238 * PHPFSPOT_DB check table exists
240 * This function checks if the given table exists in the
242 * @param string, table name
243 * @return true if table found otherwise false
245 public function db_check_table_exists($table_name = "")
247 if($this->getConnStatus()) {
249 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
250 switch($this->parent->cfg->db_access) {
252 while($table = $this->db_fetch_object($result)) {
253 if($table['name'] == $table_name)
258 foreach($result as $table ){
259 if($table['NAME'] == $table_name)
268 $this->ThrowError("Can't check table - we are not connected!");
270 } // db_check_table_exists()
273 * PHPFSPOT_DB get connection status
275 * This function checks the internal state variable if already
276 * connected to database.
278 private function setConnStatus($status)
280 $this->is_connected = $status;
285 * PHPFSPOT_DB set connection status
286 * This function sets the internal state variable to indicate
287 * current database connection status.
289 private function getConnStatus()
291 return $this->is_connected;
296 * PHPFSPOT_DB throw error
298 * This function shows up error messages and afterwards through exceptions.
300 private function ThrowError($string)
302 if(!defined('DB_NOERROR')) {
303 print "Error during query: ". $this->last_query ."<br /><br />\n";
304 print "<br /><br />". $string ."<br /><br />\n";
308 catch(Exectpion $e) {
312 $this->last_error = $string;
316 private function getLastError()
318 switch($this->parent->cfg->db_access) {
320 return sqlite3_error($this->db);
322 return "". $this->db->errorInfo();