Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/db/db_abstract.class.php
<?php
/**
 * warpe to the good db engine, depending on proto option.
 *  
 * @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_abstract {
	
	private $DBRef;
	private static $_DBS;
	private static $_ISCONN;
	
	public function __construct($DBRef){
		if ( !chrono::isinit('db') ){ chrono::init('db'); }
		$k = chrono::start('db');
		$this->DBRef = $DBRef;
		//load db_$proto object and keep them into a static var.
		if ( !isset(self::$_DBS[$this->DBRef]) ){
			if ( $vendor = self::getvendor() ){
				$class = 'db_'.$vendor;
				self::$_DBS[$this->DBRef] = new	$class($this->DBRef);
			} else {
				errors::raise('Vendor could not be determined for DBRef '.$this->DBRef, CORE_LOG_ERR, 'DB');
			}
		}
		chrono::pause('db', $k);
	}
	
	protected function getvendor(){ 
		return db_accessor::getvendor($this->DBRef);
	}
	
	private function preQchecks(){
		if ( !isset($this->DBRef) ){	
			return FALSE;
		} elseif ( !isset(self::$_DBS[$this->DBRef]) ){
			return FALSE;
		}
		return TRUE;
	}
		
	public function connect(){
		if( isset(self::$_ISCONN[$this->DBRef]) ){ return self::$_ISCONN[$this->DBRef]; }
		if ( !$this->preQchecks() ){ return errors::getnew('Pre Query Checks Failed.', CORE_LOG_ERR, 'DB'); }
		$r = self::$_DBS[$this->DBRef]->connect();
		self::$_ISCONN[$this->DBRef] = !CORE::isError($r);
		if ( CORE::isError($r) ){ errors::broadcast($r); 
		}
		return self::$_ISCONN[$this->DBRef];
	}
	
	public function isconnected(){
		if( isset(self::$_ISCONN[$this->DBRef]) ){ return self::$_ISCONN[$this->DBRef]; }
		return FALSE;
	}
	
	public function disconnect(){
		if ( !$this->preQchecks() ){ return errors::getnew('Pre Query Checks Failed.', CORE_LOG_ERR, 'DB'); }
		return self::$_DBS[$this->DBRef]->disconnect();
	}

	private function realcall($q, $method){
		$k = chrono::start('db');
		//$method = str_replace('db_', '', $method);
		if ( !$this->preQchecks() ){ return errors::getnew('Pre Query Checks Failed.', CORE_LOG_ERR, 'DB'); }
		if ( !method_exists(self::$_DBS[$this->DBRef], $method) ){
			return errors::getnew('Are you sure this vendor is valid, cause it do not have method "'.$method.'"', CORE_LOG_ERR, 'DB');
		}
		$r = self::$_DBS[$this->DBRef]->$method($q);
		chrono::pause('db', $k);
		return $r;
	}
	
	protected function checkval($q){ 		return $this->realcall($q, __FUNCTION__); }
	protected function create($q){ 			return $this->realcall($q, __FUNCTION__); }
	protected function insert($q){ 			return $this->realcall($q, __FUNCTION__); }
	protected function update($q){ 			return $this->realcall($q, __FUNCTION__); }
	protected function delete($q){ 			return $this->realcall($q, __FUNCTION__); }
	protected function drop($q){ 			return $this->realcall($q, __FUNCTION__); }
	protected function getonefield($q){ 	return $this->realcall($q, __FUNCTION__); }
	protected function getonerow($q){ 		return $this->realcall($q, __FUNCTION__); }
	protected function fetchrow($q){ 		return $this->realcall($q, __FUNCTION__); }
	
	protected function create_table($q){ 	return $this->realcall($q, 'insert'); }
	protected function table_exists($table){return $this->realcall($table, __FUNCTION__); }
	
}

return TRUE;
Return current item: Simple Way to Usenet