issue56, auto switch in single photo view
[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 /* from pear "MDB2" package. use "pear install MDB2" if you don't have this! */
25 require_once('MDB2.php');
26
27 class PHPFSPOT_DB {
28
29    private $db;
30    private $db_path;
31    private $parent;
32    private $is_connected;
33    private $last_error;
34
35    /**
36     * PHPFSPOT_DB class constructor
37     *
38     * This constructor initially connect to the database.
39     */
40    public function __construct($parent, $db_path)
41    {
42       $this->parent = $parent;
43       $this->db_path = $db_path;
44
45       /* We are starting disconnected */
46       $this->setConnStatus(false);
47
48       /* Connect to MySQL Database */
49       $this->db_connect();
50
51    } // __construct()
52          
53    /**
54     * PHPFSPOT_DB class deconstructor
55     *
56     * This destructor will close the current database connection.
57     */ 
58    public function __destruct()
59    {
60       $this->db_disconnect();
61
62    } // _destruct()
63
64    /**
65     * PHPFSPOT_DB database connect
66     *
67     * This function will connect to the database via MDB2
68     */
69    private function db_connect()
70    {
71       if(($this->db = sqlite3_open($this->db_path)) === false) {
72
73          $this->throwError("Unable to connect to database:" . $this->getLastError());
74          $this->setConnStatus(false);
75       }
76
77       $this->setConnStatus(true);
78
79    } // db_connect()
80
81    /**
82     * PHPFSPOT_DB database disconnect
83     *
84     * This function will disconnected an established database connection.
85     */
86    private function db_disconnect()
87    {
88       if($this->getConnStatus())
89          sqlite3_close($this->db);
90
91    } // db_disconnect()
92
93    /**
94     * PHPFSPOT_DB database query
95     *
96     * This function will execute a SQL query and return the result as
97     * object.
98     */
99    public function db_query($query = "")
100    {
101       if($this->getConnStatus()) {
102
103          if(($result = sqlite3_query($this->db, $query)) === false)
104             $this->ThrowError($this->getLastError());
105         
106          return $result;
107       }
108       else 
109          $this->ThrowError("Can't execute query - we are not connected!");
110
111    } // db_query()
112
113    /**
114     * PHPFSPOT_DB database query & execute
115     *
116     * This function will execute a SQL query and return nothing.
117     */
118    public function db_exec($query = "")
119    {
120       if($this->getConnStatus()) {
121
122          if(($result = sqlite3_exec($this->db, $query)) === false)
123             $this->ThrowError($this->getLastError());
124
125       }
126       else 
127          $this->ThrowError("Can't execute query - we are not connected!");
128
129    } // db_exec()
130
131    public function db_fetch_object(&$resource)
132    {
133       return sqlite3_fetch_array($resource);
134
135    } // db_fetch_object
136
137    /**
138     * PHPFSPOT_DB fetch ONE row
139     *
140     * This function will execute the given but only return the
141     * first result.
142     */
143    public function db_fetchSingleRow($query = "") 
144    {
145       if($this->getConnStatus()) {
146
147          $result = $this->db_query($query);
148          $row = $result->fetchRow();
149          return $row;
150       }
151       else 
152          $this->ThrowError("Can't fetch row - we are not connected!");
153       
154    } // db_fetchSingleRow()
155
156    /**
157     * PHPFSPOT_DB number of affected rows
158     *
159     * This functions returns the number of affected rows but the
160     * given SQL query.
161     */
162    public function db_getNumRows($query = "")
163    {
164       /* Execute query */
165       $result = $this->db_query($query);
166
167       /* Errors? */
168       if(PEAR::isError($result)) 
169          $this->throwError($result->getMessage());
170
171       return $result->numRows();
172
173    } // db_getNumRows()
174
175    /**
176     * PHPFSPOT_DB get primary key
177     *
178     * This function returns the primary key of the last
179     * operated insert SQL query.
180     */
181    public function db_getid()
182    {
183       /* Get the last primary key ID from execute query */
184       return mysql_insert_id($this->db->connection);
185       
186    } // db_getid()
187
188    /**
189     * PHPFSPOT_DB check table exists
190     *
191     * This function checks if the given table exists in the
192     * database
193     * @param string, table name
194     * @return true if table found otherwise false
195     */
196    public function db_check_table_exists($table_name = "")
197    {
198       if($this->getConnStatus()) {
199
200          $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
201          while($table = $this->db_fetch_object($result)) {
202             if($table['name'] == $table_name)
203                return true;
204          }
205          return false;
206       }
207       else
208          $this->ThrowError("Can't check table - we are not connected!");
209          
210    } // db_check_table_exists()
211
212    /**
213     * PHPFSPOT_DB get connection status
214     *
215     * This function checks the internal state variable if already
216     * connected to database.
217     */
218    private function setConnStatus($status)
219    {
220       $this->is_connected = $status;
221       
222    } // setConnStatus()
223
224    /**
225     * PHPFSPOT_DB set connection status
226     * This function sets the internal state variable to indicate
227     * current database connection status.
228     */
229    private function getConnStatus()
230    {
231       return $this->is_connected;
232
233    } // getConnStatus()
234
235    /**
236     * PHPFSPOT_DB throw error
237     *
238     * This function shows up error messages and afterwards through exceptions.
239     */
240    private function ThrowError($string)
241    {
242       if(!defined('DB_NOERROR'))  {
243          print "<br /><br />". $string ."<br /><br />\n";
244          try {
245             throw new Exception;
246          }
247          catch(Exectpion $e) {
248          }
249       }
250
251       $this->last_error = $string;
252          
253    } // ThrowError()
254
255    private function getLastError()
256    {
257       return sqlite3_error($this->db);
258
259    } // getLastError()
260
261 }
262
263 ?>