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;
33 * PHPFSPOT_DB class constructor
35 * This constructor initially connect to the database.
37 public function __construct($parent, $db_path)
39 $this->parent = $parent;
40 $this->db_path = $db_path;
42 /* We are starting disconnected */
43 $this->setConnStatus(false);
45 /* Connect to MySQL Database */
51 * PHPFSPOT_DB class deconstructor
53 * This destructor will close the current database connection.
55 public function __destruct()
57 $this->db_disconnect();
62 * PHPFSPOT_DB database connect
64 * This function will connect to the database via MDB2
66 private function db_connect()
68 switch($this->parent->cfg->db_access) {
70 if(($this->db = sqlite3_open($this->db_path)) === false) {
71 $this->throwError("Unable to connect to database:" . $this->getLastError());
72 $this->setConnStatus(false);
75 $this->setConnStatus(true);
80 $this->db = new PDO("sqlite:".$this->db_path);
81 $this->setConnStatus(true);
83 catch (Exception $e) {
84 $this->throwError("Unable to connect to database:" . $e->getMessage());
85 $this->setConnStatus(false);
94 * PHPFSPOT_DB database disconnect
96 * This function will disconnected an established database connection.
98 private function db_disconnect()
100 switch($this->parent->cfg->db_access) {
102 if($this->getConnStatus())
103 sqlite3_close($this->db);
113 * PHPFSPOT_DB database query
115 * This function will execute a SQL query and return the result as
118 public function db_query($query = "")
120 if($this->getConnStatus()) {
122 switch($this->parent->cfg->db_access) {
124 if(($result = sqlite3_query($this->db, $query)) === false)
125 $this->ThrowError($this->getLastError());
129 $result = $this->db->query($query);
132 catch (Exception $e) {
133 $this->ThrowError($e->getMessage());
143 $this->ThrowError("Can't execute query - we are not connected!");
148 * PHPFSPOT_DB database query & execute
150 * This function will execute a SQL query and return nothing.
152 public function db_exec($query = "")
154 if($this->getConnStatus()) {
156 switch($this->parent->cfg->db_access) {
158 if(($result = sqlite3_exec($this->db, $query)) === false)
159 $this->ThrowError($this->getLastError());
163 $result = $this->db->query($query);
165 catch (Exception $e){
166 $this->ThrowError($e->getLMessage());
172 $this->ThrowError("Can't execute query - we are not connected!");
176 public function db_fetch_object($resource)
178 switch($this->parent->cfg->db_access) {
180 return sqlite3_fetch_array($resource);
182 return $resource->fetch();
188 * PHPFSPOT_DB fetch ONE row
190 * This function will execute the given but only return the
193 public function db_fetchSingleRow($query = "")
195 if($this->getConnStatus()) {
197 $result = $this->db_query($query);
198 switch($this->parent->cfg->db_access) {
200 $row = $result->fetchRow();
209 $this->ThrowError("Can't fetch row - we are not connected!");
211 } // db_fetchSingleRow()
214 * PHPFSPOT_DB number of affected rows
216 * This functions returns the number of affected rows but the
219 public function db_getNumRows($query = "")
222 $result = $this->db_query($query);
225 if(PEAR::isError($result))
226 $this->throwError($result->getMessage());
228 return $result->numRows();
233 * PHPFSPOT_DB get primary key
235 * This function returns the primary key of the last
236 * operated insert SQL query.
238 public function db_getid()
240 /* Get the last primary key ID from execute query */
241 return mysql_insert_id($this->db->connection);
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 "<br /><br />". $string ."<br /><br />\n";
315 catch(Exectpion $e) {
319 $this->last_error = $string;
323 private function getLastError()
325 switch($this->parent->cfg->db_access) {
327 return sqlite3_error($this->db);
329 return "". $this->db->errorInfo();