parseCriteria ( $criteria ) : ''; $justthese = $justthese ? '"'.implode('", "', $justthese).'"' : '*'; return $this->execResultSql( 'SELECT '.$justthese.' FROM '.$uri['concept'].' '.$criteria ); } public function read ( $uri, $justthese = false ){ $justthese = $justthese ? '"'.implode('", "', $justthese).'"' : '*'; return $this->execResultSql( 'SELECT '.$justthese.' FROM '.$uri['concept'].' WHERE id = \''.addslashes( $uri['id'] ).'\'', true ); } public function deleteAll ( $uri, $justthese = false, $criteria = false ){ if(!self::parseCriteria ( $criteria )) return false; //Validador para não apagar tabela inteira return $this->execSql( 'DELETE FROM '.$uri['concept'].' '.self::parseCriteria ( $criteria ) ); } public function delete ( $uri, $justthese = false ){ return $this->execSql('DELETE FROM '.$uri['concept'].' WHERE id = '.addslashes($uri['id'])); } public function replace ( $uri, $data, $criteria = false ){ return $this->execSql('UPDATE '.$uri['concept'].' '. self::parseUpdateData( $data ).' '.self::parseCriteria($criteria)); } public function update ( $uri, $data ){ return $this->execSql('UPDATE '.$uri['concept'].' '. self::parseUpdateData( $data ).' WHERE id = \''. addslashes( $uri['id']) .'\''); } public function create ( $uri, $data ){ return $this->execResultSql( 'INSERT INTO '.$uri['concept'].' '.self::parseInsertData( $data ), true ); } private function execSql( $sql ){ if(!$this->con) $this->open( $this->config ); $rs = pg_query( $this->con, $sql ); if ( !$rs ){ $this->error = pg_last_error ( $this->con ); return false; } return $rs; } public function execResultSql( $sql, $unique = false ){ $rs = $this->execSql( $sql ); if( $rs && $rs > 0 ) return self::parseSelectResult( $rs , $unique ); return $rs; } public function begin( $uri ) { if(!$this->con) $this->open( $this->config ); $this->error = false; pg_query($this->con, "BEGIN WORK"); } public function commit($uri ) { if( $this->error !== false ) { $error = $this->error; $this->error = false; throw new Exception( $error ); } pg_query($this->con, "COMMIT"); return( true ); } public function rollback( $uri ){ pg_query($this->con, "ROLLBACK"); } public function open ( $config ){ $this->config = $config; $rs = ''; $rs .= ( isset($this->config['host']) && $this->config['host'] ) ? ' host='.$this->config['host'] : '' ; $rs .= ( isset($this->config['user']) && $this->config['user'] ) ? ' user='.$this->config['user'] : '' ; $rs .= ( isset($this->config['password']) && $this->config['password'] ) ? ' password='.$this->config['password'] : '' ; $rs .= ( isset($this->config['dbname']) && $this->config['dbname'] ) ? ' dbname='.$this->config['dbname'] : '' ; $rs .= ( isset($this->config['port']) && $this->config['port'] ) ? ' port='.$this->config['port'] : '' ; return( $this->con = pg_connect( $rs ) ); //$this->con = pg_connect('host='.$config['host'].' user='.$config['user'].' password='.$config['password'].' dbname='.$config['dbname'].' options=\'--client_encoding=UTF8\''); } public function close(){ pg_close($this->con); $this->con = false; } public function setup(){} public function teardown(){} private static function parseInsertData( $data ){ $ind = array(); $val = array(); foreach ($data as $i => $v){ $ind[] = $i; $val[] = '\''.addslashes($v).'\''; } return '('.implode(',', $ind).') VALUES ('.implode(',', $val).') RETURNING id'; } private static function parseUpdateData( $data ){ $d = array(); foreach ($data as $i => $v) $d[] = $i.' = \''.addslashes ($v).'\''; return 'SET '.implode(',', $d); } private static function parseSelectResult( $result , $unique = false){ $return = array(); if (!$result) return false; if (pg_num_rows($result) === 0) return $return; else while( $row = pg_fetch_assoc( $result )) $return[] = $row; if($unique === true) return $return[0]; return $return; } private static function parseCriteria( $criteria ){ $result = ''; if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) { /* * ex: array ( * [0] 'OR', * [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ), * [2] array( '=', 'campo' , 'valor' ), * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) * ) * OR * array( '=' , 'campo' , 'valor' ) */ $result .= 'WHERE '.self::parseFilter( $criteria['filter'] ); } /* * ex: array( 'table1' => 'table2' , 'table1' => 'table2') * */ if( isset($criteria["join"]) ) { foreach ($criteria["join"] as $i => $v) $result .= ' AND '.$i.' = '.$v.' '; } if( isset($criteria["group"]) ) { $result .= ' GROUP BY '.( is_array($criteria["group"]) ? implode(', ', $criteria["group"]) : $criteria["group"] ).' '; } if( isset($criteria["order"]) ) { $result .= ' ORDER BY '.( is_array($criteria["order"]) ? implode(', ', $criteria["order"]) : $criteria["order"] ).' '; } if( isset($criteria["limit"]) ) { $result .= ' LIMIT '. $criteria["limit"] .' '; } if( isset($criteria["offset"]) ) { $result .= ' OFFSET '. $criteria["offset"] .' '; } return $result; } private static function parseFilter( $filter ){ if( !is_array( $filter ) || count($filter) <= 0) return null; $op = self::parseOperator( array_shift( $filter ) ); if( is_array($filter[0]) ) { $nested = array(); foreach( $filter as $i => $f ) $nested[] = self::parseFilter( $f ); return( '('.implode( ' '.$op.' ', $nested ).')' ); } $igSuffix = $igPrefix = ''; if( strpos( $op[0], 'i' ) === 0 ) { $op[0] = substr( $op[0], 1 ); $filter[0] = 'upper("'.$filter[0].'")'; $igPrefix = 'upper('; $igSuffix = ')'; } if( is_array($filter[1]) ) return( $filter[0].' '.$op[0]." ($igPrefix'".implode( "'$igSuffix,$igPrefix'", array_map("addslashes" , $filter[1]) )."'$igSuffix)" ); return( $filter[0].' '.$op[0]." $igPrefix'".$op[1].addslashes( $filter[1] ).$op[2]."'$igSuffix" ); } private static function parseOperator( $op ){ switch(strtolower($op)) { case 'and': case 'or': return( $op ); case 'in': return array( $op ); case '^': return array( 'like', '%', '' ); case '$': return array( 'like', '', '%' ); case '*': return array( 'like', '%', '%' ); case 'i^': return array( 'ilike', '%', '' ); case 'i$': return array( 'ilike', '', '%' ); case 'i*': return array( 'ilike', '%', '%' ); default : return array( $op, '', '' ); } } } ?>