3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
22 /** Class for handling database requests. It is a wrapper around a permanent
23 * MySQL database connection with some commonly used functions.
25 class DiogenesDatabase
{
26 /** The id of the permanent database connection.
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
39 /** Historical trace data.
40 * If our queries are being logged, all of the relevant data will end up
45 var $_trace_data = Array();
47 /** Whether database errors are fatal.
51 /** The numeric code of the last error that occured.
55 /** The string describing the last error that occured.
59 /** Extra info about what might have caused the error.
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
70 function DiogenesDatabase($database, $host, $user, $password) {
73 // make sure that we have MySQL support, try loading it
74 if (!extension_loaded('mysql') && !dl('mysql.so'))
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";
81 $this->_fatal
= @$this->database_error_fatal
;
84 $this->connect_id
=@mysql_connect
();
86 $this->connect_id
=@mysql_connect
($host, $user, $password);
89 if (!$this->connect_id
) {
90 $this->_handleError("");
94 if (!@mysql_select_db
($database,$this->connect_id
))
96 $this->_handleError("");
100 // when the script exits, we close the connection to the DB
101 register_shutdown_function(array(&$this, 'close'));
105 /** Close connection to database
108 mysql_close($this->connect_id
);
109 $this->connect_id
= FALSE
;
113 /** Deactivate trace mode.
115 function trace_off() {
116 $this->_trace
= false
;
120 /** Activate trace mode.
122 function trace_on() {
123 $this->_trace
= true
;
127 /** Formats tracing information for output.
132 function trace_format(&$page,$template='') {
135 $template = $globals->libroot
. '/templates/database-debug.tpl';
136 $page->assign_by_ref('trace_data', $this->_trace_data
);
137 return $page->fetch($template);
141 /** Execute a database query.
145 function query($query) {
146 if (!empty($query)) {
149 $_res = mysql_query("EXPLAIN $query", $this->connect_id
);
151 while($row = @mysql_fetch_assoc
($_res)) $explain[] = $row;
152 $trace_data = Array('query' => $query, 'explain' => $explain );
153 @mysql_free_result
($_res);
156 $res = mysql_query($query, $this->connect_id
);
159 $trace_data['error'] = $this->error();
160 $this->_trace_data
[] = $trace_data;
165 $this->_handleError($query);
177 return @mysql_insert_id
($this->connect_id
);
181 /** Return whether there is currently an error in effect.
183 * @return boolean true if error, false otherwise
187 return ($this->_errno
!= 0);
191 /** Return the last error string.
194 return $this->_errstr
;
198 /** Return the last error number.
201 return $this->_errno
;
205 /** Return extra info which might help in determining the cause of the
210 return $this->_errinfo
;
214 /** Forget about any errors previously raised.
216 function ResetError()
220 $this->_errinfo
= '';
224 /** Return the number rows affected by the last query.
226 function affected_rows() {
227 return @mysql_affected_rows
($this->connect_id
);
231 /** Return an array with the possibly values of a set column.
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);
245 /** Handle an error in the database.
247 * Updates the error status information in the system, and possibly dies
248 * if we're doing that sort of thing.
252 function _handleError($extras = '')
254 $this->_errinfo
= $extras;
256 if ($this->connect_id
)
258 $this->_errno
= mysql_errno($this->connect_id
);
259 $this->_errstr
= mysql_error($this->connect_id
);
263 $this->_errno
= mysql_errno();
264 $this->_errstr
= mysql_error();
269 die(sprintf("Database error: (%i) %s\n%s\n", $this->_errno
, $this->_errstr
, $this->_errinfo
));