<script language="php">
 /* (c) VASEN S.A. Junio 2004 , rch@todo.com.uy */


class TDB_ds { // data set
  var $hds; // handle del data set
  var $row;
  
  function TDB_ds ( $con, $sql ){
    if ($this->hds= pg_exec( $con->hcon, $sql )){
	$this->krow= -1;
    }
  }


  function ncols( ){
    $res=pg_num_fields($this->hds);
    return $res;
  }

  function nrows() {
    $res=  pg_numrows( $this->hds );
	return $res;
  }

  function go( $krow){
    $this->krow=$krow;
    if ($this->row=@pg_fetch_array($this->hds, $krow, PGSQL_BOTH)){
      return $this->row;
    }else{
      return false;
    }
  }

	
  function first(){
    $this->krow=0;
    if ($this->row=@pg_fetch_array($this->hds,0, PGSQL_BOTH)){
      return $this->row;
    }else{
      return false;
    }
  }

  function next( ){
    if ($this->row= @pg_fetch_array($this->hds, $this->krow+1, PGSQL_BOTH ) ){
      $this->krow++;
      return $this->row;
    }else{
      return false;
    }      
  }

  function go_obj( $krow){
    $this->krow=$krow;
    if ($this->row=@pg_fetch_object($this->hds,$krow, PGSQL_BOTH)){
      return $this->row;
    }else{
      return false;
    }
  }


  function first_obj( ){
    $this->krow=0;
    if ($this->row=@pg_fetch_object($this->hds,0, PGSQL_BOTH)){
      return $this->row;
    }else{
      return false;
    }
  }



  function next_obj(){
    if ($this->row= @pg_fetch_object($this->hds, $this->krow+1 ) ){
      $this->krow++;
      return $this->row;
    }else{
      return false;
    }      
  }

  function fname( $kcol){
    return pg_field_name($this->hds,$kcol);
  }

  function ftype( $kcol ){
	 return pg_field_type($this->hds, $kcol);
  }


  function flen( $kcol ){
	 return pg_field_size($this->hds, $kcol);
  }



  function fgetval( $kcol ){
    return $this->row[$kcol];
  }


}


class TDB_con {  // conexión
  	var $hcon;   // handle de la conexión
	var $dbg_on;
	var $_rgxp_;
	
  function TDB_con($host, $base, $usuario, $clave ){
	$this->_rgxp_= '~*';

	$con_str = "host=$host user=$usuario port=5432 dbname=$base password=$clave";
    $this->hcon= pg_connect( $con_str );
    $this->dbg_on= false;
    if (!$this->hcon) {
		die('Could not connect: ' . pg_last_error());
    }
  }

  function query( $sql_str ){
    $ds= new TDB_ds( $this, $sql_str );
    if ( $ds->hds ){
      return $ds;
    }else{
        if ($this->dbg_on ) echo ("ERROR query_sql: $sql_str <br>".pg_last_error() );
        return false;
    }
  }

  function exec( $sql_str ){
  	$res= pg_exec( $this->hcon, $sql_str );
        if ( !$res && $this->dbg_on ) echo ("ERROR exec_sql: $sql_str<br>".pg_last_error() );
        return $res;
    }



  function close( ){
    pg_close( $this->hcon );
  }


    function BEGIN_WORK(){
        $sql= 'BEGIN WORK;';
            return $this->exec( $sql );
        }

    function COMMIT(){
        $sql= 'COMMIT;';
        return $this->exec( $sql );
    }
    
    function ROLLBACK(){
        $sql= 'ROLLBACK;';
        return $this->exec( $sql );
    }
    

// retorna un valor es como evaluar la funcion sql
	function f( $sql ){
		$ds= $this->query($sql);
        if ($ds != '') {
		$r= $ds->next();
		return $r[0];
        } else {
            return '';
	}
    }

    function f_obj($sql) {
        $ds = $this->query($sql);
        if ($ds != '') {
            $r = $ds->next_obj();
            return $r;
        } else {
            return '';
        }
    }

    function f_rec($sql) {
        $ds = $this->query($sql);
        if ($ds != '') {

            $r = $ds->next();
            return $r;
        } else {
            return '';
        }
    }

   

    function now() {
        return $this->f("SELECT DATE_FORMAT(now(), '%d/%m/%Y&nbsp;%H:%i') ");
    }

    function nextval( $seqname ){
    	return $this->f( "SELECT nextval('$seqname') ");
	 }



	function booleval( $aval ){
		return $aval=='t';
	}



	function error(){
	 return pg_last_error();
	}


}
</script>