c_mysqlDb.php 4.43 KB
<?php

require_once LIBDIR . 'errException.php';

class mysqlQueryException extends errException
{
	private $query;

	function __construct ($msg, $no, $file, $line, $context, $query)
	{
		parent::__construct ($msg, $no, $file, $line, $context);

		$this->query = $query;
	}

	function __toString ()
	{
		$retStr  = "Fehler in Query: \n";
		$retStr .= $this->query . "\n\n";
		$retStr .= $this->message . "\n\n";
		$retStr .= 'Datei: ' . $this->file . "\n";
		$retStr .= 'Zeile: ' . $this->line . "\n\n";

		if ($this->errContext != NULL)
		{
			$retStr .= "Fehler Context: \n";
			$retStr .= print_r ($this->errContext, TRUE) . "\n";
		}

		$retStr .= "Aufruf Stack: \n";
		$retStr .= $this->getTraceAsString () . "\n";

		return $retStr;
	}
}

/***
 * This class depends on errException.php, as it does not handle any
 * error at all, except it catches exceptions.
 */
class c_mysqlDb
{
	private $connectRes;
	private $host;
	private $user;
	private $pass;
	private $database;

	private $result   = array ();
	private $resCount = 0;

	private function getAllData ($resId, $method, $class = NULL, $parm = NULL)
	{
		$ret = array ();

		if (isset ($this->result[$resId]) && $this->result[$resId] != FALSE)
		{
			try 
			{
				mysql_data_seek ($this->result[$resId], 0);
			}
			catch (Exception $e) 
			{
				if ($e->getCode () != 2)
					throw ($e);
				// wenn der Fehlercode gleich 2 ist war das resultset nur leer
				// und ich mache weiter.
			}

			if ($class === NULL)
				$row = $method ($this->result[$resId]);
			else
				$row = $method ($this->result[$resId], $class, $parm);

			while ($row != FALSE)
			{
				$ret[] = $row;
				if ($class === NULL)
					$row = $method ($this->result[$resId]);
				else
					$row = $method ($this->result[$resId], $class, $parm);
			}
		}

		return $ret;
	}

	function __construct ($host, $user, $pass, $db)
	{
		/* init member */
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->database = $db;

		/* connect and select db */
		$this->connectRes = mysql_pconnect (
				$this->host, 
				$this->user,
				$this->pass);
		mysql_select_db ($this->database);
		mysql_query ('set character set utf8');
	}

	function __destruct ()
	{
		/* close connection */
		mysql_close ($this->connectRes);
	}

	function query ($query)
	{
		try
		{
			for ($i = 0; $i < $this->resCount; $i++)
				if (! isset ($this->result[$i]))
					break;

			if ($i == $this->resCount)
				$i = $this->resCount = $this->resCount + 1;

			$this->result[$i] = mysql_query ($query);

			if ($this->result[$i] === FALSE)
				throw new Exception (mysql_error (), mysql_errno ());

			return $i;
		}
		catch (errException $e)
		{
			throw new mysqlQueryException (
					$e->getMessage (),
					$e->getCode (),
					$e->getFile (),
					$e->getLine (),
					$e->getErrContext (),
					$query);
		}
		catch (Exception $e)
		{
			throw new mysqlQueryException (
					$e->getMessage (),
					$e->getCode (),
					$e->getFile (),
					$e->getLine (),
					NULL,
					$query);
		}
	}

	function freeResult ($resId)
	{
		if (is_resource ($this->result[$resId]))
			mysql_free_result ($this->result[$resId]);
		unset ($this->result[$resId]);
	}

	function resetResult ($resId)
	{
		mysql_data_seek ($this->result[$resId], 0);
	}

	function getResult ($resId)
	{
		return $this->result[$resId];
	}

	function getAssoc ($resId)
	{
		return $this->getAllData ($resId, 'mysql_fetch_assoc');
	}

	function getArray ($resId)
	{
		return $this->getAllData ($resId, 'mysql_fetch_row');
	}

	function getObject ($resId, $class, $parm = NULL)
	{
		return $this->getAllData ($resId, 'mysql_fetch_object', $class, $parm);
	}

	function rows ($resId)
	{
		return mysql_num_rows ($this->result[$resId]);
	}

	function get ($resId, $row, $col)
	{
		if ($row > mysql_num_rows ($this->result[$resId]))
			return NULL;

		mysql_data_seek ($this->result[$resId], $row);

		if (is_int ($col) && $col > mysql_num_fields ($this->result[$resId]))
			return NULL;

		if (is_string ($col))
		{
			$rowData = mysql_fetch_assoc ($this->result[$resId]);

			if (! array_key_exists ($col, $rowData))
				return NULL;
		}

		return mysql_result ($this->result[$resId], $row, $col);
	}

	// === einige nuetzlich Sachen ========================================
	function setLocale ($locale)
	{
		$resId = $this->query ('set @LANG=\'' . $locale . '\'');
		$this->freeResult ($resId);
	}
};

/* quote string (und pack ' herum) */
function quoteS ($var)
{
	if ($var === NULL)
		return 'NULL';

	return "'" . addslashes ($var) . "'";
}

?>