Location: PHPKode > projects > Gestin > gestin/per/db/Conexion.class.php
<?php
/*
  GESTIN - Sistema de Gestion de Incidencias
  Copyright (C) 2007 Mathias Rodriguez, Diego Martorell, Matias Bisay

  This file is part of GESTIN.
 
  GESTIN is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  GESTIN is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GESTIN.  If not, see <http://www.gnu.org/licenses/>.
 */

include_once($_SERVER['DOCUMENT_ROOT'] . '/gestin/config.inc.php');
require_once('Transaccion.class.php');
require_once(EXCEPTION_PATH . 'ConsultaVaciaException.class.php');
require_once(EXCEPTION_PATH . 'ErrorConsultaException.class.php');

class Conexion {
	private $database;
	private $host;
	private $user;
	private $password;
	private $odbc;
	private $resultSet;
	private $enlace;
	private $transaccion;
	
	public function __construct($hostDB, $db, $usesOdbc)  {
		$this->host = $hostDB;
		$this->odbc = $usesOdbc;
		$this->database = $db;
		$this->user = "";
		$this->password = "";				
		$this->transaccion = new Transaccion('',LOG_PATH . 'trx.log');
		}
	
	public function __destruct()  {
		if(!$this->odbc)  {
			$close = mysql_close($this->enlace) or 
				     $this->transaccion->setMensaje('No se pudo cerrar la conexión: ' . mysql_error());
			
			if(!$close)  {
				$this->transaccion->grabarLog();
				echo "No se pudo cerrar la conexion";
				}
			else  {
				$this->transaccion->setMensaje('Conexión cerrada');
				$this->transaccion->grabarLog();
				}			
			}		
		}
	
	public function getEnlace()  { return $this->enlace;}
	
	public function getMensaje()  { return "Info: " . mysql_info(); }
	
	public function conectar($dbUser, $dbPassword)  {
		$this->user = $dbUser;
		$this->password = $dbPassword;
		
		if(!$this->odbc)  {
			$this->enlace = mysql_connect($this->host, $dbUser, 
										  $dbPassword, true) 
							or $this->transaccion->setMensaje('Error en la conexión: '.mysql_error());			
			
			if(!isset($this->enlace)) {
				$this->transaccion->grabarLog();
				die('No se pudo abrir la conexion');
				}
				
			$result = mysql_select_db($this->database) or 
					  $this->transaccion->setMensaje('Error al seleccionar la base de datos:' . mysql_error());					
			
			if(!isset($result))  {
				$this->transaccion->grabarLog();			
				exit;
				}			
			}
		}
			
	public function consultar($tabla, $campos, $condicion)  {		
		if($tabla != '')  {
			$query = 'Select ' . (($campos == '*') ? $campos : $campos) . 
					' from ' . $tabla . ' ' .
					(($condicion != '') ? 'where ' . $condicion . ';': ';');		
			
			$this->transaccion->setMensaje($query);			
			$this->transaccion->grabarLog();
			
			$this->resultSet = mysql_query($query) or 
					$this->transaccion->setMensaje('No se pudo realizar la consulta: ' . mysql_error());
			
			if($this->resultSet == null)  {
				$this->transaccion->grabarLog();
				throw new ErrorConsultaException('Error en la consulta');
				}				
			
			$rs = array();
			
			while($row = mysql_fetch_array($this->resultSet,MYSQL_ASSOC))  
				$rs[] = $row;
			
			if ($rs == null)  {
				$this->transaccion->setMensaje('La consulta no devolvíó valores');
				$this->transaccion->grabarLog();
				throw new ConsultaVaciaException('La consulta no devolvió valores');
				}
			else
				return $rs;
			}
		else  {
			echo 'Debe especificar la tabla';
			$this->transaccion->setMensaje('Debe especificar la tabla');
			$this->transaccion->grabarLog();
			}
		}	
	
	public function eliminar($tabla, $condicion)  {
		if($tabla != '')  {
			$query = 'delete from ' . $tabla . ' where ' .$condicion;
			
			$this->resultSet = mysql_query($query) or 
						       $this->transaccion->setMensaje('No se pudo realizar la consulta: ' . mysql_error());
			
			if($this->resultSet == null)  {				
				$this->transaccion->grabarLog();
				echo 'No se pudo realizar la consulta';
				}
			else  {
				$this->transaccion->setMensaje($query);
				$this->transaccion->grabarLog();
				}
			}
		else  {
			echo 'Debe especificar la tabla';
			$this->transaccion->setMensaje('Debe especificar la tabla');
			$this->transaccion->grabarLog();
			}
		}
		
	public function insertar($tabla, $campos, $datos, $condicion)  {
		if($tabla != '')  {
			$queryAtt = self::formatear($campos,$datos,1);
			
			$query = 'Insert into '. $tabla . '(' . $campos . ') values('. $queryAtt . ')' . 
					(($condicion != '') ? 'where ' . $condicion . ';': ';');
			
			$this->transaccion->setMensaje($query);
			$this->transaccion->grabarLog();
			
			$this->resultSet = mysql_query($query) or 
							  $this->transaccion->setMensaje('No se pudo realizar la consulta: ' . mysql_error());
			
			if($this->resultSet == null)  {
				$this->transaccion->grabarLog();				
				throw new ErrorConsultaException('No se pudo realizar la consulta');
				}		
			}
		else  {
			$this->transaccion->setMensaje('Debe especificar la tabla');
			$this->transaccion->grabarLog();
			}
		}
	
	public function actualizar($tabla, $campos, $datos, $condicion)  {
		if($tabla!='')  {
			$queryAtt = self::formatear($campos,$datos,0);
			
			$query = 'update '. $tabla . ' set ' . $queryAtt . 
					 (($condicion != '') ? 'where ' . $condicion . ';' : ';');
			
			$this->resultSet = mysql_query($query) or 
						       $this->transaccion->setMensaje('No se pudo realizar la consulta: ' . mysql_error());

			if(!isset($this->resultSet))  {
				$this->transaccion->grabarLog();
				throw new ErrorConsultaException('NO se pudo realizar la consulta');
				}
			else  {
				$this->transaccion->setMensaje($query);
				$this->transaccion->grabarLog();
				}
			}
		else  {
			$this->transaccion->setMensaje('Debe especificar la tabla');
			$this->transaccion->grabarLog();
			}
		}
	
	private function formatear($campos, $datos, $insertar)  {		
		$fields = explode(',',$campos);
		$data = explode(',',$datos);
		$queryAtt = '';			
		
		if($insertar != 1)  {
			
			for($i = 0; $i < count($fields); $i++)  {
				if(count($fields) == 1)					
					$queryAtt .= $fields[$i]."=" . ((is_string($data[$i])) ? "'" . $data[$i]."' " : $data[$i]." ");
				else if($i == count($fields))
					$queryAtt .= ' ';
				else if($i == (count($fields) - 1))
					$queryAtt .= $fields[$i]."=" . ((is_string($data[$i])) ? "'" . $data[$i]."' " : $data[$i]." ");
				else if($i <= (count($fields) - 1))
					$queryAtt .= $fields[$i]."=" . ((is_string($data[$i])) ? "'" . $data[$i]."', " : $data[$i].", ");
				}			
			}
		else  {			
			for($i = 0; $i < count($data); $i++)  {
				if(count($fields) == 1)					
					$queryAtt .= ((is_string($data[$i])) ? "'" . $data[$i]."' " : $data[$i]." ");
				else if($i == count($fields))
					$queryAtt .= ' ';
				else if($i == (count($fields) - 1))
					$queryAtt .= ((is_string($data[$i])) ? "'" . $data[$i]."' " : $data[$i]." ");
				else if($i <= (count($fields) - 1))
					$queryAtt .= ((is_string($data[$i])) ? "'" . $data[$i]."', " : $data[$i].", ");
				}			
			}	
			
		return $queryAtt;
		}
	}
?>
Return current item: Gestin