130 |
kaklik |
1 |
<?php |
|
|
2 |
/*************************************************************************** |
|
|
3 |
* postgres7.php |
|
|
4 |
* ------------------- |
|
|
5 |
* begin : Saturday, Feb 13, 2001 |
|
|
6 |
* copyright : (C) 2001 The phpBB Group |
|
|
7 |
* email : supportphpbb.com |
|
|
8 |
* |
|
|
9 |
* $Id: postgres7.php,v 1.19.2.3 2005/05/06 20:50:10 acydburn Exp $ |
|
|
10 |
* |
|
|
11 |
***************************************************************************/ |
|
|
12 |
|
|
|
13 |
/*************************************************************************** |
|
|
14 |
* |
|
|
15 |
* This program is free software; you can redistribute it and/or modify |
|
|
16 |
* it under the terms of the GNU General Public License as published by |
|
|
17 |
* the Free Software Foundation; either version 2 of the License, or |
|
|
18 |
* (at your option) any later version. |
|
|
19 |
* |
|
|
20 |
***************************************************************************/ |
|
|
21 |
|
|
|
22 |
if(!defined("SQL_LAYER")) |
|
|
23 |
{ |
|
|
24 |
|
|
|
25 |
define("SQL_LAYER","postgresql"); |
|
|
26 |
|
|
|
27 |
class sql_db |
|
|
28 |
{ |
|
|
29 |
|
|
|
30 |
var $db_connect_id; |
|
|
31 |
var $query_result; |
|
|
32 |
var $in_transaction = 0; |
|
|
33 |
var $row = array(); |
|
|
34 |
var $rowset = array(); |
|
|
35 |
var $rownum = array(); |
|
|
36 |
var $num_queries = 0; |
|
|
37 |
|
|
|
38 |
// |
|
|
39 |
// Constructor |
|
|
40 |
// |
|
|
41 |
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true) |
|
|
42 |
{ |
|
|
43 |
$this->connect_string = ""; |
|
|
44 |
|
|
|
45 |
if( $sqluser ) |
|
|
46 |
{ |
|
|
47 |
$this->connect_string .= "user=$sqluser "; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
if( $sqlpassword ) |
|
|
51 |
{ |
|
|
52 |
$this->connect_string .= "password=$sqlpassword "; |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
if( $sqlserver ) |
|
|
56 |
{ |
|
|
57 |
if( ereg(":", $sqlserver) ) |
|
|
58 |
{ |
|
|
59 |
list($sqlserver, $sqlport) = split(":", $sqlserver); |
|
|
60 |
$this->connect_string .= "host=$sqlserver port=$sqlport "; |
|
|
61 |
} |
|
|
62 |
else |
|
|
63 |
{ |
|
|
64 |
if( $sqlserver != "localhost" ) |
|
|
65 |
{ |
|
|
66 |
$this->connect_string .= "host=$sqlserver "; |
|
|
67 |
} |
|
|
68 |
} |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
if( $database ) |
|
|
72 |
{ |
|
|
73 |
$this->dbname = $database; |
|
|
74 |
$this->connect_string .= "dbname=$database"; |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
$this->persistency = $persistency; |
|
|
78 |
|
|
|
79 |
$this->db_connect_id = ( $this->persistency ) ? pg_pconnect($this->connect_string) : pg_connect($this->connect_string); |
|
|
80 |
|
|
|
81 |
return ( $this->db_connect_id ) ? $this->db_connect_id : false; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
// |
|
|
85 |
// Other base methods |
|
|
86 |
// |
|
|
87 |
function sql_close() |
|
|
88 |
{ |
|
|
89 |
if( $this->db_connect_id ) |
|
|
90 |
{ |
|
|
91 |
// |
|
|
92 |
// Commit any remaining transactions |
|
|
93 |
// |
|
|
94 |
if( $this->in_transaction ) |
|
|
95 |
{ |
|
|
96 |
@pg_exec($this->db_connect_id, "COMMIT"); |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
if( $this->query_result ) |
|
|
100 |
{ |
|
|
101 |
@pg_freeresult($this->query_result); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
return @pg_close($this->db_connect_id); |
|
|
105 |
} |
|
|
106 |
else |
|
|
107 |
{ |
|
|
108 |
return false; |
|
|
109 |
} |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
// |
|
|
113 |
// Query method |
|
|
114 |
// |
|
|
115 |
function sql_query($query = "", $transaction = false) |
|
|
116 |
{ |
|
|
117 |
// |
|
|
118 |
// Remove any pre-existing queries |
|
|
119 |
// |
|
|
120 |
unset($this->query_result); |
|
|
121 |
if( $query != "" ) |
|
|
122 |
{ |
|
|
123 |
$this->num_queries++; |
|
|
124 |
|
|
|
125 |
$query = preg_replace("/LIMIT ([0-9]+),([ 0-9]+)/", "LIMIT \\2 OFFSET \\1", $query); |
|
|
126 |
|
|
|
127 |
if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction ) |
|
|
128 |
{ |
|
|
129 |
$this->in_transaction = TRUE; |
|
|
130 |
|
|
|
131 |
if( !@pg_exec($this->db_connect_id, "BEGIN") ) |
|
|
132 |
{ |
|
|
133 |
return false; |
|
|
134 |
} |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
$this->query_result = @pg_exec($this->db_connect_id, $query); |
|
|
138 |
if( $this->query_result ) |
|
|
139 |
{ |
|
|
140 |
if( $transaction == END_TRANSACTION ) |
|
|
141 |
{ |
|
|
142 |
$this->in_transaction = FALSE; |
|
|
143 |
|
|
|
144 |
if( !@pg_exec($this->db_connect_id, "COMMIT") ) |
|
|
145 |
{ |
|
|
146 |
@pg_exec($this->db_connect_id, "ROLLBACK"); |
|
|
147 |
return false; |
|
|
148 |
} |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
$this->last_query_text[$this->query_result] = $query; |
|
|
152 |
$this->rownum[$this->query_result] = 0; |
|
|
153 |
|
|
|
154 |
unset($this->row[$this->query_result]); |
|
|
155 |
unset($this->rowset[$this->query_result]); |
|
|
156 |
|
|
|
157 |
return $this->query_result; |
|
|
158 |
} |
|
|
159 |
else |
|
|
160 |
{ |
|
|
161 |
if( $this->in_transaction ) |
|
|
162 |
{ |
|
|
163 |
@pg_exec($this->db_connect_id, "ROLLBACK"); |
|
|
164 |
} |
|
|
165 |
$this->in_transaction = FALSE; |
|
|
166 |
|
|
|
167 |
return false; |
|
|
168 |
} |
|
|
169 |
} |
|
|
170 |
else |
|
|
171 |
{ |
|
|
172 |
if( $transaction == END_TRANSACTION && $this->in_transaction ) |
|
|
173 |
{ |
|
|
174 |
$this->in_transaction = FALSE; |
|
|
175 |
|
|
|
176 |
if( !@pg_exec($this->db_connect_id, "COMMIT") ) |
|
|
177 |
{ |
|
|
178 |
@pg_exec($this->db_connect_id, "ROLLBACK"); |
|
|
179 |
return false; |
|
|
180 |
} |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
return true; |
|
|
184 |
} |
|
|
185 |
} |
|
|
186 |
|
|
|
187 |
// |
|
|
188 |
// Other query methods |
|
|
189 |
// |
|
|
190 |
function sql_numrows($query_id = 0) |
|
|
191 |
{ |
|
|
192 |
if( !$query_id ) |
|
|
193 |
{ |
|
|
194 |
$query_id = $this->query_result; |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
return ( $query_id ) ? @pg_numrows($query_id) : false; |
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
function sql_numfields($query_id = 0) |
|
|
201 |
{ |
|
|
202 |
if( !$query_id ) |
|
|
203 |
{ |
|
|
204 |
$query_id = $this->query_result; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
return ( $query_id ) ? @pg_numfields($query_id) : false; |
|
|
208 |
} |
|
|
209 |
|
|
|
210 |
function sql_fieldname($offset, $query_id = 0) |
|
|
211 |
{ |
|
|
212 |
if( !$query_id ) |
|
|
213 |
{ |
|
|
214 |
$query_id = $this->query_result; |
|
|
215 |
} |
|
|
216 |
|
|
|
217 |
return ( $query_id ) ? @pg_fieldname($query_id, $offset) : false; |
|
|
218 |
} |
|
|
219 |
|
|
|
220 |
function sql_fieldtype($offset, $query_id = 0) |
|
|
221 |
{ |
|
|
222 |
if( !$query_id ) |
|
|
223 |
{ |
|
|
224 |
$query_id = $this->query_result; |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
return ( $query_id ) ? @pg_fieldtype($query_id, $offset) : false; |
|
|
228 |
} |
|
|
229 |
|
|
|
230 |
function sql_fetchrow($query_id = 0) |
|
|
231 |
{ |
|
|
232 |
if( !$query_id ) |
|
|
233 |
{ |
|
|
234 |
$query_id = $this->query_result; |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
if($query_id) |
|
|
238 |
{ |
|
|
239 |
$this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]); |
|
|
240 |
|
|
|
241 |
if( $this->row ) |
|
|
242 |
{ |
|
|
243 |
$this->rownum[$query_id]++; |
|
|
244 |
return $this->row; |
|
|
245 |
} |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
return false; |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
function sql_fetchrowset($query_id = 0) |
|
|
252 |
{ |
|
|
253 |
if( !$query_id ) |
|
|
254 |
{ |
|
|
255 |
$query_id = $this->query_result; |
|
|
256 |
} |
|
|
257 |
|
|
|
258 |
if( $query_id ) |
|
|
259 |
{ |
|
|
260 |
unset($this->rowset[$query_id]); |
|
|
261 |
unset($this->row[$query_id]); |
|
|
262 |
$this->rownum[$query_id] = 0; |
|
|
263 |
|
|
|
264 |
while( $this->rowset = @pg_fetch_array($query_id, $this->rownum[$query_id], PGSQL_ASSOC) ) |
|
|
265 |
{ |
|
|
266 |
$result[] = $this->rowset; |
|
|
267 |
$this->rownum[$query_id]++; |
|
|
268 |
} |
|
|
269 |
|
|
|
270 |
return $result; |
|
|
271 |
} |
|
|
272 |
|
|
|
273 |
return false; |
|
|
274 |
} |
|
|
275 |
|
|
|
276 |
function sql_fetchfield($field, $row_offset=-1, $query_id = 0) |
|
|
277 |
{ |
|
|
278 |
if( !$query_id ) |
|
|
279 |
{ |
|
|
280 |
$query_id = $this->query_result; |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
if( $query_id ) |
|
|
284 |
{ |
|
|
285 |
if( $row_offset != -1 ) |
|
|
286 |
{ |
|
|
287 |
$this->row = @pg_fetch_array($query_id, $row_offset, PGSQL_ASSOC); |
|
|
288 |
} |
|
|
289 |
else |
|
|
290 |
{ |
|
|
291 |
if( $this->rownum[$query_id] ) |
|
|
292 |
{ |
|
|
293 |
$this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]-1, PGSQL_ASSOC); |
|
|
294 |
} |
|
|
295 |
else |
|
|
296 |
{ |
|
|
297 |
$this->row = @pg_fetch_array($query_id, $this->rownum[$query_id], PGSQL_ASSOC); |
|
|
298 |
|
|
|
299 |
if( $this->row ) |
|
|
300 |
{ |
|
|
301 |
$this->rownum[$query_id]++; |
|
|
302 |
} |
|
|
303 |
} |
|
|
304 |
} |
|
|
305 |
|
|
|
306 |
return $this->row[$field]; |
|
|
307 |
} |
|
|
308 |
|
|
|
309 |
return false; |
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
function sql_rowseek($offset, $query_id = 0) |
|
|
313 |
{ |
|
|
314 |
|
|
|
315 |
if(!$query_id) |
|
|
316 |
{ |
|
|
317 |
$query_id = $this->query_result; |
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
if( $query_id ) |
|
|
321 |
{ |
|
|
322 |
if( $offset > -1 ) |
|
|
323 |
{ |
|
|
324 |
$this->rownum[$query_id] = $offset; |
|
|
325 |
return true; |
|
|
326 |
} |
|
|
327 |
else |
|
|
328 |
{ |
|
|
329 |
return false; |
|
|
330 |
} |
|
|
331 |
} |
|
|
332 |
|
|
|
333 |
return false; |
|
|
334 |
} |
|
|
335 |
|
|
|
336 |
function sql_nextid() |
|
|
337 |
{ |
|
|
338 |
$query_id = $this->query_result; |
|
|
339 |
|
|
|
340 |
if($query_id && $this->last_query_text[$query_id] != "") |
|
|
341 |
{ |
|
|
342 |
if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text[$query_id], $tablename) ) |
|
|
343 |
{ |
|
|
344 |
$query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value"; |
|
|
345 |
$temp_q_id = @pg_exec($this->db_connect_id, $query); |
|
|
346 |
if( !$temp_q_id ) |
|
|
347 |
{ |
|
|
348 |
return false; |
|
|
349 |
} |
|
|
350 |
|
|
|
351 |
$temp_result = @pg_fetch_array($temp_q_id, 0, PGSQL_ASSOC); |
|
|
352 |
|
|
|
353 |
return ( $temp_result ) ? $temp_result['last_value'] : false; |
|
|
354 |
} |
|
|
355 |
} |
|
|
356 |
|
|
|
357 |
return false; |
|
|
358 |
} |
|
|
359 |
|
|
|
360 |
function sql_affectedrows($query_id = 0) |
|
|
361 |
{ |
|
|
362 |
if( !$query_id ) |
|
|
363 |
{ |
|
|
364 |
$query_id = $this->query_result; |
|
|
365 |
} |
|
|
366 |
|
|
|
367 |
return ( $query_id ) ? @pg_cmdtuples($query_id) : false; |
|
|
368 |
} |
|
|
369 |
|
|
|
370 |
function sql_freeresult($query_id = 0) |
|
|
371 |
{ |
|
|
372 |
if( !$query_id ) |
|
|
373 |
{ |
|
|
374 |
$query_id = $this->query_result; |
|
|
375 |
} |
|
|
376 |
|
|
|
377 |
return ( $query_id ) ? @pg_freeresult($query_id) : false; |
|
|
378 |
} |
|
|
379 |
|
|
|
380 |
function sql_error($query_id = 0) |
|
|
381 |
{ |
|
|
382 |
if( !$query_id ) |
|
|
383 |
{ |
|
|
384 |
$query_id = $this->query_result; |
|
|
385 |
} |
|
|
386 |
|
|
|
387 |
$result['message'] = @pg_errormessage($this->db_connect_id); |
|
|
388 |
$result['code'] = -1; |
|
|
389 |
|
|
|
390 |
return $result; |
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
} // class ... db_sql |
|
|
394 |
|
|
|
395 |
} // if ... defined |
|
|
396 |
|
|
|
397 |
?> |