summaryrefslogtreecommitdiffstats
path: root/phpfspot_db.php
blob: e0cf39f31a89df601b5352608849228cfdd7ad90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php

/***************************************************************************
 *
 * phpfspot, presents your F-Spot photo collection in Web browsers.
 *
 * Copyright (c) by Andreas Unterkircher
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ***************************************************************************/

/**
 * PHPFSPOT_DB class
 *
 * @package phpfspot
 */
class PHPFSPOT_DB {

   private $db;
   private $db_path;
   private $parent;
   private $is_connected;
   private $last_error;
   private $last_query;

   /**
    * PHPFSPOT_DB class constructor
    *
    * This constructor initially connect to the database.
    */
   public function __construct($parent, $db_path)
   {
      global $phpfspot;

      $this->parent = $phpfspot;
      $this->db_path = $db_path;

      /* We are starting disconnected */
      $this->setConnStatus(false);

      /* Connect to database */
      $this->db_connect();

   } // __construct()
	 
   /**
    * PHPFSPOT_DB class deconstructor
    *
    * This destructor will close the current database connection.
    */ 
   public function __destruct()
   {
      $this->db_disconnect();

   } // _destruct()

   /**
    * PHPFSPOT_DB database connect
    *
    * This function will connect to the database
    */
   private function db_connect()
   {
      switch($this->parent->cfg->db_access) {
         case 'native':
            if(($this->db = sqlite3_open($this->db_path)) === false) {
               $this->throwError("Unable to connect to database:" . $this->getLastError());
               $this->setConnStatus(false);
            }
            else {
               sqlite3_create_function($this->db, 'basename', 1, 'basename');
               $this->setConnStatus(true);
            }
            break;
         case 'pdo':
            try {
               $this->db =  new PDO("sqlite:".$this->db_path);
               $this->setConnStatus(true);
            }
            catch (Exception $e) {
               $this->throwError("Unable to connect to database:" . $e->getMessage());
               $this->setConnStatus(false);
            }
            break;

      }

   } // db_connect()

   /**
    * PHPFSPOT_DB database disconnect
    *
    * This function will disconnected an established database connection.
    */
   private function db_disconnect()
   {
      switch($this->parent->cfg->db_access) {
         case 'native':
            if($this->getConnStatus())
               sqlite3_close($this->db);
            break;
         case 'pdo':
            $this->db = NULL;
            break;
      }

   } // db_disconnect()

   /**
    * PHPFSPOT_DB database query
    *
    * This function will execute a SQL query and return the result as
    * object.
    */
   public function db_query($query = "")
   {
      if($this->getConnStatus()) {
   
         $this->last_query = $query;

         switch($this->parent->cfg->db_access) {
            case 'native':
               if(($result = sqlite3_query($this->db, $query)) === false)
                  $this->ThrowError($this->getLastError());
               break;
            case 'pdo':
               try{
                  $result = $this->db->query($query);
                  return $result;
               }
               catch (Exception $e) {
                  $this->ThrowError($e->getMessage());
                  $result= NULL;
               }
               break;

         }
	
         return $result;
      }
      else 
         $this->ThrowError("Can't execute query - we are not connected!");

   } // db_query()

   /**
    * PHPFSPOT_DB database query & execute
    *
    * This function will execute a SQL query and return nothing.
    */
   public function db_exec($query = "")
   {
      if($this->getConnStatus()) {

         $this->last_query = $query;

         switch($this->parent->cfg->db_access) {
            case 'native':
               if(($result = sqlite3_exec($this->db, $query)) === false)
                  $this->ThrowError($this->getLastError());
               break;
            case 'pdo':
               try {
                  $result = $this->db->query($query);
               }
               catch (Exception $e){
                  $this->ThrowError($e->getLMessage());
               }
               break;
         }
      }
      else 
         $this->ThrowError("Can't execute query - we are not connected!");

   } // db_exec()

   public function db_fetch_object($resource)
   {
      switch($this->parent->cfg->db_access) {
         case 'native':
            return sqlite3_fetch_array($resource);
         case 'pdo':
            return $resource->fetch();
      }

   } // db_fetch_object

   /**
    * PHPFSPOT_DB fetch ONE row
    *
    * This function will execute the given but only return the
    * first result.
    */
   public function db_fetchSingleRow($query = "") 
   {
      if($this->getConnStatus()) {

         $result = $this->db_query($query);
         switch($this->parent->cfg->db_access) {
            case 'native':
               $row = $this->db_fetch_object($result);
               break;
            case 'pdo':
               $row = $result->fetch();
               break;
         }
         return $row;
      }
      else 
         $this->ThrowError("Can't fetch row - we are not connected!");
      
   } // db_fetchSingleRow()

   /**
    * PHPFSPOT_DB number of affected rows
    *
    * This functions returns the number of affected rows but the
    * given SQL query.
    */
   public function db_getNumRows($query = "")
   {
      /* Execute query */
      $result = $this->db_query($query);

      /* Errors? */
      if(PEAR::isError($result)) 
         $this->throwError($result->getMessage());

      return $result->numRows();

   } // db_getNumRows()

   /**
    * PHPFSPOT_DB check table exists
    *
    * This function checks if the given table exists in the
    * database
    * @param string, table name
    * @return true if table found otherwise false
    */
   public function db_check_table_exists($table_name = "")
   {
      if($this->getConnStatus()) {

         $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
         switch($this->parent->cfg->db_access) {
            case 'native':
               while($table = $this->db_fetch_object($result)) {
                  if($table['name'] == $table_name)
                     return true;
               }
               break;
            case 'pdo':
               foreach($result as $table ){
                  if($table['NAME'] == $table_name)
                     return true;
               }
               break;
          }

         return false;
      }
      else
         $this->ThrowError("Can't check table - we are not connected!");
	 
   } // db_check_table_exists()

   /**
    * PHPFSPOT_DB get connection status
    *
    * This function checks the internal state variable if already
    * connected to database.
    */
   private function setConnStatus($status)
   {
      $this->is_connected = $status;
      
   } // setConnStatus()

   /**
    * PHPFSPOT_DB set connection status
    * This function sets the internal state variable to indicate
    * current database connection status.
    */
   private function getConnStatus()
   {
      return $this->is_connected;

   } // getConnStatus()

   /**
    * PHPFSPOT_DB throw error
    *
    * This function shows up error messages and afterwards through exceptions.
    */
   private function ThrowError($string)
   {
      if(!defined('DB_NOERROR'))  {
         print "Error during query: ". $this->last_query ."<br /><br />\n";
         print "<br /><br />". $string ."<br /><br />\n";
         try {
            throw new Exception;
         }
         catch(Exectpion $e) {
         }
      }

      $this->last_error = $string;
	 
   } // ThrowError()

   private function getLastError()
   {
      switch($this->parent->cfg->db_access) {
         case 'native':
            return sqlite3_error($this->db);
         case 'pdo':
            return "". $this->db->errorInfo();
      }

   } // getLastError()

}

?>