443
by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0 |
1 |
<?php
|
2 |
/**
|
|
3 |
*
|
|
4 |
* @package dbal
|
|
5 |
* @version $Id: sqlite.php,v 1.38 2007/10/05 14:36:32 acydburn Exp $
|
|
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 |
}
|
|
45 |
||
46 |
||
47 |
return ($this->db_connect_id) ? true : array('message' => $error); |
|
48 |
}
|
|
49 |
||
50 |
/**
|
|
51 |
* Version information about used database
|
|
52 |
*/
|
|
53 |
function sql_server_info() |
|
54 |
{
|
|
55 |
return 'SQLite ' . @sqlite_libversion(); |
|
56 |
}
|
|
57 |
||
58 |
/**
|
|
59 |
* SQL Transaction
|
|
60 |
* @access private
|
|
61 |
*/
|
|
62 |
function _sql_transaction($status = 'begin') |
|
63 |
{
|
|
64 |
switch ($status) |
|
65 |
{
|
|
66 |
case 'begin': |
|
67 |
return @sqlite_query('BEGIN', $this->db_connect_id); |
|
68 |
break; |
|
69 |
||
70 |
case 'commit': |
|
71 |
return @sqlite_query('COMMIT', $this->db_connect_id); |
|
72 |
break; |
|
73 |
||
74 |
case 'rollback': |
|
75 |
return @sqlite_query('ROLLBACK', $this->db_connect_id); |
|
76 |
break; |
|
77 |
}
|
|
78 |
||
79 |
return true; |
|
80 |
}
|
|
81 |
||
82 |
/**
|
|
83 |
* Base query method
|
|
84 |
*
|
|
85 |
* @param string $query Contains the SQL query which shall be executed
|
|
86 |
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|
87 |
* @return mixed When casted to bool the returned value returns true on success and false on failure
|
|
88 |
*
|
|
89 |
* @access public
|
|
90 |
*/
|
|
91 |
function sql_query($query = '', $cache_ttl = 0) |
|
92 |
{
|
|
93 |
if ($query != '') |
|
94 |
{
|
|
95 |
global $cache; |
|
96 |
||
97 |
// EXPLAIN only in extra debug mode
|
|
98 |
if (defined('DEBUG_EXTRA')) |
|
99 |
{
|
|
100 |
$this->sql_report('start', $query); |
|
101 |
}
|
|
102 |
||
103 |
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; |
|
104 |
$this->sql_add_num_queries($this->query_result); |
|
105 |
||
106 |
if ($this->query_result === false) |
|
107 |
{
|
|
108 |
if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false) |
|
109 |
{
|
|
110 |
$this->sql_error($query); |
|
111 |
}
|
|
112 |
||
113 |
if (defined('DEBUG_EXTRA')) |
|
114 |
{
|
|
115 |
$this->sql_report('stop', $query); |
|
116 |
}
|
|
117 |
||
118 |
if ($cache_ttl && method_exists($cache, 'sql_save')) |
|
119 |
{
|
|
120 |
$this->open_queries[(int) $this->query_result] = $this->query_result; |
|
121 |
$cache->sql_save($query, $this->query_result, $cache_ttl); |
|
122 |
}
|
|
123 |
else if (strpos($query, 'SELECT') === 0 && $this->query_result) |
|
124 |
{
|
|
125 |
$this->open_queries[(int) $this->query_result] = $this->query_result; |
|
126 |
}
|
|
127 |
}
|
|
128 |
else if (defined('DEBUG_EXTRA')) |
|
129 |
{
|
|
130 |
$this->sql_report('fromcache', $query); |
|
131 |
}
|
|
132 |
}
|
|
133 |
else
|
|
134 |
{
|
|
135 |
return false; |
|
136 |
}
|
|
137 |
||
138 |
return ($this->query_result) ? $this->query_result : false; |
|
139 |
}
|
|
140 |
||
141 |
/**
|
|
142 |
* Build LIMIT query
|
|
143 |
*/
|
|
144 |
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) |
|
145 |
{
|
|
146 |
$this->query_result = false; |
|
147 |
||
148 |
// if $total is set to 0 we do not want to limit the number of rows
|
|
149 |
if ($total == 0) |
|
150 |
{
|
|
151 |
$total = -1; |
|
152 |
}
|
|
153 |
||
154 |
$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); |
|
155 |
||
156 |
return $this->sql_query($query, $cache_ttl); |
|
157 |
}
|
|
158 |
||
159 |
/**
|
|
160 |
* Return number of affected rows
|
|
161 |
*/
|
|
162 |
function sql_affectedrows() |
|
163 |
{
|
|
164 |
return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false; |
|
165 |
}
|
|
166 |
||
167 |
/**
|
|
168 |
* Fetch current row
|
|
169 |
*/
|
|
170 |
function sql_fetchrow($query_id = false) |
|
171 |
{
|
|
172 |
global $cache; |
|
173 |
||
174 |
if ($query_id === false) |
|
175 |
{
|
|
176 |
$query_id = $this->query_result; |
|
177 |
}
|
|
178 |
||
179 |
if (isset($cache->sql_rowset[$query_id])) |
|
180 |
{
|
|
181 |
return $cache->sql_fetchrow($query_id); |
|
182 |
}
|
|
183 |
||
184 |
return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false; |
|
185 |
}
|
|
186 |
||
187 |
/**
|
|
188 |
* Seek to given row number
|
|
189 |
* rownum is zero-based
|
|
190 |
*/
|
|
191 |
function sql_rowseek($rownum, &$query_id) |
|
192 |
{
|
|
193 |
global $cache; |
|
194 |
||
195 |
if ($query_id === false) |
|
196 |
{
|
|
197 |
$query_id = $this->query_result; |
|
198 |
}
|
|
199 |
||
200 |
if (isset($cache->sql_rowset[$query_id])) |
|
201 |
{
|
|
202 |
return $cache->sql_rowseek($rownum, $query_id); |
|
203 |
}
|
|
204 |
||
205 |
return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false; |
|
206 |
}
|
|
207 |
||
208 |
/**
|
|
209 |
* Get last inserted id after insert statement
|
|
210 |
*/
|
|
211 |
function sql_nextid() |
|
212 |
{
|
|
213 |
return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false; |
|
214 |
}
|
|
215 |
||
216 |
/**
|
|
217 |
* Free sql result
|
|
218 |
*/
|
|
219 |
function sql_freeresult($query_id = false) |
|
220 |
{
|
|
221 |
global $cache; |
|
222 |
||
223 |
if ($query_id === false) |
|
224 |
{
|
|
225 |
$query_id = $this->query_result; |
|
226 |
}
|
|
227 |
||
228 |
if (isset($cache->sql_rowset[$query_id])) |
|
229 |
{
|
|
230 |
return $cache->sql_freeresult($query_id); |
|
231 |
}
|
|
232 |
||
233 |
return true; |
|
234 |
}
|
|
235 |
||
236 |
/**
|
|
237 |
* Escape string used in sql query
|
|
238 |
*/
|
|
239 |
function sql_escape($msg) |
|
240 |
{
|
|
241 |
return @sqlite_escape_string($msg); |
|
242 |
}
|
|
243 |
||
244 |
/**
|
|
245 |
* Correctly adjust LIKE expression for special characters
|
|
246 |
* For SQLite an underscore is a not-known character... this may change with SQLite3
|
|
247 |
*/
|
|
248 |
function sql_like_expression($expression) |
|
249 |
{
|
|
250 |
// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
|
|
251 |
// We only catch * and ? here, not the character map possible on file globbing.
|
|
252 |
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression); |
|
253 |
||
254 |
$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression); |
|
255 |
$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression); |
|
256 |
||
257 |
return 'GLOB \'' . $this->sql_escape($expression) . '\''; |
|
258 |
}
|
|
259 |
||
260 |
/**
|
|
261 |
* return sql error array
|
|
262 |
* @access private
|
|
263 |
*/
|
|
264 |
function _sql_error() |
|
265 |
{
|
|
266 |
return array( |
|
267 |
'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)), |
|
268 |
'code' => @sqlite_last_error($this->db_connect_id) |
|
269 |
);
|
|
270 |
}
|
|
271 |
||
272 |
/**
|
|
273 |
* Build db-specific query data
|
|
274 |
* @access private
|
|
275 |
*/
|
|
276 |
function _sql_custom_build($stage, $data) |
|
277 |
{
|
|
278 |
return $data; |
|
279 |
}
|
|
280 |
||
281 |
/**
|
|
282 |
* Close sql connection
|
|
283 |
* @access private
|
|
284 |
*/
|
|
285 |
function _sql_close() |
|
286 |
{
|
|
287 |
return @sqlite_close($this->db_connect_id); |
|
288 |
}
|
|
289 |
||
290 |
/**
|
|
291 |
* Build db-specific report
|
|
292 |
* @access private
|
|
293 |
*/
|
|
294 |
function _sql_report($mode, $query = '') |
|
295 |
{
|
|
296 |
switch ($mode) |
|
297 |
{
|
|
298 |
case 'start': |
|
299 |
break; |
|
300 |
||
301 |
case 'fromcache': |
|
302 |
$endtime = explode(' ', microtime()); |
|
303 |
$endtime = $endtime[0] + $endtime[1]; |
|
304 |
||
305 |
$result = @sqlite_query($query, $this->db_connect_id); |
|
306 |
while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC)) |
|
307 |
{
|
|
308 |
// Take the time spent on parsing rows into account
|
|
309 |
}
|
|
310 |
||
311 |
$splittime = explode(' ', microtime()); |
|
312 |
$splittime = $splittime[0] + $splittime[1]; |
|
313 |
||
314 |
$this->sql_report('record_fromcache', $query, $endtime, $splittime); |
|
315 |
||
316 |
break; |
|
317 |
}
|
|
318 |
}
|
|
319 |
}
|
|
320 |
||
321 |
?>
|