limit displayed photos within index to tag selection
[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  *  (at your option) 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->trowError($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    public function db_fetch_object(&$resource)
114    {
115       return sqlite3_fetch_array($resource);
116
117    } // db_fetch_object
118
119    /**
120     * PHPFSPOT_DB fetch ONE row
121     *
122     * This function will execute the given but only return the
123     * first result.
124     */
125    public function db_fetchSingleRow($query = "") 
126    {
127       if($this->getConnStatus()) {
128
129          $result = $this->db_query($query);
130          $row = $result->fetchRow();
131          return $row;
132       }
133       else 
134          $this->ThrowError("Can't fetch row - we are not connected!");
135       
136    } // db_fetchSingleRow()
137
138    /**
139     * PHPFSPOT_DB number of affected rows
140     *
141     * This functions returns the number of affected rows but the
142     * given SQL query.
143     */
144    public function db_getNumRows($query = "")
145    {
146       /* Execute query */
147       $result = $this->db_query($query);
148
149       /* Errors? */
150       if(PEAR::isError($result)) 
151          $this->throwError($result->getMessage());
152
153       return $result->numRows();
154
155    } // db_getNumRows()
156
157    /**
158     * PHPFSPOT_DB get primary key
159     *
160     * This function returns the primary key of the last
161     * operated insert SQL query.
162     */
163    public function db_getid()
164    {
165       /* Get the last primary key ID from execute query */
166       return mysql_insert_id($this->db->connection);
167       
168    } // db_getid()
169
170    /**
171     * PHPFSPOT_DB check table exists
172     *
173     * This function checks if the given table exists in the
174     * database
175     * @param string, table name
176     * @return true if table found otherwise false
177     */
178    public function db_check_table_exists($table_name = "")
179    {
180       if($this->getConnStatus()) {
181          $result = $this->db_query("SHOW TABLES");
182          $tables_in = "Tables_in_". MYSQL_DB;
183         
184          while($row = $result->fetchRow()) {
185             if($row->$tables_in == $table_name)
186                return true;
187          }
188          return false;
189       }
190       else
191          $this->ThrowError("Can't check table - we are not connected!");
192          
193    } // db_check_table_exists()
194
195    /**
196     * PHPFSPOT_DB get connection status
197     *
198     * This function checks the internal state variable if already
199     * connected to database.
200     */
201    private function setConnStatus($status)
202    {
203       $this->is_connected = $status;
204       
205    } // setConnStatus()
206
207    /**
208     * PHPFSPOT_DB set connection status
209     * This function sets the internal state variable to indicate
210     * current database connection status.
211     */
212    private function getConnStatus()
213    {
214       return $this->is_connected;
215
216    } // getConnStatus()
217
218    /**
219     * PHPFSPOT_DB throw error
220     *
221     * This function shows up error messages and afterwards through exceptions.
222     */
223    private function ThrowError($string)
224    {
225       if(!defined('DB_NOERROR'))  {
226          print "<br /><br />". $string ."<br /><br />\n";
227          try {
228             throw new Exception;
229          }
230          catch(Exectpion $e) {
231          }
232       }
233
234       $this->last_error = $string;
235          
236    } // ThrowError()
237
238    private function getLastError()
239    {
240
241       return sqlite3_error_string(sqlite3_last_error($this->db));
242
243    } // getLastError()
244
245 }
246
247 ?>