Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/01/2005, 14:27
Avatar de Reynier
Reynier
 
Fecha de Ingreso: noviembre-2002
Ubicación: Por ahí en algún sitio
Mensajes: 1.844
Antigüedad: 22 años, 2 meses
Puntos: 1
Clase para usar con BDatos Parte1

Bueno está fue una clase que me encontré un poco incompleta en Internet y la bajé y le hice modificaciones para que trabajara con MSSQL. Estoy estudiando un poco más para poder extenderla a Oracle y a Firebird. La tengo que picar en partes porque el Foro no me deja publicar mensajes muy extensos.
Parte 1: Fijarse que todo pertenece a la clase EasySQL por lo que el parentesis de la clase EasySQL cierra al final de todo.
Código PHP:
<?php
 
class EasySQL{
  var 
$SQLDatabase// (objeto)   Tipo Mixto. Puede tener instancias diferentes dependiendo del SGBD usado
  
var $SQLQuery;    // (objeto)   Crea consultas SQL de tipo: SQLQuery
  
var $connection;  // (recurso)  Conección con la BDatos
  
var $result;      // (recurso)  Recurso identificativo retornado por la clausula SELECT, o booleano si las clausulas fueron INSERT, UPDATE o DELETE
  
var $total_rows;  // (valor)    Número de filas seleccionadas o afectadas (INSERT, UPDATE, DELETE) por la última consulta ejecutada

  
function EasySQL($SQLDatabase ""$host ""$user ""$password ""$db_name ""$port ""){
   
$_default['SQLDatabase'] = "";          //  SGBD por defecto (Ejemplo: "MySQL", "PostGreSQL", "SQLite", "SQL")
   
$_default['host']        = "localhost"//  Servidor o Host donde se haya hospedada la BDatos
   
$_default['user']        = "root";      //  Usuario de conexión a la BDatos
   
$_default['password']    = "";          //  Contraseña para acceder a la BDatos
   
$_default['db_name']     = "";          //  BDatos que será seleccionada
   
$_default['port']        = "";          //  Puerto de acceso a el SGBD. Dejar en blanco para usar el que trae la clase por defecto
   
   
$SQLDatabase = ($SQLDatabase != "")  ?  (string)$SQLDatabase  :  $_default['SQLDatabase'];
   
$host        = ($host != "")         ?  (string)$host         :  $_default['host'];
   
$user        = ($user != "")         ?  (string)$user         :  $_default['user'];
   
$password    = ($password != "")     ?  (string)$password     :  $_default['password'];
   
$db_name     = ($db_name != "")      ?  (string)$db_name      :  $_default['db_name'];
   
$port        = ($port != "")         ?  (int)$port            :  $_default['port'];
   
   
$this->SQLQuery = new SQLQuery();
   if(
preg_match("/^(0|mysql)$/i",trim($SQLDatabase))){
   
// MySQL
    
if (!function_exists("mysql_connect")){
     
$this->error("Librería MySQL no encontrada.");
     return 
false;
    }else if(!
class_exists("MySQL")){
     
$this->error("No se ha podido cargar la clase <strong>MySQL</strong>. Abortando <strong>" __CLASS__ "</strong> ejecución de la clase.");
     return 
false;
    }
    
$this->SQLDatabase = new MySQL($host$user$password$db_name$port);
   }else if(
preg_match("/^(1|pg|postgre(s|sql)?)$/i",trim($SQLDatabase))){
   
// PostGreSQL
    
if (!function_exists("pg_connect")){
     
$this->error("Librería PostGreSQL no encontrada.");
     return 
false;
    }else if (!
class_exists("PostGreSQL")){
     
$this->error("No se ha podido cargar la clase <strong>PostGreSQL</strong>. Abortando ejecución de la clase <strong>" __CLASS__ "</strong>.");
     return 
false;
    }
     
$this->SQLDatabase = new PostGreSQL($host$user$password$db_name$port);
   }else if (
preg_match("/^(2|sqlite)$/i",trim($SQLDatabase))){
   
// SQLite
    
if (!function_exists("sqlite_open")){
     
$this->error("Librería SQLite no encontrada.");
     return 
false;
    }else if (!
class_exists("SQLite")){
     
$this->error("No se ha podido cargar la clase <strong>SQLite</strong>. Abortando ejecución de la clase <strong>" __CLASS__ "</strong>.");
     return 
false;
    }
     
$this->SQLDatabase = new SQLite($host$user$password$db_name$port);
   }else if (
preg_match("/^(2|sql)$/i",trim($SQLDatabase))){
   
// SQL Server
    
if (!function_exists("sql_open")){
     
$this->error("Librería SQL no encontrada.");
     return 
false;
    }else if (!
class_exists("SQL")){
     
$this->error("No se ha podido cargar la clase <strong>SQL</strong>. Abortando ejecución de la clase <strong>" __CLASS__ "</strong>.");
     return 
false;
    }
     
$this->SQLDatabase = new SQL($host$user$password$db_name$port);
   }else{
    
$this->error("No se ha definido el tipo de Bases de Datos! Abortando ejecución de la clase <strong>" __CLASS__ "</strong>.");
    return 
false;
   }

   
$this->connection false;
   
$this->result     false;
   
$this->total_rows 0;
  }

  function 
connect($db_name ""){
   
$db_name = ($db_name != "")?$db_name:$this->SQLDatabase->db_name;
   
$this->SQLDatabase->connect($this->connection$db_namefalse);
   return 
$this->connection;
  }

  function 
pconnect($db_name ""){
   
$db_name = ($db_name != "")?$db_name:$this->SQLDatabase->db_name;
   
$this->SQLDatabase->connect($this->connection$db_nametrue);
   return 
$this->connection;
  }

  function 
disconnect(){
   return 
$this->SQLDatabase->disconnect($this->connection);
  }

  function 
close(){
   return 
$this->disconnect();
  }

  function 
query($query){
   
$was_connected    true;
   
$this->result     false;
   
$this->total_rows 0;
   if(!
$this->connection){
    
$was_connected false;
     if(!
$this->connect()){
      
$this->error();
      return 
false;
     }
   }
   
$this->result $this->SQLDatabase->query($query$this->connection);
   if(!
$this->result){
    
$this->error();
    return 
false;
   }
   if(
is_bool($this->result)){
    
$this->total_rows $this->SQLDatabase->affectedRows($this->result$this->connection);
   }
   if(!
$was_connected){
    
$this->disconnect();
   }
   return 
$this->result;
  }

  function 
fetchArray($result=""){
   if(
$result==""){
    
$result = &$this->result;
   }
   return 
$this->SQLDatabase->fetchArray($result);
  }

  function 
fetchRow($result=""){
   if(
$result==""){
    
$result = &$this->result;
   }
   return 
$this->SQLDatabase->fetchRow($result);
  }

  function 
fetchAssoc($result=""){
   if (
$result==""){
    
$result = &$this->result;
   }
   return 
$this->SQLDatabase->fetchAssoc($result);
  }

  function 
error($message=""){
   if(
$message==""){
    
$message $this->SQLDatabase->error($this->connection);
   }
   echo 
'<br /><span style="padding: 1px 7px 1px 7px; background-color: #ffd7d7; font-family: verdana; color: #000000; font-size: 13px;"><span style="color: #ff0000; font-weight: bold;">Error!</span> ' $message '</span><br />';
  }

  function 
useDatabase($db_name){
   
$this->SQLDatabase->db_name = (string)$db_name;
  }

  function 
useTable($table){
   
$this->SQLQuery->table = (string)$table;
  }

  function 
setFieldValue($field,$value,$is_sql_function=false){
   
$field = (string)$this->SQLDatabase->escape($field);
   
$value = (string)$this->SQLDatabase->escape($value);
   
$this->SQLQuery->values[$field]['value']           = $value;
   
$this->SQLQuery->values[$field]['is_sql_function'] = (bool)$is_sql_function;
  }

  function 
setWhere($where){
   
$this->SQLQuery->where = (string)$where;
  }

  function 
setLimit($limit){
   
$this->SQLQuery->limit = (string)$limit;
  }

  function 
setSelection($selection){
   
$this->SQLQuery->selection = (string)$selection;
  }

  function 
select(){
   
$no_limit_query $this->SQLQuery->createSelect(false);
   
$return_query   $this->SQLQuery->createSelect();
   
$result         $this->query($return_query);
   if(!
$result){
    return 
false;
   }
   
$this->total_rows $this->SQLDatabase->numRows($this->query($no_limit_query));
   return 
$result;
  }

  function 
Insert(){
   return 
$this->query($this->SQLQuery->createInsert());
  }

  function 
Update(){
   return 
$this->query($this->SQLQuery->createUpdate());
  }

  function 
Delete(){
   return 
$this->query($this->SQLQuery->createDelete());
  }

  function 
Truncate(){
   return 
$this->query($this->SQLQuery->createTruncate());
  }
 }
__________________
Ing. Reynier Pérez Mira