Ignore:
Timestamp:
01/28/13 13:25:41 (11 years ago)
Author:
cristiano
Message:

Ticket #3325 - Melhoria de funcionalidade na API (serviço Postgres)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/prototype/services/PostgreSQL.php

    r7673 r7795  
    4747    private $config; //Configuração 
    4848    public  $error = false; //Armazena um erro caso ocorra 
    49      
    50     public function find ( $uri, $justthese = false, $criteria = false ){ 
    51                     
    52         $map =  Config::get($uri['concept'], 'PostgreSQL.mapping'); 
    53          
    54     $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map) : ''; 
    55  
    56     $justthese = self::parseJustthese($justthese, $map); 
    57  
    58     return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' '.$criteria ); 
    59     } 
    60  
    61    public function read ( $uri, $justthese = false , $criteria = false){ 
    62     
    63       $map =  Config::get($uri['concept'], 'PostgreSQL.mapping');    
    64       $justthese = self::parseJustthese($justthese, $map); 
    65       $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; 
    66     
    67       return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .$criteria , true ); 
     49    private $maps; //Cache de maps 
     50    private $tables; 
     51 
     52    public function find ( $uri, $justthese = false, $criteria = false ) 
     53    { 
     54        $condition = ''; 
     55 
     56        if(!isset($this->maps[$uri['concept']]) || !isset($this->tables[$uri['concept']])) 
     57        { 
     58            $this->maps[$uri['concept']] = Config::get($uri['concept'], 'PostgreSQL.mapping'); 
     59            $this->tables[$uri['concept']] =  Config::get($uri['concept'],'PostgreSQL.concept'); 
     60        } 
     61 
     62        $tables = $this->tables[$uri['concept']]; 
     63 
     64        $justthese = self::parseJustthese($justthese, $this->maps[$uri['concept']] , $this->tables[$uri['concept']]); 
     65 
     66        if(isset($criteria['condition'])) 
     67        { 
     68            $pc = $this->parseCondition($criteria['condition']); 
     69 
     70            if(is_array($pc)) 
     71            { 
     72                if(!in_array($this->tables[$uri['concept']], $pc['tables'])) 
     73                    $pc['tables'][] = $this->tables[$uri['concept']]; 
     74 
     75                $tables = implode(',', $pc['tables'] ); 
     76                $condition .= ' WHERE ' . $pc['conditions']; 
     77            } 
     78 
     79        } 
     80 
     81        $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $this->maps[$uri['concept']] , $condition , $this->tables[$uri['concept']]) : $condition; 
     82 
     83        return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. $tables .' '.$criteria ); 
     84    } 
     85 
     86   public function read ( $uri, $justthese = false , $criteria = false) 
     87   { 
     88       if(!isset($this->maps[$uri['concept']]) || !isset($this->tables[$uri['concept']])) 
     89       { 
     90           $this->maps[$uri['concept']] = Config::get($uri['concept'], 'PostgreSQL.mapping'); 
     91           $this->tables[$uri['concept']] =  Config::get($uri['concept'],'PostgreSQL.concept'); 
     92       } 
     93 
     94       $condition = ' WHERE '.$this->tables[$uri['concept']].'.'.$this->maps[$uri['concept']]['id'].' = \''.addslashes( $uri['id'] ).'\''; 
     95       $justthese = self::parseJustthese($justthese, $this->maps[$uri['concept']] , $this->tables[$uri['concept']]); 
     96       $tables = $this->tables[$uri['concept']]; 
     97 
     98       if(isset($criteria['condition'])) 
     99       { 
     100           $pc = $this->parseCondition($criteria['condition']); 
     101 
     102           if(is_array($pc)) 
     103           { 
     104               if(!in_array($this->tables[$uri['concept']], $pc['tables'])) 
     105                   $pc['tables'][] = $this->tables[$uri['concept']]; 
     106 
     107               $tables = implode(',', $pc['tables'] ); 
     108               $condition .= ' AND ' .  $pc['conditions']; 
     109           } 
     110       } 
     111 
     112       $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $this->maps[$uri['concept']] , $condition , $this->tables[$uri['concept']]) : $condition; 
     113 
     114       return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. $tables . ' ' . $criteria , true ); 
    68115    } 
    69116     
     
    85132        return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); 
    86133    } 
    87          
     134 
    88135    public function update ( $uri,  $data, $criteria = false ){ 
    89136            $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); 
    90         $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; 
     137        $criteria = ($criteria !== false) ? 
     138            $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; 
    91139 
    92140        return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria); 
     
    204252     
    205253    private static function parseUpdateData( $data , &$map){ 
    206                                          
     254 
    207255        $d = array(); 
    208256        foreach ($data as $i => $v) 
     
    216264    } 
    217265 
    218     private static function parseCriteria( $criteria  , &$map , $query = '' ){                
    219      
     266    private static function parseCriteria( $criteria  , &$map , $query = '' ){ 
     267 
    220268        if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) 
    221269        { 
     
    230278          *     array( '=' , 'campo' , 'valor' ) 
    231279        */ 
    232                 $query .= ($query === '') ?  'WHERE ('.self::parseFilter( $criteria['filter'] , $map).')' : ' AND ('.self::parseFilter( $criteria['filter'] , $map).')'; 
     280               if($fc = self::parseFilter( $criteria['filter'] , $map)) 
     281                    $query .= ($query === '') ?  'WHERE ('.$fc.')' : ' AND ('.$fc.')'; 
    233282        } 
    234283        /* 
     
    323372    } 
    324373    } 
    325      
    326     private static function parseJustthese($justthese , &$map) 
    327     { 
    328                    
     374 
     375    static function parseJustthese($justthese , &$map , $table = '') 
     376    { 
     377 
    329378        if(!is_array($justthese)) //Caso seja um full select pegar todas as keys 
    330379            $justthese = array_keys($map); 
     
    332381        $return = array(); 
    333382 
    334         foreach ($justthese as &$value)  
     383        if($table) 
     384            $table .= '.'; 
     385 
     386        foreach ($justthese as &$value) 
    335387        { 
    336388            if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa 
     
    339391                $return['deepness'][$value] = $map[$value]; 
    340392            else 
    341                 $return['select'][] = $map[$value] .' as "'. $value. '"'; 
    342         } 
    343          
     393                $return['select'][] = $table . $map[$value] .' as "'. $value. '"'; 
     394        } 
     395 
    344396        $return['select'] = implode(', ', $return['select']); 
    345         return $return;   
     397        return $return; 
     398    } 
     399 
     400    private function parseCondition( $condition ) 
     401    { 
     402        $tables = array(); 
     403        $conditions = ''; 
     404 
     405            $matches = array(); 
     406            if(preg_match_all('/\s*(AND|^)\s*([a-z]+)\.([a-z]+)\s+\=\s+([a-z]+)\.([a-z]+)(\s|$)+/i', $condition ,$matches,PREG_SET_ORDER)) 
     407            { 
     408               foreach ($matches as $i => $v) 
     409               { 
     410                   if(!isset($this->maps[$v[2]]) || !isset($this->tables[$v[2]])) 
     411                   { 
     412                       $this->maps[$v[2]] = Config::get($v[2], 'PostgreSQL.mapping'); 
     413                       $this->tables[$v[2]] =  Config::get($v[2],'PostgreSQL.concept'); 
     414                   } 
     415                   if(!isset($this->maps[$v[4]]) || !isset($this->tables[$v[4]])) 
     416                   { 
     417                       $this->maps[$v[4]] = Config::get($v[4], 'PostgreSQL.mapping'); 
     418                       $this->tables[$v[4]] =  Config::get($v[4],'PostgreSQL.concept'); 
     419                   } 
     420 
     421                   if(isset($this->maps[$v[2]][$v[3]]) && isset($this->maps[$v[4]][$v[5]])) 
     422                       $conditions .= ' '. $v[1] .' '. $this->tables[$v[2]] . '.' . $this->maps[$v[2]][$v[3]] .' = '. $this->tables[$v[4]] . '.' . $this->maps[$v[4]][$v[5]]; 
     423                   else 
     424                       continue; 
     425 
     426                   if(!in_array( $this->tables[$v[2]], $tables)) 
     427                       $tables[] = $this->tables[$v[2]]; 
     428 
     429                   if(!in_array( $this->tables[$v[4]], $tables)) 
     430                       $tables[] = $this->tables[$v[4]]; 
     431               } 
     432 
     433            } 
     434 
     435            if(preg_match_all('/\s*(AND|OR|^)\s*([a-z]+)\.([a-z]+)\s+([\=\>\<\!]+|like)+\s+([a-z0-9\/\+\=]+)(\s|$)+/i', $condition , $matches ,PREG_SET_ORDER)) 
     436            { 
     437                foreach ($matches as $i => $v) 
     438                { 
     439                    if(!isset($this->maps[$v[2]]) || !isset($this->tables[$v[2]])) 
     440                    { 
     441                        $this->maps[$v[2]] = Config::get($v[2], 'PostgreSQL.mapping'); 
     442                        $this->tables[$v[2]] =  Config::get($v[2],'PostgreSQL.concept'); 
     443                    } 
     444 
     445                    if(isset($this->maps[$v[2]][$v[3]])) 
     446                        $conditions .= ' '. $v[1] .' '. $this->tables[$v[2]] . '.' . $this->maps[$v[2]][$v[3]] .' '.$v[4].' \''. pg_escape_string(base64_decode($v[5])) .'\''; 
     447                    else 
     448                        continue; 
     449 
     450                    if(!in_array( $this->tables[$v[2]], $tables)) 
     451                        $tables[] = $this->tables[$v[2]]; 
     452                } 
     453            } 
     454 
     455        return (count($tables) > 0 && count($conditions ) > 0) ? array('tables' => $tables , 'conditions' => $conditions ) : '' ; 
    346456    } 
    347457 
Note: See TracChangeset for help on using the changeset viewer.