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
11 * (at your option) any later version.
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 ***************************************************************************/
24 /* from pear "MDB2" package. use "pear install MDB2" if you don't have this! */
25 require_once('MDB2.php');
32 private $is_connected;
36 * PHPFSPOT_DB class constructor
38 * This constructor initially connect to the database.
40 public function __construct($parent, $db_path)
42 $this->parent = $parent;
43 $this->db_path = $db_path;
45 /* We are starting disconnected */
46 $this->setConnStatus(false);
48 /* Connect to MySQL Database */
54 * PHPFSPOT_DB class deconstructor
56 * This destructor will close the current database connection.
58 public function __destruct()
60 $this->db_disconnect();
65 * PHPFSPOT_DB database connect
67 * This function will connect to the database via MDB2
69 private function db_connect()
71 if(($this->db = sqlite3_open($this->db_path)) === false) {
73 $this->throwError("Unable to connect to database:" . $this->getLastError());
74 $this->setConnStatus(false);
77 $this->setConnStatus(true);
82 * PHPFSPOT_DB database disconnect
84 * This function will disconnected an established database connection.
86 private function db_disconnect()
88 if($this->getConnStatus())
89 sqlite3_close($this->db);
94 * PHPFSPOT_DB database query
96 * This function will execute a SQL query and return the result as
99 public function db_query($query = "")
101 if($this->getConnStatus()) {
103 if(($result = sqlite3_query($this->db, $query)) === false)
104 $this->ThrowError($this->getLastError());
109 $this->ThrowError("Can't execute query - we are not connected!");
113 public function db_fetch_object(&$resource)
115 return sqlite3_fetch_array($resource);
120 * PHPFSPOT_DB fetch ONE row
122 * This function will execute the given but only return the
125 public function db_fetchSingleRow($query = "")
127 if($this->getConnStatus()) {
129 $result = $this->db_query($query);
130 $row = $result->fetchRow();
134 $this->ThrowError("Can't fetch row - we are not connected!");
136 } // db_fetchSingleRow()
139 * PHPFSPOT_DB number of affected rows
141 * This functions returns the number of affected rows but the
144 public function db_getNumRows($query = "")
147 $result = $this->db_query($query);
150 if(PEAR::isError($result))
151 $this->throwError($result->getMessage());
153 return $result->numRows();
158 * PHPFSPOT_DB get primary key
160 * This function returns the primary key of the last
161 * operated insert SQL query.
163 public function db_getid()
165 /* Get the last primary key ID from execute query */
166 return mysql_insert_id($this->db->connection);
171 * PHPFSPOT_DB check table exists
173 * This function checks if the given table exists in the
175 * @param string, table name
176 * @return true if table found otherwise false
178 public function db_check_table_exists($table_name = "")
180 if($this->getConnStatus()) {
181 $result = $this->db_query("SHOW TABLES");
182 $tables_in = "Tables_in_". MYSQL_DB;
184 while($row = $result->fetchRow()) {
185 if($row->$tables_in == $table_name)
191 $this->ThrowError("Can't check table - we are not connected!");
193 } // db_check_table_exists()
196 * PHPFSPOT_DB get connection status
198 * This function checks the internal state variable if already
199 * connected to database.
201 private function setConnStatus($status)
203 $this->is_connected = $status;
208 * PHPFSPOT_DB set connection status
209 * This function sets the internal state variable to indicate
210 * current database connection status.
212 private function getConnStatus()
214 return $this->is_connected;
219 * PHPFSPOT_DB throw error
221 * This function shows up error messages and afterwards through exceptions.
223 private function ThrowError($string)
225 if(!defined('DB_NOERROR')) {
226 print "<br /><br />". $string ."<br /><br />\n";
230 catch(Exectpion $e) {
234 $this->last_error = $string;
238 private function getLastError()
240 return sqlite3_error($this->db);