[3ec149c] | 1 | <?php |
---|
| 2 | /*================================================================================ |
---|
| 3 | Clase para conectar con una base de datos. |
---|
| 4 | |
---|
| 5 | Especificaciones: |
---|
| 6 | - Estado de la conexión($estado) |
---|
| 7 | 0: No conectado |
---|
| 8 | 1: Conectado |
---|
| 9 | 2: Se est�intentando conectar |
---|
| 10 | |
---|
| 11 | ================================================================================*/ |
---|
| 12 | |
---|
| 13 | class Conexion{ |
---|
| 14 | var $basedatos; // Base de datos |
---|
| 15 | var $servidor; // Servidor de Base de datos |
---|
| 16 | var $usuario; // Nombre de usuario |
---|
| 17 | var $password; // Clave de usuario |
---|
| 18 | var $controlador; // Controlador |
---|
| 19 | var $estado; // Estado de la conexion |
---|
| 20 | var $proveedor; // Proveedor de BD |
---|
| 21 | var $error; // Colecci� de errores ocurridos durante el proceso (C�igo de error) |
---|
| 22 | var $ultimoerror; // Ultimo error detectado |
---|
| 23 | var $inderror; // Nmero de errores ocurridos durante el proceso |
---|
| 24 | var $msgerrores=array( |
---|
| 25 | "No se ha producido ningn error", |
---|
| 26 | "001 : conexiónError - La conexion no se pudo establecer", |
---|
| 27 | "002 : conexiónError - Se estableci� la conexióncon el servidor pero la base de datos no responde", |
---|
| 28 | "003 : conexiónError - No se ha podido cerrar la actual conexi�", |
---|
| 29 | "004 : conexiónError - El objeto est�ocupado intentando establecer una conexiónanterior", |
---|
| 30 | "005 : conexiónError - La conexiónya est�cerrada", |
---|
| 31 | "006 : conexiónError - No se ha especificado ningn servidor de base de datos", |
---|
| 32 | "007 : conexiónError - No se ha especificado ningn usuario de la base de datos", |
---|
| 33 | "008 : conexiónError - No se ha especificado password de usuario", |
---|
| 34 | "009 : conexiónError - No se ha especificado ninguna base de datos", |
---|
| 35 | "010 : conexiónError - No se ha especificado ningn proveedor de bases de datos", |
---|
| 36 | ); |
---|
| 37 | /*--------------------------------------------------------------------------------------------*/ |
---|
| 38 | function Conexion(){ // Constructor de la clase |
---|
| 39 | $this->inderror=0; |
---|
| 40 | $this->ultimoerror=0; |
---|
| 41 | $this->estado=0; |
---|
| 42 | } |
---|
| 43 | /* ------------------------------------------------------------------------------------------- |
---|
| 44 | Adquiere o actualiza los datos necesarias para establecer conexiones |
---|
| 45 | |
---|
| 46 | Par�etros de entrada: |
---|
| 47 | servidor: Servidor donde se ubica la base de datos |
---|
| 48 | usuario : Un usuario con acceso al servidor |
---|
| 49 | passwor : Clave de usuario |
---|
| 50 | basedato: Base de datos a la se quiere acceder |
---|
| 51 | proveedor: Proveedor de Base de datos |
---|
| 52 | |
---|
| 53 | Devuelve : |
---|
| 54 | true : Si los datos aportadospara establecer conexiones son correctos |
---|
| 55 | false: En caso contrario |
---|
| 56 | |
---|
| 57 | En el caso de devolver false, la funci� TomaUltimoError() devuelve el error ocurrido |
---|
| 58 | ----------------------------------------------------------------------------------------------*/ |
---|
| 59 | function CadenaConexion($servidor,$usuario,$password,$basedatos,$proveedor){ |
---|
| 60 | $this->servidor=$servidor; |
---|
| 61 | $this->usuario=$usuario; |
---|
| 62 | $this->password=$password; |
---|
| 63 | $this->basedatos=$basedatos; |
---|
| 64 | $this->proveedor=$proveedor; |
---|
| 65 | if (!$this->_cadena_conexion()) return(false); else return(true); |
---|
| 66 | } |
---|
| 67 | /* ------------------------------------------------------------------------------------------- |
---|
| 68 | Abre una conexión |
---|
| 69 | |
---|
| 70 | Devuelve : |
---|
| 71 | true : Si la apertura de la conexiónha sido satisfactoria |
---|
| 72 | false: En caso contrario |
---|
| 73 | |
---|
| 74 | En el caso de devolver false, la funci� TomaUltimoError() devuelve el error ocurrido |
---|
| 75 | ----------------------------------------------------------------------------------------------*/ |
---|
| 76 | function Abrir(){ |
---|
| 77 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 78 | $this->ultimoerror=-1; |
---|
| 79 | $MAXIMOS_INTENTOS_DE_CONECCION=10; |
---|
| 80 | if (!$this->_cadena_conexion()) return(false); // Comprueba si los datos necesarios para conexiones se han aportado |
---|
| 81 | switch ($this->estado) { |
---|
| 82 | case 1: // Existe actualmente una conexiónabierta que se sustituir�por la nueva |
---|
| 83 | if (mysql_close($this->controlador)){ // Se cierra la conexion actual |
---|
| 84 | $this->estado=0; |
---|
| 85 | $intentos_de_conexion=0; |
---|
| 86 | while(true){ |
---|
| 87 | $intentos_de_conexion++; |
---|
| 88 | $resul=($this->_nueva_conexion()); |
---|
| 89 | if ($resul || $intentos_de_conexion>$MAXIMOS_INTENTOS_DE_CONECCION) return($resul); |
---|
| 90 | sleep(1); // Espera 1 segundo para intentar la conexiónde nuevo |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | else{ // Error al cerrar la conexi� |
---|
| 94 | $this->error[$this->inderror++]=3; |
---|
| 95 | $this->ultimoerror=3; |
---|
| 96 | return(false); |
---|
| 97 | } |
---|
| 98 | break; |
---|
| 99 | case 2: // Actualmente est�objeto est�ocupado intentando establecer otra conexi� |
---|
| 100 | $this->error[$this->inderror++]=4; |
---|
| 101 | $this->ultimoerror=4; |
---|
| 102 | return(false); |
---|
| 103 | break; |
---|
| 104 | default : // No existe actualmente ninguna conexiónabierta, se abrir�una nueva |
---|
| 105 | $intentos_de_conexion=0; |
---|
| 106 | while(true){ |
---|
| 107 | $intentos_de_conexion++; |
---|
| 108 | $resul=($this->_nueva_conexion()); |
---|
| 109 | if ($resul || $intentos_de_conexion>$MAXIMOS_INTENTOS_DE_CONECCION) return($resul); |
---|
| 110 | sleep(1); // Espera 1 segundo para intentar la conexiónde nuevo |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | /* ------------------------------------------------------------------------------------------- |
---|
| 115 | Cierra una conexión |
---|
| 116 | |
---|
| 117 | Devuelve : |
---|
| 118 | true : Si la conexiónse ha cerrado satisfactoriamente |
---|
| 119 | false: En caso contrario |
---|
| 120 | |
---|
| 121 | En el caso de devolver false, la funci� TomaUltimoError() devuelve el error ocurrido |
---|
| 122 | ----------------------------------------------------------------------------------------------*/ |
---|
| 123 | function Cerrar(){ |
---|
| 124 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 125 | $this->ultimoerror=-1; |
---|
| 126 | switch ($this->estado) { |
---|
| 127 | case 1: // Actualmente la conexion est�abierta |
---|
| 128 | if (mysql_close($this->controlador)){ // Se cierra la conexion actual |
---|
| 129 | $this->estado=0; |
---|
| 130 | $this->error[$this->inderror++]=0; |
---|
| 131 | $this->ultimoerror=0; |
---|
| 132 | return(true); |
---|
| 133 | } |
---|
| 134 | else{ // Error al cerrar la conexi� |
---|
| 135 | $this->error[$this->inderror++]=3; |
---|
| 136 | $this->ultimoerror=3; |
---|
| 137 | return(false); |
---|
| 138 | } |
---|
| 139 | break; |
---|
| 140 | case 2: // Actualmente est�objeto est�ocupado intentando establecer otra conexi� |
---|
| 141 | $this->error[$this->inderror++]=4; |
---|
| 142 | $this->ultimoerror=4; |
---|
| 143 | return(false); |
---|
| 144 | break; |
---|
| 145 | |
---|
| 146 | default : // Actualmente la conexiónest�ya cerrada |
---|
| 147 | $this->error[$this->inderror++]=5; |
---|
| 148 | $this->ultimoerror=5; |
---|
| 149 | return(false); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | /* ------------------------------------------------------------------------------------------- |
---|
| 153 | Establece una nueva conexi�. Este m�odo es privado y s�o lo puede ejecutar la propia |
---|
| 154 | clase desde el m�odo pblico Abrir. |
---|
| 155 | ----------------------------------------------------------------------------------------------*/ |
---|
| 156 | function _nueva_conexion(){ |
---|
| 157 | $this->estado=2;// Intenta la conexion |
---|
| 158 | if ($this->controlador=mysql_connect($this->servidor,$this->usuario,$this->password)){// Conexion O.K. |
---|
| 159 | $this->estado=1; // La conexion con el servidor se estableci� |
---|
| 160 | if (mysql_select_db($this->basedatos, $this->controlador)){// Base datos O.K. |
---|
| 161 | $this->error[$this->inderror++]=0; |
---|
| 162 | $this->ultimoerror=0; |
---|
| 163 | return(true); |
---|
| 164 | } |
---|
| 165 | else{ // Problemas con la base de datos |
---|
| 166 | $this->error[$this->inderror++]=2; |
---|
| 167 | $this->ultimoerror=2; |
---|
| 168 | if (mysql_close ($this->controlador)) $this->estado=0; // Se cierra la conexion |
---|
| 169 | return(false); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | else{ // Problemas con la conexion |
---|
| 173 | $this->estado=0; |
---|
| 174 | $this->error[$this->inderror++]=1; |
---|
| 175 | $this->ultimoerror=1; |
---|
| 176 | return(false); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | /* ------------------------------------------------------------------------------------------- |
---|
| 180 | Establece una sistema UTF8 para las consultas |
---|
| 181 | ----------------------------------------------------------------------------------------------*/ |
---|
| 182 | function SetUtf8(){ |
---|
| 183 | mysql_query("SET NAMES 'utf8'"); |
---|
| 184 | } |
---|
| 185 | /* ------------------------------------------------------------------------------------------- |
---|
| 186 | Revisa y detecta las condiciones que deben cumplir los datos necesarios para establecer |
---|
| 187 | conexiones |
---|
| 188 | |
---|
| 189 | Devuelve : |
---|
| 190 | true : Si los datos aportados son correctos |
---|
| 191 | false: Si algn dato NO ha sido aportado o es incorrecto |
---|
| 192 | |
---|
| 193 | Este m�odo es privado y s�o lo ejecutan m�odos pblicos de la propia clase |
---|
| 194 | ----------------------------------------------------------------------------------------------*/ |
---|
| 195 | function _cadena_conexion(){ |
---|
| 196 | |
---|
| 197 | if ($this->servidor==null){ |
---|
| 198 | $this->error[$this->inderror++]=6; // Servidor no establecido |
---|
| 199 | $this->ultimoerror=6; |
---|
| 200 | return(false); |
---|
| 201 | } |
---|
| 202 | if ($this->usuario==null){ |
---|
| 203 | $this->error[$this->inderror++]=7;// usuario no establecido |
---|
| 204 | $this->ultimoerror=7; |
---|
| 205 | return(false); |
---|
| 206 | } |
---|
| 207 | if ($this->password==null){ |
---|
| 208 | $this->error[$this->inderror++]=8; // password no establecido |
---|
| 209 | $this->ultimoerror=8; |
---|
| 210 | return(false); |
---|
| 211 | } |
---|
| 212 | if ($this->basedatos==null){ |
---|
| 213 | $this->error[$this->inderror++]=9; // base de datos no establecido |
---|
| 214 | $this->ultimoerror=9; |
---|
| 215 | return(false); |
---|
| 216 | } |
---|
| 217 | if ($this->proveedor==null){ |
---|
| 218 | $this->error[$this->inderror++]=10; // proveedor no establecido |
---|
| 219 | $this->ultimoerror=10; |
---|
| 220 | return(false); |
---|
| 221 | } |
---|
| 222 | $this->error[$this->inderror++]=0; // Datos de conexióncorrectos |
---|
| 223 | $this->ultimoerror=0; |
---|
| 224 | return(true); |
---|
| 225 | } |
---|
| 226 | /* ------------------------------------------------------------------------------------------- |
---|
| 227 | Devuelve el c�igo del ltimo error ocurrido durante el proceso anterior. |
---|
| 228 | ----------------------------------------------------------------------------------------------*/ |
---|
| 229 | function UltimoError(){ |
---|
| 230 | return($this->ultimoerror); |
---|
| 231 | } |
---|
| 232 | /* ------------------------------------------------------------------------------------------- |
---|
| 233 | Devuelve una cadena con el mensage del ltimo error ocurrido durante el proceso anterior. |
---|
| 234 | ----------------------------------------------------------------------------------------------*/ |
---|
| 235 | function DescripUltimoError(){ |
---|
| 236 | return($this->msgerrores[$this->ultimoerror]); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | /*========================================================================================= |
---|
| 240 | Clase para usarla con la clase comando. |
---|
| 241 | |
---|
| 242 | Especificaciones: |
---|
| 243 | |
---|
| 244 | Esta clase tiene dos propiedades que definen su contenido |
---|
| 245 | nombre=nombre del parametro |
---|
| 246 | valor = valor de dicho par�etro |
---|
| 247 | tipo = tipo de parametro: |
---|
| 248 | 0: El valor del par�etro debe ir encerrado entre comillas simples |
---|
| 249 | 1: El valor del par�etro no necesita ir entre comillas simples |
---|
| 250 | ========================================================================================*/ |
---|
| 251 | class parametro{ |
---|
| 252 | var $nombre; |
---|
| 253 | var $valor; |
---|
| 254 | var $tipo; |
---|
| 255 | /*--------------------------------------------------------------------------------------------*/ |
---|
| 256 | function parametro($nombre="SinNombre",$valor="",$tipo="0"){ // Constructor de la clase |
---|
| 257 | $this->SetParametro($nombre,$valor,$tipo); |
---|
| 258 | } |
---|
| 259 | /* ------------------------------------------------------------------------------------------- |
---|
| 260 | Modifica los valores de las propiedades de la clase |
---|
| 261 | ----------------------------------------------------------------------------------------------*/ |
---|
| 262 | function SetParametro($nombre,$valor,$tipo){ |
---|
| 263 | $this->nombre=$nombre; |
---|
| 264 | $this->valor=$valor; |
---|
| 265 | $this->tipo=$tipo; |
---|
| 266 | if($tipo==1 && empty($valor)) $this->valor=0; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | /*========================================================================================== |
---|
| 270 | Clase para manipular bases de datos a traves de una conexiónprevia. |
---|
| 271 | |
---|
| 272 | Especificaciones: |
---|
| 273 | |
---|
| 274 | Las sentencias SQL pueden contener par�etros que pueden ser sustituidos por el valor |
---|
| 275 | de los objetos par�etro. Estos par�etros tendr� la forma:@nombre_del_parametro |
---|
| 276 | ==================================================================================================*/ |
---|
| 277 | class Comando{ |
---|
| 278 | var $texto; |
---|
| 279 | var $Conexion; |
---|
| 280 | var $parametros=array(); |
---|
| 281 | var $Recordset; |
---|
| 282 | var $resul; |
---|
| 283 | var $error; // Error |
---|
| 284 | var $ultimoerror; // Ultimo error detectado |
---|
| 285 | var $inderror; // Contador de errores |
---|
| 286 | var $msgerrores=array( |
---|
| 287 | "No se ha producido ningn error", |
---|
| 288 | "001 : Comando Error - No se ha establecido el texto del comando", |
---|
| 289 | "002 : Comando Error - No se ha establecido la conexióndel comando", |
---|
| 290 | "003 : Comando Error - No se ha abierto la conexi�", |
---|
| 291 | "004 : Comando Error - La sentencia SQl del comando no es correcta", |
---|
| 292 | "005 : Comando Error - No se ha podido recuperar el valor @@identity de la ltima clave insertada", |
---|
| 293 | ); |
---|
| 294 | /*--------------------------------------------------------------------------------------------*/ |
---|
| 295 | function Comando(){ // Constructor de la clase |
---|
| 296 | $this->inderror=0; |
---|
| 297 | $this->ultimoerror=0; |
---|
| 298 | $this->Recordset=new Recordset; |
---|
| 299 | } |
---|
| 300 | /* ------------------------------------------------------------------------------------------- |
---|
| 301 | Devuelve el c�igo del ltimo error ocurrido durante el proceso anterior. |
---|
| 302 | ----------------------------------------------------------------------------------------------*/ |
---|
| 303 | function UltimoError(){ |
---|
| 304 | return($this->ultimoerror); |
---|
| 305 | } |
---|
| 306 | /* ------------------------------------------------------------------------------------------- |
---|
| 307 | Devuelve una cadena con el mensage del ltimo error ocurrido durante el proceso anterior. |
---|
| 308 | ----------------------------------------------------------------------------------------------*/ |
---|
| 309 | function DescripUltimoError(){ |
---|
| 310 | return($this->msgerrores[$this->ultimoerror]); |
---|
| 311 | } |
---|
| 312 | /* ------------------------------------------------------------------------------------------- |
---|
| 313 | A�de un par�etro a la colecci� de parametros. La matriz que implementa la colecci� |
---|
| 314 | es una matriz asociativa cuyo indice asociativo es el nombre del par�etro |
---|
| 315 | |
---|
| 316 | Par�etros de entrada: |
---|
| 317 | objparam: Un objeto parametro |
---|
| 318 | ---------------------------------------------------------------------------------------------*/ |
---|
| 319 | function AddParametro($objparam){ |
---|
| 320 | $tbparametro["nombre"]=$objparam->nombre; |
---|
| 321 | $tbparametro["valor"]=$objparam->valor; |
---|
| 322 | $tbparametro["tipo"]=$objparam->tipo; |
---|
| 323 | $this->parametros[]=$tbparametro; |
---|
| 324 | } |
---|
| 325 | /* ------------------------------------------------------------------------------------------- |
---|
| 326 | A�de un par�etro a la colecci� de parametros. La matriz que implementa la colecci� |
---|
| 327 | es una matriz asociativa cuyo indice asociativo es el del par�etro |
---|
| 328 | |
---|
| 329 | Par�etros de entrada: |
---|
| 330 | nombre: El nombre del par�etro |
---|
| 331 | valor : El valor del par�etro |
---|
| 332 | tipo = tipo de parametro: |
---|
| 333 | 0: El valor del par�etro debe ir encerrado entre comillas simples |
---|
| 334 | 1: El valor del par�etro no necesita ir entre comillas simples |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | ---------------------------------------------------------------------------------------------*/ |
---|
| 338 | function CreaParametro($nombre,$valor,$tipo){ |
---|
| 339 | for($i=0;$i<sizeof($this->parametros);$i++){ |
---|
| 340 | if($this->parametros[$i]["nombre"]==$nombre){ |
---|
| 341 | $this->parametros[$i]["valor"]=$valor; |
---|
| 342 | return; |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | $p = new parametro($nombre,$valor,$tipo); |
---|
| 346 | $this->AddParametro($p); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /* ------------------------------------------------------------------------------------------- |
---|
| 350 | Sustituye el valor de un par�etro existente por otro |
---|
| 351 | Par�etros de entrada: |
---|
| 352 | nombre: El nombre del par�etro |
---|
| 353 | valor : El nuevo valor del par�etro |
---|
| 354 | ---------------------------------------------------------------------------------------------*/ |
---|
| 355 | function ParamSetValor($nombre,$valor){ |
---|
| 356 | for($i=0;$i<sizeof($this->parametros);$i++){ |
---|
| 357 | if($this->parametros[$i]["nombre"]==$nombre) |
---|
| 358 | $this->parametros[$i]["valor"]=$valor; |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | /* ------------------------------------------------------------------------------------------- |
---|
| 362 | Establece la conexiónque se usar�para ejecutar las acciones pertinentes |
---|
| 363 | |
---|
| 364 | Par�etros de entrada: |
---|
| 365 | objconexion: Un objeto conexion |
---|
| 366 | ---------------------------------------------------------------------------------------------*/ |
---|
| 367 | function EstableceConexion($objconexion){ |
---|
| 368 | $this->Conexion= $objconexion; |
---|
| 369 | } |
---|
| 370 | /* ------------------------------------------------------------------------------------------- |
---|
| 371 | Establece la conexiónque se usar�para ejecutar las acciones pertinentes |
---|
| 372 | |
---|
| 373 | Par�etros de entrada: |
---|
| 374 | textocomando: Un texto con la sentencia SQL (Puede contener par�etros) |
---|
| 375 | ---------------------------------------------------------------------------------------------*/ |
---|
| 376 | function EstableceTexto($textocomando){ |
---|
| 377 | $this->texto=$textocomando; |
---|
| 378 | } |
---|
| 379 | /* ------------------------------------------------------------------------------------------- |
---|
| 380 | Sustituye el valor de los parametros en la expresi� que forma el texto del Comando |
---|
| 381 | ---------------------------------------------------------------------------------------------*/ |
---|
| 382 | function Traduce(){ |
---|
| 383 | $execomando=$this->texto; |
---|
| 384 | if (sizeof($this->parametros)>0){ // Hay par�etros que sustituir |
---|
| 385 | foreach($this->parametros as $parametro){ |
---|
| 386 | if ($parametro["tipo"]==0) // Tipo alfanum�ico |
---|
| 387 | $execomando=str_replace($parametro["nombre"],"'".$parametro["valor"]."'",$execomando); |
---|
| 388 | else |
---|
| 389 | $execomando=str_replace($parametro["nombre"],$parametro["valor"],$execomando); |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | $this->texto=$execomando; |
---|
| 393 | } |
---|
| 394 | /* ------------------------------------------------------------------------------------------- |
---|
| 395 | Ejecuta la sentencia SQL contenida en la propiedad texto |
---|
| 396 | ---------------------------------------------------------------------------------------------*/ |
---|
| 397 | function Ejecutar(){ |
---|
| 398 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 399 | $this->ultimoerror=-1; |
---|
| 400 | if ($this->texto==null){ |
---|
| 401 | $this->error[$this->inderror++]=1; // Texto no especificado |
---|
| 402 | $this->ultimoerror=1; |
---|
| 403 | return(false); |
---|
| 404 | } |
---|
| 405 | else{ |
---|
| 406 | if ($this->Conexion==null){ |
---|
| 407 | $this->error[$this->inderror++]=2; // conexiónNO establecida |
---|
| 408 | $this->ultimoerror=2; |
---|
| 409 | return(false); |
---|
| 410 | } |
---|
| 411 | else{ |
---|
| 412 | if ($this->Conexion->estado==0){ |
---|
| 413 | $this->error[$this->inderror++]=3; // conexiónNO abierta |
---|
| 414 | $this->ultimoerror=3; |
---|
| 415 | return(false); |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | $this->Traduce(); |
---|
| 420 | if (!$this->resul=mysql_query($this->texto,$this->Conexion->controlador)){ |
---|
| 421 | $this->error[$this->inderror++]=4; // Error en la sentencia SQL del comando |
---|
| 422 | $this->ultimoerror=4; |
---|
| 423 | return(false); |
---|
| 424 | } |
---|
| 425 | if (stristr($this->texto,"select")){ |
---|
| 426 | $this->Recordset->Inicializar(); |
---|
| 427 | $this->Recordset->filas=$this->resul; |
---|
| 428 | $this->Recordset->numerodecampos=mysql_num_fields($this->Recordset->filas); |
---|
| 429 | $this->Recordset->numeroderegistros=mysql_num_rows($this->Recordset->filas); |
---|
| 430 | if ($this->Recordset->numeroderegistros>0){ |
---|
| 431 | $this->Recordset->BOF=false; |
---|
| 432 | $this->Recordset->EOF=false; |
---|
| 433 | $this->Recordset->campos=mysql_fetch_array($this->Recordset->filas); |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | $this->error[$this->inderror++]=0; // Comando ejecutado correctamante |
---|
| 438 | $this->ultimoerror=0; |
---|
| 439 | return(true); |
---|
| 440 | } |
---|
| 441 | /* ------------------------------------------------------------------------------------------- |
---|
| 442 | Esta funci� recupera el ltimo nmero asignado a una clave autonum�ica de una tabla |
---|
| 443 | ---------------------------------------------------------------------------------------------*/ |
---|
| 444 | function Autonumerico(){ |
---|
| 445 | $ulreg=mysql_insert_id(); |
---|
| 446 | return($ulreg); |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | /*========================================================================================= |
---|
| 450 | Clase para consultar tablas y vistas de una base de datos. |
---|
| 451 | |
---|
| 452 | Especificaciones: |
---|
| 453 | - Estado del recordset ($estado) |
---|
| 454 | 0: Cerrado |
---|
| 455 | 1: Abierto |
---|
| 456 | =========================================================================================*/ |
---|
| 457 | class Recordset{ |
---|
| 458 | var $Comando; |
---|
| 459 | var $filas= array(); |
---|
| 460 | var $BOF,$EOF,$estado; |
---|
| 461 | var $campos; |
---|
| 462 | var $numeroderegistros,$numerodecampos,$posicion; |
---|
| 463 | |
---|
| 464 | var $error; // Error |
---|
| 465 | var $ultimoerror; // Ultimo error detectado |
---|
| 466 | var $inderror; // Contador de errores |
---|
| 467 | var $msgerrores=array( |
---|
| 468 | "No se ha producido ningn error", |
---|
| 469 | "001 : Recordset Error - Comando no establecido", |
---|
| 470 | "002 : Recordset Error - No se ha establecido la conexióndel comando", |
---|
| 471 | "003 : Recordset Error - No se ha abierto la conexi�", |
---|
| 472 | "004 : Recordset Error - No se pudo abrir la consulta", |
---|
| 473 | "005 : Recordset Error - La sentencia SQl del comando no contiene la clausula SELECT", |
---|
| 474 | "006 : Recordset Error - No se puede liberar la consulta", |
---|
| 475 | ); |
---|
| 476 | /*--------------------------------------------------------------------------------------------*/ |
---|
| 477 | function Recordset(){ // Constructor de la clase |
---|
| 478 | $this->Inicializar(); |
---|
| 479 | } |
---|
| 480 | /* ------------------------------------------------------------------------------------------- |
---|
| 481 | Inicializa propiedades de las clase |
---|
| 482 | ----------------------------------------------------------------------------------------------*/ |
---|
| 483 | function Inicializar(){ |
---|
| 484 | $this->BOF=true; |
---|
| 485 | $this->EOF=true; |
---|
| 486 | $this->posicion=0; |
---|
| 487 | $this->numeroderegistros=0; |
---|
| 488 | $this->numerodecampos=0; |
---|
| 489 | $this->estado=0; |
---|
| 490 | } |
---|
| 491 | /* ------------------------------------------------------------------------------------------- |
---|
| 492 | Devuelve el c�igo del ltimo error ocurrido durante el proceso anterior. |
---|
| 493 | ----------------------------------------------------------------------------------------------*/ |
---|
| 494 | function UltimoError(){ |
---|
| 495 | return($this->ultimoerror); |
---|
| 496 | } |
---|
| 497 | /* ------------------------------------------------------------------------------------------- |
---|
| 498 | Devuelve una cadena con el mensage del ltimo error ocurrido durante el proceso anterior. |
---|
| 499 | ----------------------------------------------------------------------------------------------*/ |
---|
| 500 | function DescripUltimoError(){ |
---|
| 501 | return($this->msgerrores[$this->ultimoerror]); |
---|
| 502 | } |
---|
| 503 | /* ------------------------------------------------------------------------------------------- |
---|
| 504 | Establece el comando que se usar�para ejecutar las consultas pertinentes |
---|
| 505 | |
---|
| 506 | Par�etros de entrada: |
---|
| 507 | objcomando: Un objeto comando con la sentencia SQL (Puede contener par�etros) |
---|
| 508 | |
---|
| 509 | Devuelve : |
---|
| 510 | true : Si el texto del comando contiene la clausula SELECT |
---|
| 511 | false: En caso contrario |
---|
| 512 | |
---|
| 513 | En el caso de devolver false, la funci� TomaUltimoError() devuelve el error ocurrido |
---|
| 514 | ---------------------------------------------------------------------------------------------*/ |
---|
| 515 | function EstableceComando($objcomando){ |
---|
| 516 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 517 | $this->ultimoerror=-1; |
---|
| 518 | if (stristr($objcomando->texto,"select")){ |
---|
| 519 | $this->Comando=$objcomando; |
---|
| 520 | $this->error[$this->inderror++]=0; // Comando v�ido, contiene "SELECT" |
---|
| 521 | $this->ultimoerror=0; |
---|
| 522 | return(true); |
---|
| 523 | } |
---|
| 524 | else{ |
---|
| 525 | $this->error[$this->inderror++]=5; // Comando no valido, NO contiene "SELECT" |
---|
| 526 | $this->ultimoerror=5; |
---|
| 527 | return(false); |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | /* ------------------------------------------------------------------------------------------- |
---|
| 531 | Sustituye el valor de los parametros en la expresi� que forma el texto del Comando |
---|
| 532 | ---------------------------------------------------------------------------------------------*/ |
---|
| 533 | function Traduce(){ |
---|
| 534 | $execomando=$this->Comando->texto; |
---|
| 535 | if (sizeof($this->Comando->parametros)>0){ // Hay par�etros que sustituir |
---|
| 536 | foreach($this->Comando->parametros as $parametro){ |
---|
| 537 | if ($parametro["tipo"]==0) // Tipo alfanum�ico |
---|
| 538 | $execomando=str_replace($parametro["nombre"],"'".$parametro["valor"]."'",$execomando); |
---|
| 539 | else |
---|
| 540 | $execomando=str_replace($parametro["nombre"],$parametro["valor"],$execomando); |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | $this->Comando->texto=$execomando; |
---|
| 544 | } |
---|
| 545 | /* ------------------------------------------------------------------------------------------- |
---|
| 546 | Recupera registros de la base de datos |
---|
| 547 | ---------------------------------------------------------------------------------------------*/ |
---|
| 548 | function Abrir(){ |
---|
| 549 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 550 | $this->ultimoerror=-1; |
---|
| 551 | if ($this->Comando==null){ |
---|
| 552 | $this->error[$this->inderror++]=1; // Comando no especificado |
---|
| 553 | $this->ultimoerror=1; |
---|
| 554 | return(false); |
---|
| 555 | } |
---|
| 556 | else{ |
---|
| 557 | if ($this->Comando->Conexion==null){ |
---|
| 558 | $this->error[$this->inderror++]=2; // conexiónNO establecida |
---|
| 559 | $this->ultimoerror=2; |
---|
| 560 | return(false); |
---|
| 561 | } |
---|
| 562 | else{ |
---|
| 563 | if ($this->Comando->Conexion->estado==0){ |
---|
| 564 | $this->error[$this->inderror++]=3; // conexiónNO abierta |
---|
| 565 | $this->ultimoerror=3; |
---|
| 566 | return(false); |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | $this->Traduce(); |
---|
| 571 | $this->Inicializar(); |
---|
| 572 | if (!$this->filas=mysql_query($this->Comando->texto,$this->Comando->Conexion->controlador)){ |
---|
| 573 | $this->error[$this->inderror++]=4; // Error en la sentencia SQL del comando o al abrir la consula |
---|
| 574 | $this->ultimoerror=4; |
---|
| 575 | return(false); |
---|
| 576 | } |
---|
| 577 | $this->numeroderegistros=mysql_num_rows($this->filas); // La consulta se ha realizado con �ito |
---|
| 578 | $this->numerodecampos=mysql_num_fields($this->filas); |
---|
| 579 | if ($this->numeroderegistros>0){ |
---|
| 580 | $this->BOF=false; |
---|
| 581 | $this->EOF=false; |
---|
| 582 | $this->campos=mysql_fetch_array($this->filas); |
---|
| 583 | } |
---|
| 584 | $this->estado=1; // Recordset abierto |
---|
| 585 | $this->error[$this->inderror++]=0; // Recuperaci� de registros correcta |
---|
| 586 | $this->ultimoerror=0; |
---|
| 587 | return(true); |
---|
| 588 | } |
---|
| 589 | /* ------------------------------------------------------------------------------------------- |
---|
| 590 | Libera los registros de una consulta de la base de datos |
---|
| 591 | ---------------------------------------------------------------------------------------------*/ |
---|
| 592 | function Cerrar(){ |
---|
| 593 | $this->inderror=-1; // Inicializar contador de errores |
---|
| 594 | $this->ultimoerror=-1; |
---|
| 595 | if (!mysql_free_result($this->filas)){ |
---|
| 596 | $this->error[$this->inderror++]=6; // Error al cerrar la consulta (Al liberar memoria) |
---|
| 597 | $this->ultimoerror=6; |
---|
| 598 | return(false); |
---|
| 599 | } |
---|
| 600 | $this->Inicializar(); |
---|
| 601 | $this->error[$this->inderror++]=0; // Recuperaci� de registros correcta |
---|
| 602 | $this->ultimoerror=0; |
---|
| 603 | return(true); |
---|
| 604 | } |
---|
| 605 | /* ------------------------------------------------------------------------------------------- |
---|
| 606 | Mueve el puntero de lectura al siguiente registro del recordset |
---|
| 607 | ---------------------------------------------------------------------------------------------*/ |
---|
| 608 | function Siguiente(){ |
---|
| 609 | if (!$this->EOF){ |
---|
| 610 | $this->posicion++; |
---|
| 611 | if ($this->posicion==$this->numeroderegistros) |
---|
| 612 | $this->EOF=true; |
---|
| 613 | else{ |
---|
| 614 | if (mysql_data_seek($this->filas,$this->posicion)) |
---|
| 615 | $this->campos=mysql_fetch_array($this->filas); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | } |
---|
| 619 | /* ------------------------------------------------------------------------------------------- |
---|
| 620 | Mueve el puntero de lectura al anterior registro del recordset |
---|
| 621 | ---------------------------------------------------------------------------------------------*/ |
---|
| 622 | function Anterior(){ |
---|
| 623 | if (!$this->BOF){ |
---|
| 624 | $this->posicion--; |
---|
| 625 | if ($this->posicion<0) |
---|
| 626 | $this->BOF=true; |
---|
| 627 | else{ |
---|
| 628 | if (mysql_data_seek($this->filas,$this->posicion)); |
---|
| 629 | $this->campos=mysql_fetch_array($this->filas); |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | /* ------------------------------------------------------------------------------------------- |
---|
| 634 | Mueve el puntero de lectura al primer registro del recordset |
---|
| 635 | ---------------------------------------------------------------------------------------------*/ |
---|
| 636 | function Primero(){ |
---|
| 637 | if ($this->numeroderegistros>0){ |
---|
| 638 | $this->posicion=0; |
---|
| 639 | if (mysql_data_seek($this->filas,$this->posicion)) |
---|
| 640 | $this->campos=mysql_fetch_array($this->filas); |
---|
| 641 | } |
---|
| 642 | } |
---|
| 643 | /* ------------------------------------------------------------------------------------------- |
---|
| 644 | Mueve el puntero de lectura al ltimo registro del recordset |
---|
| 645 | ---------------------------------------------------------------------------------------------*/ |
---|
| 646 | function Ultimo(){ |
---|
| 647 | if ($this->numeroderegistros>0){ |
---|
| 648 | $this->posicion=$this->numeroderegistros-1; |
---|
| 649 | if (mysql_data_seek($this->filas,$this->posicion)) |
---|
| 650 | $this->campos=mysql_fetch_array($this->filas); |
---|
| 651 | } |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | /* ------------------------------------------------------------------------------------------- |
---|
| 655 | Esta funci�n devuelve una matriz asociativa con el nombre de los campos del recordset |
---|
| 656 | ---------------------------------------------------------------------------------------------*/ |
---|
| 657 | function DatosNombres(){ |
---|
| 658 | if (mysql_data_seek($this->filas,$this->posicion)) |
---|
| 659 | return(mysql_fetch_assoc($this->filas)); |
---|
| 660 | return(""); |
---|
| 661 | } |
---|
| 662 | /* ------------------------------------------------------------------------------------------- |
---|
| 663 | Esta funci�n devuelve informaci�n sobre los campos de la tabla |
---|
| 664 | ---------------------------------------------------------------------------------------------*/ |
---|
| 665 | function InfoCampos(){ |
---|
| 666 | $infocampos= array (); |
---|
| 667 | while ($row = mssql_fetch_field($this->filas)) { |
---|
| 668 | $campo["name"]=$row->name; |
---|
| 669 | $campo["column_source"]=$row->column_source; |
---|
| 670 | $campo["maxlon"]=$row->max_length; |
---|
| 671 | $campo["numeric"]=$row->numeric; |
---|
| 672 | array_push($infocampos,$campo); |
---|
| 673 | } |
---|
| 674 | return($infocampos); |
---|
| 675 | } |
---|
| 676 | ?> |
---|