root / phpfspot_db.php

View | Annotate | Download (10.5 KB)

1 61f2970e Andreas
 <?php
2 61f2970e Andreas
3 61f2970e Andreas
 /***************************************************************************
4 61f2970e Andreas
  *
5 7617d447 Andreas
  * phpfspot, presents your F-Spot photo collection in Web browsers.
6 7617d447 Andreas
  *
7 7617d447 Andreas
  * Copyright (c) by Andreas Unterkircher
8 61f2970e Andreas
  *
9 61f2970e Andreas
  *  This program is free software; you can redistribute it and/or modify
10 61f2970e Andreas
  *  it under the terms of the GNU General Public License as published by
11 61f2970e Andreas
  *  the Free Software Foundation; either version 2 of the License, or
12 6e2d319e Andreas
  *  any later version.
13 61f2970e Andreas
  *
14 61f2970e Andreas
  *  This program is distributed in the hope that it will be useful,
15 61f2970e Andreas
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 61f2970e Andreas
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 61f2970e Andreas
  *  GNU General Public License for more details.
18 61f2970e Andreas
  *
19 61f2970e Andreas
  *  You should have received a copy of the GNU General Public License
20 61f2970e Andreas
  *  along with this program; if not, write to the Free Software
21 61f2970e Andreas
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 61f2970e Andreas
  *
23 61f2970e Andreas
  ***************************************************************************/
24 61f2970e Andreas
25 2765fc08 Andreas
 /**
26 2765fc08 Andreas
  * PHPFSPOT_DB class
27 55677896 Andreas
  *
28 2765fc08 Andreas
  * @package phpfspot
29 2765fc08 Andreas
  */
30 61f2970e Andreas
 class PHPFSPOT_DB {
31 61f2970e Andreas
32 61f2970e Andreas
    private $db;
33 61f2970e Andreas
    private $db_path;
34 61f2970e Andreas
    private $parent;
35 61f2970e Andreas
    private $is_connected;
36 61f2970e Andreas
    private $last_error;
37 e4cfdbe7 Andreas
    private $last_query;
38 61f2970e Andreas
39 61f2970e Andreas
    /**
40 61f2970e Andreas
     * PHPFSPOT_DB class constructor
41 61f2970e Andreas
     *
42 61f2970e Andreas
     * This constructor initially connect to the database.
43 61f2970e Andreas
     */
44 61f2970e Andreas
    public function __construct($parent, $db_path)
45 61f2970e Andreas
    {
46 7688cbcd Andreas
       global $phpfspot;
47 7688cbcd Andreas
48 7688cbcd Andreas
       $this->parent = $phpfspot;
49 61f2970e Andreas
       $this->db_path = $db_path;
50 61f2970e Andreas
51 61f2970e Andreas
       /* We are starting disconnected */
52 61f2970e Andreas
       $this->setConnStatus(false);
53 61f2970e Andreas
54 af140085 Andreas
       /* Connect to database */
55 61f2970e Andreas
       $this->db_connect();
56 61f2970e Andreas
57 61f2970e Andreas
    } // __construct()
58 61f2970e Andreas
59 61f2970e Andreas
    /**
60 61f2970e Andreas
     * PHPFSPOT_DB class deconstructor
61 61f2970e Andreas
     *
62 61f2970e Andreas
     * This destructor will close the current database connection.
63 61f2970e Andreas
     */
64 61f2970e Andreas
    public function __destruct()
65 61f2970e Andreas
    {
66 61f2970e Andreas
       $this->db_disconnect();
67 61f2970e Andreas
68 61f2970e Andreas
    } // _destruct()
69 61f2970e Andreas
70 61f2970e Andreas
    /**
71 61f2970e Andreas
     * PHPFSPOT_DB database connect
72 61f2970e Andreas
     *
73 dc12bd17 Andreas
     * This function will connect to the database
74 61f2970e Andreas
     */
75 61f2970e Andreas
    private function db_connect()
76 61f2970e Andreas
    {
77 5f717dc4 Andreas
       switch($this->parent->cfg->db_access) {
78 5f717dc4 Andreas
          case 'native':
79 5f717dc4 Andreas
             if(($this->db = sqlite3_open($this->db_path)) === false) {
80 5f717dc4 Andreas
                $this->throwError("Unable to connect to database:" . $this->getLastError());
81 5f717dc4 Andreas
                $this->setConnStatus(false);
82 5f717dc4 Andreas
             }
83 5f717dc4 Andreas
             else {
84 afadeef5 Andreas
                sqlite3_create_function($this->db, 'basename', 1, 'basename');
85 5f717dc4 Andreas
                $this->setConnStatus(true);
86 5f717dc4 Andreas
             }
87 5f717dc4 Andreas
             break;
88 5f717dc4 Andreas
          case 'pdo':
89 5f717dc4 Andreas
             try {
90 5f717dc4 Andreas
                $this->db =  new PDO("sqlite:".$this->db_path);
91 5f717dc4 Andreas
                $this->setConnStatus(true);
92 5f717dc4 Andreas
             }
93 5f717dc4 Andreas
             catch (Exception $e) {
94 5f717dc4 Andreas
                $this->throwError("Unable to connect to database:" . $e->getMessage());
95 5f717dc4 Andreas
                $this->setConnStatus(false);
96 5f717dc4 Andreas
             }
97 5f717dc4 Andreas
             break;
98 61f2970e Andreas
99 61f2970e Andreas
       }
100 61f2970e Andreas
101 61f2970e Andreas
    } // db_connect()
102 61f2970e Andreas
103 61f2970e Andreas
    /**
104 61f2970e Andreas
     * PHPFSPOT_DB database disconnect
105 61f2970e Andreas
     *
106 61f2970e Andreas
     * This function will disconnected an established database connection.
107 61f2970e Andreas
     */
108 61f2970e Andreas
    private function db_disconnect()
109 61f2970e Andreas
    {
110 5f717dc4 Andreas
       switch($this->parent->cfg->db_access) {
111 5f717dc4 Andreas
          case 'native':
112 5f717dc4 Andreas
             if($this->getConnStatus())
113 5f717dc4 Andreas
                sqlite3_close($this->db);
114 5f717dc4 Andreas
             break;
115 5f717dc4 Andreas
          case 'pdo':
116 5f717dc4 Andreas
             $this->db = NULL;
117 5f717dc4 Andreas
             break;
118 5f717dc4 Andreas
       }
119 61f2970e Andreas
120 61f2970e Andreas
    } // db_disconnect()
121 61f2970e Andreas
122 61f2970e Andreas
    /**
123 61f2970e Andreas
     * PHPFSPOT_DB database query
124 61f2970e Andreas
     *
125 61f2970e Andreas
     * This function will execute a SQL query and return the result as
126 61f2970e Andreas
     * object.
127 61f2970e Andreas
     */
128 61f2970e Andreas
    public function db_query($query = "")
129 61f2970e Andreas
    {
130 6198d8ac Andreas
       if(!$this->getConnStatus())
131 6198d8ac Andreas
          return false;
132 e4cfdbe7 Andreas
133 6198d8ac Andreas
       $this->last_query = $query;
134 6198d8ac Andreas
135 6198d8ac Andreas
       switch($this->parent->cfg->db_access) {
136 6198d8ac Andreas
          case 'native':
137 6198d8ac Andreas
             if(($result = sqlite3_query($this->db, $query)) === false)
138 6198d8ac Andreas
                $this->ThrowError($this->getLastError());
139 6198d8ac Andreas
             break;
140 6198d8ac Andreas
          case 'pdo':
141 6198d8ac Andreas
             try{
142 6198d8ac Andreas
                $result = $this->db->query($query);
143 6198d8ac Andreas
                return $result;
144 6198d8ac Andreas
             }
145 6198d8ac Andreas
             catch (Exception $e) {
146 6198d8ac Andreas
                $this->ThrowError($e->getMessage());
147 6198d8ac Andreas
                $result= NULL;
148 6198d8ac Andreas
             }
149 6198d8ac Andreas
             break;
150 5f717dc4 Andreas
151 61f2970e Andreas
       }
152 6198d8ac Andreas
153 6198d8ac Andreas
       return $result;
154 61f2970e Andreas
155 61f2970e Andreas
    } // db_query()
156 61f2970e Andreas
157 dac334b4 Andreas
    /**
158 dac334b4 Andreas
     * PHPFSPOT_DB database query & execute
159 dac334b4 Andreas
     *
160 dac334b4 Andreas
     * This function will execute a SQL query and return nothing.
161 dac334b4 Andreas
     */
162 dac334b4 Andreas
    public function db_exec($query = "")
163 dac334b4 Andreas
    {
164 6198d8ac Andreas
       if(!$this->getConnStatus())
165 6198d8ac Andreas
          return false;
166 6198d8ac Andreas
167 6198d8ac Andreas
       $this->last_query = $query;
168 6198d8ac Andreas
169 6198d8ac Andreas
       switch($this->parent->cfg->db_access) {
170 6198d8ac Andreas
          case 'native':
171 6198d8ac Andreas
             if(($result = sqlite3_exec($this->db, $query)) === false) {
172 6198d8ac Andreas
                $this->ThrowError($this->getLastError());
173 6198d8ac Andreas
                return false;
174 6198d8ac Andreas
             }
175 6198d8ac Andreas
             break;
176 6198d8ac Andreas
          case 'pdo':
177 6198d8ac Andreas
             try {
178 6198d8ac Andreas
                $result = $this->db->query($query);
179 6198d8ac Andreas
             }
180 6198d8ac Andreas
             catch (Exception $e){
181 6198d8ac Andreas
                $this->ThrowError($e->getLMessage());
182 6198d8ac Andreas
                return false;
183 6198d8ac Andreas
             }
184 6198d8ac Andreas
             break;
185 dac334b4 Andreas
       }
186 6198d8ac Andreas
187 6198d8ac Andreas
       return true;
188 dac334b4 Andreas
189 dac334b4 Andreas
    } // db_exec()
190 dac334b4 Andreas
191 454facb4 Andreas
    public function db_fetch_object($resource)
192 61f2970e Andreas
    {
193 5f717dc4 Andreas
       switch($this->parent->cfg->db_access) {
194 5f717dc4 Andreas
          case 'native':
195 5f717dc4 Andreas
             return sqlite3_fetch_array($resource);
196 5f717dc4 Andreas
          case 'pdo':
197 5f717dc4 Andreas
             return $resource->fetch();
198 5f717dc4 Andreas
       }
199 61f2970e Andreas
200 61f2970e Andreas
    } // db_fetch_object
201 61f2970e Andreas
202 61f2970e Andreas
    /**
203 61f2970e Andreas
     * PHPFSPOT_DB fetch ONE row
204 61f2970e Andreas
     *
205 61f2970e Andreas
     * This function will execute the given but only return the
206 61f2970e Andreas
     * first result.
207 61f2970e Andreas
     */
208 61f2970e Andreas
    public function db_fetchSingleRow($query = "")
209 61f2970e Andreas
    {
210 6198d8ac Andreas
       if(!$this->getConnStatus())
211 6198d8ac Andreas
          return false;
212 6198d8ac Andreas
213 6198d8ac Andreas
       $result = $this->db_query($query);
214 6198d8ac Andreas
       switch($this->parent->cfg->db_access) {
215 6198d8ac Andreas
          case 'native':
216 6198d8ac Andreas
             $row = $this->db_fetch_object($result);
217 6198d8ac Andreas
             break;
218 6198d8ac Andreas
          case 'pdo':
219 6198d8ac Andreas
             $row = $result->fetch();
220 6198d8ac Andreas
             break;
221 61f2970e Andreas
       }
222 6198d8ac Andreas
       return $row;
223 61f2970e Andreas
224 61f2970e Andreas
    } // db_fetchSingleRow()
225 61f2970e Andreas
226 61f2970e Andreas
    /**
227 61f2970e Andreas
     * PHPFSPOT_DB number of affected rows
228 61f2970e Andreas
     *
229 61f2970e Andreas
     * This functions returns the number of affected rows but the
230 61f2970e Andreas
     * given SQL query.
231 61f2970e Andreas
     */
232 61f2970e Andreas
    public function db_getNumRows($query = "")
233 61f2970e Andreas
    {
234 61f2970e Andreas
       /* Execute query */
235 61f2970e Andreas
       $result = $this->db_query($query);
236 61f2970e Andreas
237 61f2970e Andreas
       /* Errors? */
238 61f2970e Andreas
       if(PEAR::isError($result))
239 61f2970e Andreas
          $this->throwError($result->getMessage());
240 61f2970e Andreas
241 61f2970e Andreas
       return $result->numRows();
242 61f2970e Andreas
243 61f2970e Andreas
    } // db_getNumRows()
244 61f2970e Andreas
245 61f2970e Andreas
    /**
246 61f2970e Andreas
     * PHPFSPOT_DB check table exists
247 61f2970e Andreas
     *
248 61f2970e Andreas
     * This function checks if the given table exists in the
249 61f2970e Andreas
     * database
250 61f2970e Andreas
     * @param string, table name
251 61f2970e Andreas
     * @return true if table found otherwise false
252 61f2970e Andreas
     */
253 61f2970e Andreas
    public function db_check_table_exists($table_name = "")
254 61f2970e Andreas
    {
255 6198d8ac Andreas
       if(!$this->getConnStatus())
256 61f2970e Andreas
          return false;
257 6198d8ac Andreas
258 6198d8ac Andreas
       $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
259 79ce4a0a Andreas
260 6198d8ac Andreas
       switch($this->parent->cfg->db_access) {
261 79ce4a0a Andreas
262 6198d8ac Andreas
          case 'native':
263 6198d8ac Andreas
             while($table = $this->db_fetch_object($result)) {
264 6198d8ac Andreas
                if($table['name'] == $table_name)
265 6198d8ac Andreas
                   return true;
266 6198d8ac Andreas
             }
267 6198d8ac Andreas
             break;
268 79ce4a0a Andreas
269 6198d8ac Andreas
          case 'pdo':
270 6198d8ac Andreas
             foreach($result as $table ){
271 79ce4a0a Andreas
                if($table['name'] == $table_name)
272 6198d8ac Andreas
                   return true;
273 6198d8ac Andreas
             }
274 6198d8ac Andreas
             break;
275 79ce4a0a Andreas
       }
276 6198d8ac Andreas
277 6198d8ac Andreas
       return false;
278 61f2970e Andreas
279 61f2970e Andreas
    } // db_check_table_exists()
280 61f2970e Andreas
281 61f2970e Andreas
    /**
282 6198d8ac Andreas
     * PHPFSPOT_DB check column exist
283 6198d8ac Andreas
     *
284 6198d8ac Andreas
     * This function checks if the given column exists within
285 6198d8ac Andreas
     * the specified table.
286 6198d8ac Andreas
     */
287 6198d8ac Andreas
    public function db_check_column_exists($table_name, $column)
288 6198d8ac Andreas
    {
289 6198d8ac Andreas
       if(!$this->getConnStatus())
290 6198d8ac Andreas
          return false;
291 6198d8ac Andreas
292 6198d8ac Andreas
       $result = $this->db_query("
293 6198d8ac Andreas
          SELECT sql
294 6198d8ac Andreas
          FROM
295 6198d8ac Andreas
             (SELECT * FROM sqlite_master UNION ALL
296 6198d8ac Andreas
              SELECT * FROM sqlite_temp_master)
297 6198d8ac Andreas
          WHERE
298 6198d8ac Andreas
             tbl_name LIKE '". $table_name ."'
299 6198d8ac Andreas
          AND type!='meta'
300 6198d8ac Andreas
          AND sql NOT NULL
301 6198d8ac Andreas
          AND name NOT LIKE 'sqlite_%'
302 6198d8ac Andreas
          ORDER BY substr(type,2,1), name
303 6198d8ac Andreas
       ");
304 6198d8ac Andreas
305 6198d8ac Andreas
       while($row = $this->db_fetch_object($result)) {
306 6198d8ac Andreas
          /* CREATE TABLE xx ( col1 int, col2 bool, col3 ...) */
307 6198d8ac Andreas
          if(strstr($row['sql'], " ". $column ." ") !== false)
308 6198d8ac Andreas
                return true;
309 6198d8ac Andreas
       }
310 6198d8ac Andreas
311 6198d8ac Andreas
       return false;
312 6198d8ac Andreas
313 6198d8ac Andreas
    } // db_check_column_exists()
314 6198d8ac Andreas
315 6198d8ac Andreas
    /**
316 61f2970e Andreas
     * PHPFSPOT_DB get connection status
317 61f2970e Andreas
     *
318 61f2970e Andreas
     * This function checks the internal state variable if already
319 61f2970e Andreas
     * connected to database.
320 61f2970e Andreas
     */
321 61f2970e Andreas
    private function setConnStatus($status)
322 61f2970e Andreas
    {
323 61f2970e Andreas
       $this->is_connected = $status;
324 61f2970e Andreas
325 61f2970e Andreas
    } // setConnStatus()
326 61f2970e Andreas
327 61f2970e Andreas
    /**
328 61f2970e Andreas
     * PHPFSPOT_DB set connection status
329 61f2970e Andreas
     * This function sets the internal state variable to indicate
330 61f2970e Andreas
     * current database connection status.
331 61f2970e Andreas
     */
332 61f2970e Andreas
    private function getConnStatus()
333 61f2970e Andreas
    {
334 61f2970e Andreas
       return $this->is_connected;
335 61f2970e Andreas
336 61f2970e Andreas
    } // getConnStatus()
337 61f2970e Andreas
338 61f2970e Andreas
    /**
339 61f2970e Andreas
     * PHPFSPOT_DB throw error
340 61f2970e Andreas
     *
341 61f2970e Andreas
     * This function shows up error messages and afterwards through exceptions.
342 61f2970e Andreas
     */
343 61f2970e Andreas
    private function ThrowError($string)
344 61f2970e Andreas
    {
345 61f2970e Andreas
       if(!defined('DB_NOERROR'))  {
346 e4cfdbe7 Andreas
          print "Error during query: ". $this->last_query ."<br /><br />\n";
347 61f2970e Andreas
          print "<br /><br />". $string ."<br /><br />\n";
348 61f2970e Andreas
          try {
349 61f2970e Andreas
             throw new Exception;
350 61f2970e Andreas
          }
351 61f2970e Andreas
          catch(Exectpion $e) {
352 61f2970e Andreas
          }
353 61f2970e Andreas
       }
354 61f2970e Andreas
355 61f2970e Andreas
       $this->last_error = $string;
356 61f2970e Andreas
357 61f2970e Andreas
    } // ThrowError()
358 61f2970e Andreas
359 61f2970e Andreas
    private function getLastError()
360 61f2970e Andreas
    {
361 5f717dc4 Andreas
       switch($this->parent->cfg->db_access) {
362 5f717dc4 Andreas
          case 'native':
363 5f717dc4 Andreas
             return sqlite3_error($this->db);
364 5f717dc4 Andreas
          case 'pdo':
365 5f717dc4 Andreas
             return "". $this->db->errorInfo();
366 5f717dc4 Andreas
       }
367 61f2970e Andreas
368 61f2970e Andreas
    } // getLastError()
369 61f2970e Andreas
370 6198d8ac Andreas
    /**
371 6198d8ac Andreas
     * start transaction
372 6198d8ac Andreas
     *
373 6198d8ac Andreas
     * this will start a transaction on ACID-supporting database
374 6198d8ac Andreas
     * systems.
375 6198d8ac Andreas
     *
376 6198d8ac Andreas
     * @return bool
377 6198d8ac Andreas
     */
378 6198d8ac Andreas
    public function db_start_transaction()
379 6198d8ac Andreas
    {
380 6198d8ac Andreas
       if(!$this->getConnStatus())
381 6198d8ac Andreas
          return false;
382 6198d8ac Andreas
383 6198d8ac Andreas
       $result = $this->db_exec("BEGIN");
384 6198d8ac Andreas
385 6198d8ac Andreas
       /* Errors? */
386 6198d8ac Andreas
       if(PEAR::isError($result))
387 6198d8ac Andreas
          $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
388 6198d8ac Andreas
389 6198d8ac Andreas
       return true;
390 6198d8ac Andreas
391 6198d8ac Andreas
    } // db_start_transaction()
392 6198d8ac Andreas
393 6198d8ac Andreas
    /**
394 6198d8ac Andreas
     * commit transaction
395 6198d8ac Andreas
     *
396 6198d8ac Andreas
     * this will commit an ongoing transaction on ACID-supporting
397 6198d8ac Andreas
     * database systems
398 6198d8ac Andreas
     *
399 6198d8ac Andreas
     * @return bool
400 6198d8ac Andreas
     */
401 6198d8ac Andreas
    public function db_commit_transaction()
402 6198d8ac Andreas
    {
403 6198d8ac Andreas
       if(!$this->getConnStatus())
404 6198d8ac Andreas
          return false;
405 6198d8ac Andreas
406 6198d8ac Andreas
       $result = $this->db_exec("COMMIT");
407 6198d8ac Andreas
408 6198d8ac Andreas
       /* Errors? */
409 6198d8ac Andreas
       if(PEAR::isError($result))
410 6198d8ac Andreas
          $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
411 6198d8ac Andreas
412 6198d8ac Andreas
       return true;
413 6198d8ac Andreas
414 6198d8ac Andreas
    } // db_commit_transaction()
415 6198d8ac Andreas
416 6198d8ac Andreas
    /**
417 6198d8ac Andreas
     * rollback transaction()
418 6198d8ac Andreas
     *
419 6198d8ac Andreas
     * this function aborts a on going transaction
420 6198d8ac Andreas
     *
421 6198d8ac Andreas
     * @return bool
422 6198d8ac Andreas
     */
423 6198d8ac Andreas
    public function db_rollback_transaction()
424 6198d8ac Andreas
    {
425 6198d8ac Andreas
       if(!$this->getConnStatus())
426 6198d8ac Andreas
          return false;
427 6198d8ac Andreas
428 6198d8ac Andreas
       $result = $this->db_exec("ROLLBACK");
429 6198d8ac Andreas
430 6198d8ac Andreas
       /* Errors? */
431 6198d8ac Andreas
       if(PEAR::isError($result))
432 6198d8ac Andreas
          $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
433 6198d8ac Andreas
434 6198d8ac Andreas
       return true;
435 6198d8ac Andreas
436 6198d8ac Andreas
    } // db_rollback_transaction()
437 6198d8ac Andreas
438 6198d8ac Andreas
 } // PHPFSPOT_DB
439 61f2970e Andreas
440 61f2970e Andreas
 ?>