[5341] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class PostgreSQL implements Service |
---|
| 4 | { |
---|
| 5 | private $con; //Conexão com o banco de dados |
---|
| 6 | private $config; //Configuração |
---|
| 7 | public $error = false; //Armazena um erro caso ocorra |
---|
| 8 | |
---|
| 9 | public function find ( $uri, $justthese = false, $criteria = false ){ |
---|
[5441] | 10 | |
---|
[5437] | 11 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 12 | |
---|
| 13 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map) : ''; |
---|
[5341] | 14 | |
---|
[5437] | 15 | $justthese = self::parseJustthese($justthese, $map); |
---|
[5341] | 16 | |
---|
[5437] | 17 | return $this->execResultSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' '.$criteria ); |
---|
[5341] | 18 | } |
---|
| 19 | |
---|
| 20 | public function read ( $uri, $justthese = false ){ |
---|
[5437] | 21 | |
---|
| 22 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 23 | |
---|
| 24 | $justthese = self::parseJustthese($justthese, $map); |
---|
[5341] | 25 | |
---|
[5437] | 26 | return $this->execResultSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' WHERE id = \''.addslashes( $uri['id'] ).'\'', true ); |
---|
[5341] | 27 | } |
---|
| 28 | |
---|
[5437] | 29 | public function deleteAll ( $uri, $justthese = false, $criteria = false ){ |
---|
| 30 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 31 | if(!self::parseCriteria ( $criteria , $map)) return false; //Validador para não apagar tabela inteira |
---|
| 32 | return $this->execSql( 'DELETE FROM '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '.self::parseCriteria ( $criteria ,$map) ); |
---|
[5341] | 33 | } |
---|
| 34 | |
---|
| 35 | public function delete ( $uri, $justthese = false ){ |
---|
[5437] | 36 | return $this->execSql('DELETE FROM '.(Config::get($uri['concept'],'PostgreSQL.concept')).' WHERE id = '.addslashes($uri['id'])); |
---|
[5341] | 37 | } |
---|
| 38 | |
---|
| 39 | public function replace ( $uri, $data, $criteria = false ){ |
---|
[5441] | 40 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 41 | return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map)); |
---|
[5341] | 42 | } |
---|
| 43 | |
---|
| 44 | public function update ( $uri, $data ){ |
---|
[5441] | 45 | $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); |
---|
| 46 | return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' WHERE id = \''. addslashes( $uri['id']) .'\''); |
---|
[5341] | 47 | } |
---|
| 48 | |
---|
| 49 | public function create ( $uri, $data ){ |
---|
[5437] | 50 | return $this->execResultSql( 'INSERT INTO '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '.self::parseInsertData( $data , $uri['concept'] ), true ); |
---|
[5341] | 51 | } |
---|
| 52 | |
---|
| 53 | private function execSql( $sql ){ |
---|
| 54 | if(!$this->con) $this->open( $this->config ); |
---|
| 55 | |
---|
| 56 | $rs = pg_query( $this->con, $sql ); |
---|
| 57 | |
---|
| 58 | if ( !$rs ){ |
---|
| 59 | $this->error = pg_last_error ( $this->con ); |
---|
| 60 | return false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | return $rs; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public function execResultSql( $sql, $unique = false ){ |
---|
| 67 | $rs = $this->execSql( $sql ); |
---|
| 68 | |
---|
| 69 | if( $rs && $rs > 0 ) |
---|
| 70 | return self::parseSelectResult( $rs , $unique ); |
---|
| 71 | |
---|
| 72 | return $rs; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public function begin( $uri ) { |
---|
| 76 | |
---|
| 77 | if(!$this->con) |
---|
| 78 | $this->open( $this->config ); |
---|
| 79 | |
---|
| 80 | $this->error = false; |
---|
| 81 | pg_query($this->con, "BEGIN WORK"); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public function commit($uri ) { |
---|
| 85 | |
---|
| 86 | if( $this->error !== false ) |
---|
| 87 | { |
---|
| 88 | $error = $this->error; |
---|
| 89 | $this->error = false; |
---|
| 90 | |
---|
| 91 | throw new Exception( $error ); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | pg_query($this->con, "COMMIT"); |
---|
| 95 | |
---|
| 96 | return( true ); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public function rollback( $uri ){ |
---|
| 100 | |
---|
| 101 | pg_query($this->con, "ROLLBACK"); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | public function open ( $config ){ |
---|
[5399] | 105 | |
---|
[5341] | 106 | $this->config = $config; |
---|
| 107 | |
---|
| 108 | $rs = ''; |
---|
| 109 | $rs .= ( isset($this->config['host']) && $this->config['host'] ) ? ' host='.$this->config['host'] : '' ; |
---|
| 110 | $rs .= ( isset($this->config['user']) && $this->config['user'] ) ? ' user='.$this->config['user'] : '' ; |
---|
| 111 | $rs .= ( isset($this->config['password']) && $this->config['password'] ) ? ' password='.$this->config['password'] : '' ; |
---|
| 112 | $rs .= ( isset($this->config['dbname']) && $this->config['dbname'] ) ? ' dbname='.$this->config['dbname'] : '' ; |
---|
| 113 | $rs .= ( isset($this->config['port']) && $this->config['port'] ) ? ' port='.$this->config['port'] : '' ; |
---|
| 114 | |
---|
| 115 | return( $this->con = pg_connect( $rs ) ); |
---|
| 116 | //$this->con = pg_connect('host='.$config['host'].' user='.$config['user'].' password='.$config['password'].' dbname='.$config['dbname'].' options=\'--client_encoding=UTF8\''); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public function close(){ |
---|
| 120 | |
---|
| 121 | pg_close($this->con); |
---|
| 122 | |
---|
| 123 | $this->con = false; |
---|
| 124 | |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | public function setup(){} |
---|
| 128 | |
---|
| 129 | public function teardown(){} |
---|
| 130 | |
---|
[5437] | 131 | private static function parseInsertData( $data , $concept){ |
---|
| 132 | |
---|
| 133 | $map = Config::get($concept, 'PostgreSQL.mapping'); |
---|
| 134 | |
---|
[5341] | 135 | $ind = array(); |
---|
| 136 | $val = array(); |
---|
| 137 | |
---|
| 138 | foreach ($data as $i => $v){ |
---|
[5437] | 139 | if(!isset($map[$i])) continue; |
---|
| 140 | |
---|
| 141 | $ind[] = $map[$i]; |
---|
[5341] | 142 | $val[] = '\''.addslashes($v).'\''; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | return '('.implode(',', $ind).') VALUES ('.implode(',', $val).') RETURNING id'; |
---|
| 146 | } |
---|
| 147 | |
---|
[5441] | 148 | private static function parseUpdateData( $data , &$map){ |
---|
| 149 | |
---|
[5341] | 150 | $d = array(); |
---|
| 151 | foreach ($data as $i => $v) |
---|
[5437] | 152 | { |
---|
| 153 | if(!isset($map[$i])) continue; |
---|
| 154 | |
---|
| 155 | $d[] = $map[$i].' = \''.addslashes ($v).'\''; |
---|
| 156 | } |
---|
[5341] | 157 | |
---|
| 158 | return 'SET '.implode(',', $d); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | private static function parseSelectResult( $result , $unique = false){ |
---|
| 162 | |
---|
| 163 | $return = array(); |
---|
| 164 | |
---|
| 165 | if (!$result) return false; |
---|
| 166 | if (pg_num_rows($result) === 0) return $return; |
---|
| 167 | else |
---|
| 168 | while( $row = pg_fetch_assoc( $result )) |
---|
| 169 | $return[] = $row; |
---|
| 170 | |
---|
| 171 | if($unique === true) return $return[0]; |
---|
| 172 | |
---|
| 173 | return $return; |
---|
| 174 | } |
---|
| 175 | |
---|
[5437] | 176 | private static function parseCriteria( $criteria , &$map ){ |
---|
[5341] | 177 | |
---|
| 178 | $result = ''; |
---|
| 179 | |
---|
| 180 | if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) |
---|
| 181 | { |
---|
| 182 | /* |
---|
| 183 | * ex: array ( |
---|
| 184 | * [0] 'OR', |
---|
| 185 | * [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ), |
---|
| 186 | * [2] array( '=', 'campo' , 'valor' ), |
---|
| 187 | * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) |
---|
| 188 | * ) |
---|
| 189 | * OR |
---|
| 190 | * array( '=' , 'campo' , 'valor' ) |
---|
| 191 | */ |
---|
| 192 | |
---|
[5441] | 193 | $result .= 'WHERE ('.self::parseFilter( $criteria['filter'] , $map).')'; |
---|
[5341] | 194 | } |
---|
| 195 | /* |
---|
| 196 | * ex: array( 'table1' => 'table2' , 'table1' => 'table2') |
---|
| 197 | * |
---|
| 198 | */ |
---|
| 199 | if( isset($criteria["join"]) ) |
---|
| 200 | { |
---|
| 201 | foreach ($criteria["join"] as $i => $v) |
---|
| 202 | $result .= ' AND '.$i.' = '.$v.' '; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if( isset($criteria["group"]) ) |
---|
| 206 | { |
---|
| 207 | $result .= ' GROUP BY '.( is_array($criteria["group"]) ? implode(', ', $criteria["group"]) : $criteria["group"] ).' '; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if( isset($criteria["order"]) ) |
---|
| 211 | { |
---|
[5514] | 212 | $result .= ' ORDER BY '.self::parseOrder( $criteria["order"], $map ).' '; |
---|
[5341] | 213 | } |
---|
| 214 | if( isset($criteria["limit"]) ) |
---|
| 215 | { |
---|
| 216 | $result .= ' LIMIT '. $criteria["limit"] .' '; |
---|
| 217 | } |
---|
| 218 | if( isset($criteria["offset"]) ) |
---|
| 219 | { |
---|
| 220 | $result .= ' OFFSET '. $criteria["offset"] .' '; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | return $result; |
---|
| 224 | } |
---|
| 225 | |
---|
[5437] | 226 | private static function parseFilter( $filter ,&$map){ |
---|
[5341] | 227 | |
---|
| 228 | if( !is_array( $filter ) || count($filter) <= 0) return null; |
---|
[5437] | 229 | |
---|
[5341] | 230 | $op = self::parseOperator( array_shift( $filter ) ); |
---|
[5437] | 231 | |
---|
[5341] | 232 | if( is_array($filter[0]) ) |
---|
| 233 | { |
---|
| 234 | $nested = array(); |
---|
| 235 | |
---|
| 236 | foreach( $filter as $i => $f ) |
---|
[5441] | 237 | if( $n = self::parseFilter( $f , $map)) |
---|
| 238 | $nested[] = $n; |
---|
[5341] | 239 | |
---|
[5441] | 240 | |
---|
| 241 | return (count($nested) > 0 ) ? '('.implode( ' '.$op.' ', $nested ).')' : ''; |
---|
[5341] | 242 | } |
---|
| 243 | |
---|
[5441] | 244 | if(!isset($map[$filter[0]])) return ''; |
---|
| 245 | |
---|
| 246 | $filter[0] = $map[$filter[0]]; |
---|
[5437] | 247 | |
---|
[5341] | 248 | $igSuffix = $igPrefix = ''; |
---|
[5437] | 249 | |
---|
[5341] | 250 | if( strpos( $op[0], 'i' ) === 0 ) |
---|
| 251 | { |
---|
| 252 | $op[0] = substr( $op[0], 1 ); |
---|
| 253 | $filter[0] = 'upper("'.$filter[0].'")'; |
---|
| 254 | $igPrefix = 'upper('; |
---|
| 255 | $igSuffix = ')'; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | if( is_array($filter[1]) ) |
---|
| 259 | return( $filter[0].' '.$op[0]." ($igPrefix'".implode( "'$igSuffix,$igPrefix'", array_map("addslashes" , $filter[1]) )."'$igSuffix)" ); |
---|
| 260 | |
---|
| 261 | return( $filter[0].' '.$op[0]." $igPrefix'".$op[1].addslashes( $filter[1] ).$op[2]."'$igSuffix" ); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | private static function parseOperator( $op ){ |
---|
| 265 | |
---|
| 266 | switch(strtolower($op)) |
---|
| 267 | { |
---|
| 268 | case 'and': |
---|
| 269 | case 'or': return( $op ); |
---|
| 270 | case 'in': return array( $op ); |
---|
| 271 | case '^': return array( 'like', '%', '' ); |
---|
| 272 | case '$': return array( 'like', '', '%' ); |
---|
| 273 | case '*': return array( 'like', '%', '%' ); |
---|
| 274 | case 'i^': return array( 'ilike', '%', '' ); |
---|
| 275 | case 'i$': return array( 'ilike', '', '%' ); |
---|
| 276 | case 'i*': return array( 'ilike', '%', '%' ); |
---|
| 277 | default : return array( $op, '', '' ); |
---|
| 278 | } |
---|
| 279 | } |
---|
[5437] | 280 | |
---|
| 281 | private static function parseJustthese($justthese , &$map) |
---|
| 282 | { |
---|
| 283 | |
---|
| 284 | if(!is_array($justthese)) //Caso seja um full select pegar todas as keys |
---|
| 285 | $justthese = array_keys($map); |
---|
| 286 | |
---|
| 287 | $return = array(); |
---|
| 288 | |
---|
| 289 | foreach ($justthese as &$value) |
---|
| 290 | { |
---|
| 291 | if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa |
---|
| 292 | |
---|
| 293 | if(is_array($map[$value])) |
---|
| 294 | $return['deepness'][$value] = $map[$value]; |
---|
| 295 | else |
---|
| 296 | $return['select'][] = $map[$value] .' as "'. $value. '"'; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | $return['select'] = implode(', ', $return['select']); |
---|
| 300 | return $return; |
---|
| 301 | } |
---|
[5514] | 302 | |
---|
| 303 | private static function parseOrder($order , &$map) |
---|
| 304 | { |
---|
| 305 | |
---|
| 306 | if($notArray = !is_array($order)) //Caso seja um full select pegar todas as keys |
---|
| 307 | $order = array( $order ); |
---|
| 308 | |
---|
| 309 | $return = array(); |
---|
| 310 | |
---|
| 311 | foreach ($order as &$value) |
---|
| 312 | { |
---|
| 313 | if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa |
---|
| 314 | |
---|
| 315 | $value = $map[$value]; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | return ( $notArray ? $order[0] : implode(', ', $order) ); |
---|
| 319 | } |
---|
[5341] | 320 | } |
---|
| 321 | |
---|
| 322 | ?> |
---|