<? 
 /* Database abstraction layer for PHP 3.0
  *
  * Instituut ICT 1998
  * Bestemd voor module IG8_INT 
  *
  * K.Tukker k.tukker@ict.hen.nl
  * W.Smith  w.f.b.smith@ict.hen.nl
  */	 

  function debug($str) {
    echo "[$str]";
  }

class db {

  var $db_type;
  var $db_result;
  var $connect_id;
  var $cursor;
  var $row;
 

  function init($database_type) {
    if ($this->db_type && $this->db_type!="") {
      return 0;
    }
    $this->db_type=$database_type;
    return 1;
  }

  function open($database, $host, $user, $password) {
    switch($this->db_type) {
      case "adabas":
             $this->connect_id=ada_connect($database, $user, $password);
             break;
      case "msql":
             $this->connect_id=msql_pconnect($host);
             if ($this->connect_id) {
               $result=msql_select_db($database, $this->connect_id);
               if (!$result) {
                 msql_close($this->connect_id);
                 $this->connect_id=$result;
               }
             }
             break;
      case "mysql":
             if ($user=="") {
               $this->connect_id=mysql_pconnect($host);
             } else if ($password=="") { 
               $this->connect_id=mysql_pconnect($host, $user);
             } else { 
               $this->connect_id=mysql_pconnect($host, $user, $password); 
             }
             if ($this->connect_id) {
               $result=mysql_select_db($database);
               if (!$result) {
                 mysql_close($this->connect_id);
                 $this->connect_id=$result;
               }
             }
             break;
      case "odbc":
             $this->connect_id=odbc_connect($database, $user, $password);
             break;
      case "oracle":
             $this->connect_id=Ora_Logon($user, $password); 
             $this->cursor=Ora_Open($this->connect_id); 
             Ora_CommitOn($this->connect_id);
             break;
      case "postgres":
             $this->connect_id=pg_pconnect($host, "", "", "", $database);
             break;
    }
    return $this->connect_id;
  }

  function query($query) {
    switch($this->db_type) {
      case "adabas":
             $result=ada_exec($this->connect_id, $query);
             break;
      case "msql":
             $result=msql_query($query, $this->connect_id);
             break;
      case "mysql":
             $result=mysql_query($query, $this->connect_id);
             break;
      case "odbc":
             $result=odbc_exec($this->connect_id, $query);
             break;
      case "oracle":
             $result=Ora_Parse($this->cursor, $query); 
             if ($result) {
               $result=Ora_Exec($this->cursor);
             }
             break;
      case "postgres":
             $result=pg_exec($this->connect_id, $query);
             break;
    }
    $this->db_result=$result;
    return $result;
  }
  
  function getrow() {
    switch($this->db_type) {
      case "adabas":
             $this->row=ada_fetchrow($this->db_result);
             break;
      case "msql":
             $this->row=msql_fetch_array($this->db_result);
             break;
      case "mysql":
             $this->row=mysql_fetch_array($this->db_result);
             break;
      case "odbc":
             $this->row=odbc_fetch_row($this->db_result);
             break;
      case "oracle":
             $this->row=Ora_Fetch($this->cursor);
             break;
      case "postgres":
             $this->row++;
    }
    return $this->row;
  }
   
  function field($field) {
    switch($this->db_type) {
      case "adabas":
             $result=ada_result($this->row, $field);
             break;
      case "msql":
      case "mysql":
             $result=$this->row[$field];
             break;
      case "odbc":
             $result=odbc_result($this->db_result, $field);
             break;
      case "oracle":
             $result=Ora_GetColumn($this->cursor,$field);
             break;
      case "postgres":
             $result=pg_Result($this->connect_id, $this->row, $field);
             break;
    }
    return $result;
  }

  function firstrow() {
    switch($this->db_type) {
      case "adabas":
             $this->row=ada_fetchrow($this->db_result,1);
             break;
      case "msql":
             $result=msql_data_seek($this->db_result,0);
             if ($result) {
               $result=$this->getrow();
             }
             break;
      case "mysql":
             $result=mysql_data_seek($this->db_result,0);
             if ($result) {
               $result=$this->getrow();
             }
             break;
      case "odbc":
             $this->$row=odbc_fetch_row($this->db_result,1);
             break;
      case "oracle":
             $result=Ora_Exec($this->cursor);
             if ($result) {
               $result=$this->getrow();
             }
             break;
      case "postgres":
             $this->$row=1;
             break;
    }
    return $this->row;
  }
     
  function close() {
    switch($this->$db_type) {
      case "adabas":
             $result=ada_close($this->connect_id);
             break;
      case "msql":
             $result=msql_close($this->connect_id);
             break;
      case "mysql":
             $result=mysql_close($this->connect_id);
             break;
      case "odbc":
             odbc_close();
             $result=1; // can't check.
             break;
      case "oracle":
             $result=Ora_Logoff($this->connect_id);
             break;
      case "postgres":
             $result=pg_Close();
             break;
    }
    return $result;
  }

}

?>
