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