Ignore:
Timestamp:
10/08/13 16:20:46 (11 years ago)
Author:
douglas
Message:

Ticket #0000 - Copiadas as alterações do Trunk. Versão final 2.5.1.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.5/prototype/services/PostgreSQL.php

    r7961 r8232  
    4747    private $config; //Configuração 
    4848    public  $error = false; //Armazena um erro caso ocorra 
    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 );  
     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 ); 
    115115    } 
    116116     
     
    132132        return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); 
    133133    } 
    134   
     134 
    135135    public function update ( $uri,  $data, $criteria = false ){ 
    136136            $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); 
    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'] ).'\'';  
     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'] ).'\''; 
    139139 
    140140        return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria); 
     
    252252     
    253253    private static function parseUpdateData( $data , &$map){ 
    254   
     254 
    255255        $d = array(); 
    256256        foreach ($data as $i => $v) 
     
    265265 
    266266    private static function parseCriteria( $criteria  , &$map , $query = '' ){ 
    267      
     267 
    268268        if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) 
    269269        { 
     
    278278          *     array( '=' , 'campo' , 'valor' ) 
    279279        */ 
    280                if($fc = self::parseFilter( $criteria['filter'] , $map))  
    281                     $query .= ($query === '') ?  'WHERE ('.$fc.')' : ' AND ('.$fc.')';  
     280               if($fc = self::parseFilter( $criteria['filter'] , $map)) 
     281                    $query .= ($query === '') ?  'WHERE ('.$fc.')' : ' AND ('.$fc.')'; 
    282282        } 
    283283        /* 
     
    372372    } 
    373373    } 
    374      
     374 
    375375    static function parseJustthese($justthese , &$map , $table = '') 
    376376    { 
    377                    
     377 
    378378        if(!is_array($justthese)) //Caso seja um full select pegar todas as keys 
    379379            $justthese = array_keys($map); 
     
    381381        $return = array(); 
    382382 
    383         if($table)  
    384             $table .= '.';  
    385   
    386         foreach ($justthese as &$value)  
     383        if($table) 
     384            $table .= '.'; 
     385 
     386        foreach ($justthese as &$value) 
    387387        { 
    388388            if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa 
     
    393393                $return['select'][] = $table . $map[$value] .' as "'. $value. '"'; 
    394394        } 
    395          
     395 
    396396        $return['select'] = implode(', ', $return['select']); 
    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 ) : '' ;  
     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 ) : '' ; 
    456456    } 
    457457 
Note: See TracChangeset for help on using the changeset viewer.