<?php
require_once ("err.php");
global $errStrings, $errSeverities;

class PgErr extends Err {
    function PgErr() {
	global $db;
	$this->Err ("DBERR", pg_errormessage($db->conn));
    }
}

class DB {
    var $conn, $connected, $trans;
    function DB () {

    }

    function connect ($dbname, $dbuser,$pass ) {
        if (!$this->connected) {
	    $this->conn = pg_connect("dbname=$dbname user=$dbuser password=$pass ")
		or terminate (/*new PgErr*/"Chyba komunikace se DB.");
	    $this->connected = 1;
        }
    }

    function close() {
	if ($this->connected) {pg_close ($this->conn);$this->connected = 0;}
    }

    function begin() {
        $res = pg_exec ($this->conn, "begin") or terminate (/*new PgErr*/"Chyba komunikace se DB.");
        $res = pg_exec ($this->conn, "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
            or terminate (/*new PgErr*/"Chyba komunikace se DB.");
   }

    function commit() {
	if ($this->trans) return;
        $res = pg_exec ($this->conn, "commit") or terminate (/*new PgErr*/"Chyba komunikace se DB.");
	$this->trans=1;
    }

    function rollback() {
	if (!$this->trans) return;
        $res = pg_exec ($this->conn, "rollback");
	$this->trans=0;
    }

    function query ($qstring) {
	$res = pg_exec ($this->conn, $qstring);
        if (gettype ($res) != 'resource')  {
	    //echo "$qstring<br>\n";
	    return /*new PgErr*/"Chyba komunikace se DB.";
	}
	return $res;
    }
}
$db=new DB;
?>
