|
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 MySQL |
|
27 if (!function_exists('mysql_connect')) |
|
28 exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL 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 |
|
37 var $saved_queries = array(); |
|
38 var $num_queries = 0; |
|
39 |
|
40 |
|
41 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
|
42 { |
|
43 $this->prefix = $db_prefix; |
|
44 |
|
45 if ($p_connect) |
|
46 $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); |
|
47 else |
|
48 $this->link_id = @mysql_connect($db_host, $db_username, $db_password); |
|
49 |
|
50 if ($this->link_id) |
|
51 { |
|
52 if (@mysql_select_db($db_name, $this->link_id)) |
|
53 return $this->link_id; |
|
54 else |
|
55 error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
|
56 } |
|
57 else |
|
58 error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
|
59 |
|
60 // Setup the client-server character set (UTF-8) |
|
61 if (!defined('PUN_NO_SET_NAMES')) |
|
62 mysql_query('SET NAMES \'utf8\'', $this->link_id) or error(__FILE__, __LINE__); |
|
63 } |
|
64 |
|
65 |
|
66 function start_transaction() |
|
67 { |
|
68 return; |
|
69 } |
|
70 |
|
71 |
|
72 function end_transaction() |
|
73 { |
|
74 return; |
|
75 } |
|
76 |
|
77 |
|
78 function query($sql, $unbuffered = false) |
|
79 { |
|
80 if (strlen($sql) > 140000) |
|
81 exit('Insane query. Aborting.'); |
|
82 |
|
83 if (defined('PUN_SHOW_QUERIES')) |
|
84 $q_start = get_microtime(); |
|
85 |
|
86 if ($unbuffered) |
|
87 $this->query_result = @mysql_unbuffered_query($sql, $this->link_id); |
|
88 else |
|
89 $this->query_result = @mysql_query($sql, $this->link_id); |
|
90 |
|
91 if ($this->query_result) |
|
92 { |
|
93 if (defined('PUN_SHOW_QUERIES')) |
|
94 $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
|
95 |
|
96 ++$this->num_queries; |
|
97 |
|
98 return $this->query_result; |
|
99 } |
|
100 else |
|
101 { |
|
102 if (defined('PUN_SHOW_QUERIES')) |
|
103 $this->saved_queries[] = array($sql, 0); |
|
104 |
|
105 return false; |
|
106 } |
|
107 } |
|
108 |
|
109 |
|
110 function query_build($query, $unbuffered = false) |
|
111 { |
|
112 $sql = ''; |
|
113 |
|
114 if (isset($query['SELECT'])) |
|
115 { |
|
116 $sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM']; |
|
117 |
|
118 if (isset($query['JOINS'])) |
|
119 { |
|
120 foreach ($query['JOINS'] as $cur_join) |
|
121 $sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON']; |
|
122 } |
|
123 |
|
124 if (!empty($query['WHERE'])) |
|
125 $sql .= ' WHERE '.$query['WHERE']; |
|
126 if (!empty($query['GROUP BY'])) |
|
127 $sql .= ' GROUP BY '.$query['GROUP BY']; |
|
128 if (!empty($query['HAVING'])) |
|
129 $sql .= ' HAVING '.$query['HAVING']; |
|
130 if (!empty($query['ORDER BY'])) |
|
131 $sql .= ' ORDER BY '.$query['ORDER BY']; |
|
132 if (!empty($query['LIMIT'])) |
|
133 $sql .= ' LIMIT '.$query['LIMIT']; |
|
134 } |
|
135 else if (isset($query['INSERT'])) |
|
136 { |
|
137 $sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; |
|
138 |
|
139 if (!empty($query['INSERT'])) |
|
140 $sql .= ' ('.$query['INSERT'].')'; |
|
141 |
|
142 $sql .= ' VALUES('.$query['VALUES'].')'; |
|
143 } |
|
144 else if (isset($query['UPDATE'])) |
|
145 { |
|
146 $query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE']; |
|
147 |
|
148 if (isset($query['PARAMS']['LOW_PRIORITY'])) |
|
149 $query['UPDATE'] = 'LOW_PRIORITY '.$query['UPDATE']; |
|
150 |
|
151 $sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET']; |
|
152 |
|
153 if (!empty($query['WHERE'])) |
|
154 $sql .= ' WHERE '.$query['WHERE']; |
|
155 } |
|
156 else if (isset($query['DELETE'])) |
|
157 { |
|
158 $sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE']; |
|
159 |
|
160 if (!empty($query['WHERE'])) |
|
161 $sql .= ' WHERE '.$query['WHERE']; |
|
162 } |
|
163 else if (isset($query['REPLACE'])) |
|
164 { |
|
165 $sql = 'REPLACE INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; |
|
166 |
|
167 if (!empty($query['REPLACE'])) |
|
168 $sql .= ' ('.$query['REPLACE'].')'; |
|
169 |
|
170 $sql .= ' VALUES('.$query['VALUES'].')'; |
|
171 } |
|
172 |
|
173 return $this->query($sql, $unbuffered); |
|
174 } |
|
175 |
|
176 |
|
177 function result($query_id = 0, $row = 0) |
|
178 { |
|
179 return ($query_id) ? @mysql_result($query_id, $row) : false; |
|
180 } |
|
181 |
|
182 |
|
183 function fetch_assoc($query_id = 0) |
|
184 { |
|
185 return ($query_id) ? @mysql_fetch_assoc($query_id) : false; |
|
186 } |
|
187 |
|
188 |
|
189 function fetch_row($query_id = 0) |
|
190 { |
|
191 return ($query_id) ? @mysql_fetch_row($query_id) : false; |
|
192 } |
|
193 |
|
194 |
|
195 function num_rows($query_id = 0) |
|
196 { |
|
197 return ($query_id) ? @mysql_num_rows($query_id) : false; |
|
198 } |
|
199 |
|
200 |
|
201 function affected_rows() |
|
202 { |
|
203 return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false; |
|
204 } |
|
205 |
|
206 |
|
207 function insert_id() |
|
208 { |
|
209 return ($this->link_id) ? @mysql_insert_id($this->link_id) : false; |
|
210 } |
|
211 |
|
212 |
|
213 function get_num_queries() |
|
214 { |
|
215 return $this->num_queries; |
|
216 } |
|
217 |
|
218 |
|
219 function get_saved_queries() |
|
220 { |
|
221 return $this->saved_queries; |
|
222 } |
|
223 |
|
224 |
|
225 function free_result($query_id = false) |
|
226 { |
|
227 return ($query_id) ? @mysql_free_result($query_id) : false; |
|
228 } |
|
229 |
|
230 |
|
231 function escape($str) |
|
232 { |
|
233 if (is_array($str)) |
|
234 return ''; |
|
235 else if (function_exists('mysql_real_escape_string')) |
|
236 return mysql_real_escape_string($str, $this->link_id); |
|
237 else |
|
238 return mysql_escape_string($str); |
|
239 } |
|
240 |
|
241 |
|
242 function error() |
|
243 { |
|
244 $result['error_sql'] = @current(@end($this->saved_queries)); |
|
245 $result['error_no'] = @mysql_errno($this->link_id); |
|
246 $result['error_msg'] = @mysql_error($this->link_id); |
|
247 |
|
248 return $result; |
|
249 } |
|
250 |
|
251 |
|
252 function close() |
|
253 { |
|
254 if ($this->link_id) |
|
255 { |
|
256 if ($this->query_result) |
|
257 @mysql_free_result($this->query_result); |
|
258 |
|
259 return @mysql_close($this->link_id); |
|
260 } |
|
261 else |
|
262 return false; |
|
263 } |
|
264 |
|
265 |
|
266 function table_exists($table_name) |
|
267 { |
|
268 $result = $this->query('SHOW TABLES LIKE \''.$this->escape($table_name).'\''); |
|
269 return $this->num_rows($result) > 0; |
|
270 } |
|
271 |
|
272 |
|
273 function field_exists($table_name, $field_name) |
|
274 { |
|
275 $result = $this->query('SHOW COLUMNS FROM '.$table_name.' LIKE \''.$this->escape($field_name).'\''); |
|
276 return $this->num_rows($result) > 0; |
|
277 } |
|
278 |
|
279 |
|
280 function index_exists($table_name, $index_name) |
|
281 { |
|
282 $exists = false; |
|
283 |
|
284 $result = $this->query('SHOW INDEX FROM '.$table_name); |
|
285 while ($cur_index = $this->fetch_assoc($result)) |
|
286 { |
|
287 if ($cur_index['Key_name'] == $index_name) |
|
288 { |
|
289 $exists = true; |
|
290 break; |
|
291 } |
|
292 } |
|
293 |
|
294 return $exists; |
|
295 } |
|
296 |
|
297 |
|
298 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null) |
|
299 { |
|
300 if ($this->field_exists($table_name, $field_name)) |
|
301 return; |
|
302 |
|
303 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
|
304 $default_value = '\''.$this->escape($default_value).'\''; |
|
305 |
|
306 $this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) or error(__FILE__, __LINE__); |
|
307 } |
|
308 |
|
309 |
|
310 function drop_field($table_name, $field_name) |
|
311 { |
|
312 if (!$this->field_exists($table_name, $field_name)) |
|
313 return; |
|
314 |
|
315 $this->query('ALTER TABLE '.$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__); |
|
316 } |
|
317 } |