<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

class CBaseMysql
{
	
	var  $query_class = 'CMysqlQuery';
	
	var $handle;
	var $errno;
	var $errstr;
	var $quc = 0;
	
	function CBaseMysql($host, $user, $pass, $db)
	{
	
		$this->handle = @mysql_connect($host, $user, $pass);
		if(!$this->handle || !@mysql_select_db($db, $this->handle))
		{
			$this->errno = mysql_errno();
			$this->errstr = mysql_error();
			echo $this->errno.' : '.$this->errstr.'<br>';
			$this->Close();
		}
	}
	
	function getLimit($pagesize, $pagenum)
	{
		return ' LIMIT '.($pagenum * $pagesize).', '.$pagesize;
	}
	
	function &Select($query, $pagesize = 0, $pagenum = 0)
	{
		if(isset($GLOBALS['DEBUG']) && $GLOBALS['DEBUG']) echo $query.'<br>';
		$qc = false;
		if($pagesize > 0)
		{
			$query .= $this->getLimit($pagesize, $pagenum);
		}
		if(isset($GLOBALS['DEBUG']) && $GLOBALS['DEBUG']) echo "query after limit: " . $query.'<br>';
		if($res = @mysql_query($query, $this->handle))
		{
			$this->quc++;
			$qc =& new $this->query_class($res);
			/*
			if(!$qc->getNumRows())
			{
				$qc->Close();
				$qc = false;
			}
			*/
		}
		else
		{
			$this->errno = mysql_errno();
			$this->errstr = mysql_error();
		}
		return $qc;
	}
	
	function Insert($query)
	{
		//if($this->Execute($query))
		//{
		//	return $this->LastID();
		//}
		return $this->Execute($query);
		//return false;
	}
	
	function Update($query)
	{
		return $this->Execute($query);
	}
	
	function Delete($query)
	{
		return $this->Execute($query);
	}
	
	function Execute($query)
	{
		if(isset($GLOBALS['DEBUG']) && $GLOBALS['DEBUG']) echo $query.'<br>';
		if(@mysql_query($query, $this->handle))
		{
			$this->quc++;
			return true;
		}
		else
		{
			$this->errno = mysql_errno();
			$this->errstr = mysql_error();
			return false;
		}
	}

	function lockTables ( $read, $write=array() )
	{
		if ( !count($read) && !count($write) )
			return true;
		$read = array_map ( array( &$this, "appendRead" ), is_array($read) ? $read : array($read) );
		$write = array_map ( array( &$this, "appendWrite" ), is_array($write) ? $write : array($write) );
		return $this->Execute ( "LOCK TABLES " . implode( ",", array_merge( $read, $write ) ) );
	}

	function unLockTables()
	{
		return $this->Execute ( "UNLOCK TABLES" );
	}

	function appendRead ( $value ) // to help lockTables
	{
		return $value.' READ';
	}

	function appendWrite ( $value ) // to help lockTables
	{
		return $value.' WRITE';
	}
	
	function getLastID()
	{
		return @mysql_insert_id($this->handle);
	}
	
	function Quote($str)
	{
		return addslashes($str);
	}
	
	function Close()
	{
		if($this->handle)
		{
			@mysql_close($this->handle);
		}
	}
}

class CMysqlQuery
{
	var $errno;
	var $errstr;
	var $result;
	//var $row;
	//var $thisrow = -1;
	//var $numrows = 0;
	
	function getNumRows()
	{
		//return $this->numrows;
		if($this->result)
		{
			return @mysql_num_rows($this->result);
		}
		return false;
	}
	
	//function getValue($name)
	//{
	//	return $this->row[$name];
	//}
	
	function &nextrow()
	{
		/*
		if($this->row = @mysql_fetch_array($this->result))
		{
			$this->thisrow++;
			return true;
		}
		return false;
		*/
		return @mysql_fetch_array($this->result);
	}
	
	function getAll()
	{
		$all = array();
		while ($arr = $this->nextrow()) 
		{
			$all[] = $arr;
		}
		if( count($all) )
		{
			return $all;	
		}
		else
		{
			return false;	
		}
	}
	
	function CMysqlQuery(&$res)
	{
		$this->result = &$res;
		//if($this->numrows = @mysql_num_rows($this->result))
		//{
		//	$this->nextrow();
		//}
	}
	
	function Close()
	{
		//unset($this->numrows, $this->rowcount, $this->row);
		if($this->result)
		{
			@mysql_free_result($this->result);
		}
	}
}


function &get_db_connection()
{
	static $db_connection;
	global $db_db, $db_host, $db_user, $db_pass, $db_port;
		
	if ( $db_port ) {
	  $server = $db_host.':'.$db_port;
	} else {
	  $server = $db_host;
	}

	if( !isset($db_connection) )
	{
		$db_connection = new CBaseMysql($server, $db_user, $db_pass, $db_db); 
	}
	return $db_connection;
}

function db_quote($str)
{
	return CBaseMysql::Quote($str);
}

/*
$db_db = 'confixx';
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'pass';
*/
//$db_connection = new CBaseMysql($db_host, $db_user, $db_pass, $db_db); 

//$DATABASE_CONNECTION = new CBaseMysql($db_host, $db_user, $db_pass, $db_name);
/*
if($rs = $DATABASE_CONNECTION->Select('select a,b from aaa'))
{
	do {
		echo $rs->getValue('a');
	
	} while ($rs->nextrow());
}
*/


?>
