Location: PHPKode > projects > TheoPlan > pm/dbaccess.inc
<?
$dbversion = "2.6";
$_Handle = -1;
$_hCount = 0;

function OpenDB($host = "", $dbname = "") {
	global $_Handle;
	global $_hCount;

	if ($_hCount > 0) {
	   $_hCount++;
	   return $_Handle;
	}

	if (!strlen($host) || !strlen($dbname)) {
	   $ini_array = parse_ini_file("setup/setup.dat");
	   $dbname = $ini_array['dbname'];
	   $host = $ini_array['domain'];
	   $port = $ini_array['port'];
	   $user = $ini_array['dbuser'];
	   $passwd = $ini_array['dbpasswd'];
	}

	if (isset($host) && strlen($host) > 0)
	   $connStr = "host=$host ";
	else
	   $connStr = "host=localhost";

	if (isset($dbname) && strlen($dbname) > 0)
	   $connStr .= " dbname=$dbname";

	if (isset($port) && strlen($port) > 0)
	   $connStr .= " port=$port";

	if (isset($user) && strlen($user) > 0)
	   $connStr .= " user=$user";

	if (isset($passwd) && strlen($passwd) > 0)
	   $connStr .= " password=$passwd";

	$db = pg_connect($connStr);

	if ($db == false) {
	   echo "<p class=\"error\">OpenDB Error: Datenbank $dbname konnte nicht ge&ouml;ffnet werden!</p>\n";
	   return false;
	}

	$_Handle = $db;
	$_hCount++;
	return $db;
}

function QueryDB($db, $qstr) {
	global $_hCount;

	if ($_hCount == 0) {
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
	   return NULL;
	}

	if (!isset($db) || !isset($qstr) || $qstr == "") {
		echo "<p class=\"error\">ERROR: Keine g&uuml;ltige SQL-Abfrage!</p>\n";
		return NULL;
	}

	$result = pg_query($db, $qstr);

	if (!$result) {
		$errormessage = pg_errormessage($db);
		echo "<p class=\"error\">ERROR: $errormessage<br>\n";
		echo "<br>SQL-Error: $qstr</p>\n";
		return NULL;
	}

	return $result;
}

function TQueryDB($db, $qstr) {
	global $_hCount;

	if ($_hCount == 0) {
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
	   return NULL;
	}

	if (!isset($db) || !isset($qstr) || $qstr == "") {
		echo "<p class=\"error\">ERROR: Keine g&uuml;ltige SQL-Abfrage!</p>\n";
		return NULL;
	}

	$result = pg_query($db, "BEGIN");

	if (!$result) {
		$errormessage = pg_errormessage($db);
		echo "<p class=\"error\">ERROR: $errormessage</td>\n";
		return NULL;
	}

	$result = pg_query($db, $qstr);

	if (!$result) {
		$errormessage = pg_errormessage($db);
		echo "<p class=\"error\">ERROR: $errormessage<br>\n";
		echo "<br>SQL-Error: $qstr</p>\n";
		pg_query($db, "ROLLBACK");
		return NULL;
	}

	$result = pg_query($db, "COMMIT");

	if (!$result) {
		$errormessage = pg_errormessage($db);
		echo "<p class=\"error\">ERROR: $errormessage</p>\n";
		return NULL;
	}

	return $result;
}

function fetchDB($result, $pos) {
	global $_hCount;

	if ($_hCount == 0) {
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
	   return false;
	}

	if (!isset($result)) {
	   Error("fetchDB: Es wurde kein Ergebnisset &uuml;bergeben!");
	   return false;
	}

	if (!isset($pos) || $pos < 0) {
	   Error("fetchDB: Keine oder ung&uuml;ltige Position!");
	   return false;
	}

	if (($erg = pg_fetch_row($result, $pos)) == false) {
	   Error("fetchDB: Fehler beim Auslesen eines Datensatzes!");
	   return false;
	}

	return $erg;
}

function numrowsDB($result) {
	global $_hCount;

	if ($_hCount == 0) {
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
	   return -1;
	}

	if (!isset($result)) {
	   Error("numrowDB: Es wurde kein Ergebnisset &uuml;bergeben!");
	   return -1;
	}

	if (($rows = pg_num_rows($result)) == -1) {
	   Error("numrowDB: Anzahl Datens&auml;tze konnte nicht bestimmt werden!");
	   return -1;
	}

	return $rows;
}

function numrowDB($result) {
	return numrowsDB($result);
}

function closeDB($db) {
	global $_hCount;
	global $_Handle;

	if ($_hCount > 1) {
	   $_hCount--;
	   return true;
	}

	$_hCount = 0;
	$_Handle = -1;
	return pg_close($db);
}

function Journal($typ, $message, $db=NULL) {
	global $unum;
	$dbflag = false;

	if (!isset($unum) || $unum <= 0)
	   return;

	if ($db == NULL) {
	   $db = OpenDB();
	   $dbflag = true;
	}

	$query = "select kj_num from key_jtype where kj_num = $typ";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	if (pg_num_rows($result) <= 0)
	   $typ = 999;

	$query = "select co_ejournal from counter";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = pg_fetch_row($result, 0);
	$co_ejournal = $data[0] + 1;
	$zeit = time();

	$query = "insert into ejournal (ej_num, ej_date, ej_uid, ej_type,";
	$query .= "ej_text) values ($co_ejournal, $zeit, $unum, $typ, '$message')";

	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return;
	}

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return;
	}

	$query = "update counter set co_ejournal = $co_ejournal";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return;
	}

	QueryDB($db, "commit");

	if ($dbflag)
	   closeDB($db);
}

function Error($message) {
	echo "<p class=\"error\">$message</p>\n";
}

# Pruefen der Datenbankversion:
#
$dbver = OpenDB();
$qs = "select version from counter";

if (!($erg = QueryDB($dbver, $qs))) {
   Error("Falsche oder keine Datenbank!");
   exit;
}

$data = pg_fetch_row($erg, 0);

if ($data[0] != $dbversion) {
   Error("Die Datenbank hat eine ung&uuml;ltige Versionsnummer!");
   closeDB($dbver);
   exit;
}

closeDB($dbver);
unset($dbver);
unset($erg);
unset($qs);
unset($data);
?>
Return current item: TheoPlan