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!");
114 * PHPFSPOT_DB database query & execute
116 * This function will execute a SQL query and return nothing.
118 public function db_exec($query = "")
120 if($this->getConnStatus()) {
122 if(($result = sqlite3_exec($this->db, $query)) === false)
123 $this->ThrowError($this->getLastError());
127 $this->ThrowError("Can't execute query - we are not connected!");
131 public function db_fetch_object(&$resource)
133 return sqlite3_fetch_array($resource);
138 * PHPFSPOT_DB fetch ONE row
140 * This function will execute the given but only return the
143 public function db_fetchSingleRow($query = "")
145 if($this->getConnStatus()) {
147 $result = $this->db_query($query);
148 $row = $result->fetchRow();
152 $this->ThrowError("Can't fetch row - we are not connected!");
154 } // db_fetchSingleRow()
157 * PHPFSPOT_DB number of affected rows
159 * This functions returns the number of affected rows but the
162 public function db_getNumRows($query = "")
165 $result = $this->db_query($query);
168 if(PEAR::isError($result))
169 $this->throwError($result->getMessage());
171 return $result->numRows();
176 * PHPFSPOT_DB get primary key
178 * This function returns the primary key of the last
179 * operated insert SQL query.
181 public function db_getid()
183 /* Get the last primary key ID from execute query */
184 return mysql_insert_id($this->db->connection);
189 * PHPFSPOT_DB check table exists
191 * This function checks if the given table exists in the
193 * @param string, table name
194 * @return true if table found otherwise false
196 public function db_check_table_exists($table_name = "")
198 if($this->getConnStatus()) {
200 $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
201 while($table = $this->db_fetch_object($result)) {
202 if($table['name'] == $table_name)
208 $this->ThrowError("Can't check table - we are not connected!");
210 } // db_check_table_exists()
213 * PHPFSPOT_DB get connection status
215 * This function checks the internal state variable if already
216 * connected to database.
218 private function setConnStatus($status)
220 $this->is_connected = $status;
225 * PHPFSPOT_DB set connection status
226 * This function sets the internal state variable to indicate
227 * current database connection status.
229 private function getConnStatus()
231 return $this->is_connected;
236 * PHPFSPOT_DB throw error
238 * This function shows up error messages and afterwards through exceptions.
240 private function ThrowError($string)
242 if(!defined('DB_NOERROR')) {
243 print "<br /><br />". $string ."<br /><br />\n";
247 catch(Exectpion $e) {
251 $this->last_error = $string;
255 private function getLastError()
257 return sqlite3_error($this->db);