[6528] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2012 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 5 | * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it under |
---|
| 7 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
| 8 | * the Free Software Foundation with the addition of the following permission |
---|
| 9 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
| 10 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
| 11 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 15 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 16 | * details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU Affero General Public License |
---|
| 19 | * along with this program; if not, see www.gnu.org/licenses or write to |
---|
| 20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
| 21 | * MA 02110-1301 USA. |
---|
| 22 | * |
---|
| 23 | * This code is based on the OpenXchange Connector and on the Prognus pSync |
---|
| 24 | * Connector both developed by the community and licensed under the GPL |
---|
| 25 | * version 2 or above as published by the Free Software Foundation. |
---|
| 26 | * |
---|
| 27 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
| 28 | * 6731, PTI, Edifício do Saber, 3º floor, room 306, Foz do Iguaçu - PR - Brasil or at |
---|
| 29 | * e-mail address prognus@prognus.com.br. |
---|
| 30 | * |
---|
| 31 | * Classe de abstração que implementa métodos de manipulação de banco de dados |
---|
| 32 | * executando instruções SQL a partir de parâmetros passados pelos métodos. |
---|
| 33 | * |
---|
| 34 | * @package Prototype |
---|
| 35 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 36 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 37 | * @version 2.4 |
---|
| 38 | * @sponsor Caixa Econômica Federal |
---|
| 39 | * @since Arquivo disponibilizado na versão 2.4 |
---|
| 40 | */ |
---|
| 41 | |
---|
| 42 | use prototype\api\Config as Config; |
---|
| 43 | |
---|
| 44 | class PostgreSQL implements Service |
---|
| 45 | { |
---|
| 46 | private $con; //Conexão com o banco de dados |
---|
| 47 | private $config; //Configuração |
---|
| 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 | |
---|
[6962] | 54 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map) : ''; |
---|
[6528] | 55 | |
---|
[6962] | 56 | $justthese = self::parseJustthese($justthese, $map); |
---|
[6528] | 57 | |
---|
[6962] | 58 | return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' '.$criteria ); |
---|
[6528] | 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); |
---|
[6962] | 65 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; |
---|
[6528] | 66 | |
---|
| 67 | return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .$criteria , true ); |
---|
| 68 | } |
---|
[6962] | 69 | |
---|
[6528] | 70 | public function deleteAll ( $uri, $justthese = false, $criteria = false ){ |
---|
| 71 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
[6962] | 72 | if(!self::parseCriteria ( $criteria , $map)) return false; //Validador para não apagar tabela inteira |
---|
| 73 | return $this->execSql( 'DELETE FROM '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '.self::parseCriteria ( $criteria ,$map) ); |
---|
[6528] | 74 | } |
---|
| 75 | |
---|
| 76 | public function delete ( $uri, $justthese = false, $criteria = false ){ |
---|
| 77 | if(!isset($uri['id']) && !is_int($uri['id'])) return false; //Delete chamado apenas passando id inteiros |
---|
[6962] | 78 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 79 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; |
---|
| 80 | return $this->execSql('DELETE FROM '.(Config::get($uri['concept'],'PostgreSQL.concept')).$criteria); |
---|
[6528] | 81 | } |
---|
| 82 | |
---|
| 83 | public function replace ( $uri, $data, $criteria = false ){ |
---|
| 84 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
[6962] | 85 | return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); |
---|
[6528] | 86 | } |
---|
[6962] | 87 | |
---|
[6528] | 88 | public function update ( $uri, $data, $criteria = false ){ |
---|
| 89 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
[6962] | 90 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\''; |
---|
[6528] | 91 | |
---|
[6962] | 92 | return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria); |
---|
[6528] | 93 | } |
---|
| 94 | |
---|
[6962] | 95 | public function create ( $uri, $data ){ |
---|
| 96 | return $this->execSql( 'INSERT INTO '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '.self::parseInsertData( $data , $uri['concept'] ), true ); |
---|
[6528] | 97 | } |
---|
| 98 | |
---|
| 99 | public function execSql( $sql, $unique = false ) |
---|
| 100 | { |
---|
[6962] | 101 | if(!$this->con) $this->open( $this->config ); |
---|
[6528] | 102 | |
---|
[6962] | 103 | $rs = pg_query( $this->con, $sql ); |
---|
[6528] | 104 | |
---|
[6962] | 105 | switch( pg_num_rows( $rs ) ) |
---|
| 106 | { |
---|
| 107 | case -1: |
---|
| 108 | $this->error = pg_last_error ( $this->con ); |
---|
| 109 | return( false ); |
---|
[6528] | 110 | |
---|
[6962] | 111 | case 0: |
---|
| 112 | return( pg_affected_rows( $rs ) ? true : array() ); |
---|
[6528] | 113 | |
---|
[6962] | 114 | default: |
---|
| 115 | $return = array(); |
---|
[6528] | 116 | |
---|
[6962] | 117 | while( $row = pg_fetch_assoc( $rs ) ) |
---|
| 118 | $return[] = $row; |
---|
[6528] | 119 | |
---|
[6962] | 120 | return( $unique ? $return[0] : $return ); |
---|
| 121 | } |
---|
[6528] | 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | //@DEPRECATED |
---|
| 126 | public function execResultSql( $sql, $unique = false ){ |
---|
[6962] | 127 | return $this->execSql( $sql, $unique ); |
---|
[6528] | 128 | } |
---|
| 129 | |
---|
| 130 | public function begin( $uri ) { |
---|
| 131 | |
---|
[6962] | 132 | if(!$this->con) |
---|
| 133 | $this->open( $this->config ); |
---|
[6528] | 134 | |
---|
| 135 | $this->error = false; |
---|
[6962] | 136 | pg_query($this->con, "BEGIN WORK"); |
---|
[6528] | 137 | } |
---|
| 138 | |
---|
| 139 | public function commit($uri ) { |
---|
| 140 | |
---|
[6962] | 141 | if( $this->error !== false ) |
---|
| 142 | { |
---|
| 143 | $error = $this->error; |
---|
| 144 | $this->error = false; |
---|
[6528] | 145 | |
---|
[6962] | 146 | throw new Exception( $error ); |
---|
| 147 | } |
---|
[6528] | 148 | |
---|
[6962] | 149 | pg_query($this->con, "COMMIT"); |
---|
[6528] | 150 | |
---|
[6962] | 151 | return( true ); |
---|
[6528] | 152 | } |
---|
| 153 | |
---|
| 154 | public function rollback( $uri ){ |
---|
| 155 | |
---|
[6962] | 156 | pg_query($this->con, "ROLLBACK"); |
---|
[6528] | 157 | } |
---|
| 158 | |
---|
| 159 | public function open ( $config ){ |
---|
[6962] | 160 | |
---|
[6528] | 161 | $this->config = $config; |
---|
| 162 | |
---|
| 163 | $rs = ''; |
---|
| 164 | $rs .= ( isset($this->config['host']) && $this->config['host'] ) ? ' host='.$this->config['host'] : '' ; |
---|
| 165 | $rs .= ( isset($this->config['user']) && $this->config['user'] ) ? ' user='.$this->config['user'] : '' ; |
---|
| 166 | $rs .= ( isset($this->config['password']) && $this->config['password'] ) ? ' password='.$this->config['password'] : '' ; |
---|
| 167 | $rs .= ( isset($this->config['dbname']) && $this->config['dbname'] ) ? ' dbname='.$this->config['dbname'] : '' ; |
---|
| 168 | $rs .= ( isset($this->config['port']) && $this->config['port'] ) ? ' port='.$this->config['port'] : '' ; |
---|
| 169 | |
---|
[6962] | 170 | if($this->con = pg_connect( $rs )) |
---|
| 171 | return $this->con; |
---|
[6528] | 172 | |
---|
[6962] | 173 | throw new Exception('It was not possible to enable the target connection!'); |
---|
| 174 | //$this->con = pg_connect('host='.$config['host'].' user='.$config['user'].' password='.$config['password'].' dbname='.$config['dbname'].' options=\'--client_encoding=UTF8\''); |
---|
[6528] | 175 | } |
---|
| 176 | |
---|
| 177 | public function close(){ |
---|
| 178 | |
---|
[6962] | 179 | pg_close($this->con); |
---|
[6528] | 180 | |
---|
| 181 | $this->con = false; |
---|
| 182 | |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | public function setup(){} |
---|
| 186 | |
---|
| 187 | public function teardown(){} |
---|
| 188 | |
---|
| 189 | private static function parseInsertData( $data , $concept){ |
---|
[6962] | 190 | |
---|
[6528] | 191 | $map = Config::get($concept, 'PostgreSQL.mapping'); |
---|
| 192 | |
---|
[6962] | 193 | $ind = array(); |
---|
| 194 | $val = array(); |
---|
| 195 | |
---|
| 196 | foreach ($data as $i => $v){ |
---|
[6528] | 197 | if(!isset($map[$i])) continue; |
---|
| 198 | |
---|
[6962] | 199 | $ind[] = $map[$i]; |
---|
| 200 | $val[] = '\''.pg_escape_string($v).'\''; |
---|
| 201 | } |
---|
| 202 | return '('.implode(',', $ind).') VALUES ('.implode(',', $val).') RETURNING '.$map['id'].' as id'; |
---|
[6528] | 203 | } |
---|
[6962] | 204 | |
---|
[6528] | 205 | private static function parseUpdateData( $data , &$map){ |
---|
[6962] | 206 | |
---|
| 207 | $d = array(); |
---|
| 208 | foreach ($data as $i => $v) |
---|
[6528] | 209 | { |
---|
| 210 | if(!isset($map[$i])) continue; |
---|
| 211 | |
---|
[6962] | 212 | $d[] = $map[$i].' = \''.pg_escape_string ($v).'\''; |
---|
[6528] | 213 | } |
---|
[6962] | 214 | |
---|
| 215 | return 'SET '.implode(',', $d); |
---|
[6528] | 216 | } |
---|
| 217 | |
---|
| 218 | private static function parseCriteria( $criteria , &$map , $query = '' ){ |
---|
[6962] | 219 | |
---|
| 220 | if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) |
---|
| 221 | { |
---|
| 222 | /* |
---|
| 223 | * ex: array ( |
---|
| 224 | * [0] 'OR', |
---|
| 225 | * [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ), |
---|
| 226 | * [2] array( '=', 'campo' , 'valor' ), |
---|
| 227 | * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) |
---|
| 228 | * ) |
---|
| 229 | * OR |
---|
| 230 | * array( '=' , 'campo' , 'valor' ) |
---|
| 231 | */ |
---|
[6528] | 232 | $query .= ($query === '') ? 'WHERE ('.self::parseFilter( $criteria['filter'] , $map).')' : ' AND ('.self::parseFilter( $criteria['filter'] , $map).')'; |
---|
[6962] | 233 | } |
---|
| 234 | /* |
---|
| 235 | * ex: array( 'table1' => 'table2' , 'table1' => 'table2') |
---|
| 236 | * |
---|
| 237 | */ |
---|
| 238 | if( isset($criteria["join"]) ) |
---|
| 239 | { |
---|
| 240 | foreach ($criteria["join"] as $i => $v) |
---|
| 241 | $query .= ' AND '.$i.' = '.$v.' '; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | if( isset($criteria["group"]) ) |
---|
| 245 | { |
---|
| 246 | $query .= ' GROUP BY '.( is_array($criteria["group"]) ? implode(', ', $criteria["group"]) : $criteria["group"] ).' '; |
---|
| 247 | } |
---|
[6528] | 248 | |
---|
[6962] | 249 | if( isset($criteria["order"]) ) |
---|
| 250 | { |
---|
| 251 | //Verificar se os atributos para o ORDER BY serao ordenados em ordem decrescente [DESC] |
---|
| 252 | $orderDesc = ( isset($criteria["orderDesc"]) && count($criteria["order"]) == count($criteria["orderDesc"]) ) ? $criteria["orderDesc"] : false; |
---|
| 253 | |
---|
| 254 | $query .= ' ORDER BY '.self::parseOrder( $criteria["order"], $map, $orderDesc ).' '; |
---|
| 255 | |
---|
| 256 | } |
---|
[6528] | 257 | |
---|
[6962] | 258 | if( isset($criteria["limit"]) ) |
---|
| 259 | { |
---|
| 260 | $query .= ' LIMIT '. $criteria["limit"] .' '; |
---|
| 261 | } |
---|
| 262 | if( isset($criteria["offset"]) ) |
---|
| 263 | { |
---|
| 264 | $query .= ' OFFSET '. $criteria["offset"] .' '; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | return $query; |
---|
[6528] | 268 | } |
---|
| 269 | |
---|
| 270 | private static function parseFilter( $filter ,&$map){ |
---|
| 271 | |
---|
[6962] | 272 | if( !is_array( $filter ) || count($filter) <= 0) return null; |
---|
[6528] | 273 | |
---|
[6962] | 274 | $op = self::parseOperator( array_shift( $filter ) ); |
---|
[6528] | 275 | |
---|
[6962] | 276 | if( is_array($filter[0]) ) |
---|
| 277 | { |
---|
| 278 | $nested = array(); |
---|
[6528] | 279 | |
---|
[6962] | 280 | foreach( $filter as $i => $f ) |
---|
[6528] | 281 | if( $n = self::parseFilter( $f , $map)) |
---|
| 282 | $nested[] = $n; |
---|
| 283 | |
---|
| 284 | |
---|
[6962] | 285 | return (count($nested) > 0 ) ? '('.implode( ' '.$op.' ', $nested ).')' : ''; |
---|
| 286 | } |
---|
[6528] | 287 | |
---|
| 288 | if(!isset($map[$filter[0]])) return ''; |
---|
| 289 | |
---|
| 290 | $filter[0] = $map[$filter[0]]; |
---|
| 291 | |
---|
[6962] | 292 | $igSuffix = $igPrefix = ''; |
---|
[6528] | 293 | |
---|
[6962] | 294 | if( strpos( $op[0], 'i' ) === 0 ) |
---|
| 295 | { |
---|
| 296 | $op[0] = substr( $op[0], 1 ); |
---|
| 297 | $filter[0] = 'upper("'.$filter[0].'")'; |
---|
| 298 | $igPrefix = 'upper('; |
---|
| 299 | $igSuffix = ')'; |
---|
| 300 | } |
---|
[6528] | 301 | |
---|
[6962] | 302 | if( is_array($filter[1]) ) |
---|
| 303 | return( $filter[0].' '.$op[0]." ($igPrefix'".implode( "'$igSuffix,$igPrefix'", array_map("pg_escape_string" , $filter[1]) )."'$igSuffix)" ); |
---|
[6528] | 304 | |
---|
[6962] | 305 | return( $filter[0].' '.$op[0]." $igPrefix'".$op[1].pg_escape_string( $filter[1] ).$op[2]."'$igSuffix" ); |
---|
[6528] | 306 | } |
---|
| 307 | |
---|
| 308 | private static function parseOperator( $op ){ |
---|
| 309 | |
---|
[6962] | 310 | switch(strtolower($op)) |
---|
| 311 | { |
---|
| 312 | case 'and': |
---|
| 313 | case 'or': return( $op ); |
---|
| 314 | case 'in': return array( $op ); |
---|
| 315 | case '!in': return array( 'NOT IN' ); |
---|
| 316 | case '^': return array( 'like', '%', '' ); |
---|
| 317 | case '$': return array( 'like', '', '%' ); |
---|
| 318 | case '*': return array( 'like', '%', '%' ); |
---|
| 319 | case 'i^': return array( 'ilike', '%', '' ); |
---|
| 320 | case 'i$': return array( 'ilike', '', '%' ); |
---|
| 321 | case 'i*': return array( 'ilike', '%', '%' ); |
---|
| 322 | default : return array( $op, '', '' ); |
---|
[6528] | 323 | } |
---|
[6962] | 324 | } |
---|
[6528] | 325 | |
---|
| 326 | private static function parseJustthese($justthese , &$map) |
---|
| 327 | { |
---|
| 328 | |
---|
| 329 | if(!is_array($justthese)) //Caso seja um full select pegar todas as keys |
---|
| 330 | $justthese = array_keys($map); |
---|
| 331 | |
---|
| 332 | $return = array(); |
---|
| 333 | |
---|
| 334 | foreach ($justthese as &$value) |
---|
| 335 | { |
---|
| 336 | if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa |
---|
| 337 | |
---|
| 338 | if(is_array($map[$value])) |
---|
| 339 | $return['deepness'][$value] = $map[$value]; |
---|
| 340 | else |
---|
| 341 | $return['select'][] = $map[$value] .' as "'. $value. '"'; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | $return['select'] = implode(', ', $return['select']); |
---|
| 345 | return $return; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | private static function parseOrder($order , &$map, $orderDesc=false) |
---|
| 349 | { |
---|
| 350 | |
---|
| 351 | if($notArray = !is_array($order)) //Caso seja um full select pegar todas as keys |
---|
| 352 | $order = array( $order ); |
---|
| 353 | |
---|
[6962] | 354 | //Caso seja feita ordenacao em ordem descrescente |
---|
| 355 | //concatenar DESC em cada atributo |
---|
| 356 | if($orderDesc !== false){ |
---|
| 357 | if(!is_array($orderDesc)){ |
---|
| 358 | $orderDesc = array( $orderDesc ); |
---|
| 359 | } |
---|
[6528] | 360 | |
---|
[6962] | 361 | for($i=0; $i<count($order); $i++){ |
---|
| 362 | $order[$i] .= ($orderDesc[$i] === true) ? ' DESC' : ''; |
---|
| 363 | } |
---|
| 364 | } |
---|
[6528] | 365 | |
---|
| 366 | $return = array(); |
---|
| 367 | |
---|
| 368 | foreach ($order as &$value) |
---|
| 369 | { |
---|
| 370 | if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa |
---|
| 371 | |
---|
| 372 | $value = $map[$value]; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | return ( $notArray ? $order[0] : implode(', ', $order) ); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | } |
---|
| 379 | |
---|
[6962] | 380 | ?> |
---|