<?php

class Database {
	var $Host;
	var $Database;
	var $User;
	var $Password;
	var $Port;

	var $Link_ID = 0;
	var $Last_ID   = 0;
	var $Query_ID = 0;
	var $Record = array();
	var $Row = 0;
	var $Tuples = 0;
	var $lo_oid = 0;

	var $Seq_Table = 'db_sequence';

	var $Errors = array();

	var $Auto_Free = 1;

	function Database($host = '', $db = '', $user = '', $pwd = '', $port = '5432') {
		$this->Host = $host;
		$this->Database = $db;
		$this->User = $user;
		$this->Password = $pwd;
		$this->Port = $port;
		$this->query_count = 0;
		$this->debugmessage = '';
	}
	
	function setError($error){
		$this->Errors[] = $error;
		return false;
	}

	function ifadd($add, $me) {
		if('' != $add)
			return ' '.$me.$add;
	}

	function connect() {
		if($this->Link_ID != 0)
			return true;
		$cstr = 'dbname='.$this->Database.$this->ifadd($this->Host, 'host=').
		$this->ifadd($this->Port, 'port=').$this->ifadd($this->User, 'user=').
		$this->ifadd($this->Password, 'password=');
		$result = @pg_connect($cstr);
		if($result === false)
			return $this->setError('Unable to connect to PostgreSQL server');
		$this->Link_ID = $result;
		return true;
	}
	
	function close() {
		if($this->Link_ID == 0)
			return true;
		$result = pg_close($this->Link_ID);
		return $result;
	}

	function query($Query_String) {
		GLOBAL $Debug;
		if(!empty($this->Errors)) return false;
		if($Query_String == '')
			return 0;
		if($this->Link_ID == 0)
			$this->connect();
		if($Debug)
			cd($Query_String, "Query ('".$this->Host."' => '".$this->Database."' pgsql)", $this->debugmessage);
		ci("Query ('".$this->Host."' => '".$this->Database."' pgsql)");
		$this->Query_ID = pg_query($this->Link_ID, $Query_String);
		$this->query_count++;
		$this->Tuples = pg_affected_rows($this->Query_ID);
		$this->Last_ID = pg_last_oid($this->Query_ID);
		$this->Row = 0;
		$error = pg_last_error($this->Link_ID);
		if ($error != '')
			$this->Errors[] = $error;
		cs("Query ('".$this->Host."' => '".$this->Database."' pgsql)", "Query execution time (sec.) ");
		if($Debug && !$this->Query_ID)
			cd(end($this->errors), "'".$this->Host."' => '".$this->Database."' Error", $this->debugmessage);
		return $this->Query_ID;
	}

	function next_record() {
		$this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
		$error = pg_last_error($this->Link_ID);
		if ($error != '')
			$this->Errors[] = $error;
		$stat = is_array($this->Record);
		if(!$stat && $this->Auto_Free) {
			pg_free_result($this->Query_ID);
			$this->Query_ID = 0;
		}
		return $stat;
	}

	function seek($pos) {
		$this->Row = $pos;
	}

	function lock($table, $mode = 'write') {
		if($mode == 'write')
			$result = pg_query($this->Link_ID, 'LOCK TABLE '.$table);
		else
			$result = 1;
		return $result;
	}

	function unlock() {
		return pg_query($this->Link_ID, "COMMIT");
	}

	function metadata($table) {
		$res = array();
		$this->query('SELECT * FROM "'.$table.'" LIMIT 1');
		for($i = 0; $i < $this->num_fields(); $i++)
			$res[$i] = array(
				'table' => $table,
				'name' => pg_field_name($this->Query_ID, $i),
				'type' => pg_field_type($this->Query_ID, $i),
				'len' => pg_field_size($this->Query_ID, $i),
				'flags' => '',
			);
		return $res;
	}

	function affected_rows() {
		return pg_affected_rows($this->Query_ID);
	}

	function num_rows() {
		return pg_num_rows($this->Query_ID);
	}

	function num_fields() {
		return pg_num_fields($this->Query_ID);
	}

	function f($Name) {
		return $this->Record[$Name];
	}

	function table_names() {
		$this->query("SELECT relname FROM pg_class WHERE relkind = 'r' AND NOT relname LIKE 'pg_%'");
		while($this->next_record())
			$return[] = array(
				'table_name' => $this->f(0),
				'tablespace_name' => $this->Database,
				'database' => $this->Database
			);
		return $return;
	}
	
	function SelectAny($query) {
		if(!empty($this->Errors)) return false;
		$array = array();
		$this->query($query);
		while($this->next_record())
			$array[] = removeIntegerKeysFromArray($this->Record);
		return $array;
	}
	
	function update($table, $values = '', $conditions = '', $prepared = 1) {
		if(empty($table)) return FALSE;
		$query = 'UPDATE "'.$table.'" SET '.listArrayKeysAndValues($this->addSlashesToArray($values), '=', ', ', $prepared);
		$query.= ' WHERE '.listArrayKeysAndValues($conditions, '=', ' and ');
		return $this->query($query);
	}
	
	function addSlashesToArray($arr){
		foreach($arr as $k => $v) $slashedarr[$k] = addslashes($v);
		return $slashedarr;
	}
}

?>