|
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 SQLite |
|
27 if (!function_exists('sqlite_open')) |
|
28 exit('This PHP environment doesn\'t have SQLite support built in. SQLite support is required if you want to use a SQLite 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 $in_transaction = 0; |
|
37 |
|
38 var $saved_queries = array(); |
|
39 var $num_queries = 0; |
|
40 |
|
41 var $error_no = false; |
|
42 var $error_msg = 'Unknown'; |
|
43 |
|
44 |
|
45 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
|
46 { |
|
47 // Prepend $db_name with the path to the forum root directory |
|
48 $db_name = PUN_ROOT.$db_name; |
|
49 |
|
50 $this->prefix = $db_prefix; |
|
51 |
|
52 if (!file_exists($db_name)) |
|
53 { |
|
54 @touch($db_name); |
|
55 @chmod($db_name, 0666); |
|
56 if (!file_exists($db_name)) |
|
57 error('Unable to create new database \''.$db_name.'\'. Permission denied.', __FILE__, __LINE__); |
|
58 } |
|
59 |
|
60 if (!is_readable($db_name)) |
|
61 error('Unable to open database \''.$db_name.'\' for reading. Permission denied.', __FILE__, __LINE__); |
|
62 |
|
63 if (!is_writable($db_name)) |
|
64 error('Unable to open database \''.$db_name.'\' for writing. Permission denied.', __FILE__, __LINE__); |
|
65 |
|
66 if ($p_connect) |
|
67 $this->link_id = @sqlite_popen($db_name, 0666, $sqlite_error); |
|
68 else |
|
69 $this->link_id = @sqlite_open($db_name, 0666, $sqlite_error); |
|
70 |
|
71 if (!$this->link_id) |
|
72 error('Unable to open database \''.$db_name.'\'. SQLite reported: '.$sqlite_error, __FILE__, __LINE__); |
|
73 else |
|
74 return $this->link_id; |
|
75 } |
|
76 |
|
77 |
|
78 function start_transaction() |
|
79 { |
|
80 ++$this->in_transaction; |
|
81 |
|
82 return (@sqlite_query($this->link_id, 'BEGIN')) ? true : false; |
|
83 } |
|
84 |
|
85 |
|
86 function end_transaction() |
|
87 { |
|
88 --$this->in_transaction; |
|
89 |
|
90 if (@sqlite_query($this->link_id, 'COMMIT')) |
|
91 return true; |
|
92 else |
|
93 { |
|
94 @sqlite_query($this->link_id, 'ROLLBACK'); |
|
95 return false; |
|
96 } |
|
97 } |
|
98 |
|
99 |
|
100 function query($sql, $unbuffered = false) |
|
101 { |
|
102 if (strlen($sql) > 140000) |
|
103 exit('Insane query. Aborting.'); |
|
104 |
|
105 if (defined('PUN_SHOW_QUERIES')) |
|
106 $q_start = get_microtime(); |
|
107 |
|
108 if ($unbuffered) |
|
109 $this->query_result = @sqlite_unbuffered_query($this->link_id, $sql); |
|
110 else |
|
111 $this->query_result = @sqlite_query($this->link_id, $sql); |
|
112 |
|
113 if ($this->query_result) |
|
114 { |
|
115 if (defined('PUN_SHOW_QUERIES')) |
|
116 $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
|
117 |
|
118 ++$this->num_queries; |
|
119 |
|
120 return $this->query_result; |
|
121 } |
|
122 else |
|
123 { |
|
124 if (defined('PUN_SHOW_QUERIES')) |
|
125 $this->saved_queries[] = array($sql, 0); |
|
126 |
|
127 $this->error_no = @sqlite_last_error($this->link_id); |
|
128 $this->error_msg = @sqlite_error_string($this->error_no); |
|
129 |
|
130 if ($this->in_transaction) |
|
131 @sqlite_query($this->link_id, 'ROLLBACK'); |
|
132 |
|
133 --$this->in_transaction; |
|
134 |
|
135 return false; |
|
136 } |
|
137 } |
|
138 |
|
139 |
|
140 function query_build($query, $unbuffered = false) |
|
141 { |
|
142 $sql = ''; |
|
143 |
|
144 if (isset($query['SELECT'])) |
|
145 { |
|
146 $sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM']; |
|
147 |
|
148 if (isset($query['JOINS'])) |
|
149 { |
|
150 foreach ($query['JOINS'] as $cur_join) |
|
151 $sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON']; |
|
152 } |
|
153 |
|
154 if (!empty($query['WHERE'])) |
|
155 $sql .= ' WHERE '.$query['WHERE']; |
|
156 if (!empty($query['GROUP BY'])) |
|
157 $sql .= ' GROUP BY '.$query['GROUP BY']; |
|
158 if (!empty($query['HAVING'])) |
|
159 $sql .= ' HAVING '.$query['HAVING']; |
|
160 if (!empty($query['ORDER BY'])) |
|
161 $sql .= ' ORDER BY '.$query['ORDER BY']; |
|
162 if (!empty($query['LIMIT'])) |
|
163 $sql .= ' LIMIT '.$query['LIMIT']; |
|
164 } |
|
165 else if (isset($query['INSERT'])) |
|
166 { |
|
167 $sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; |
|
168 |
|
169 if (!empty($query['INSERT'])) |
|
170 $sql .= ' ('.$query['INSERT'].')'; |
|
171 |
|
172 $sql .= ' VALUES('.$query['VALUES'].')'; |
|
173 } |
|
174 else if (isset($query['UPDATE'])) |
|
175 { |
|
176 $query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE']; |
|
177 |
|
178 $sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET']; |
|
179 |
|
180 if (!empty($query['WHERE'])) |
|
181 $sql .= ' WHERE '.$query['WHERE']; |
|
182 } |
|
183 else if (isset($query['DELETE'])) |
|
184 { |
|
185 $sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE']; |
|
186 |
|
187 if (!empty($query['WHERE'])) |
|
188 $sql .= ' WHERE '.$query['WHERE']; |
|
189 } |
|
190 |
|
191 return $this->query($sql, $unbuffered); |
|
192 } |
|
193 |
|
194 |
|
195 function result($query_id = 0, $row = 0) |
|
196 { |
|
197 if ($query_id) |
|
198 { |
|
199 if ($row != 0) |
|
200 @sqlite_seek($query_id, $row); |
|
201 |
|
202 return @current(@sqlite_current($query_id)); |
|
203 } |
|
204 else |
|
205 return false; |
|
206 } |
|
207 |
|
208 |
|
209 function fetch_assoc($query_id = 0) |
|
210 { |
|
211 if ($query_id) |
|
212 { |
|
213 $cur_row = @sqlite_fetch_array($query_id, SQLITE_ASSOC); |
|
214 if ($cur_row) |
|
215 { |
|
216 // Horrible hack to get rid of table names and table aliases from the array keys |
|
217 while (list($key, $value) = @each($cur_row)) |
|
218 { |
|
219 $dot_spot = strpos($key, '.'); |
|
220 if ($dot_spot !== false) |
|
221 { |
|
222 unset($cur_row[$key]); |
|
223 $key = substr($key, $dot_spot+1); |
|
224 $cur_row[$key] = $value; |
|
225 } |
|
226 } |
|
227 } |
|
228 |
|
229 return $cur_row; |
|
230 } |
|
231 else |
|
232 return false; |
|
233 } |
|
234 |
|
235 |
|
236 function fetch_row($query_id = 0) |
|
237 { |
|
238 return ($query_id) ? @sqlite_fetch_array($query_id, SQLITE_NUM) : false; |
|
239 } |
|
240 |
|
241 |
|
242 function num_rows($query_id = 0) |
|
243 { |
|
244 return ($query_id) ? @sqlite_num_rows($query_id) : false; |
|
245 } |
|
246 |
|
247 |
|
248 function affected_rows() |
|
249 { |
|
250 return ($this->query_result) ? @sqlite_changes($this->query_result) : false; |
|
251 } |
|
252 |
|
253 |
|
254 function insert_id() |
|
255 { |
|
256 return ($this->link_id) ? @sqlite_last_insert_rowid($this->link_id) : false; |
|
257 } |
|
258 |
|
259 |
|
260 function get_num_queries() |
|
261 { |
|
262 return $this->num_queries; |
|
263 } |
|
264 |
|
265 |
|
266 function get_saved_queries() |
|
267 { |
|
268 return $this->saved_queries; |
|
269 } |
|
270 |
|
271 |
|
272 function free_result($query_id = false) |
|
273 { |
|
274 return true; |
|
275 } |
|
276 |
|
277 |
|
278 function escape($str) |
|
279 { |
|
280 return is_array($str) ? '' : sqlite_escape_string($str); |
|
281 } |
|
282 |
|
283 |
|
284 function error() |
|
285 { |
|
286 $result['error_sql'] = @current(@end($this->saved_queries)); |
|
287 $result['error_no'] = $this->error_no; |
|
288 $result['error_msg'] = $this->error_msg; |
|
289 |
|
290 return $result; |
|
291 } |
|
292 |
|
293 |
|
294 function close() |
|
295 { |
|
296 if ($this->link_id) |
|
297 { |
|
298 if ($this->in_transaction) |
|
299 { |
|
300 if (defined('PUN_SHOW_QUERIES')) |
|
301 $this->saved_queries[] = array('COMMIT', 0); |
|
302 |
|
303 @sqlite_query($this->link_id, 'COMMIT'); |
|
304 } |
|
305 |
|
306 return @sqlite_close($this->link_id); |
|
307 } |
|
308 else |
|
309 return false; |
|
310 } |
|
311 |
|
312 |
|
313 function table_exists($table_name) |
|
314 { |
|
315 $result = $this->query('SELECT 1 FROM sqlite_master WHERE name = \''.$this->escape($table_name).'\' AND type=\'table\''); |
|
316 return $this->num_rows($result) > 0; |
|
317 } |
|
318 |
|
319 |
|
320 function field_exists($table_name, $field_name) |
|
321 { |
|
322 $result = $this->query('SELECT sql FROM sqlite_master WHERE name = \''.$this->escape($table_name).'\' AND type=\'table\''); |
|
323 if (!$this->num_rows($result)) |
|
324 return false; |
|
325 |
|
326 return preg_match('/[\r\n]'.preg_quote($field_name).' /', $this->result($result)); |
|
327 } |
|
328 |
|
329 |
|
330 function index_exists($table_name, $index_name) |
|
331 { |
|
332 $result = $this->query('SELECT 1 FROM sqlite_master WHERE tbl_name = \''.$this->escape($table_name).'\' AND name = \''.$this->escape($index_name).'\' AND type=\'index\''); |
|
333 return $this->num_rows($result) > 0; |
|
334 } |
|
335 |
|
336 |
|
337 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null) |
|
338 { |
|
339 if ($this->field_exists($table_name, $field_name)) |
|
340 return; |
|
341 |
|
342 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
|
343 $default_value = '\''.$this->escape($default_value).'\''; |
|
344 |
|
345 $this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ')) or error(__FILE__, __LINE__); |
|
346 } |
|
347 |
|
348 |
|
349 function drop_field($table_name, $field_name) |
|
350 { |
|
351 // No DROP column in SQLite |
|
352 return; |
|
353 } |
|
354 } |