Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/db/vendors/db_mssql.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_mssql extends db_accessor {
	
	private $phpext = 'mssql';
	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 mssql extention could be found or loaded on this system,
										plz contact the administrator and ask for mssql 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['hostspec']) ){
			if ( isset($conf['port']) ){
				//we happend port to host if set
				$conf['hostspec'] .= ':'.$conf['port'];
			}
			if ( isset($conf['user']) ){
				if ( !isset($conf['pass']) ){
					//host, user but no pass
					$this->_HDL  = @mssql_pconnect($conf['hostspec'], $conf['user']);
				} else {
					//host, user, pass
					$this->_HDL  = @mssql_pconnect($conf['hostspec'], $conf['user'], $conf['pass']);
				}
			} else {
				//host only
				$this->_HDL  = @mssql_pconnect($conf['hostspec']);
			}
		} else {
			//nothing we try login with default settings
			$this->_HDL  = @mssql_pconnect();
		}
		if ( !$this->_HDL ){
			return errors::getnew('Connection could not be established with mssql database server with DBRef "'.$this->DBRef.'"', CORE_LOG_ERR, 'DB');
		}
		if ( isset($conf['database']) ){
			if ( ! mssql_select_db($conf['database'], $this->_HDL) ){
				return errors::getnew('Could not use the database "'.$conf['database'].'" using DBRef "'.$this->DBRef.'" :
										'.mssql_get_last_message(), CORE_LOG_ERR, 'DB');
			}
		}
		$this->isconn = TRUE;
		return TRUE;
	}
	
	protected function disconnect(){
		if ( $this->isconnected() ){
			$this->isconn = FALSE;
			return @mssql_close($this->_HDL);
		}
		return TRUE;
	}
	
	protected function isconnected(){
		return $this->isconn;
	}
	
	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 = @mysql_query($this->_HDL, $q) ){
			return errors::getnew('SQL query "'.$q.'" has returned an error : '.mssql_get_last_message(), 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 = @mssql_fetch_array($result);
		if ( $row === FALSE ){
			$bool = TRUE;
			unset($result);
			return FALSE;
		}
		return $row;
	}
	
}
Return current item: Simple Way to Usenet