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 | $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria ) : ''; |
---|
12 | |
---|
13 | $justthese = $justthese ? '"'.implode('", "', $justthese).'"' : '*'; |
---|
14 | |
---|
15 | return $this->execResultSql( 'SELECT '.$justthese.' FROM '.$uri['concept'].' '.$criteria ); |
---|
16 | } |
---|
17 | |
---|
18 | 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 ) ); |
---|
28 | } |
---|
29 | |
---|
30 | public function delete ( $uri, $justthese = false ){ |
---|
31 | return $this->execSql('DELETE FROM '.$uri['concept'].' WHERE id = '.addslashes($uri['id'])); |
---|
32 | } |
---|
33 | |
---|
34 | public function replace ( $uri, $data, $criteria = false ){ |
---|
35 | return $this->execSql('UPDATE '.$uri['concept'].' '. self::parseUpdateData( $data ).' '.self::parseCriteria($criteria)); |
---|
36 | } |
---|
37 | |
---|
38 | public function update ( $uri, $data ){ |
---|
39 | return $this->execSql('UPDATE '.$uri['concept'].' '. self::parseUpdateData( $data ).' WHERE id = \''. addslashes( $uri['id']) .'\''); |
---|
40 | } |
---|
41 | |
---|
42 | public function create ( $uri, $data ){ |
---|
43 | return $this->execResultSql( 'INSERT INTO '.$uri['concept'].' '.self::parseInsertData( $data ), true ); |
---|
44 | } |
---|
45 | |
---|
46 | private function execSql( $sql ){ |
---|
47 | if(!$this->con) $this->open( $this->config ); |
---|
48 | |
---|
49 | $rs = pg_query( $this->con, $sql ); |
---|
50 | |
---|
51 | if ( !$rs ){ |
---|
52 | $this->error = pg_last_error ( $this->con ); |
---|
53 | return false; |
---|
54 | } |
---|
55 | |
---|
56 | return $rs; |
---|
57 | } |
---|
58 | |
---|
59 | public function execResultSql( $sql, $unique = false ){ |
---|
60 | $rs = $this->execSql( $sql ); |
---|
61 | |
---|
62 | if( $rs && $rs > 0 ) |
---|
63 | return self::parseSelectResult( $rs , $unique ); |
---|
64 | |
---|
65 | return $rs; |
---|
66 | } |
---|
67 | |
---|
68 | public function begin( $uri ) { |
---|
69 | |
---|
70 | if(!$this->con) |
---|
71 | $this->open( $this->config ); |
---|
72 | |
---|
73 | $this->error = false; |
---|
74 | pg_query($this->con, "BEGIN WORK"); |
---|
75 | } |
---|
76 | |
---|
77 | public function commit($uri ) { |
---|
78 | |
---|
79 | if( $this->error !== false ) |
---|
80 | { |
---|
81 | $error = $this->error; |
---|
82 | $this->error = false; |
---|
83 | |
---|
84 | throw new Exception( $error ); |
---|
85 | } |
---|
86 | |
---|
87 | pg_query($this->con, "COMMIT"); |
---|
88 | |
---|
89 | return( true ); |
---|
90 | } |
---|
91 | |
---|
92 | public function rollback( $uri ){ |
---|
93 | |
---|
94 | pg_query($this->con, "ROLLBACK"); |
---|
95 | } |
---|
96 | |
---|
97 | public function open ( $config ){ |
---|
98 | |
---|
99 | $this->config = $config; |
---|
100 | |
---|
101 | $rs = ''; |
---|
102 | $rs .= ( isset($this->config['host']) && $this->config['host'] ) ? ' host='.$this->config['host'] : '' ; |
---|
103 | $rs .= ( isset($this->config['user']) && $this->config['user'] ) ? ' user='.$this->config['user'] : '' ; |
---|
104 | $rs .= ( isset($this->config['password']) && $this->config['password'] ) ? ' password='.$this->config['password'] : '' ; |
---|
105 | $rs .= ( isset($this->config['dbname']) && $this->config['dbname'] ) ? ' dbname='.$this->config['dbname'] : '' ; |
---|
106 | $rs .= ( isset($this->config['port']) && $this->config['port'] ) ? ' port='.$this->config['port'] : '' ; |
---|
107 | |
---|
108 | return( $this->con = pg_connect( $rs ) ); |
---|
109 | //$this->con = pg_connect('host='.$config['host'].' user='.$config['user'].' password='.$config['password'].' dbname='.$config['dbname'].' options=\'--client_encoding=UTF8\''); |
---|
110 | } |
---|
111 | |
---|
112 | public function close(){ |
---|
113 | |
---|
114 | pg_close($this->con); |
---|
115 | |
---|
116 | $this->con = false; |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | public function setup(){} |
---|
121 | |
---|
122 | public function teardown(){} |
---|
123 | |
---|
124 | private static function parseInsertData( $data ){ |
---|
125 | |
---|
126 | $ind = array(); |
---|
127 | $val = array(); |
---|
128 | |
---|
129 | foreach ($data as $i => $v){ |
---|
130 | $ind[] = $i; |
---|
131 | $val[] = '\''.addslashes($v).'\''; |
---|
132 | } |
---|
133 | |
---|
134 | return '('.implode(',', $ind).') VALUES ('.implode(',', $val).') RETURNING id'; |
---|
135 | } |
---|
136 | |
---|
137 | private static function parseUpdateData( $data ){ |
---|
138 | |
---|
139 | $d = array(); |
---|
140 | |
---|
141 | foreach ($data as $i => $v) |
---|
142 | $d[] = $i.' = \''.addslashes ($v).'\''; |
---|
143 | |
---|
144 | return 'SET '.implode(',', $d); |
---|
145 | } |
---|
146 | |
---|
147 | private static function parseSelectResult( $result , $unique = false){ |
---|
148 | |
---|
149 | $return = array(); |
---|
150 | |
---|
151 | if (!$result) return false; |
---|
152 | if (pg_num_rows($result) === 0) return $return; |
---|
153 | else |
---|
154 | while( $row = pg_fetch_assoc( $result )) |
---|
155 | $return[] = $row; |
---|
156 | |
---|
157 | if($unique === true) return $return[0]; |
---|
158 | |
---|
159 | return $return; |
---|
160 | } |
---|
161 | |
---|
162 | private static function parseCriteria( $criteria ){ |
---|
163 | |
---|
164 | $result = ''; |
---|
165 | |
---|
166 | if( isset($criteria["filter"]) && $criteria["filter"] !== NULL ) |
---|
167 | { |
---|
168 | /* |
---|
169 | * ex: array ( |
---|
170 | * [0] 'OR', |
---|
171 | * [1] array( 'OR', array( array( '=', 'campo', 'valor' ) ), |
---|
172 | * [2] array( '=', 'campo' , 'valor' ), |
---|
173 | * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) |
---|
174 | * ) |
---|
175 | * OR |
---|
176 | * array( '=' , 'campo' , 'valor' ) |
---|
177 | */ |
---|
178 | |
---|
179 | $result .= 'WHERE '.self::parseFilter( $criteria['filter'] ); |
---|
180 | } |
---|
181 | /* |
---|
182 | * ex: array( 'table1' => 'table2' , 'table1' => 'table2') |
---|
183 | * |
---|
184 | */ |
---|
185 | if( isset($criteria["join"]) ) |
---|
186 | { |
---|
187 | foreach ($criteria["join"] as $i => $v) |
---|
188 | $result .= ' AND '.$i.' = '.$v.' '; |
---|
189 | } |
---|
190 | |
---|
191 | if( isset($criteria["group"]) ) |
---|
192 | { |
---|
193 | $result .= ' GROUP BY '.( is_array($criteria["group"]) ? implode(', ', $criteria["group"]) : $criteria["group"] ).' '; |
---|
194 | } |
---|
195 | |
---|
196 | if( isset($criteria["order"]) ) |
---|
197 | { |
---|
198 | $result .= ' ORDER BY '.( is_array($criteria["order"]) ? implode(', ', $criteria["order"]) : $criteria["order"] ).' '; |
---|
199 | } |
---|
200 | if( isset($criteria["limit"]) ) |
---|
201 | { |
---|
202 | $result .= ' LIMIT '. $criteria["limit"] .' '; |
---|
203 | } |
---|
204 | if( isset($criteria["offset"]) ) |
---|
205 | { |
---|
206 | $result .= ' OFFSET '. $criteria["offset"] .' '; |
---|
207 | } |
---|
208 | |
---|
209 | return $result; |
---|
210 | } |
---|
211 | |
---|
212 | private static function parseFilter( $filter ){ |
---|
213 | |
---|
214 | if( !is_array( $filter ) || count($filter) <= 0) return null; |
---|
215 | |
---|
216 | $op = self::parseOperator( array_shift( $filter ) ); |
---|
217 | |
---|
218 | if( is_array($filter[0]) ) |
---|
219 | { |
---|
220 | $nested = array(); |
---|
221 | |
---|
222 | foreach( $filter as $i => $f ) |
---|
223 | $nested[] = self::parseFilter( $f ); |
---|
224 | |
---|
225 | return( '('.implode( ' '.$op.' ', $nested ).')' ); |
---|
226 | } |
---|
227 | |
---|
228 | $igSuffix = $igPrefix = ''; |
---|
229 | |
---|
230 | if( strpos( $op[0], 'i' ) === 0 ) |
---|
231 | { |
---|
232 | $op[0] = substr( $op[0], 1 ); |
---|
233 | $filter[0] = 'upper("'.$filter[0].'")'; |
---|
234 | $igPrefix = 'upper('; |
---|
235 | $igSuffix = ')'; |
---|
236 | } |
---|
237 | |
---|
238 | if( is_array($filter[1]) ) |
---|
239 | return( $filter[0].' '.$op[0]." ($igPrefix'".implode( "'$igSuffix,$igPrefix'", array_map("addslashes" , $filter[1]) )."'$igSuffix)" ); |
---|
240 | |
---|
241 | return( $filter[0].' '.$op[0]." $igPrefix'".$op[1].addslashes( $filter[1] ).$op[2]."'$igSuffix" ); |
---|
242 | } |
---|
243 | |
---|
244 | private static function parseOperator( $op ){ |
---|
245 | |
---|
246 | switch(strtolower($op)) |
---|
247 | { |
---|
248 | case 'and': |
---|
249 | case 'or': return( $op ); |
---|
250 | case 'in': return array( $op ); |
---|
251 | case '^': return array( 'like', '%', '' ); |
---|
252 | case '$': return array( 'like', '', '%' ); |
---|
253 | case '*': return array( 'like', '%', '%' ); |
---|
254 | case 'i^': return array( 'ilike', '%', '' ); |
---|
255 | case 'i$': return array( 'ilike', '', '%' ); |
---|
256 | case 'i*': return array( 'ilike', '%', '%' ); |
---|
257 | default : return array( $op, '', '' ); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | } |
---|
262 | |
---|
263 | ?> |
---|