|
1 <?php |
|
2 /** |
|
3 * |
|
4 * @package dbal |
|
5 * @version $Id$ |
|
6 * @copyright (c) 2005 phpBB Group |
|
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
8 * |
|
9 */ |
|
10 |
|
11 /** |
|
12 * @ignore |
|
13 */ |
|
14 if (!defined('IN_PHPBB')) |
|
15 { |
|
16 exit; |
|
17 } |
|
18 |
|
19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx); |
|
20 |
|
21 /** |
|
22 * Sqlite Database Abstraction Layer |
|
23 * Minimum Requirement: 2.8.2+ |
|
24 * @package dbal |
|
25 */ |
|
26 class dbal_sqlite extends dbal |
|
27 { |
|
28 /** |
|
29 * Connect to server |
|
30 */ |
|
31 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) |
|
32 { |
|
33 $this->persistency = $persistency; |
|
34 $this->user = $sqluser; |
|
35 $this->server = $sqlserver . (($port) ? ':' . $port : ''); |
|
36 $this->dbname = $database; |
|
37 |
|
38 $error = ''; |
|
39 $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error); |
|
40 |
|
41 if ($this->db_connect_id) |
|
42 { |
|
43 @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id); |
|
44 // @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id); |
|
45 } |
|
46 |
|
47 return ($this->db_connect_id) ? true : array('message' => $error); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Version information about used database |
|
52 * @param bool $raw if true, only return the fetched sql_server_version |
|
53 * @param bool $use_cache if true, it is safe to retrieve the stored value from the cache |
|
54 * @return string sql server version |
|
55 */ |
|
56 function sql_server_info($raw = false, $use_cache = true) |
|
57 { |
|
58 global $cache; |
|
59 |
|
60 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false) |
|
61 { |
|
62 $result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id); |
|
63 $row = @sqlite_fetch_array($result, SQLITE_ASSOC); |
|
64 |
|
65 $this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0; |
|
66 |
|
67 if (!empty($cache) && $use_cache) |
|
68 { |
|
69 $cache->put('sqlite_version', $this->sql_server_version); |
|
70 } |
|
71 } |
|
72 |
|
73 return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version; |
|
74 } |
|
75 |
|
76 /** |
|
77 * SQL Transaction |
|
78 * @access private |
|
79 */ |
|
80 function _sql_transaction($status = 'begin') |
|
81 { |
|
82 switch ($status) |
|
83 { |
|
84 case 'begin': |
|
85 return @sqlite_query('BEGIN', $this->db_connect_id); |
|
86 break; |
|
87 |
|
88 case 'commit': |
|
89 return @sqlite_query('COMMIT', $this->db_connect_id); |
|
90 break; |
|
91 |
|
92 case 'rollback': |
|
93 return @sqlite_query('ROLLBACK', $this->db_connect_id); |
|
94 break; |
|
95 } |
|
96 |
|
97 return true; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Base query method |
|
102 * |
|
103 * @param string $query Contains the SQL query which shall be executed |
|
104 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache |
|
105 * @return mixed When casted to bool the returned value returns true on success and false on failure |
|
106 * |
|
107 * @access public |
|
108 */ |
|
109 function sql_query($query = '', $cache_ttl = 0) |
|
110 { |
|
111 if ($query != '') |
|
112 { |
|
113 global $cache; |
|
114 |
|
115 // EXPLAIN only in extra debug mode |
|
116 if (defined('DEBUG_EXTRA')) |
|
117 { |
|
118 $this->sql_report('start', $query); |
|
119 } |
|
120 |
|
121 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; |
|
122 $this->sql_add_num_queries($this->query_result); |
|
123 |
|
124 if ($this->query_result === false) |
|
125 { |
|
126 if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false) |
|
127 { |
|
128 $this->sql_error($query); |
|
129 } |
|
130 |
|
131 if (defined('DEBUG_EXTRA')) |
|
132 { |
|
133 $this->sql_report('stop', $query); |
|
134 } |
|
135 |
|
136 if ($cache_ttl && method_exists($cache, 'sql_save')) |
|
137 { |
|
138 $this->open_queries[(int) $this->query_result] = $this->query_result; |
|
139 $cache->sql_save($query, $this->query_result, $cache_ttl); |
|
140 } |
|
141 else if (strpos($query, 'SELECT') === 0 && $this->query_result) |
|
142 { |
|
143 $this->open_queries[(int) $this->query_result] = $this->query_result; |
|
144 } |
|
145 } |
|
146 else if (defined('DEBUG_EXTRA')) |
|
147 { |
|
148 $this->sql_report('fromcache', $query); |
|
149 } |
|
150 } |
|
151 else |
|
152 { |
|
153 return false; |
|
154 } |
|
155 |
|
156 return $this->query_result; |
|
157 } |
|
158 |
|
159 /** |
|
160 * Build LIMIT query |
|
161 */ |
|
162 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) |
|
163 { |
|
164 $this->query_result = false; |
|
165 |
|
166 // if $total is set to 0 we do not want to limit the number of rows |
|
167 if ($total == 0) |
|
168 { |
|
169 $total = -1; |
|
170 } |
|
171 |
|
172 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); |
|
173 |
|
174 return $this->sql_query($query, $cache_ttl); |
|
175 } |
|
176 |
|
177 /** |
|
178 * Return number of affected rows |
|
179 */ |
|
180 function sql_affectedrows() |
|
181 { |
|
182 return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Fetch current row |
|
187 */ |
|
188 function sql_fetchrow($query_id = false) |
|
189 { |
|
190 global $cache; |
|
191 |
|
192 if ($query_id === false) |
|
193 { |
|
194 $query_id = $this->query_result; |
|
195 } |
|
196 |
|
197 if (isset($cache->sql_rowset[$query_id])) |
|
198 { |
|
199 return $cache->sql_fetchrow($query_id); |
|
200 } |
|
201 |
|
202 return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Seek to given row number |
|
207 * rownum is zero-based |
|
208 */ |
|
209 function sql_rowseek($rownum, &$query_id) |
|
210 { |
|
211 global $cache; |
|
212 |
|
213 if ($query_id === false) |
|
214 { |
|
215 $query_id = $this->query_result; |
|
216 } |
|
217 |
|
218 if (isset($cache->sql_rowset[$query_id])) |
|
219 { |
|
220 return $cache->sql_rowseek($rownum, $query_id); |
|
221 } |
|
222 |
|
223 return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false; |
|
224 } |
|
225 |
|
226 /** |
|
227 * Get last inserted id after insert statement |
|
228 */ |
|
229 function sql_nextid() |
|
230 { |
|
231 return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false; |
|
232 } |
|
233 |
|
234 /** |
|
235 * Free sql result |
|
236 */ |
|
237 function sql_freeresult($query_id = false) |
|
238 { |
|
239 global $cache; |
|
240 |
|
241 if ($query_id === false) |
|
242 { |
|
243 $query_id = $this->query_result; |
|
244 } |
|
245 |
|
246 if (isset($cache->sql_rowset[$query_id])) |
|
247 { |
|
248 return $cache->sql_freeresult($query_id); |
|
249 } |
|
250 |
|
251 return true; |
|
252 } |
|
253 |
|
254 /** |
|
255 * Escape string used in sql query |
|
256 */ |
|
257 function sql_escape($msg) |
|
258 { |
|
259 return @sqlite_escape_string($msg); |
|
260 } |
|
261 |
|
262 /** |
|
263 * Correctly adjust LIKE expression for special characters |
|
264 * For SQLite an underscore is a not-known character... this may change with SQLite3 |
|
265 */ |
|
266 function sql_like_expression($expression) |
|
267 { |
|
268 // Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it! |
|
269 // We only catch * and ? here, not the character map possible on file globbing. |
|
270 $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression); |
|
271 |
|
272 $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression); |
|
273 $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression); |
|
274 |
|
275 return 'GLOB \'' . $this->sql_escape($expression) . '\''; |
|
276 } |
|
277 |
|
278 /** |
|
279 * return sql error array |
|
280 * @access private |
|
281 */ |
|
282 function _sql_error() |
|
283 { |
|
284 return array( |
|
285 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)), |
|
286 'code' => @sqlite_last_error($this->db_connect_id) |
|
287 ); |
|
288 } |
|
289 |
|
290 /** |
|
291 * Build db-specific query data |
|
292 * @access private |
|
293 */ |
|
294 function _sql_custom_build($stage, $data) |
|
295 { |
|
296 return $data; |
|
297 } |
|
298 |
|
299 /** |
|
300 * Close sql connection |
|
301 * @access private |
|
302 */ |
|
303 function _sql_close() |
|
304 { |
|
305 return @sqlite_close($this->db_connect_id); |
|
306 } |
|
307 |
|
308 /** |
|
309 * Build db-specific report |
|
310 * @access private |
|
311 */ |
|
312 function _sql_report($mode, $query = '') |
|
313 { |
|
314 switch ($mode) |
|
315 { |
|
316 case 'start': |
|
317 break; |
|
318 |
|
319 case 'fromcache': |
|
320 $endtime = explode(' ', microtime()); |
|
321 $endtime = $endtime[0] + $endtime[1]; |
|
322 |
|
323 $result = @sqlite_query($query, $this->db_connect_id); |
|
324 while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC)) |
|
325 { |
|
326 // Take the time spent on parsing rows into account |
|
327 } |
|
328 |
|
329 $splittime = explode(' ', microtime()); |
|
330 $splittime = $splittime[0] + $splittime[1]; |
|
331 |
|
332 $this->sql_report('record_fromcache', $query, $endtime, $splittime); |
|
333 |
|
334 break; |
|
335 } |
|
336 } |
|
337 } |
|
338 |
|
339 ?> |