<?php
	
/**
 * class db is used for establishing connection to the database
 * 
 * 
 * @copyright RMplus d.o.o. 2009
 * @author Bostjan Kodric <bostjan.kodric@rmplus.si>
 * 
 */



	class db
	{
		
		var $ErrorCode;
		private $DataBaseEngine;
		
		/**
		 * function establishes connection to mysql database
		 * 
		 *
		 * @return MySQL connection identifier
		 */
		
		function __construct($DatabaseEngine = "mysql")
		{
			$this->DataBaseEngine = $DatabaseEngine;
		}
		
		
		/**
		 * database Initialization
		 *
		 * @param string $username
		 * @param string $password
		 * @param string $database
		 * @param string $server
		 * @param string $setname
		 */
		function SetDatabaseConnection($username = "", $password = "", $database = "", $server = "localhost",$setname = "utf8") 
		{ 
			
			switch ($this->DataBaseEngine) {
				case "mysql" : 
					$this->SQLconnect = mysql_pconnect($server,$username,$password);
					mysql_select_db($database);
					mysql_query("SET NAMES '".$setname."'");
					break;
				default :
					$this->SQLconnect = mysql_pconnect($server,$username,$password);
					mysql_select_db($database);
					mysql_query("SET NAMES '".$setname."'");
					break;

			}
		}
		
		/**
		 * function resets error code array
		 *
		 */
		function resetErrorCode() { unset($this->ErrorCode); }
	
		/**
		 * runs sql and returns query result object. 
		 * Same functionality as $result = mysql_query in MySql
		 *
		 * @param string $sql
		 * @return query object result
		 */
		
		function SQLexecute($sql)
		{
			$this->resetErrorCode();
			$this->ErrorCode[code] = 0;		
			if ( ($result = mysql_query($sql,$this->SQLconnect)) ) return $result;
			else 
			{
					//echo mysql_error();
					$this->ErrorCode[code] = mysql_errno();
					$this->ErrorCode[text] = mysql_error();
					return 0;
			} 
		}
		
		function IsError() { return $this->ErrorCode[code]; } 
		function GetError() { return $this->ErrorCode[text]; }
		/**
		 * Function works as $record = mysql_fetch_assoc($result)
		 *
		 * @param database object $result
		 * @return actual record set
		 */
		function fetchAssoc(&$result) { return mysql_fetch_assoc($result); }
		
			
		/**
		 * Function works as $record = mysql_fetch_array($result)
		 *
		 * @param database object $result
		 * @return actual record set
		 */
		function fetchArray(&$result) { return mysql_fetch_array($result); }
		
		
		/**
		 * Function reeturns last inserted id in database
		 *
		 * @return id of last inserted id
		 */
		function GetLastInsertId() {
			$sql = "SELECT LAST_INSERT_ID();";
			$result = $this->SQLexecute($sql);
			$zadetek = mysql_fetch_array($result);
			return $zadetek[0];	
		}
		
		/**
		 *  Function returns number on rows in result
		 * 
		 * @param query object $result
		 * @return integer number of records in result
		 */
		function NumRows($result) { return mysql_num_rows($result); }
		
		/**
		 *  Function returns number on rows in result
		 * 
		 * @param query object $result
		 * @return integer number of records in result
		 */
		function SetPointer($result, $i) { 
			if($this->NumRows($result)>0){
				return mysql_data_seek($result,$i);
			} 
		}
		
		
	}
?>