Location: PHPKode > scripts > metafire > metafire/metafire.lib.php
<?
/*************************************************************/
// Filename	: metafire.lib.php                             
// Version	: 0.3                                         
// Author       : Meta Nurwidyanto                            
// Date         : 09/17/2002                                   
// Description  : Firebird DB Abstraction Layer                 
/**************************************************************/

class metafire {

	var $strHost;
	var $strUser;
	var $strDatabase;
	var $intDebug;
	var $intUsePersistent;
	var $intTrans;
	var $intTransStatus;
	var $intCommit;

	var $intConn;
	var $intQuery;
	var $aryResult;
	var $aryRecord;
	
	// Constructor 
	function metafire($strHost = 'localhost', $strDatabase = '/opt/interbase/examples/employee.gdb', $strUser = 'sysdba', $strPassword = 'd4rk5ky', $intUsePersistent = 1, $intDebug = 1) {
			$this->strHost = $strHost;
			$this->strDatabase = $strDatabase;
			$this->strUser = $strUser;
			$this->strPassword = $strPassword;
			$this->intUsePersistent = $intUsePersistent;
			$this->intDebug = $intDebug;

	} // end of Constructor

	//Connector
	function Connect() {

		if (!$this->intConn) {
			if ($this->intDebug) {
				echo "Establishing connection...		\n";
			}
				
			if($this->$intUsePersistent) 
			{
				$this->intConn = @ibase_pconnect($this->strHost . ':' . $this->strDatabase, $this->strUser, $this->strPassword)

							or die("Error:" . ibase_errmsg() . "<br>\n");
			} else {
				$this->intConn = @ibase_connect($this->strHost . ':' . $this->strDatabase, $this->strUser, $this->strPassword)
							or die("Error:" . ibase_errmsg() . "<br>\n");
			}
					
			
		}
		
		if (!$this->intConn) {
			if ($this->intDebug) {


				echo "Error establishing connection!\n";

			}
		} else {
			if ($this->intDebug) {
				echo "Connection established!<br>";
			}
		}
		
		return $this->intConn;
			
	}
	
	// end of Connector

	function Query($strSQL, $intUseTransaction =0, $intAutoCommit = 1) {
	
		if (!$this->intConn) {
			$this->intConn=$this->Connect();
		}
		
		$this->strQuery= $strSQL;
		if ($this->intDebug) {
			echo "Executing query...		<br>";
		}
		
		if  ($intUseTransaction==0) {
			$this->intQuery=ibase_query($this->intConn,$this->strQuery) or die ("error :".ibase_errmsg()."! <br>");
			$this->intTransStatus++;
		} else {
			$this->intCommit=$intAutoCommit;
			$this->intTrans=$this->Transaction($this->intQuery);
			$this->intQuery= ibase_query($this->intTrans,$this->strQuery); 
			
			
			if (ibase_errmsg()) {
				$this->intTrans=$this->RollbackTransaction($this->intTrans);
				echo "Error !<br> Query: <br><b>".$this->strQuery."</b><br> cannot be executed! <br>";
				return $this->intQuery;
			}
			
			
			
			
			//if ($this->intCommit==1 && $this->intTransStatus==0) {
			if ($this->intCommit==1) {
				$this->intQuery=$this->CommitTransaction($this->intTrans);				
				
			}	
		}
		
		
		

		if ($this->intDebug && $this->intTransStatus>0) {
			echo "Query executed!		<br>";
		}		
		
		return $this->intQuery;
	}

	// if using transaction	
	function Transaction($intQuery) {
		if ($this->intDebug) {
			echo "Use transaction...		<br>";
		}
	
		$this->intTrans=ibase_trans($this->intConn);
		$this->intTranStatus=0;
		
		
		return $this->intTrans;
	}
	
	//end of transaction
	
	//if Commiting transaction
	function CommitTransaction($intTrans) {
		if ($this->intDebug) {
			echo "Committing transaction...		<br>";
		}
		
		$this->intTrans=ibase_commit($this->intTrans);
		$this->intTransStatus++;
		
		return $this->intQuery;
	}
	
	//Rollback Transaction if there's some errors
	function RollbackTransaction($intTrans) {
		if ($this->intDebug) {
			echo "Rolling back transaction...	 <br>";
		}
	
		$this->intTrans=ibase_rollback($this->intTrans);
		$this->intTransStatus--;
		
		return $this->intQuery;
	}
	
	//if using Prepared Query with or without transaction
	function PreQuery($strSQL, $intTrans=0) {
		if (!$this->intConn) {
			$this->intConn=$this->Connect();
		}
		
		$this->strQuery= $strSQL;
		if ($this->intDebug) {
			echo "Preparing query...		<br>";
		}
		
		if ($intTrans==0) {
			$this->intQuery=ibase_prepare($this->intConn,$this->strQuery);
		} else {
			$this->intQuery=$this->Transaction($this->intConn);
			$this->intQuery=ibase_prepare($this->intTrans,$this->strQuery);
		}
		
		return $this->intQuery;
	}

	
	//Free prepared query
	function FreePreQuery($intPreQuery) {
		$this->intQuery=ibase_free_query($intPreQuery);
		
		if ($this->intDebug) {
			echo "Query prepared free...		<br>";
		}
	}
	
	//Free query result
	function FreeQueryResult($intQuery) {
	$this->intQuery=@ibase_free_result($intQuery);
		
		if ($this->intDebug) {
			echo "Query result free...		<br>";
		}
		
	}
	
	//Closing database connection
	function CloseConnection($intConn) {
	$this->intQuery=ibase_close($intConn);
		
		if ($this->intDebug) {
			echo "Database connection closed!		<br>";
		}
	}
	 
        //Getting number of fields
	function GetNumFields($intQuery) {
	if ($this->intDebug) {
			echo "Getting number of fields!		<br>";
		}
		
		$this->intFields=ibase_num_fields($intQuery);
		
		return $this->intFields;
	}
	
	//Getting field info
	function GetFieldInfo($intQuery,$i) {
	if ($this->intDebug) {
			echo "Getting field info!		<br>";
		}
		
		$this->strField=ibase_field_info($intQuery,$i);
		
		return $this->strField;
	}

        //Query with Paging
	function PagingSelect($strTable,$aryField,$strClause=' ',$intMax,$intOffset) {
		if (!$this->intConn) {
			$this->intConn=$this->Connect();
		}		
		
		if ($this->intDebug) {
			echo "Paging query.......		<br>";
		}
		
		//building sql script;
		$this->intMax=$intMax;
		$this->strTable = $strTable;
		$this->aryField = $aryField;
		$this->intOffset=$intOffset;
		$this->strSQL="select first ".$this->intMax." skip ".$this->intOffset." ".$this->aryField. " from ".$this->strTable."  ".$strClause;
		
		//executing sql script;
		$this->intQuery=$this->Query($this->strSQL);
		
		return $this->intQuery;
	}
	
         //get All row until EOF
	function getRow($intQuery) { 
		if ($this->intDebug) { 
			echo "Fetching row.......		<br>"; 
		} 
		 
		$this->aryRecord = @ibase_fetch_row($intQuery);  
		if ($this->intDebug && $this->aryRecord) echo "OK <br>\n\n";  
		elseif ($this->intDebug) echo "EOF <br>\n\n";  
		 
		return $this->aryRecord; 
	}  


	// from class.interbase.php by Vinicius Gallafrio/BR - hide@address.com 
	//----------------------------------------------------------------------------
	function fetchObject($intQuery) {
		if ($this->intDebug) {
			echo "Fetching object.......		<br>";
		}
		
		$this->aryRecord = @ibase_fetch_object($intQuery); 
		if ($this->intDebug && $this->aryRecord) echo "OK <br>\n\n"; 
		elseif ($this->intDebug) echo "EOF <br>\n\n"; 
		
		return $this->aryRecord;
	}
	
	
	
	function fetchRow($fieldname) {
		$fieldname = strtoupper($fieldname); 
		if ($this->aryRecord->$fieldname) { 
			return $this->aryRecord->$fieldname; 
		} else {  
		/** 
		 * look to the field writing in lower 
		 */	 
			$field = strtolower($field); 
			return $this->aryRecord->$fieldname; 
		} 
	
	}
	
	//-------------------------------------------------------------------------------
}

?>
Return current item: metafire