Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/db/vendors/db_sqlite.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class db_sqlite extends db_accessor {
	
	private $phpext = 'SQLite';
	private $isconn = FALSE;
	private $_HDL;
	
	public function __construct($DBRef){
		parent::__construct($DBRef);
	}
	
	public function connect(){
		if ( !CORE::isphpModLoaded($this->phpext) ){
			if ( ! CORE::LoadphpMod($this->phpext) ){
				return errors::getnew('No sqlite extention could be found or loaded on this system,
										plz contact the administrator and ask for sqlite support in php or
										simply use an other vendor.', CORE_LOG_ERR, 'DB');
			}
		}
		if ( !$conf = $this->getconf() ){ 
			return errors::getnew('No configuration could be loaded for DBRef "'.$this->DBRef.'",
									plz check your db.conf.php file or load a valid db configuration with that reference.', CORE_LOG_ERR, 'DB');
		}
		if ( !isset($conf['path']) ){
			return errors::getnew('Configuration for DBRef "'.$this->DBRef.'" is incomplete,
									plz set the "path" key in your conf for this DBRef.', CORE_LOG_ERR, 'DB');
		}
		if ( !isset($conf['mode']) ){
			return errors::getnew('Configuration for DBRef "'.$this->DBRef.'" is incomplete,
									plz set the "mode" key in your conf for this DBRef.', CORE_LOG_ERR, 'DB');
		}
		$this->_HDL  = @sqlite_popen($conf['path'], $conf['mode'], $err);	
		if ( !$this->_HDL ){
			return errors::getnew('Connection could not be established w database DBRef "'.$this->DBRef.'" : '.$err, CORE_LOG_ERR, 'DB');
		}
		$this->isconn = TRUE;
		errors::raise('Database Connection established to DBRef : '.$this->DBRef, CORE_LOG_DEBUG, 'DB');
		return TRUE;
	}
	
	public function disconnect(){
		if ( $this->isconnected() ){
			$this->isconn = FALSE;
			return @sqlite_close($this->_HDL);
		}
		return TRUE;
	}
	
	public function isconnected(){
		return $this->isconn;
	}
	
	public function checkval($val){
		return sqlite_escape_string($val);
	}
	
	public function sendquery($q){
		if ( empty($q) ){ 
			return errors::getnew('SQL query is empty', CORE_LOG_WARNING, 'DB');
		}
		if ( !$this->isconnected() ){
			$try = $this->connect();
			if ( CORE::isError($try) ){
				return errors::getnew('You are about to execute an SQL query on a handler that as not yet been connected, 
										the trouble is that it was not able to connect to the server :
										'.$try->getmsg(), CORE_LOG_WARNING, 'DB');
			}
		}
		if ( !$result = sqlite_query($this->_HDL, $q, SQLITE_ASSOC, $err) ){
			return errors::getnew('SQL query "'.$q.'" has returned an error : '.$err, CORE_LOG_ERR, 'DB');
		}
		return $result;
	}
	
	public function insert($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	public function update($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	public function delete($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	public function drop($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){ return $result; }
		return TRUE;
	}
	
	public function getonefield($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		$buf = sqlite_fetch_array($result);
		return $buf[0];
	}
	
	
	public function getonerow($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		$r = sqlite_fetch_array($result);
		return $r;
	}
	
	public function fetchrow($q){
		
		static $HDL;
		
		$hash = CORE::hash($q);
		if ( !isset($HDL[$hash]) ){
			$HDL[$hash] = $this->sendquery($q);
			unset($q);
			if ( CORE::isError($HDL[$hash]) ){
				$b=$HDL[$hash];
				unset($HDL[$hash]);
				return $b;
			}
		}
		$row = sqlite_fetch_array($HDL[$hash]);
		if ( $row === FALSE ){
			unset($HDL[$hash]);
			return FALSE;
		}
		return $row;
	}
	
	public function table_exists($table){
		$q = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){ return $result; }
		while ($row=sqlite_fetch_array($result)) {
			if ($row['name'] == $table){  return TRUE; }
		}
		return FALSE;
	}
}
Return current item: Simple Way to Usenet