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 if(($this->db = sqlite3_open($this->db_path)) === false) {
70 $this->throwError("Unable to connect to database:" . $this->getLastError());
71 $this->setConnStatus(false);
74 $this->setConnStatus(true);
79 * PHPFSPOT_DB database disconnect
81 * This function will disconnected an established database connection.
83 private function db_disconnect()
85 if($this->getConnStatus())
86 sqlite3_close($this->db);
91 * PHPFSPOT_DB database query
93 * This function will execute a SQL query and return the result as
96 public function db_query($query = "")
98 if($this->getConnStatus()) {
100 if(($result = sqlite3_query($this->db, $query)) === false)
101 $this->ThrowError($this->getLastError());
106 $this->ThrowError("Can't execute query - we are not connected!");
111 * PHPFSPOT_DB database query & execute
113 * This function will execute a SQL query and return nothing.
115 public function db_exec($query = "")
117 if($this->getConnStatus()) {
119 if(($result = sqlite3_exec($this->db, $query)) === false)
120 $this->ThrowError($this->getLastError());
124 $this->ThrowError("Can't execute query - we are not connected!");
128 public function db_fetch_object(&$resource)
130 return sqlite3_fetch_array($resource);
135 * PHPFSPOT_DB fetch ONE row
137 * This function will execute the given but only return the
140 public function db_fetchSingleRow($query = "")
142 if($this->getConnStatus()) {
144 $result = $this->db_query($query);
145 $row = $result->fetchRow();
149 $this->ThrowError("Can't fetch row - we are not connected!");
151 } // db_fetchSingleRow()
154 * PHPFSPOT_DB number of affected rows
156 * This functions returns the number of affected rows but the
159 public function db_getNumRows($query = "")
162 $result = $this->db_query($query);
165 if(PEAR::isError($result))
166 $this->throwError($result->getMessage());
168 return $result->numRows();
173 * PHPFSPOT_DB get primary key
175 * This function returns the primary key of the last
176 * operated insert SQL query.
178 public function db_getid()
180 /* Get the last primary key ID from execute query */
181 return mysql_insert_id($this->db->connection);
186 * PHPFSPOT_DB check table exists
188 * This function checks if the given table exists in the
190 * @param string, table name
191 * @return true if table found otherwise false
193 public function db_check_table_exists($table_name = "")
195 if($this->getConnStatus()) {
197 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
198 while($table = $this->db_fetch_object($result)) {
199 if($table['name'] == $table_name)
205 $this->ThrowError("Can't check table - we are not connected!");
207 } // db_check_table_exists()
210 * PHPFSPOT_DB get connection status
212 * This function checks the internal state variable if already
213 * connected to database.
215 private function setConnStatus($status)
217 $this->is_connected = $status;
222 * PHPFSPOT_DB set connection status
223 * This function sets the internal state variable to indicate
224 * current database connection status.
226 private function getConnStatus()
228 return $this->is_connected;
233 * PHPFSPOT_DB throw error
235 * This function shows up error messages and afterwards through exceptions.
237 private function ThrowError($string)
239 if(!defined('DB_NOERROR')) {
240 print "<br /><br />". $string ."<br /><br />\n";
244 catch(Exectpion $e) {
248 $this->last_error = $string;
252 private function getLastError()
254 return sqlite3_error($this->db);