bug3
[aidenligne_francais_universite.git] / fluxbb / include / dblayer / pgsql.php
1 <?php
2 /***********************************************************************
3
4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org)
5
6 This file is part of PunBB.
7
8 PunBB is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PunBB is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 MA 02111-1307 USA
22
23 ************************************************************************/
24
25
26 // Make sure we have built in support for PostgreSQL
27 if (!function_exists('pg_connect'))
28 exit('This PHP environment doesn\'t have PostgreSQL support built in. PostgreSQL support is required if you want to use a PostgreSQL database to run this forum. Consult the PHP documentation for further assistance.');
29
30
31 class DBLayer
32 {
33 var $prefix;
34 var $link_id;
35 var $query_result;
36 var $last_query_text = array();
37 var $in_transaction = 0;
38
39 var $saved_queries = array();
40 var $num_queries = 0;
41
42 var $error_no = false;
43 var $error_msg = 'Unknown';
44
45
46 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
47 {
48 $this->prefix = $db_prefix;
49
50 if ($db_host != '')
51 {
52 if (strpos($db_host, ':') !== false)
53 {
54 list($db_host, $dbport) = explode(':', $db_host);
55 $connect_str[] = 'host='.$db_host.' port='.$dbport;
56 }
57 else
58 {
59 if ($db_host != 'localhost')
60 $connect_str[] = 'host='.$db_host;
61 }
62 }
63
64 if ($db_name)
65 $connect_str[] = 'dbname='.$db_name;
66
67 if ($db_username != '')
68 $connect_str[] = 'user='.$db_username;
69
70 if ($db_password != '')
71 $connect_str[] = 'password='.$db_password;
72
73 if ($p_connect)
74 $this->link_id = @pg_pconnect(implode(' ', $connect_str));
75 else
76 $this->link_id = @pg_connect(implode(' ', $connect_str));
77
78 if (!$this->link_id)
79 error('Unable to connect to PostgreSQL server', __FILE__, __LINE__);
80 else
81 return $this->link_id;
82 }
83
84
85 function start_transaction()
86 {
87 ++$this->in_transaction;
88
89 return (@pg_query($this->link_id, 'BEGIN')) ? true : false;
90 }
91
92
93 function end_transaction()
94 {
95 --$this->in_transaction;
96
97 if (@pg_query($this->link_id, 'COMMIT'))
98 return true;
99 else
100 {
101 @pg_query($this->link_id, 'ROLLBACK');
102 return false;
103 }
104 }
105
106
107 function query($sql, $unbuffered = false) // $unbuffered is ignored since there is no pgsql_unbuffered_query()
108 {
109 if (strrpos($sql, 'LIMIT') !== false)
110 $sql = preg_replace('#LIMIT ([0-9]+),([ 0-9]+)#', 'LIMIT \\2 OFFSET \\1', $sql);
111
112 if (defined('PUN_SHOW_QUERIES'))
113 $q_start = get_microtime();
114
115 @pg_send_query($this->link_id, $sql);
116 $this->query_result = @pg_get_result($this->link_id);
117
118 if (pg_result_status($this->query_result) != PGSQL_FATAL_ERROR)
119 {
120 if (defined('PUN_SHOW_QUERIES'))
121 $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
122
123 ++$this->num_queries;
124
125 $this->last_query_text[$this->query_result] = $sql;
126
127 return $this->query_result;
128 }
129 else
130 {
131 if (defined('PUN_SHOW_QUERIES'))
132 $this->saved_queries[] = array($sql, 0);
133
134 $this->error_msg = @pg_result_error($this->query_result);
135
136 if ($this->in_transaction)
137 @pg_query($this->link_id, 'ROLLBACK');
138
139 --$this->in_transaction;
140
141 return false;
142 }
143 }
144
145
146 function result($query_id = 0, $row = 0)
147 {
148 return ($query_id) ? @pg_fetch_result($query_id, $row, 0) : false;
149 }
150
151
152 function fetch_assoc($query_id = 0)
153 {
154 return ($query_id) ? @pg_fetch_assoc($query_id) : false;
155 }
156
157
158 function fetch_row($query_id = 0)
159 {
160 return ($query_id) ? @pg_fetch_row($query_id) : false;
161 }
162
163
164 function num_rows($query_id = 0)
165 {
166 return ($query_id) ? @pg_num_rows($query_id) : false;
167 }
168
169
170 function affected_rows()
171 {
172 return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
173 }
174
175
176 function insert_id()
177 {
178 $query_id = $this->query_result;
179
180 if ($query_id && $this->last_query_text[$query_id] != '')
181 {
182 if (preg_match('/^INSERT INTO ([a-z0-9\_\-]+)/is', $this->last_query_text[$query_id], $table_name))
183 {
184 // Hack (don't ask)
185 if (substr($table_name[1], -6) == 'groups')
186 $table_name[1] .= '_g';
187
188 $temp_q_id = @pg_query($this->link_id, 'SELECT currval(\''.$table_name[1].'_id_seq\')');
189 return ($temp_q_id) ? intval(@pg_fetch_result($temp_q_id, 0)) : false;
190 }
191 }
192
193 return false;
194 }
195
196
197 function get_num_queries()
198 {
199 return $this->num_queries;
200 }
201
202
203 function get_saved_queries()
204 {
205 return $this->saved_queries;
206 }
207
208
209 function free_result($query_id = false)
210 {
211 if (!$query_id)
212 $query_id = $this->query_result;
213
214 return ($query_id) ? @pg_free_result($query_id) : false;
215 }
216
217
218 function escape($str)
219 {
220 return is_array($str) ? '' : pg_escape_string($str);
221 }
222
223
224 function error()
225 {
226 $result['error_sql'] = @current(@end($this->saved_queries));
227 $result['error_no'] = false;
228 /*
229 if (!empty($this->query_result))
230 {
231 $result['error_msg'] = trim(@pg_result_error($this->query_result));
232 if ($result['error_msg'] != '')
233 return $result;
234 }
235
236 $result['error_msg'] = (!empty($this->link_id)) ? trim(@pg_last_error($this->link_id)) : trim(@pg_last_error());
237 */
238 $result['error_msg'] = $this->error_msg;
239
240 return $result;
241 }
242
243
244 function close()
245 {
246 if ($this->link_id)
247 {
248 if ($this->in_transaction)
249 {
250 if (defined('PUN_SHOW_QUERIES'))
251 $this->saved_queries[] = array('COMMIT', 0);
252
253 @pg_query($this->link_id, 'COMMIT');
254 }
255
256 if ($this->query_result)
257 @pg_free_result($this->query_result);
258
259 return @pg_close($this->link_id);
260 }
261 else
262 return false;
263 }
264 }