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