Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/db/vendors/db_pgsql.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_pgsql extends db_accessor {
	
	private $phpext = 'pgsql';
	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 PostgreSQL extention could be found or loaded on this system,
										plz contact the administrator and ask for pgsql 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');
		}
		//TODO find a good way to rebuild the connection string for pgsql
		//keywords are : host, port, dbname, user, password, connect_timeout, options, sslmode, requiressl
		$str='host=%host% port=%port% dbname=%db% user=%user% password=%pass%';
		foreach( $conf as $opt => $val ){
			$str = str_replace("%$key%", $val, $str);  
		}
		$this->_HDL  = @pg_pconnect($str);
		if ( !$this->_HDL ){
			return errors::getnew('Connection could not be established with pgsql database server with DBRef "'.$this->DBRef.'"', CORE_LOG_ERR, 'DB');
		}
		return TRUE;
	}
	
	protected function disconnect(){
		if ( $this->isconnected() ){
			return @pg_close($this->_HDL);
		}
		return TRUE;
	}
	
	protected function isconnected(){
		if ( !$this->checkphpext($this->phpext) ){ return FALSE; }
	 	return ( @pg_connection_status($this->_HDL) === PGSQL_CONNECTION_OK );
	}
	
	protected 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 = @pg_query($this->_HDL, $q) ){
			return errors::getnew('SQL query "'.$q.'" has returned an error : 
									'.pg_last_error($this->_HDL), CORE_LOG_ERR, 'DB');
		}
		return $result;
	}
	
	protected function insert($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	protected function update($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	protected function delete($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		return TRUE;
	}
	
	protected function getone($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		//dunno what to return need to print
	}
	
	protected function getall($q){
		$result = $this->sendquery($q);
		unset($q);
		if ( CORE::isError($result) ){
			return $result;
		}
		//dunno what to return need to print
	}
	
	protected function fetch($q){
		//one static by, 
		static $bool=TRUE;
		static $result;
		
		if ( $bool ){
			$result = $this->sendquery($q);
			unset($q);
			if ( CORE::isError($result) ){
				return $result;
			}
		}
		$bool = FALSE;
		$row = @pg_fetch_array($result);
		if ( $row === FALSE ){
			$bool = TRUE;
			unset($result);
			return FALSE;
		}
		return $row;
	}
	
}
Return current item: Simple Way to Usenet