Changeset 5437 for trunk/prototype/services/PostgreSQL.php
- Timestamp:
- 01/27/12 15:37:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/prototype/services/PostgreSQL.php
r5399 r5437 8 8 9 9 public function find ( $uri, $justthese = false, $criteria = false ){ 10 11 $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria ) : ''; 12 13 $justthese = $justthese ? '"'.implode('", "', $justthese).'"' : '*'; 14 15 return $this->execResultSql( 'SELECT '.$justthese.' FROM '.$uri['concept'].' '.$criteria ); 10 11 $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); 12 13 $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map) : ''; 14 15 $justthese = self::parseJustthese($justthese, $map); 16 17 return $this->execResultSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' '.$criteria ); 16 18 } 17 19 18 20 public function read ( $uri, $justthese = false ){ 19 20 $justthese = $justthese ? '"'.implode('", "', $justthese).'"' : '*'; 21 22 return $this->execResultSql( 'SELECT '.$justthese.' FROM '.$uri['concept'].' WHERE id = \''.addslashes( $uri['id'] ).'\'', true ); 23 } 24 25 public function deleteAll ( $uri, $justthese = false, $criteria = false ){ 26 if(!self::parseCriteria ( $criteria )) return false; //Validador para não apagar tabela inteira 27 return $this->execSql( 'DELETE FROM '.$uri['concept'].' '.self::parseCriteria ( $criteria ) ); 21 22 $map = Config::get($uri['concept'], 'PostgreSQL.mapping'); 23 24 $justthese = self::parseJustthese($justthese, $map); 25 26 return $this->execResultSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' WHERE id = \''.addslashes( $uri['id'] ).'\'', true ); 27 } 28 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) ); 28 33 } 29 34 30 35 public function delete ( $uri, $justthese = false ){ 31 return $this->execSql('DELETE FROM '. $uri['concept'].' WHERE id = '.addslashes($uri['id']));36 return $this->execSql('DELETE FROM '.(Config::get($uri['concept'],'PostgreSQL.concept')).' WHERE id = '.addslashes($uri['id'])); 32 37 } 33 38 34 39 public function replace ( $uri, $data, $criteria = false ){ 35 return $this->execSql('UPDATE '.$uri['concept'].' '. self::parseUpdateData( $data ).' '.self::parseCriteria($criteria)); 40 41 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$uri['concept']).' '.self::parseCriteria($criteria , $uri['concept'])); 36 42 } 37 43 38 44 public function update ( $uri, $data ){ 39 return $this->execSql('UPDATE '. $uri['concept'].' '. self::parseUpdateData( $data).' WHERE id = \''. addslashes( $uri['id']) .'\'');45 return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$uri['concept']).' WHERE id = \''. addslashes( $uri['id']) .'\''); 40 46 } 41 47 42 48 public function create ( $uri, $data ){ 43 return $this->execResultSql( 'INSERT INTO '. $uri['concept'].' '.self::parseInsertData( $data), true );49 return $this->execResultSql( 'INSERT INTO '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '.self::parseInsertData( $data , $uri['concept'] ), true ); 44 50 } 45 51 … … 122 128 public function teardown(){} 123 129 124 private static function parseInsertData( $data ){ 125 130 private static function parseInsertData( $data , $concept){ 131 132 $map = Config::get($concept, 'PostgreSQL.mapping'); 133 126 134 $ind = array(); 127 135 $val = array(); 128 136 129 137 foreach ($data as $i => $v){ 130 $ind[] = $i; 138 if(!isset($map[$i])) continue; 139 140 $ind[] = $map[$i]; 131 141 $val[] = '\''.addslashes($v).'\''; 132 142 } … … 135 145 } 136 146 137 private static function parseUpdateData( $data ){ 138 147 private static function parseUpdateData( $data , $concept){ 148 149 $map = Config::get($concept, 'PostgreSQL.mapping'); 150 139 151 $d = array(); 140 141 152 foreach ($data as $i => $v) 142 $d[] = $i.' = \''.addslashes ($v).'\''; 153 { 154 if(!isset($map[$i])) continue; 155 156 $d[] = $map[$i].' = \''.addslashes ($v).'\''; 157 } 143 158 144 159 return 'SET '.implode(',', $d); … … 160 175 } 161 176 162 private static function parseCriteria( $criteria ){177 private static function parseCriteria( $criteria , &$map ){ 163 178 164 179 $result = ''; … … 177 192 */ 178 193 179 $result .= 'WHERE '.self::parseFilter( $criteria['filter'] );194 $result .= 'WHERE '.self::parseFilter( $criteria['filter'] , $map); 180 195 } 181 196 /* … … 210 225 } 211 226 212 private static function parseFilter( $filter ){227 private static function parseFilter( $filter ,&$map){ 213 228 214 229 if( !is_array( $filter ) || count($filter) <= 0) return null; 215 230 216 231 $op = self::parseOperator( array_shift( $filter ) ); 217 232 218 233 if( is_array($filter[0]) ) 219 234 { … … 221 236 222 237 foreach( $filter as $i => $f ) 223 $nested[] = self::parseFilter( $f );238 $nested[] = self::parseFilter( $f , $map); 224 239 225 240 return( '('.implode( ' '.$op.' ', $nested ).')' ); 226 241 } 227 242 243 244 if(isset($map[$filter[0]])) 245 $filter[0] = $map[$filter[0]]; 246 228 247 $igSuffix = $igPrefix = ''; 229 248 230 249 if( strpos( $op[0], 'i' ) === 0 ) 231 250 { … … 258 277 } 259 278 } 260 279 280 private static function parseJustthese($justthese , &$map) 281 { 282 283 if(!is_array($justthese)) //Caso seja um full select pegar todas as keys 284 $justthese = array_keys($map); 285 286 $return = array(); 287 288 foreach ($justthese as &$value) 289 { 290 if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa 291 292 if(is_array($map[$value])) 293 $return['deepness'][$value] = $map[$value]; 294 else 295 $return['select'][] = $map[$value] .' as "'. $value. '"'; 296 } 297 298 $return['select'] = implode(', ', $return['select']); 299 return $return; 300 } 301 261 302 } 262 303
Note: See TracChangeset
for help on using the changeset viewer.