add an RSS feed to describe a barrel's recent changes
[diogenes.git] / include / diogenes / diogenes.database.inc.php
CommitLineData
6855525e
JL
1<?php
2/*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22/** Class for handling database requests. It is a wrapper around a permanent
23 * MySQL database connection with some commonly used functions.
24 */
25class DiogenesDatabase {
26 /** The id of the permanent database connection.
27 */
28 var $connect_id;
29
30 /** Are we in debugging mode.
31 * This variable affects whether we make detailed records of everything
32 * we do. Useful for debugging, but I imagine it probably slows down
33 * regular use a bit.
34 *
35 * @see $_trace_data
36 */
37 var $_trace = false;
38
39 /** Historical trace data.
40 * If our queries are being logged, all of the relevant data will end up
41 * in this variable.
42 *
43 * @see $_trace
44 */
45 var $_trace_data = Array();
46
47 /** Whether database errors are fatal.
48 */
49 var $_fatal = false;
50
51 /** The numeric code of the last error that occured.
52 */
53 var $_errno = 0;
54
55 /** The string describing the last error that occured.
56 */
57 var $_errstr = '';
58
59 /** Extra info about what might have caused the error.
60 */
61 var $_errinfo = '';
62
63 /** The constructor.
64 *
65 * @param $database The name of the database to connect to
66 * @param $host The computer hosting the database server
67 * @param $user The username to use to authenticate to the database server
68 * @param $password The password to use in authenticating to the database server
69 */
70 function DiogenesDatabase($database, $host, $user, $password) {
71 global $globals;
72
73 // make sure that we have MySQL support, try loading it
74 if (!extension_loaded('mysql') && !dl('mysql.so'))
75 {
76 echo "MySQL support needs to be activated in your PHP configuration!<br>\n";
77 echo "Add a line with 'extension=mysql.so' in your php.ini file.\n";
78 exit(1);
79 }
80
81 $this->_fatal = @$this->database_error_fatal;
82
83 if(empty($user)){
84 $this->connect_id=@mysql_connect();
85 } else {
86 $this->connect_id=@mysql_connect($host, $user, $password);
87 }
88
89 if (!$this->connect_id) {
90 $this->_handleError("");
91 return;
92 }
93
94 if (!@mysql_select_db($database,$this->connect_id))
95 {
96 $this->_handleError("");
97 return;
98 }
99
100 // when the script exits, we close the connection to the DB
101 register_shutdown_function(array(&$this, 'close'));
102 }
103
104
105 /** Close connection to database
106 */
107 function close() {
108 mysql_close($this->connect_id);
109 $this->connect_id = FALSE;
110 }
111
112
113 /** Deactivate trace mode.
114 */
115 function trace_off() {
116 $this->_trace = false;
117 }
118
119
120 /** Activate trace mode.
121 */
122 function trace_on() {
123 $this->_trace = true;
124 }
125
126
127 /** Formats tracing information for output.
128 *
129 * @param $page
130 * @param $template
131 */
132 function trace_format(&$page,$template='') {
133 global $globals;
134 if(empty($template))
135 $template = $globals->libroot . '/templates/database-debug.tpl';
136 $page->assign_by_ref('trace_data', $this->_trace_data);
137 return $page->fetch($template);
138 }
139
140
141 /** Execute a database query.
142 *
143 * @param $query
144 */
145 function query($query) {
146 if (!empty($query)) {
147
148 if ($this->_trace) {
149 $_res = mysql_query("EXPLAIN $query", $this->connect_id);
150 $explain = Array();
151 while($row = @mysql_fetch_assoc($_res)) $explain[] = $row;
152 $trace_data = Array('query' => $query, 'explain' => $explain );
153 @mysql_free_result($_res);
154 }
155
156 $res = mysql_query($query, $this->connect_id);
157
158 if ($this->_trace) {
159 $trace_data['error'] = $this->error();
160 $this->_trace_data[] = $trace_data;
161 }
162
163 if (!$res)
164 {
165 $this->_handleError($query);
166 }
167
168 return $res;
169 }
170 }
171
172
173 /** Return insert_id
174 */
175 function insert_id()
176 {
177 return @mysql_insert_id($this->connect_id);
178 }
179
180
181 /** Return whether there is currently an error in effect.
182 *
183 * @return boolean true if error, false otherwise
184 */
185 function err()
186 {
187 return ($this->_errno != 0);
188 }
189
190
191 /** Return the last error string.
192 */
193 function error() {
194 return $this->_errstr;
195 }
196
197
198 /** Return the last error number.
199 */
200 function errno() {
201 return $this->_errno;
202 }
203
204
205 /** Return extra info which might help in determining the cause of the
206 * previous error.
207 */
208 function errinfo()
209 {
210 return $this->_errinfo;
211 }
212
213
214 /** Forget about any errors previously raised.
215 */
216 function ResetError()
217 {
218 $this->_errno = 0;
219 $this->_errstr = '';
220 $this->_errinfo = '';
221 }
222
223
224 /** Return the number rows affected by the last query.
225 */
226 function affected_rows() {
227 return @mysql_affected_rows($this->connect_id);
228 }
229
230
231 /** Return an array with the possibly values of a set column.
232 *
233 * @param $table
234 * @param $column
235 */
236 function get_set($table,$column) {
237 $res = $this->query("show columns from $table like '$column'");
238 $line = mysql_fetch_assoc($res);
239 $set = $line['Type'];
240 $set = substr($set,5,strlen($set)-7);
241 return preg_split("/','/",$set);
242 }
243
244
245 /** Handle an error in the database.
246 *
247 * Updates the error status information in the system, and possibly dies
248 * if we're doing that sort of thing.
249 *
250 * @param $extras
251 */
252 function _handleError($extras = '')
253 {
254 $this->_errinfo = $extras;
255
256 if ($this->connect_id)
257 {
258 $this->_errno = mysql_errno($this->connect_id);
259 $this->_errstr = mysql_error($this->connect_id);
260 }
261 else
262 {
263 $this->_errno = mysql_errno();
264 $this->_errstr = mysql_error();
265 }
266
267 if ($this->_fatal)
268 {
269 die(sprintf("Database error: (%i) %s\n%s\n", $this->_errno, $this->_errstr, $this->_errinfo));
270 }
271 }
272}
273
274?>