
<?php

/*#######################################################
# dbAccess.php
# 
# Deze klasse handelt alle database acties af
# Methoden: 
# 	dbConnect()
# 	dbClose()
# 	executeSQL()
# 	eof(), bof()
# 	isEmpty()
# 	moveNext(), movePrevious(), moveFirst(), moveLast()
# 	numRows(), affectedRows()
# 	fetchArray(), fetchAssoc()
# 	getLastInsertedId()
# 	startTransaction(), commit(), rollback(), endTransaction() - enkel voor MSSQL
# 
# Auteur: Addow (info at addow dot be)
# Datum aangemaakt: 7 juli 2006
# Datum laatst gewijzigd: 5 november 2006
#######################################################*/

class dbAccess {
	// Configureer variabelen
	private $m_bPersistent = false; // Persistente verbindingen
	private $m_sQuery = "";         // Query om geopend te worden
	private $m_rConnection = null;  // Databaseverbinding
	private $m_nCurrentRow = 0;     // Huidige database rij
	private $m_nRecordSetRow = 0;   // Laatste opgehaalde rij
	private $m_rResult = null;      // Resultaat
	
	private $m_sDBHost = "";  // Hostnaam
	private $m_sDBName = ""; // Database naam
	private $m_sUser = ""; // Gebruikersnaam
	private $m_sPassword = "";   // Wachtwoord
	
	function __construct($p_sQuery = "", $p_bPersistent = false, $p_sDBName = "") {
		global $g_sDBHost;
		global $g_sUser;
		global $g_sPassword;
		global $g_sDBName;
		
		$this->m_sDBHost = $g_sDBHost;
		$this->m_sDBName = $g_sDBName;
		$this->m_sUser = $g_sUser;
		$this->m_sPassword = $g_sPassword;
		
		// Alle argumenten overzetten naar member variabelen
		$this->m_bPersistent = $p_bPersistent;
		if($p_sDBName != "")
			$this->m_sDBName = $p_sDBName;
		
		// Verbindt met de database
		$this->dbConnect();
		
		// Wanneer een query is meegegeven, voer deze uit...
		if($p_sQuery != "")
			$this->executeSQL($p_sQuery);
	}
	
	function _destruct() {
		$this->dbClose();
	}
	
	public function dbConnect() {
		if(!$this->m_rConnection) {
			// Geen SQL Server verbinding opgezet
			// Maak verbinding...
			if($this->m_bPersistent) {
				// Open een persistente verbinding
				$this->m_rConnection = @mysql_pconnect($this->m_sDBHost, $this->m_sUser, $this->m_sPassword);
				@mysql_select_db($this->m_sDBName);
			} else {
				// Open een gewone verbinding
				$this->m_rConnection = @mysql_connect($this->m_sDBHost, $this->m_sUser, $this->m_sPassword);
				@mysql_select_db($this->m_sDBName);
			}
		}
		
		if(!$this->m_rConnection) {
			$sErrorMessage = "SQL Error [".mysql_errno()."] ".mysql_error();
			throw new Exception($sErrorMessage);
		}
	}
	
	public function dbClose() {
		if($this->m_rConnection) {
			@mysql_close($this->m_rConnection);
			$this->m_rConnection = null;
		}
	}
	
	public function executeSQL($p_sQuery) {
		// Bewaar de query
		$this->m_sQuery = $p_sQuery;
		
		try {
			// Open verbinding indien deze nog niet eerder geopend is
			if(!$this->m_rConnection)
				$this->dbConnect();
			
			// Probeer de query uit te voeren
			$this->m_rResult = @mysql_query($this->m_sQuery, $this->m_rConnection);
		} catch(Exception $e) {
			// hergenereer de exception
			throw new Exception($e->getMessage());
		}
		
		// Controleer op fouten, benodigd aangezien msql_query vanaf deze fase geen exceptions meer genereert.
		if($this->m_rResult == false)
		{
			$nErrorID = mysql_errno($this->m_rConnection);
			$sErrorMessage = "SQL Error: [".$nErrorID."[ ".mysql_error($this->m_rConnection)." [".$this->m_sQuery."]";
			throw new Exception($sErrorMessage, $nErrorID);
		}
		
		// Plaats huidige rij op 0
		$this->m_nCurrentRow = 0;
		return true;
	}
	
	public function eof() {
		$rows = mysql_num_rows($this->m_rResult);
		
		if($rows == 0)
			return true;
		if($rows == $this->m_nCurrentRow)
			return true;
		return false;
	}
	
	public function bof() {
		if($this->m_nCurrentRow == 0)
			return true;
	}
	
	public function isEmpty() {
		if(mysql_num_rows($this->m_rResult) == 0)
			return true;
		return false;
	}
	
	public function moveNext() {
		$this->m_nCurrentRow++;
	}
	
	public function movePrevious() {
		$this->m_nCurrentRow--;
		if($this->m_nCurrentRow < 0)
			$this->m_nCurrentRow = 0;
	}
	
	public function moveFirst() {
		$this->m_nCurrentRow = 0;
	}
	
	public function moveLast() {
		try {
			$this->m_nCurrentRow = mysql_num_rows($this->m_rResult) - 1;
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function numRows() {
		$nResult = 0;
		try {
			$nResult = mysql_num_rows($this->m_rResult);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function affectedRows() {
		return mysql_affected_rows();
	}
	
	public function fetchArray() {
		$aCurrentRec = array();
		
		try {
			if($this->m_nRecordSetRow != $this->m_nCurrentRow) {
				mysql_data_seek($this->m_rResult, $this->m_nCurrentRow);
			}
			
			$this->m_nRecordSetRow = $this->m_nCurrentRow + 1;
			$aCurrentRec = mysql_fetch_array($this->m_rResult);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
		return $aCurrentRec;
	}
	
	public function fetchAssoc() {
		$aCurrentRec = array();
		
		try {
			if($this->m_nRecordSetRow != $this->m_nCurrentRow) {
				mysql_data_seek($this->m_rResult, $this->m_nCurrentRow);
			}
			
			$this->m_nRecordSetRow = $this->m_nCurrentRow + 1;
			$aCurrentRec = mysql_fetch_assoc($this->m_rResult);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
		return $aCurrentRec;
	}
	
	public function getLastInsertedId() {
		return mysql_insert_id();
	}
	
	public function startTransaction() {
		try {
			$sQuery = "SET AUTOCOMMIT=0;";
			$this->executeSQL($sQuery);
			$sQuery = "BEGIN;";
			$this->executeSQL($sQuery);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function commit() {
		try {
			$sQuery = "COMMIT;";
			$this->executeSQL($sQuery);
			$sQuery = "SET AUTOCOMMIT=1";
			$this->executeSQL($sQuery);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function rollBack() {
		try {
			$sQuery = "ROLLBACK;";
			$this->executeSQL($sQuery);
			$sQuery = "SET AUTOCOMMIT=1;";
			$this->executeSQL($sQuery);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function endTransaction() {
		try {
			$sQuery = "SET AUTOCOMMIT=1";
			$this->executeSQL($sQuery);
		} catch(Exception $e) {
			throw new Exception($e->getMessage(), $e->getCode());
		}
	}
}

?>
