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