issue63, license-, readme- and install-file
[phpfspot.git] / phpfspot_db.php
1 <?php
2
3 /***************************************************************************
4  *
5  * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
6  * All rights reserved
7  *
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  *  any later version.
12  *
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.
17  *
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.
21  *
22  ***************************************************************************/
23
24 class PHPFSPOT_DB {
25
26    private $db;
27    private $db_path;
28    private $parent;
29    private $is_connected;
30    private $last_error;
31
32    /**
33     * PHPFSPOT_DB class constructor
34     *
35     * This constructor initially connect to the database.
36     */
37    public function __construct($parent, $db_path)
38    {
39       $this->parent = $parent;
40       $this->db_path = $db_path;
41
42       /* We are starting disconnected */
43       $this->setConnStatus(false);
44
45       /* Connect to MySQL Database */
46       $this->db_connect();
47
48    } // __construct()
49          
50    /**
51     * PHPFSPOT_DB class deconstructor
52     *
53     * This destructor will close the current database connection.
54     */ 
55    public function __destruct()
56    {
57       $this->db_disconnect();
58
59    } // _destruct()
60
61    /**
62     * PHPFSPOT_DB database connect
63     *
64     * This function will connect to the database via MDB2
65     */
66    private function db_connect()
67    {
68       if(($this->db = sqlite3_open($this->db_path)) === false) {
69
70          $this->throwError("Unable to connect to database:" . $this->getLastError());
71          $this->setConnStatus(false);
72       }
73
74       $this->setConnStatus(true);
75
76    } // db_connect()
77
78    /**
79     * PHPFSPOT_DB database disconnect
80     *
81     * This function will disconnected an established database connection.
82     */
83    private function db_disconnect()
84    {
85       if($this->getConnStatus())
86          sqlite3_close($this->db);
87
88    } // db_disconnect()
89
90    /**
91     * PHPFSPOT_DB database query
92     *
93     * This function will execute a SQL query and return the result as
94     * object.
95     */
96    public function db_query($query = "")
97    {
98       if($this->getConnStatus()) {
99
100          if(($result = sqlite3_query($this->db, $query)) === false)
101             $this->ThrowError($this->getLastError());
102         
103          return $result;
104       }
105       else 
106          $this->ThrowError("Can't execute query - we are not connected!");
107
108    } // db_query()
109
110    /**
111     * PHPFSPOT_DB database query & execute
112     *
113     * This function will execute a SQL query and return nothing.
114     */
115    public function db_exec($query = "")
116    {
117       if($this->getConnStatus()) {
118
119          if(($result = sqlite3_exec($this->db, $query)) === false)
120             $this->ThrowError($this->getLastError());
121
122       }
123       else 
124          $this->ThrowError("Can't execute query - we are not connected!");
125
126    } // db_exec()
127
128    public function db_fetch_object(&$resource)
129    {
130       return sqlite3_fetch_array($resource);
131
132    } // db_fetch_object
133
134    /**
135     * PHPFSPOT_DB fetch ONE row
136     *
137     * This function will execute the given but only return the
138     * first result.
139     */
140    public function db_fetchSingleRow($query = "") 
141    {
142       if($this->getConnStatus()) {
143
144          $result = $this->db_query($query);
145          $row = $result->fetchRow();
146          return $row;
147       }
148       else 
149          $this->ThrowError("Can't fetch row - we are not connected!");
150       
151    } // db_fetchSingleRow()
152
153    /**
154     * PHPFSPOT_DB number of affected rows
155     *
156     * This functions returns the number of affected rows but the
157     * given SQL query.
158     */
159    public function db_getNumRows($query = "")
160    {
161       /* Execute query */
162       $result = $this->db_query($query);
163
164       /* Errors? */
165       if(PEAR::isError($result)) 
166          $this->throwError($result->getMessage());
167
168       return $result->numRows();
169
170    } // db_getNumRows()
171
172    /**
173     * PHPFSPOT_DB get primary key
174     *
175     * This function returns the primary key of the last
176     * operated insert SQL query.
177     */
178    public function db_getid()
179    {
180       /* Get the last primary key ID from execute query */
181       return mysql_insert_id($this->db->connection);
182       
183    } // db_getid()
184
185    /**
186     * PHPFSPOT_DB check table exists
187     *
188     * This function checks if the given table exists in the
189     * database
190     * @param string, table name
191     * @return true if table found otherwise false
192     */
193    public function db_check_table_exists($table_name = "")
194    {
195       if($this->getConnStatus()) {
196
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)
200                return true;
201          }
202          return false;
203       }
204       else
205          $this->ThrowError("Can't check table - we are not connected!");
206          
207    } // db_check_table_exists()
208
209    /**
210     * PHPFSPOT_DB get connection status
211     *
212     * This function checks the internal state variable if already
213     * connected to database.
214     */
215    private function setConnStatus($status)
216    {
217       $this->is_connected = $status;
218       
219    } // setConnStatus()
220
221    /**
222     * PHPFSPOT_DB set connection status
223     * This function sets the internal state variable to indicate
224     * current database connection status.
225     */
226    private function getConnStatus()
227    {
228       return $this->is_connected;
229
230    } // getConnStatus()
231
232    /**
233     * PHPFSPOT_DB throw error
234     *
235     * This function shows up error messages and afterwards through exceptions.
236     */
237    private function ThrowError($string)
238    {
239       if(!defined('DB_NOERROR'))  {
240          print "<br /><br />". $string ."<br /><br />\n";
241          try {
242             throw new Exception;
243          }
244          catch(Exectpion $e) {
245          }
246       }
247
248       $this->last_error = $string;
249          
250    } // ThrowError()
251
252    private function getLastError()
253    {
254       return sqlite3_error($this->db);
255
256    } // getLastError()
257
258 }
259
260 ?>