00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00025 class DiogenesDatabase {
00028 var $connect_id;
00029
00037 var $_trace = false;
00038
00045 var $_trace_data = Array();
00046
00049 var $_fatal = false;
00050
00053 var $_errno = 0;
00054
00057 var $_errstr = '';
00058
00061 var $_errinfo = '';
00062
00070 function DiogenesDatabase($database, $host, $user, $password) {
00071 global $globals;
00072
00073
00074 if (!extension_loaded('mysql') && !dl('mysql.so'))
00075 {
00076 echo "MySQL support needs to be activated in your PHP configuration!<br>\n";
00077 echo "Add a line with 'extension=mysql.so' in your php.ini file.\n";
00078 exit(1);
00079 }
00080
00081 $this->_fatal = @$this->database_error_fatal;
00082
00083 if(empty($user)){
00084 $this->connect_id=@mysql_connect();
00085 } else {
00086 $this->connect_id=@mysql_connect($host, $user, $password);
00087 }
00088
00089 if (!$this->connect_id) {
00090 $this->_handleError("");
00091 return;
00092 }
00093
00094 if (!@mysql_select_db($database,$this->connect_id))
00095 {
00096 $this->_handleError("");
00097 return;
00098 }
00099
00100
00101 register_shutdown_function(array(&$this, 'close'));
00102 }
00103
00104
00107 function close() {
00108 mysql_close($this->connect_id);
00109 $this->connect_id = FALSE;
00110 }
00111
00112
00115 function trace_off() {
00116 $this->_trace = false;
00117 }
00118
00119
00122 function trace_on() {
00123 $this->_trace = true;
00124 }
00125
00126
00132 function trace_format(&$page,$template='') {
00133 global $globals;
00134 if(empty($template))
00135 $template = $globals->libroot . '/templates/database-debug.tpl';
00136 $page->assign_by_ref('trace_data', $this->_trace_data);
00137 return $page->fetch($template);
00138 }
00139
00140
00145 function query($query) {
00146 if (!empty($query)) {
00147
00148 if ($this->_trace) {
00149 $_res = mysql_query("EXPLAIN $query", $this->connect_id);
00150 $explain = Array();
00151 while($row = @mysql_fetch_assoc($_res)) $explain[] = $row;
00152 $trace_data = Array('query' => $query, 'explain' => $explain );
00153 @mysql_free_result($_res);
00154 }
00155
00156 $res = mysql_query($query, $this->connect_id);
00157
00158 if ($this->_trace) {
00159 $trace_data['error'] = $this->error();
00160 $this->_trace_data[] = $trace_data;
00161 }
00162
00163 if (!$res)
00164 {
00165 $this->_handleError($query);
00166 }
00167
00168 return $res;
00169 }
00170 }
00171
00172
00175 function insert_id()
00176 {
00177 return @mysql_insert_id($this->connect_id);
00178 }
00179
00180
00185 function err()
00186 {
00187 return ($this->_errno != 0);
00188 }
00189
00190
00193 function error() {
00194 return $this->_errstr;
00195 }
00196
00197
00200 function errno() {
00201 return $this->_errno;
00202 }
00203
00204
00208 function errinfo()
00209 {
00210 return $this->_errinfo;
00211 }
00212
00213
00216 function ResetError()
00217 {
00218 $this->_errno = 0;
00219 $this->_errstr = '';
00220 $this->_errinfo = '';
00221 }
00222
00223
00226 function affected_rows() {
00227 return @mysql_affected_rows($this->connect_id);
00228 }
00229
00230
00236 function get_set($table,$column) {
00237 $res = $this->query("show columns from $table like '$column'");
00238 $line = mysql_fetch_assoc($res);
00239 $set = $line['Type'];
00240 $set = substr($set,5,strlen($set)-7);
00241 return preg_split("/','/",$set);
00242 }
00243
00244
00252 function _handleError($extras = '')
00253 {
00254 $this->_errinfo = $extras;
00255
00256 if ($this->connect_id)
00257 {
00258 $this->_errno = mysql_errno($this->connect_id);
00259 $this->_errstr = mysql_error($this->connect_id);
00260 }
00261 else
00262 {
00263 $this->_errno = mysql_errno();
00264 $this->_errstr = mysql_error();
00265 }
00266
00267 if ($this->_fatal)
00268 {
00269 die(sprintf("Database error: (%i) %s\n%s\n", $this->_errno, $this->_errstr, $this->_errinfo));
00270 }
00271 }
00272 }
00273
00274 ?>