Ver Mensaje Individual
  #16  
Antiguo 03-04-2011
Avatar de movorack
[movorack] movorack is offline
Miguel A. Valero
 
Registrado: feb 2007
Ubicación: Bogotá - Colombia
Posts: 1.346
Reputación: 22
movorack Va camino a la famamovorack Va camino a la fama
mmm no se...

Ahoritica me encuentro trabajando en un proyecto para la web y dada mi poca experiencia en otros lenguajes opté por PHP ya que necesitaba actuar raoidamente en este proyecto.

No creo que sea para hacer "paginitas"... algunos ignorantes dirian que delphi solo es para hacer "programitas"... pero que le vamos a hacer... ahi quienes comen moco y se esconden debajo de la cama pa'que no les pidan.

esta clase la hice en estos dias... me sirve para abstraer a php una tabla y ejecutar las funciones mas básicas (insertar, actualizar, eliminar) <<depende de otras clases>>

Código PHP:
class tge_dbtable {
    const 
CAN_GET_ALL = TRUE;
    const 
CANT_GET_ALL = FALSE;
    const 
SEPARATOR_DEFAULT = ",";
    const 
SEPARATOR_AND = "and";
    const 
ASSIGN = TRUE;
    const 
NOT_ASSIGN = FALSE;
    
    protected 
$db;
    protected 
$table_name;
    protected 
$table_alias;
    protected 
$sql;        
    protected 
$fields;
    protected 
$registros;
    
    public 
$listado;
    public 
$join;
    
    public function 
__construct($table_name, $table_alias = "t0"){
        
$this->db = mysql_layer::getInstance(DB_LINK);
        
$this->registros = new mysql_recordset(false);
        
$this->listado = new mysql_recordset(false);
        
        
$this->table_name = $table_name;
        
$this->table_alias = $table_alias;
        
        
$result = $this->exec_query("SHOW COLUMNS FROM {$this->table_name}");
        while (
$field = mysql_fetch_object($result)){
            
$fieldname = $field->Field;
            
$this->fields[] = $this->table_alias.".".$fieldname;
            
$this->$fieldname = $field->Default;            
        }
    }
    
    protected function 
get_fields($fields, $cangetall = self::CAN_GET_ALL, $assign = self::NOT_ASSIGN, $separator = self::SEPARATOR_DEFAULT){
        
$sql = "";
        if (
is_string($fields)){
            if (
strlen($fields) > 0){
                if (!
$assign) $fields = explode(",", $fields);
                else 
$sql .= $fields; 
            } else {
                if (
$cangetall) $sql .= "* ";
            }
        }        
        if (
is_array($fields)){
            
$fieldcount = count($fields);
            
$i = 1;
            if (!
$assign){
                foreach (
$fields as $field) {
                    
$sql .= trim($field);
                    if (
$i < $fieldcount) $sql .= "{$separator} ";
                    
$i++;
                }
            } else {
                foreach (
$fields as $field => $value) {                    
                    
$field = trim($field);
                    
$value = trim($value);
                    
$sql .= "{$field} = '{$value}'";
                    if (
$i < $fieldcount) $sql .= "{$separator} ";
                    
$i++;
                }                
            }
        }

        return 
$sql;
    }
    
    private function 
get_values($values, $separator = self::SEPARATOR_DEFAULT){
        
$sql = "";
        if (
is_string($values)){
            if (
strlen($values) > 0){
                
$values = explode(",", $values); 
            } else {
                if (
$cangetall) $sql .= "";
            }
        }        
        if (
is_array($values)){
            
$pattern = "/'(.*?)'/";            
            
$count = count($values);
            
$i = 1;
            
            foreach (
$values as $value) {                    
                
$value = trim($value);
                
$match = array(); $pm = preg_match($pattern, $value, $match);
                if (
$pm) $value = $match[1];
                
                if(
is_string($value)) $value = "'{$value}'";
                
                
$sql .= "{$value}";
                if (
$i < $count) $sql .= "{$separator} ";
                
$i++;
            }                
        }

        return 
$sql;
    }
    
    private function 
get_where($where, $operator = self::SEPARATOR_AND){
        
$sql = "";
        if (
is_string($where)){
            if (
strlen($where) > 0){
                
$sql .= " where ". $where . " "; 
            } else {
                
$sql .= " ";
            }
        }
        if (
is_array($where)){
            
$i = 0;
            foreach (
$where as $key => $value) {
                
$pattern = "/#(.*?)#/"; $condition = "";    

                if (
$i == 0) $condition = " where ";
                else  {
                    
$operator = $operator;
                    
$matches = array(); $pm = preg_match($pattern, $key, $matches);
                    if (
$pm) {
                        
$key = str_replace($matches[0], "", $key);
                        
$operator = $matches[1];
                    }                
                    
$condition = $operator . " ";    
                }
                
                
$matches = array(); $pm = preg_match($pattern, $value, $matches);                
                
$operator = "=";
                if (
$pm) {
                    
$value = str_replace($matches[0], "", $value);
                    
$operator = $matches[1];
                }
                
                
$condition .= "{$key} {$operator} '{$value}' ";
                
$sql .= $condition;
                
                
$this->$key = $value;
                
$i++;
            }
        }
        
        return 
$sql;
    }
    
    private function 
get_join($joins = array()){
        
$sql = "";
        
        foreach (
$joins as $join) {
            
//$sql .= " {$type} join {$condition} ";
            
if (is_object($join) && get_class($join) == "tge_join_data"){
                
$join = new tge_join_data($join->type, $join->table, $join->alias, $join->conditions);
                
                
//get join fields
                
$result = $this->exec_query("SHOW COLUMNS FROM {$join->table}");
                while (
$field = mysql_fetch_object($result)){
                    
$fieldname = $field->Field;
                    
$this->fields[] = $join->alias .".". $fieldname;            
                }
                
                
$conditions = "";
                if (
is_string($join->conditions)) $conditions = $join->conditions;
                else {
                    
$i = 0;
                    foreach (
$join->conditions as $condition) {
                        if (
$i > 0) {
                            
$pattern = "/#(.*?)#/"; $operator = "and";
                            
$matches = array(); $pm = preg_match($pattern, $condition, $matches);                            
                            if (
$pm) {
                                
$condition = str_replace($matches[0], "", $condition);
                                
$operator = $matches[1];
                            }
                            
$conditions .= " {$operator} ";
                        }
                        
$conditions .= $condition;
                        
$i++;
                    }
                }
                                
                
$sql .= " {$join->type} join {$join->table} {$join->alias} on $conditions";
            }
        }
        
        return 
$sql;
    }
    
    
/**
     * Obtener registros de una tabla.
     * @param string / array $fields
     * @param string / array $where
     * @param string / array $limit 
     * <br/>
     * <br/>
     * <b>$fields [string]</b> ej. "field1, field2".<br/> 
     * <b>$fields [array()]</b> ej. array("field1", "field2")<br/>
     * <b>$where [string]</b> ej. "field1 = value and field2 = value".<br/> 
     * <b>$where [array()]</b> ej. array("field1"=>"value", "field2"=>"value")<br/>
     * <b>Filtro en $where:</b> array("<font color="red"><b>#or#</b></font>field1"=>"<font color="red"><b>#like#</b></font>value")<br/>
     * <b>$limit [string]</b> ej. "0, 30".<br/>
     */
    
protected function get_registros($fields = "", $where = "", $extra = ""){
        
$sql = "select ";
        
        
//Get JOIN
        
$join = ""; if (is_array($this->join)) $join .= $this->get_join($this->join);
        
        
//Fields
        
$fields = $this->fields;
        
$sql .= $this->get_fields($fields);        
        
        
//FROM
        
$sql .= " from {$this->table_name} {$this->table_alias} ";
        
        
//WHERE
        
$sql .= $this->get_where($where);
        
        
//ADD - JOIN
        
if ($join != "") $sql .= $join;
        
        
//EXTA (ORDER BY, LIMITS)
        
if (is_string($extra)) if (strlen($extra) > 0) $sql .= " {$extra} ";    
        
        
$result = $this->exec_query($sql);
        
        
$this->registros = new mysql_recordset($result);
        
$this->listado = $this->registros;
        
        return 
$this->registros;
    }    
    public function 
get_listado(){ return $this->listado; }
    
    
    protected function 
exec_query($query){
        
//CLEAN - TRIM
        
for ($j = 0; $j < 10; $j++) {
            
$query = str_replace("  ", " ", $query);    
            
$query = str_replace("\n", " ", $query);
            
$query = str_replace("\t", " ", $query);
        }    
        
        
$this->sql = $query;
        
$result = $this->db->query($query);
        return 
$result;
    }
    
    public function 
clean_alias($fieldname){
        
$fieldname = str_ireplace($this->table_alias.".", "", $fieldname);
        return 
$fieldname;
    }
    
    protected function 
get_update($values, $where){
        
$query = "update {$this->table_name} set ";
        
$query .= $this->get_fields($values, self::CANT_GET_ALL, self::ASSIGN);
        
$query .= $this->get_where($where);
        
        return 
$query;
    }    
    public function 
update($values, $where){
        
$query = $this->get_update($values, $where);
        
$exec = $this->exec_query($query);
        return 
$exec;
    }
    
    protected function 
get_insert($fields, $values){
        
$query = "insert into {$this->table_name} ";
        
$query .= "(". $this->get_fields($fields, self::CANT_GET_ALL) .") ";
        
$query .= "values (". $this->get_values($values) .") ";
        
        return 
$query;
    }
    public function 
insert($fields, $values){
        
$query = $this->get_insert($fields, $values);
        
$exec = $this->exec_query($query);
        return 
$exec;
    }
    
    protected function 
get_delete($where = ""){
        
$query = "delete from {$this->table_name} ";
        
$query .=  $this->get_where($where);
        
        return 
$query;
    }
    public function 
delete($where = ""){
        
$query = $this->get_delete($where);
        
$exec = $this->exec_query($query);
        return 
$exec;
    }
} 
y su implementación a la hora de trabajar es bastante sencilla...

Código PHP:
class tge_clientes extends tge_dbtable {
    public function 
__construct(){
        
parent::__construct("tgv_cli_clientes");
    }
    
    public function 
get_listado(){
        
parent::get_registros($this->fields);
        return 
$this->listado;
    }
} 
no se no se... talvéz no aporte mucho pero PHP me ha dado buenos y excelentes resultados... nno conozco ni ruby ni pithon ni perl y no puedo opinar nada de ellos.. se que tienen buena pinta pero mientras logre conseguir lo que quiero con PHP para que aventurarme hasta aquellos lados.

El proyecto actual es para el control de distribución, manejo de clientes, mantenimientos etc de una marca de filtros de motor en todo el pais... no se no le veo nada de "paginita" (ya que estamos poco diplomaticos)

__________________
Buena caza y buen remar... http://mivaler.blogspot.com
Responder Con Cita