5 * @version $Id: sqlite.php,v 1.2 2005/06/10 08:52:03 devalley Exp $
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined("SQL_LAYER"))
17 define("SQL_LAYER","sqlite");
20 * @package dbal_sqlite
21 * Sqlite Database Abstraction Layer
27 var $return_on_error = false;
28 var $transaction = false;
32 var $open_queries = array();
34 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port, $persistency = false)
36 $this->persistency = $persistency;
37 $this->user = $sqluser;
38 $this->server = $sqlserver . (($port) ? ':' . $port : '');
39 $this->dbname = $database;
41 $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0, $error) : @sqlite_open($this->server, 0, $error);
43 return ($this->db_connect_id) ? true : $error;
49 if (!$this->db_connect_id)
54 return @sqlite_close($this->db_connect_id);
57 function sql_return_on_error($fail = false)
59 $this->return_on_error = $fail;
62 function sql_num_queries()
64 return $this->num_queries;
67 function sql_transaction($status = 'begin')
72 $this->transaction = true;
73 $result = @sqlite_query('BEGIN', $this->db_connect_id);
77 $this->transaction = false;
78 $result = @sqlite_query('COMMIT', $this->db_connect_id);
82 $this->transaction = false;
83 $result = @sqlite_query('ROLLBACK', $this->db_connect_id);
94 function sql_query($query = '', $expire_time = 0)
100 $query = preg_replace('#FROM \((.*?)\)(,|[\n\t ]+?WHERE) #s', 'FROM \1\2 ', $query);
102 if (!$expire_time || !$cache->sql_load($query, $expire_time))
106 $cache_result = true;
109 $this->query_result = false;
110 $this->num_queries++;
112 if (!empty($_GET['explain']))
116 $curtime = explode(' ', microtime());
117 $curtime = $curtime[0] + $curtime[1] - $starttime;
120 if (!($this->query_result = @sqlite_query($query, $this->db_connect_id)))
122 $this->sql_error($query);
125 if (!empty($_GET['explain']))
127 $endtime = explode(' ', microtime());
128 $endtime = $endtime[0] + $endtime[1] - $starttime;
130 $this->sql_report .= "<pre>Query:\t" . htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n\t", $query)) . "\n\n";
132 if ($this->query_result)
134 $this->sql_report .= "Time before: $curtime\nTime after: $endtime\nElapsed time: <b>" . ($endtime - $curtime) . "</b>\n</pre>";
138 $error = $this->sql_error();
139 $this->sql_report .= '<b>FAILED</b> - SQLite ' . $error['code'] . ': ' . htmlspecialchars($error['message']) . '<br><br><pre>';
142 $this->sql_time += $endtime - $curtime;
144 if (preg_match('#^SELECT#', $query))
147 if ($result = @sqlite_query("EXPLAIN $query", $this->db_connect_id))
149 while ($row = @sqlite_fetch_array($result, @sqlite_ASSOC))
151 if (!$html_table && sizeof($row))
154 $this->sql_report .= "<table width=100% border=1 cellpadding=2 cellspacing=1>\n";
155 $this->sql_report .= "<tr>\n<td><b>" . implode("</b></td>\n<td><b>", array_keys($row)) . "</b></td>\n</tr>\n";
157 $this->sql_report .= "<tr>\n<td>" . implode(" </td>\n<td>", array_values($row)) . " </td>\n</tr>\n";
163 $this->sql_report .= '</table><br>';
167 $this->sql_report .= "<hr>\n";
170 if (preg_match('#^SELECT#', $query))
172 $this->open_queries[] = $this->query_result;
176 if (!empty($cache_result))
178 $cache->sql_save($query, $this->query_result);
186 return ($this->query_result) ? $this->query_result : false;
189 function sql_query_limit($query, $total, $offset = 0, $expire_time = 0)
193 $this->query_result = false;
195 $query .= ' LIMIT ' . ((!empty($offset)) ? $total . ' OFFSET ' . $offset : $total);
197 return $this->sql_query($query, $expire_time);
205 // Idea for this from Ikonboard
206 function sql_build_array($query, $assoc_ary = false)
208 if (!is_array($assoc_ary))
215 if ($query == 'INSERT')
217 foreach ($assoc_ary as $key => $var)
225 elseif (is_string($var))
227 $values[] = "'" . $this->sql_escape($var) . "'";
231 $values[] = (is_bool($var)) ? intval($var) : $var;
235 $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
237 else if ($query == 'UPDATE')
240 foreach ($assoc_ary as $key => $var)
244 $values[] = "$key = NULL";
246 elseif (is_string($var))
248 $values[] = "$key = '" . $this->sql_escape($var) . "'";
252 $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var";
255 $query = implode(', ', $values);
261 // Other query methods
263 // NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ...
264 // don't want this here by a middle Milestone
265 function sql_numrows($query_id = false)
269 $query_id = $this->query_result;
272 return ($query_id) ? @sqlite_num_rows($query_id) : false;
275 function sql_affectedrows()
277 return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
280 function sql_fetchrow($query_id = 0)
286 $query_id = $this->query_result;
289 if ($cache->sql_exists($query_id))
291 return $cache->sql_fetchrow($query_id);
294 return ($query_id) ? @sqlite_fetch_array($query_id, @sqlite_ASSOC) : false;
297 function sql_fetchrowset($query_id = 0)
301 $query_id = $this->query_result;
306 unset($this->rowset[$query_id]);
307 unset($this->row[$query_id]);
308 while ($this->rowset[$query_id] = @sqlite_fetch_array($query_id, @sqlite_ASSOC))
310 $result[] = $this->rowset[$query_id];
320 function sql_fetchfield($field, $rownum = -1, $query_id = 0)
324 $query_id = $this->query_result;
329 return ($rownum > -1) ? ((@sqlite_seek($query_id, $rownum)) ? @sqlite_column($query_id, $field) : false) : @sqlite_column($query_id, $field);
333 function sql_rowseek($rownum, $query_id = 0)
337 $query_id = $this->query_result;
340 return ($query_id) ? @sqlite_seek($query_id, $rownum) : false;
343 function sql_nextid()
345 return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
348 function sql_freeresult($query_id = false)
353 function sql_escape($msg)
355 return @sqlite_escape_string(stripslashes($msg));
358 function sql_error($sql = '')
360 if (!$this->return_on_error)
362 $this_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
363 $this_page .= '&' . ((!empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : $_ENV['QUERY_STRING']);
365 $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @sqlite_error_string(@sqlite_last_error($this->db_connect_id)) . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />';
367 if ($this->transaction)
369 $this->sql_transaction('rollback');
372 trigger_error($message, E_USER_ERROR);
376 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
377 'code' => @sqlite_last_error($this->db_connect_id)