Changeset 7795 for trunk/prototype/services/PostgreSQL.php
- Timestamp:
- 01/28/13 13:25:41 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/prototype/services/PostgreSQL.php
r7673 r7795 47 47 private $config; //Configuração 48 48 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 ); 68 115 } 69 116 … … 85 132 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); 86 133 } 87 134 88 135 public function update ( $uri, $data, $criteria = false ){ 89 136 $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'] ).'\''; 91 139 92 140 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria); … … 204 252 205 253 private static function parseUpdateData( $data , &$map){ 206 254 207 255 $d = array(); 208 256 foreach ($data as $i => $v) … … 216 264 } 217 265 218 private static function parseCriteria( $criteria , &$map , $query = '' ){ 219 266 private static function parseCriteria( $criteria , &$map , $query = '' ){ 267 220 268 if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) 221 269 { … … 230 278 * array( '=' , 'campo' , 'valor' ) 231 279 */ 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.')'; 233 282 } 234 283 /* … … 323 372 } 324 373 } 325 326 private static function parseJustthese($justthese , &$map)327 { 328 374 375 static function parseJustthese($justthese , &$map , $table = '') 376 { 377 329 378 if(!is_array($justthese)) //Caso seja um full select pegar todas as keys 330 379 $justthese = array_keys($map); … … 332 381 $return = array(); 333 382 334 foreach ($justthese as &$value) 383 if($table) 384 $table .= '.'; 385 386 foreach ($justthese as &$value) 335 387 { 336 388 if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa … … 339 391 $return['deepness'][$value] = $map[$value]; 340 392 else 341 $return['select'][] = $ map[$value] .' as "'. $value. '"';342 } 343 393 $return['select'][] = $table . $map[$value] .' as "'. $value. '"'; 394 } 395 344 396 $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 ) : '' ; 346 456 } 347 457
Note: See TracChangeset
for help on using the changeset viewer.