Changeset 8232 for branches/2.5/prototype/services/PostgreSQL.php
- Timestamp:
- 10/08/13 16:20:46 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.5/prototype/services/PostgreSQL.php
r7961 r8232 47 47 private $config; //Configuração 48 48 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 ); 115 115 } 116 116 … … 132 132 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); 133 133 } 134 134 135 135 public function update ( $uri, $data, $criteria = false ){ 136 136 $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'] ).'\''; 139 139 140 140 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria); … … 252 252 253 253 private static function parseUpdateData( $data , &$map){ 254 254 255 255 $d = array(); 256 256 foreach ($data as $i => $v) … … 265 265 266 266 private static function parseCriteria( $criteria , &$map , $query = '' ){ 267 267 268 268 if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) 269 269 { … … 278 278 * array( '=' , 'campo' , 'valor' ) 279 279 */ 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.')'; 282 282 } 283 283 /* … … 372 372 } 373 373 } 374 374 375 375 static function parseJustthese($justthese , &$map , $table = '') 376 376 { 377 377 378 378 if(!is_array($justthese)) //Caso seja um full select pegar todas as keys 379 379 $justthese = array_keys($map); … … 381 381 $return = array(); 382 382 383 if($table) 384 $table .= '.'; 385 386 foreach ($justthese as &$value) 383 if($table) 384 $table .= '.'; 385 386 foreach ($justthese as &$value) 387 387 { 388 388 if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa … … 393 393 $return['select'][] = $table . $map[$value] .' as "'. $value. '"'; 394 394 } 395 395 396 396 $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 ) : '' ; 456 456 } 457 457
Note: See TracChangeset
for help on using the changeset viewer.