Location: PHPKode > projects > WebArchive > phparchive.0.1.82.beta/includes/databases.inc.php
<?php
/**
 * @version $Id: databases.inc.php,v 1.1 2003/11/23 06:08:03 freedev Exp $
 * @author Vincenzo D'Amore <hide@address.com>
 * @package wa_includes
 */

/**
 * function GetDBTypeString
 * used to get an easly description for WA_DB_* constants
 */

function GetDBTypeString($iDBType)
{
	switch ($iDBType)
	{
		case WA_DB_ODBC_ID:
			return "ODBC";
		case WA_DB_MYSQL_ID:
			return "MySql";
		case WA_DB_ORACLE_ID:
			return "DB_Oracle";
		case WA_DB_OCI_ID:
			return "Oracle Oci";
		default:
			die("GetDBTypeString: Selezione DB impossibile");		
	}
}

function GetDBTypeId($sDBType)
{
	if ($sDBType == WA_DB_MYSQL)
		return WA_DB_MYSQL_ID;
	elseif ($sDBType == WA_DB_ODBC)
		return WA_DB_ODBC_ID;
	elseif ($sDBType == WA_DB_ORACLE)
		return WA_DB_ORACLE_ID;
	elseif ($sDBType == WA_DB_OCI)
		return WA_DB_OCI_ID;
	die("GetDBTypeId: Selezione DB impossibile");		
}

/**
 * Global Database structure
 */
$GLOBALS["WADBInfo"] = array(
	"WAHost"			=> $GLOBALS['WA_CONF_DBHOST'],
	"WADBName"			=> $GLOBALS['WA_CONF_DBNAME'],
	"WAUser"			=> $GLOBALS['WA_CONF_DBUSER'],
	"WAPassword"		=> $GLOBALS['WA_CONF_DBPASS'],
	"WADBType"			=> GetDBTypeId($GLOBALS['WA_CONF_DBTYPE']) );


/**
 * @package wa_includes
 */
class WARecordset
{
	var $m_cnn;
	var $m_dbName;
	var $m_dbType;
	var $m_rec;
	var $m_row;
	var $m_debug;
	var $m_dbInfoData;
	var $m_sSql;

	function WARecordset()
	{
		if (!isset($GLOBALS["WADBINFO_CURRENTDATABASE"]))
			$GLOBALS["WADBINFO_CURRENTDATABASE"] = "";
		$this->m_dbType = 0;
		$this->m_cnn = 0;
		$this->m_rec = 0;
		$this->m_dbInfoData = "";
		$this->m_dbName = "";
		$this->m_debug = false;
	}

	function SetConnection(&$Form)
	{
//		$FormDBInfo = &$Form["WADBInfo"];
		$FormDBInfo = &$GLOBALS["WADBInfo"];
		$this->m_dbInfoData = "WADBINFO_".$FormDBInfo["WAHost"]."_".$FormDBInfo["WADBName"]."_".
				$FormDBInfo["WAUser"]."_".$FormDBInfo["WADBType"];
				
		$this->m_dbName = $FormDBInfo["WADBName"];

		if ($this->m_debug)
		{
			echo "SetConnection Previous:".$GLOBALS["WADBINFO_CURRENTDATABASE"]."<br>";
			echo "SetConnection Form:"."WADBINFO_".$FormDBInfo["WAHost"]."_".$FormDBInfo["WADBName"]."_".
						$FormDBInfo["WAUser"]."_".$FormDBInfo["WADBType"]."<br>";
		}

		if ((isset($GLOBALS[$this->m_dbInfoData])) &&
			(is_resource($GLOBALS[$this->m_dbInfoData])) )
		{
			$this->m_dbType = $FormDBInfo["WADBType"];
			$this->m_cnn = $GLOBALS[$this->m_dbInfoData];
			switch ($this->m_dbType)
			{
				case WA_DB_ODBC_ID:
					break;
				case WA_DB_MYSQL_ID:
					if ($GLOBALS["WADBINFO_CURRENTDATABASE"] != $this->m_dbInfoData)
					{
						if (!mysql_select_db($this->m_dbName, $this->m_cnn))
						{
						 echo mysql_errno().": ".mysql_error() ;
							mysql_close($this->m_cnn);
							$this->m_cnn = NULL;
							die("SetConnection: Selezione DB impossibile");
						}
						else
							$GLOBALS["WADBINFO_CURRENTDATABASE"] = $this->m_dbInfoData;
					}
					break;
				case WA_DB_ORACLE_ID:
					break;
				case WA_DB_OCI_ID:
					break;
				break;
			}
		}
		else
		{
			$GLOBALS[$this->m_dbInfoData] = $this->WAOpenConnection($FormDBInfo["WAHost"], $FormDBInfo["WADBName"], $FormDBInfo["WAUser"], $FormDBInfo["WAPassword"], $FormDBInfo["WADBType"]);
			$GLOBALS["WADBINFO_CURRENTDATABASE"] = $this->m_dbInfoData;
		}
		if (!is_resource($this->m_cnn))
		{
			die("Cannot SetConnection " . $this->m_dbInfoData);
		}
		if ($this->m_debug)
			echo "SetConnection Current:".$this->m_dbInfoData."<br>";
		$this->m_rec = 0;
	}

	function SetupFromGlobalCnn()
	{
		$FormDBInfo = &$GLOBALS["WADBInfo"];
		$this->m_dbInfoData = "WADBINFO_".$FormDBInfo["WAHost"]."_".$FormDBInfo["WADBName"]."_".
				$FormDBInfo["WAUser"]."_".$FormDBInfo["WADBType"];

		$this->m_dbName = $FormDBInfo["WADBName"];

		if ($this->m_debug)
		{
			echo "SetConnection Previous:".$GLOBALS["WADBINFO_CURRENTDATABASE"]."<br>";
			echo "SetConnection Form:"."WADBINFO_".$FormDBInfo["WAHost"]."_".$FormDBInfo["WADBName"]."_".
						$FormDBInfo["WAUser"]."_".$FormDBInfo["WADBType"]."<br>";
		}

		if ((isset($GLOBALS[$this->m_dbInfoData])) &&
			(is_resource($GLOBALS[$this->m_dbInfoData])) )
		{
			$this->m_dbType = $FormDBInfo["WADBType"];
			$this->m_cnn = $GLOBALS[$this->m_dbInfoData];
			switch ($this->m_dbType)
			{
				case WA_DB_ODBC_ID:
					break;
				case WA_DB_MYSQL_ID:
					if ($GLOBALS["WADBINFO_CURRENTDATABASE"] != $this->m_dbInfoData)
					{
						if (!mysql_select_db($this->m_dbName, $this->m_cnn))
						{
							echo $this->m_dbInfoData." [".$this->m_cnn."] : ".mysql_errno().": ".mysql_error()."<br>";
							mysql_close($this->m_cnn);
							$this->m_cnn = NULL;
							die("SetupFromGlobalCnn: Selezione DB impossibile");
						}
						else
							$GLOBALS["WADBINFO_CURRENTDATABASE"] = $this->m_dbInfoData;
					}
					break;
				case WA_DB_ORACLE_ID:
					break;
				case WA_DB_OCI_ID:
					break;
				break;
			}
		}
		else
		{
			$GLOBALS[$this->m_dbInfoData] = $this->WAOpenConnection($FormDBInfo["WAHost"], $FormDBInfo["WADBName"], $FormDBInfo["WAUser"], $FormDBInfo["WAPassword"], $FormDBInfo["WADBType"]);
//				or die("Connessione al DB impossibile");
			$GLOBALS["WADBINFO_CURRENTDATABASE"] = $this->m_dbInfoData;
		}
		$this->m_rec = 0;
		return $this->m_cnn;
	}

	function WAOpenConnection($sDBHost, $sDBName, $sDBUID, $sDBPWD, $iDBType)
	{
		$this->m_cnn = 0;
		$this->m_dbType = $iDBType;
		$this->m_dbName = $sDBName;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				// $this->m_cnn=odbc_pconnect($sDBName, $sDBUID, $sDBPWD, SQL_CUR_USE_ODBC);
				// Modificato da pconnect a connect perche' non riusciva ad aggiornare il contatore
				// degli identificatori univoci in tbTables...
				$this->m_cnn = odbc_connect($sDBName, $sDBUID, $sDBPWD, SQL_CUR_USE_ODBC);
				break;
			case WA_DB_MYSQL_ID:
				$this->m_cnn = mysql_pconnect($sDBHost, $sDBUID, $sDBPWD);
				if (is_resource($this->m_cnn))
				{
					if (!mysql_select_db($this->m_dbName, $this->m_cnn))
					{
						$this->WAErrorLog();
						mysql_close($this->m_cnn);
						$this->m_cnn = NULL;
					}
				}
				else
				{
					$this->WAErrorLog();
				}
				break;
			case WA_DB_ORACLE_ID:
				putenv("ORACLE_SID=BONIVIK");
				putenv("ORACLE_HOME=C:\Oracle\Ora81");
				$this->m_cnn= ora_logon($sDBUID."@".$sDBName, $sDBPWD);
				ora_commiton($this->m_cnn);
				break;
			case WA_DB_OCI_ID:
				putenv("ORACLE_SID=BONIVIK");
				putenv("ORACLE_HOME=C:\Oracle\Ora81");
				$this->m_cnn = OCIPLogon($sDBUID, $sDBPWD, $sDBName);
				break;
			default:
				echo "WAOpenConnection: ".$this->m_dbType." Tipo di database non gestito";
			break;
		}
		if ($this->m_debug)
		{
			echo "WAOpenConnection: ".gettype($this->m_cnn)." - ".$this->m_cnn."<br>";
			echo "OpenConnection (Host:". $sDBHost ." DB:". $sDBName." UID:".$sDBUID." PWD:".$sDBPWD." TYPE:".$this->m_dbType. ")<br>";
		}
		return $this->m_cnn;
	}

	function WACreateDatabase($sDBHost, $sDBName, $sDBUID, $sDBPWD, $iDBType)
	{
		$this->m_cnn = 0;
		$this->m_dbType = $iDBType;
		$this->m_dbName = $sDBName;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				break;
			case WA_DB_MYSQL_ID:
				$this->m_cnn = mysql_create_db($this->m_dbName);
				if (!is_resource($this->m_cnn))
				{
					$this->WAErrorLog();
				}
				break;
			case WA_DB_ORACLE_ID:
				break;
			case WA_DB_OCI_ID:
				break;
			default:
				echo "WACreateDatabase: Tipo di database non gestito";
			break;
		}
		if ($this->m_debug)
		{
			echo "WAOpenConnection: ".gettype($this->m_cnn)." - ".$this->m_cnn."<br>";
			echo "OpenConnection (Host:". $sDBHost ." DB:". $sDBName." UID:".$sDBUID." PWD:".$sDBPWD." TYPE:".$this->m_dbType. ")<br>";
		}
		return $this->m_cnn;
	}

	function WAIsConnected()
	{
		return is_resource($this->m_cnn);
	}

	function WAIsOpen()
	{
		return is_resource($this->m_rec);
	}

	function WAErrorLog()
	{
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				$sErrHdr = "ODBC Err(";
				$sErrNum = odbc_error($this->m_cnn);
			 $sErrMsg = odbc_error_msg($this->m_cnn);
				break;
			case WA_DB_MYSQL_ID:
				$sErrHdr = "MySql Err(";
				$sErrNum = mysql_errno($this->m_cnn);
			 $sErrMsg = mysql_error($this->m_cnn) ;
				break;
			case WA_DB_ORACLE_ID:
				$sErrHdr = "ORACLE Err(";
				$sErrNum = ora_errorcode($this->m_cnn);
			 $sErrMsg = ora_error($this->m_cnn) ;
				break;
			case WA_DB_OCI_ID:
//			$sErrMsg = "OCI Err(". mysql_errno()."): ".mysql_error() ;
				break;
			break;
		}
		if (!empty($sErrMsg))
		{
			error_log($sErrHdr.$sErrNum."): ".$sErrMsg."\n\t".$this->m_sSql."\n");
		}
		if ($this->m_debug)
			echo "WAErrorLog ". $sErrHdr.$sErrNum."): ".$sErrMsg."\n\t".$this->m_sSql."\n";
	}

	function WAOpenRecordset($sSql)
	{
		$this->m_rec = 0;
		$this->m_sSql = $sSql;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				$this->m_rec = odbc_exec($this->m_cnn, $sSql);
				break;
			case WA_DB_MYSQL_ID:
				if ($GLOBALS["WADBINFO_CURRENTDATABASE"] != $this->m_dbInfoData)
				{
					if (!mysql_select_db($this->m_dbName, $this->m_cnn))
					{
						echo $this->m_dbInfoData." [".$this->m_cnn."] : ".mysql_errno().": ".mysql_error()."<br>";
						mysql_close($this->m_cnn);
						$this->WAErrorLog();
						$this->m_cnn = NULL;
						die("WAOpenRecordset: Selezione DB impossibile");
					}
					else
					{
						$GLOBALS["WADBINFO_CURRENTDATABASE"] = $this->m_dbInfoData;
						$this->WAErrorLog();
					}
				}
				$this->m_rec = mysql_query($sSql, $this->m_cnn);
				if (!is_resource($this->m_rec))
					$this->WAErrorLog();
	//			$this->m_rec = mysql_unbuffered_query($sSql, $this->m_cnn);
				break;
			case WA_DB_ORACLE_ID:
				$this->m_rec = ora_open($this->m_cnn);
				ora_parse($this->m_rec, $sSql);
				ora_exec($this->m_rec);
				break;
			case WA_DB_OCI_ID:
				$this->m_rec = ociparse($this->m_cnn, $sSql);
				ociexecute($this->m_rec, OCI_DEFAULT);
				break;
			default:
				echo "WAOpenRecordset: Tipo di database non gestito";
			break;
		}
		if ($this->m_debug)
		{
			echo "WAOpenRecordset sql: <span class='Fixed' style='text-transform: none;'>".$sSql."</span><br>\n";
			echo "WAOpenRecordset: ".gettype($this->m_rec)." - ".$this->m_rec."<br>";
			// Gestione errori...
		}
		return $this->m_rec;
	}

	function WAFetchRow()
	{
		$nRows = 0;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				$nRows = odbc_fetch_row($this->m_rec);
				break;
			case WA_DB_MYSQL_ID:
				if (gettype($this->m_rec) == "resource")
				{
		//			$this->m_row = mysql_fetch_row($this->m_rec);
		//			$this->m_row = mysql_fetch_array($this->m_rec, MYSQL_ASSOC);
					$this->m_row = mysql_fetch_assoc($this->m_rec);
					if (!is_array($this->m_row))
					{
						$nRows = 0;
						$this->WAErrorLog();
					}
					else
						$nRows = 1;
//					if ($this->m_debug)
//						echo "WAFetchRow: <pre>"; print_r($this->m_row); echo "</pre>";
				}
				break;
			case WA_DB_ORACLE_ID:
				$nRows = ora_fetch($this->m_rec);
				break;
			case WA_DB_OCI_ID:
				$nRows = OCIFetch($this->m_rec);
				break;
			default:
				echo "WAFetchRow: Tipo di database non gestito";
			break;
		}
		if ($this->m_debug)
			echo "WAFetchRow: ".gettype($nRows)." - ".$nRows."<br>";
		return $nRows;
	}

	function WANumRows()
	{
		$nRows = 0;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				$nRows = odbc_num_rows($this->m_rec);
				break;
			case WA_DB_MYSQL_ID:
				$nRows = mysql_affected_rows($this->m_cnn);
				if ($nRows < 0)
					$this->WAErrorLog();
				break;
			case WA_DB_ORACLE_ID:
				$nRows = ora_numrows($this->m_rec);
				break;
			case WA_DB_OCI_ID:
				$nRows = OCIRowCount($this->m_rec);
				break;
			default:
				echo "WANumRows: Tipo di database non gestito";
			break;
		}
		if ($this->m_debug)
			echo "WANumRows: ".gettype($nRows)." - ".$nRows."<br>";
		return $nRows;
	}

	function WACloseRecordset()
	{
		$nRows = 0;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				odbc_free_result($this->m_rec) ;
				break;
			case WA_DB_MYSQL_ID:
//				if (gettype($this->m_rec) == "resource")
				if (is_resource($this->m_rec))
				{
					@mysql_free_result($this->m_rec);
					$nRows = 1;
				}
				else
					$this->WAErrorLog();
				break;
			case WA_DB_ORACLE_ID:
				ora_close($this->m_rec);
				break;
			case WA_DB_OCI_ID:
				OCIFreeStatement($this->m_rec);
				break;
			default:
				echo "WACloseRecordset: Tipo di database non gestito";
				if ($this->m_debug)
					echo "WACloseRecordset <br>";
			break;
		}
		if ($this->m_debug)
			echo "WACloseRecordset: ".gettype($this->m_rec)." - ".$this->m_rec."<br>";
		return $nRows;
	}

	function WAResult($Field)
	{
		$Result = 0;
		switch ($this->m_dbType)
		{
			case WA_DB_ODBC_ID:
				return odbc_result($this->m_rec, $Field);
			case WA_DB_MYSQL_ID:
				return $this->m_row[$Field];
	//			return mysql_result($this->m_rec, $Field);
			case WA_DB_ORACLE_ID:
				return ora_getcolumn($this->m_rec, $Field);
			case WA_DB_OCI_ID:
				return ociresult($this->m_rec, $Field);
			default:
				echo "WAResult: Tipo di database non gestito ".$Field;
			break;
		}
		return $Result;
	}
}

?>
Return current item: WebArchive