6
|
1 |
<?php
|
|
2 |
/***********************************************************************
|
|
3 |
|
|
4 |
Copyright (C) 2002-2008 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()
|
|
47 |
{
|
|
48 |
global $db;
|
|
49 |
$this->prefix = table_prefix . 'pun_';
|
|
50 |
$this->link_id =& $db->_conn;
|
|
51 |
}
|
|
52 |
|
|
53 |
|
|
54 |
function start_transaction()
|
|
55 |
{
|
|
56 |
++$this->in_transaction;
|
|
57 |
|
|
58 |
return (@pg_query($this->link_id, 'BEGIN')) ? true : false;
|
|
59 |
}
|
|
60 |
|
|
61 |
|
|
62 |
function end_transaction()
|
|
63 |
{
|
|
64 |
--$this->in_transaction;
|
|
65 |
|
|
66 |
if (@pg_query($this->link_id, 'COMMIT'))
|
|
67 |
return true;
|
|
68 |
else
|
|
69 |
{
|
|
70 |
@pg_query($this->link_id, 'ROLLBACK');
|
|
71 |
return false;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
function query($sql, $unbuffered = false) // $unbuffered is ignored since there is no pgsql_unbuffered_query()
|
|
77 |
{
|
|
78 |
if (strlen($sql) > 140000)
|
|
79 |
exit('Insane query. Aborting.');
|
|
80 |
|
|
81 |
if (strrpos($sql, 'LIMIT') !== false)
|
|
82 |
$sql = preg_replace('#LIMIT ([0-9]+),([ 0-9]+)#', 'LIMIT \\2 OFFSET \\1', $sql);
|
|
83 |
|
|
84 |
if (defined('PUN_SHOW_QUERIES'))
|
|
85 |
$q_start = get_microtime();
|
|
86 |
|
|
87 |
@pg_send_query($this->link_id, $sql);
|
|
88 |
$this->query_result = @pg_get_result($this->link_id);
|
|
89 |
|
|
90 |
if (pg_result_status($this->query_result) != PGSQL_FATAL_ERROR)
|
|
91 |
{
|
|
92 |
if (defined('PUN_SHOW_QUERIES'))
|
|
93 |
$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
|
|
94 |
|
|
95 |
++$this->num_queries;
|
|
96 |
|
|
97 |
$this->last_query_text[$this->query_result] = $sql;
|
|
98 |
|
|
99 |
return $this->query_result;
|
|
100 |
}
|
|
101 |
else
|
|
102 |
{
|
|
103 |
if (defined('PUN_SHOW_QUERIES'))
|
|
104 |
$this->saved_queries[] = array($sql, 0);
|
|
105 |
|
|
106 |
$this->error_msg = @pg_result_error($this->query_result);
|
|
107 |
|
|
108 |
if ($this->in_transaction)
|
|
109 |
@pg_query($this->link_id, 'ROLLBACK');
|
|
110 |
|
|
111 |
--$this->in_transaction;
|
|
112 |
|
|
113 |
return false;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
|
|
118 |
function query_build($query, $unbuffered = false)
|
|
119 |
{
|
|
120 |
$sql = '';
|
|
121 |
|
|
122 |
if (isset($query['SELECT']))
|
|
123 |
{
|
|
124 |
$sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM'];
|
|
125 |
|
|
126 |
if (isset($query['JOINS']))
|
|
127 |
{
|
|
128 |
foreach ($query['JOINS'] as $cur_join)
|
|
129 |
$sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON'];
|
|
130 |
}
|
|
131 |
|
|
132 |
if (!empty($query['WHERE']))
|
|
133 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
134 |
if (!empty($query['GROUP BY']))
|
|
135 |
$sql .= ' GROUP BY '.$query['GROUP BY'];
|
|
136 |
if (!empty($query['HAVING']))
|
|
137 |
$sql .= ' HAVING '.$query['HAVING'];
|
|
138 |
if (!empty($query['ORDER BY']))
|
|
139 |
$sql .= ' ORDER BY '.$query['ORDER BY'];
|
|
140 |
if (!empty($query['LIMIT']))
|
|
141 |
$sql .= ' LIMIT '.$query['LIMIT'];
|
|
142 |
}
|
|
143 |
else if (isset($query['INSERT']))
|
|
144 |
{
|
|
145 |
$sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO'];
|
|
146 |
|
|
147 |
if (!empty($query['INSERT']))
|
|
148 |
$sql .= ' ('.$query['INSERT'].')';
|
|
149 |
|
|
150 |
$sql .= ' VALUES('.$query['VALUES'].')';
|
|
151 |
}
|
|
152 |
else if (isset($query['UPDATE']))
|
|
153 |
{
|
|
154 |
$query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE'];
|
|
155 |
|
|
156 |
$sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET'];
|
|
157 |
|
|
158 |
if (!empty($query['WHERE']))
|
|
159 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
160 |
}
|
|
161 |
else if (isset($query['DELETE']))
|
|
162 |
{
|
|
163 |
$sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE'];
|
|
164 |
|
|
165 |
if (!empty($query['WHERE']))
|
|
166 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
167 |
}
|
|
168 |
|
|
169 |
return $this->query($sql, $unbuffered);
|
|
170 |
}
|
|
171 |
|
|
172 |
|
|
173 |
function result($query_id = 0, $row = 0)
|
|
174 |
{
|
|
175 |
return ($query_id) ? @pg_fetch_result($query_id, $row, 0) : false;
|
|
176 |
}
|
|
177 |
|
|
178 |
|
|
179 |
function fetch_assoc($query_id = 0)
|
|
180 |
{
|
|
181 |
return ($query_id) ? @pg_fetch_assoc($query_id) : false;
|
|
182 |
}
|
|
183 |
|
|
184 |
|
|
185 |
function fetch_row($query_id = 0)
|
|
186 |
{
|
|
187 |
return ($query_id) ? @pg_fetch_row($query_id) : false;
|
|
188 |
}
|
|
189 |
|
|
190 |
|
|
191 |
function num_rows($query_id = 0)
|
|
192 |
{
|
|
193 |
return ($query_id) ? @pg_num_rows($query_id) : false;
|
|
194 |
}
|
|
195 |
|
|
196 |
|
|
197 |
function affected_rows()
|
|
198 |
{
|
|
199 |
return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
|
|
200 |
}
|
|
201 |
|
|
202 |
|
|
203 |
function insert_id()
|
|
204 |
{
|
|
205 |
$query_id = $this->query_result;
|
|
206 |
|
|
207 |
if ($query_id && $this->last_query_text[$query_id] != '')
|
|
208 |
{
|
|
209 |
if (preg_match('/^INSERT INTO ([a-z0-9\_\-]+)/is', $this->last_query_text[$query_id], $table_name))
|
|
210 |
{
|
|
211 |
// Hack (don't ask)
|
|
212 |
if (substr($table_name[1], -6) == 'groups')
|
|
213 |
$table_name[1] .= '_g';
|
|
214 |
|
|
215 |
$temp_q_id = @pg_query($this->link_id, 'SELECT currval(\''.$table_name[1].'_id_seq\')');
|
|
216 |
return ($temp_q_id) ? intval(@pg_fetch_result($temp_q_id, 0)) : false;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
return false;
|
|
221 |
}
|
|
222 |
|
|
223 |
|
|
224 |
function get_num_queries()
|
|
225 |
{
|
|
226 |
return $this->num_queries;
|
|
227 |
}
|
|
228 |
|
|
229 |
|
|
230 |
function get_saved_queries()
|
|
231 |
{
|
|
232 |
return $this->saved_queries;
|
|
233 |
}
|
|
234 |
|
|
235 |
|
|
236 |
function free_result($query_id = false)
|
|
237 |
{
|
|
238 |
if (!$query_id)
|
|
239 |
$query_id = $this->query_result;
|
|
240 |
|
|
241 |
return ($query_id) ? @pg_free_result($query_id) : false;
|
|
242 |
}
|
|
243 |
|
|
244 |
|
|
245 |
function escape($str)
|
|
246 |
{
|
|
247 |
return is_array($str) ? '' : pg_escape_string($str);
|
|
248 |
}
|
|
249 |
|
|
250 |
|
|
251 |
function error()
|
|
252 |
{
|
|
253 |
$result['error_sql'] = @current(@end($this->saved_queries));
|
|
254 |
$result['error_no'] = false;
|
|
255 |
/*
|
|
256 |
if (!empty($this->query_result))
|
|
257 |
{
|
|
258 |
$result['error_msg'] = trim(@pg_result_error($this->query_result));
|
|
259 |
if ($result['error_msg'] != '')
|
|
260 |
return $result;
|
|
261 |
}
|
|
262 |
|
|
263 |
$result['error_msg'] = (!empty($this->link_id)) ? trim(@pg_last_error($this->link_id)) : trim(@pg_last_error());
|
|
264 |
*/
|
|
265 |
$result['error_msg'] = $this->error_msg;
|
|
266 |
|
|
267 |
return $result;
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
function close()
|
|
272 |
{
|
|
273 |
if ($this->link_id)
|
|
274 |
{
|
|
275 |
if ($this->in_transaction)
|
|
276 |
{
|
|
277 |
if (defined('PUN_SHOW_QUERIES'))
|
|
278 |
$this->saved_queries[] = array('COMMIT', 0);
|
|
279 |
|
|
280 |
@pg_query($this->link_id, 'COMMIT');
|
|
281 |
}
|
|
282 |
|
|
283 |
if ($this->query_result)
|
|
284 |
@pg_free_result($this->query_result);
|
|
285 |
}
|
|
286 |
return true;
|
|
287 |
}
|
|
288 |
|
|
289 |
|
|
290 |
function table_exists($table_name)
|
|
291 |
{
|
|
292 |
$result = $this->query('SELECT 1 FROM pg_class WHERE relname = \''.$this->escape($table_name).'\'');
|
|
293 |
return $this->num_rows($result) > 0;
|
|
294 |
}
|
|
295 |
|
|
296 |
|
|
297 |
function field_exists($table_name, $field_name)
|
|
298 |
{
|
|
299 |
$result = $this->query('SELECT 1 FROM pg_class c INNER JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname = \''.$this->escape($table_name).'\' AND a.attname = \''.$this->escape($field_name).'\'');
|
|
300 |
return $this->num_rows($result) > 0;
|
|
301 |
}
|
|
302 |
|
|
303 |
|
|
304 |
function index_exists($table_name, $index_name)
|
|
305 |
{
|
|
306 |
$result = $this->query('SELECT 1 FROM pg_index i INNER JOIN pg_class c1 ON c1.oid = i.indrelid INNER JOIN pg_class c2 ON c2.oid = i.indexrelid WHERE c1.relname = \''.$this->escape($table_name).'\' AND c2.relname = \''.$this->escape($index_name).'\'');
|
|
307 |
return $this->num_rows($result) > 0;
|
|
308 |
}
|
|
309 |
|
|
310 |
|
|
311 |
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null)
|
|
312 |
{
|
|
313 |
if ($this->field_exists($table_name, $field_name))
|
|
314 |
return;
|
|
315 |
|
|
316 |
$field_type = str_replace(array('TINY', 'FLOAT'), array('SMALL', 'REAL'), strtoupper($field_type));
|
|
317 |
$field_type = preg_replace('/(.*?INT)(\([0-9]+)\)/', '$1', $field_type);
|
|
318 |
|
|
319 |
$this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type) or error(__FILE__, __LINE__);
|
|
320 |
|
|
321 |
if ($default_value !== null)
|
|
322 |
{
|
|
323 |
if (!is_int($default_value) && !is_float($default_value))
|
|
324 |
$default_value = '\''.$this->escape($default_value).'\'';
|
|
325 |
|
|
326 |
$this->query('ALTER TABLE '.$table_name.' ALTER '.$field_name.' SET DEFAULT '.$default_value) or error(__FILE__, __LINE__);
|
|
327 |
$this->query('UPDATE '.$table_name.' SET '.$field_name.'='.$default_value) or error(__FILE__, __LINE__);
|
|
328 |
}
|
|
329 |
|
|
330 |
if (!$allow_null)
|
|
331 |
$this->query('ALTER TABLE '.$table_name.' ALTER '.$field_name.' SET NOT NULL') or error(__FILE__, __LINE__);
|
|
332 |
}
|
|
333 |
|
|
334 |
|
|
335 |
function drop_field($table_name, $field_name)
|
|
336 |
{
|
|
337 |
if (!$this->field_exists($table_name, $field_name))
|
|
338 |
return;
|
|
339 |
|
|
340 |
$this->query('ALTER TABLE '.$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__);
|
|
341 |
}
|
|
342 |
}
|