00001 <?
00005 include_once("core/system/JSystem.class.php");
00006
00013 class DBJames
00014 {
00015
00016
00017 var $dbhost;
00018 var $dbuser;
00019 var $dbpasswd;
00020 var $dbname;
00021
00022 var $rs;
00023 var $row;
00024 var $numRows;
00025 var $numFields;
00026 var $dbConnection;
00027 var $sql;
00028 var $newID;
00029 var $EOF;
00030
00031 var $msgDebug;
00032 var $numErr;
00033 var $desErr;
00034 var $debug = true;
00035 var $fileLog;
00036 var $fileHdl;
00037
00038
00039
00046 function DBJames(){
00047 $this->dbhost = JSystem::GetConfig("DBHOST");
00048 $this->dbuser = JSystem::GetConfig("DBUSER");
00049 $this->dbpasswd = JSystem::GetConfig("DBPASSWD");
00050 $this->dbname = JSystem::GetConfig("DBNAME");
00051 $this->fileLog = JSystem::GetConfig("FLOG");
00052
00053 $this->ConnectDatabase();
00054 }
00055
00056
00061 function ConnectDatabase(){
00062
00063 $this->dbConnection = mysql_connect($this->dbhost,$this->dbuser,$this->dbpasswd);
00064
00065 if(!$this->dbConnection && $this->debug){
00066 $this->numErr = mysql_errno();
00067 $this->desErr = mysql_error();
00068 $this->msgDebug = "\nError nº ".$this->numErr;
00069 $this->msgDebug .= " al conectar con ";
00070 $this->msgDebug .= $this->dbname;
00071 $this->msgDebug .= "\nDescripción: ";
00072 $this->msgDebug .= $this->desErr;
00073 $this->msgDebug .= "\nFecha: " . date("d/m/Y - H:i:s");
00074 $this->debug();
00075 }else{
00076 $res = mysql_select_db($this->dbname,$this->dbConnection);
00077 }
00078 }
00079
00080
00084 function CloseDatabase(){
00085 if($this->dbConnection){
00086 mysql_close($this->dbConnection);
00087 }
00088 }
00089
00095 function Query($sql){
00096
00097 if($this->dbConnection){
00098 $this->sql = $sql;
00099 $this->rs = mysql_query($this->sql, $this->dbConnection);
00100
00101 if (!$this->rs){
00102 $msg = "Error al ejecutar query '".$sql."': ";
00103 $msg .= mysql_errno().": ".mysql_error()."<br>\n";
00104 $this->numRows = 0;
00105 $this->EOF = true;
00106 $this->msgDebug = "Query:".$msg;
00107 $this->debug();
00108 return;
00109 }
00110
00111 if($this->rs && mysql_num_rows($this->rs)){
00112 $this->EOF = false;
00113 $this->row = mysql_fetch_array($this->rs);
00114 $this->numRows = mysql_num_rows($this->rs);
00115 $this->numFields = mysql_num_fields($this->rs);
00116 }else{
00117 $this->numRows = 0;
00118 $this->EOF = true;
00119 }
00120
00121 $this->msgDebug = "Query: [" . $sql . "] => ";
00122 $this->msgDebug .= "(". $this->numRows . ") fila/s";
00123
00124 $this->debug();
00125 }
00126 }
00127
00128
00129
00135 function Execute($sql){
00136
00137 if($this->dbConnection){
00138 $this->sql = $sql;
00139 $this->rs = mysql_query($this->sql,$this->dbConnection);
00140 $this->numRows = (mysql_affected_rows($this->dbConnection));
00141
00142 if($this->rs){
00143 $this->EOF = false;
00144 }else{
00145 $this->EOF = true;
00146 }
00147
00148 $this->msgDebug = "Execute: [" . $sql . "] => ";
00149 $this->msgDebug .= "(". $this->numRows . ") fila/s afectadas";
00150 $this->debug();
00151 }
00152
00153 }
00154
00155
00160 function SetInsertID(){
00161 if($this->dbConnection)
00162 $this->newID = mysql_insert_id($this->dbConnection);
00163 else
00164 $this->newID = false;
00165 }
00166
00167
00173 function GetFieldName($col){
00174 return mysql_field_name($this->rs,$col);
00175 }
00176
00177
00178
00184 function getValue($col){
00185
00186 if(is_int($col))
00187 return $this->row[$col];
00188 else
00189 return $this->row[$col];
00190
00191 }
00192
00193
00197 function Next(){
00198
00199 if($this->rs && $this->dbConnection){
00200
00201 $this->row = mysql_fetch_array($this->rs);
00202 if($this->row)
00203 $this->EOF = false;
00204 else
00205 $this->EOF = true;
00206
00207 }
00208 }
00209
00210
00211
00215 function First(){
00216 $this->Go(0);
00217 }
00218
00219
00220
00225 function Go($pos){
00226
00227
00228 if(($this->rs) && ($pos <= $this->numRows) && ($pos >= 0)){
00229 mysql_data_seek($this->rs,$pos);
00230 $this->Next();
00231 }
00232 }
00233
00237 function Debug(){
00238
00239 $this->OpenFile();
00240 if($this->fileHdl){
00241 $msg = "\n[".strftime("%D %T")."]";
00242 $msg .= $this->msgDebug;
00243 $msg .= "<hr>";
00244 fwrite($this->fileHdl, $msg);
00245 $this->CloseFile();
00246 }
00247
00248 }
00249
00250
00254 function OpenFile()
00255 {
00256 $this->fileHdl = fopen($this->fileLog,'a');
00257 }
00258
00259
00260
00264 function CloseFile()
00265 {
00266 if($this->fileHdl)
00267 fclose($this->fileHdl);
00268 }
00269
00270 }
00271
00272
00273
00274 ?>