remove MDB2 from a function comment
[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
65     */
66    private function db_connect()
67    {
68       switch($this->parent->cfg->db_access) {
69          case 'native':
70             if(($this->db = sqlite3_open($this->db_path)) === false) {
71                $this->throwError("Unable to connect to database:" . $this->getLastError());
72                $this->setConnStatus(false);
73             }
74             else {
75                $this->setConnStatus(true);
76             }
77             break;
78          case 'pdo':
79             try {
80                $this->db =  new PDO("sqlite:".$this->db_path);
81                $this->setConnStatus(true);
82             }
83             catch (Exception $e) {
84                $this->throwError("Unable to connect to database:" . $e->getMessage());
85                $this->setConnStatus(false);
86             }
87             break;
88
89       }
90
91    } // db_connect()
92
93    /**
94     * PHPFSPOT_DB database disconnect
95     *
96     * This function will disconnected an established database connection.
97     */
98    private function db_disconnect()
99    {
100       switch($this->parent->cfg->db_access) {
101          case 'native':
102             if($this->getConnStatus())
103                sqlite3_close($this->db);
104             break;
105          case 'pdo':
106             $this->db = NULL;
107             break;
108       }
109
110    } // db_disconnect()
111
112    /**
113     * PHPFSPOT_DB database query
114     *
115     * This function will execute a SQL query and return the result as
116     * object.
117     */
118    public function db_query($query = "")
119    {
120       if($this->getConnStatus()) {
121
122          switch($this->parent->cfg->db_access) {
123             case 'native':
124                if(($result = sqlite3_query($this->db, $query)) === false)
125                   $this->ThrowError($this->getLastError());
126                break;
127             case 'pdo':
128                try{
129                   $result = $this->db->query($query);
130                   return $result;
131                }
132                catch (Exception $e) {
133                   $this->ThrowError($e->getMessage());
134                   $result= NULL;
135                }
136                break;
137
138          }
139         
140          return $result;
141       }
142       else 
143          $this->ThrowError("Can't execute query - we are not connected!");
144
145    } // db_query()
146
147    /**
148     * PHPFSPOT_DB database query & execute
149     *
150     * This function will execute a SQL query and return nothing.
151     */
152    public function db_exec($query = "")
153    {
154       if($this->getConnStatus()) {
155
156          switch($this->parent->cfg->db_access) {
157             case 'native':
158                if(($result = sqlite3_exec($this->db, $query)) === false)
159                   $this->ThrowError($this->getLastError());
160                break;
161             case 'pdo':
162                try {
163                   $result = $this->db->query($query);
164                }
165                catch (Exception $e){
166                   $this->ThrowError($e->getLMessage());
167                }
168                break;
169          }
170       }
171       else 
172          $this->ThrowError("Can't execute query - we are not connected!");
173
174    } // db_exec()
175
176    public function db_fetch_object($resource)
177    {
178       switch($this->parent->cfg->db_access) {
179          case 'native':
180             return sqlite3_fetch_array($resource);
181          case 'pdo':
182             return $resource->fetch();
183       }
184
185    } // db_fetch_object
186
187    /**
188     * PHPFSPOT_DB fetch ONE row
189     *
190     * This function will execute the given but only return the
191     * first result.
192     */
193    public function db_fetchSingleRow($query = "") 
194    {
195       if($this->getConnStatus()) {
196
197          $result = $this->db_query($query);
198          switch($this->parent->cfg->db_access) {
199             case 'native':
200                $row = $result->fetchRow();
201                break;
202             case 'pdo':
203                $row = $result[0];
204                break;
205          }
206          return $row;
207       }
208       else 
209          $this->ThrowError("Can't fetch row - we are not connected!");
210       
211    } // db_fetchSingleRow()
212
213    /**
214     * PHPFSPOT_DB number of affected rows
215     *
216     * This functions returns the number of affected rows but the
217     * given SQL query.
218     */
219    public function db_getNumRows($query = "")
220    {
221       /* Execute query */
222       $result = $this->db_query($query);
223
224       /* Errors? */
225       if(PEAR::isError($result)) 
226          $this->throwError($result->getMessage());
227
228       return $result->numRows();
229
230    } // db_getNumRows()
231
232    /**
233     * PHPFSPOT_DB get primary key
234     *
235     * This function returns the primary key of the last
236     * operated insert SQL query.
237     */
238    public function db_getid()
239    {
240       /* Get the last primary key ID from execute query */
241       return mysql_insert_id($this->db->connection);
242       
243    } // db_getid()
244
245    /**
246     * PHPFSPOT_DB check table exists
247     *
248     * This function checks if the given table exists in the
249     * database
250     * @param string, table name
251     * @return true if table found otherwise false
252     */
253    public function db_check_table_exists($table_name = "")
254    {
255       if($this->getConnStatus()) {
256
257          $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
258          switch($this->parent->cfg->db_access) {
259             case 'native':
260                while($table = $this->db_fetch_object($result)) {
261                   if($table['name'] == $table_name)
262                      return true;
263                }
264                break;
265             case 'pdo':
266                foreach($result as $table ){
267                   if($table['NAME'] == $table_name)
268                      return true;
269                }
270                break;
271           }
272
273          return false;
274       }
275       else
276          $this->ThrowError("Can't check table - we are not connected!");
277          
278    } // db_check_table_exists()
279
280    /**
281     * PHPFSPOT_DB get connection status
282     *
283     * This function checks the internal state variable if already
284     * connected to database.
285     */
286    private function setConnStatus($status)
287    {
288       $this->is_connected = $status;
289       
290    } // setConnStatus()
291
292    /**
293     * PHPFSPOT_DB set connection status
294     * This function sets the internal state variable to indicate
295     * current database connection status.
296     */
297    private function getConnStatus()
298    {
299       return $this->is_connected;
300
301    } // getConnStatus()
302
303    /**
304     * PHPFSPOT_DB throw error
305     *
306     * This function shows up error messages and afterwards through exceptions.
307     */
308    private function ThrowError($string)
309    {
310       if(!defined('DB_NOERROR'))  {
311          print "<br /><br />". $string ."<br /><br />\n";
312          try {
313             throw new Exception;
314          }
315          catch(Exectpion $e) {
316          }
317       }
318
319       $this->last_error = $string;
320          
321    } // ThrowError()
322
323    private function getLastError()
324    {
325       switch($this->parent->cfg->db_access) {
326          case 'native':
327             return sqlite3_error($this->db);
328          case 'pdo':
329             return "". $this->db->errorInfo();
330       }
331
332    } // getLastError()
333
334 }
335
336 ?>