punbb/include/dblayer/sqlite.php
changeset 6 5e1f1e916419
parent 5 e3d7322305bf
child 7 98bbc533541c
equal deleted inserted replaced
5:e3d7322305bf 6:5e1f1e916419
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2005  Rickard Andersson (rickard@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 (defined('PUN_SHOW_QUERIES'))
       
   103 			$q_start = get_microtime();
       
   104 
       
   105 		if ($unbuffered)
       
   106 			$this->query_result = @sqlite_unbuffered_query($this->link_id, $sql);
       
   107 		else
       
   108 			$this->query_result = @sqlite_query($this->link_id, $sql);
       
   109 
       
   110 		if ($this->query_result)
       
   111 		{
       
   112 			if (defined('PUN_SHOW_QUERIES'))
       
   113 				$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
       
   114 
       
   115 			++$this->num_queries;
       
   116 
       
   117 			return $this->query_result;
       
   118 		}
       
   119 		else
       
   120 		{
       
   121 			if (defined('PUN_SHOW_QUERIES'))
       
   122 				$this->saved_queries[] = array($sql, 0);
       
   123 
       
   124 			$this->error_no = @sqlite_last_error($this->link_id);
       
   125 			$this->error_msg = @sqlite_error_string($this->error_no);
       
   126 
       
   127 			if ($this->in_transaction)
       
   128 				@sqlite_query($this->link_id, 'ROLLBACK');
       
   129 
       
   130 			--$this->in_transaction;
       
   131 
       
   132 			return false;
       
   133 		}
       
   134 	}
       
   135 
       
   136 
       
   137 	function result($query_id = 0, $row = 0)
       
   138 	{
       
   139 		if ($query_id)
       
   140 		{
       
   141 			if ($row != 0)
       
   142 				@sqlite_seek($query_id, $row);
       
   143 
       
   144 			return @current(@sqlite_current($query_id));
       
   145 		}
       
   146 		else
       
   147 			return false;
       
   148 	}
       
   149 
       
   150 
       
   151 	function fetch_assoc($query_id = 0)
       
   152 	{
       
   153 		if ($query_id)
       
   154 		{
       
   155 			$cur_row = @sqlite_fetch_array($query_id, SQLITE_ASSOC);
       
   156 			if ($cur_row)
       
   157 			{
       
   158 				// Horrible hack to get rid of table names and table aliases from the array keys
       
   159 				while (list($key, $value) = @each($cur_row))
       
   160 				{
       
   161 				    $dot_spot = strpos($key, '.');
       
   162 				    if ($dot_spot !== false)
       
   163 				    {
       
   164 				        unset($cur_row[$key]);
       
   165 				        $key = substr($key, $dot_spot+1);
       
   166 				        $cur_row[$key] = $value;
       
   167 				    }
       
   168 				}
       
   169 			}
       
   170 
       
   171 			return $cur_row;
       
   172 		}
       
   173 		else
       
   174 			return false;
       
   175 	}
       
   176 
       
   177 
       
   178 	function fetch_row($query_id = 0)
       
   179 	{
       
   180 		return ($query_id) ? @sqlite_fetch_array($query_id, SQLITE_NUM) : false;
       
   181 	}
       
   182 
       
   183 
       
   184 	function num_rows($query_id = 0)
       
   185 	{
       
   186 		return ($query_id) ? @sqlite_num_rows($query_id) : false;
       
   187 	}
       
   188 
       
   189 
       
   190 	function affected_rows()
       
   191 	{
       
   192 		return ($this->query_result) ? @sqlite_changes($this->query_result) : false;
       
   193 	}
       
   194 
       
   195 
       
   196 	function insert_id()
       
   197 	{
       
   198 		return ($this->link_id) ? @sqlite_last_insert_rowid($this->link_id) : false;
       
   199 	}
       
   200 
       
   201 
       
   202 	function get_num_queries()
       
   203 	{
       
   204 		return $this->num_queries;
       
   205 	}
       
   206 
       
   207 
       
   208 	function get_saved_queries()
       
   209 	{
       
   210 		return $this->saved_queries;
       
   211 	}
       
   212 
       
   213 
       
   214 	function free_result($query_id = false)
       
   215 	{
       
   216 		return true;
       
   217 	}
       
   218 
       
   219 
       
   220 	function escape($str)
       
   221 	{
       
   222 		return is_array($str) ? '' : sqlite_escape_string($str);
       
   223 	}
       
   224 
       
   225 
       
   226 	function error()
       
   227 	{
       
   228 		$result['error_sql'] = @current(@end($this->saved_queries));
       
   229 		$result['error_no'] = $this->error_no;
       
   230 		$result['error_msg'] = $this->error_msg;
       
   231 
       
   232 		return $result;
       
   233 	}
       
   234 
       
   235 
       
   236 	function close()
       
   237 	{
       
   238 		if ($this->link_id)
       
   239 		{
       
   240 			if ($this->in_transaction)
       
   241 			{
       
   242 				if (defined('PUN_SHOW_QUERIES'))
       
   243 					$this->saved_queries[] = array('COMMIT', 0);
       
   244 
       
   245 				@sqlite_query($this->link_id, 'COMMIT');
       
   246 			}
       
   247 
       
   248 			return @sqlite_close($this->link_id);
       
   249 		}
       
   250 		else
       
   251 			return false;
       
   252 	}
       
   253 }