A
download db_pg.php
Language: PHP
LOC: 133
Project Info
Virtual Photo Album(vphotoalb)
Server: SourceForge
Type: cvs
...alb\vphotoalb\html\include\
   .htaccess
   antispam.php
   constants.php
   db.php
   db_my.php
   db_pg.php
   db_schema.php
   faq_strings.php
   form.php
   functions.php
   globals.php
   html.php
   html_verifier.php
   ImageProcessing.php
   input.php
   mime.php
   originals.php
   PictureFinder.php
   queries.php
   read_exif_data.php
   read_tiff_data.php
   SearchParameters.php
   security.php
   strings.en.php
   strings.fr.php
   strings.php
   tables.php

<?php
/* Database function declarations for PostgresSQL.
 *
 * Written by: Chris Studholme
 * Copyright:  GPL (http://www.fsf.org/copyleft/gpl.html)
 * $Id: db_pg.php,v 1.7 2002/08/16 22:48:55 cstudhol Exp $
 */


class pg_resultset extends db_resultset {

  var $handle;
  var $row;
  var $numrows;

  // constructor: new resultset
  function pg_resultset($result) {
    $this->handle = $result;
    $this->row = 0;
    $this->numrows = pg_numrows($result);
  }

  // number of rows in result set
  function num_rows() {
    return $this->numrows;
  }

  // number of fields in result set
  function num_fields() {
    return pg_numfields($this->handle);
  }

  // last error message related to this resultset
  function last_error() {
    return pg_result_error($this->handle);
  }

  // close resultset and free memory
  function close() {
    pg_freeresult($this->handle);
    $this->handle=false;
  }

  // return field name
  function field_name($index) {
    return pg_fieldname($this->handle,$index);
  }

  // fetch a specific row and column from the result set
  function result($row,$field) {
    return pg_result($this->handle,$row,$field);
  }

  // reset to the first row in the result set
  function reset() {
    $this->row=0;
  }
  
  // return the next row as an array or false if there are no more rows
  function fetch_array($result_type=false) {
    if ($this->row<$this->numrows) {
      switch ($result_type) {
      case $this->NUM:   $rt = PGSQL_NUM;   break;
      case $this->ASSOC: $rt = PGSQL_ASSOC; break;
      default:           $rt = PGSQL_BOTH;  break;
      }
      return pg_fetch_array($this->handle,$this->row++,$rt);
    }
    return false;
  }
}


class pg_connection extends db_connection{

  var $handle=false;

  // constructor: create connection to database
  function pg_connection($conn_string=false,$persistent=false) {
    if ($conn_string) {
      if ($persistent)
	$this->pconnect($conn_string);
      else
	$this->connect($conn_string);
    }
  }

  // create connection to database
  function connect($conn_string) {
    $this->handle = pg_connect($conn_string);
    return $this->handle;
  }

  // create persistent connection
  function pconnect($conn_string) {
    $this->handle = pg_pconnect($conn_string);
    return $this->handle;
  }

  // close the connection
  function close() {
    if ($this->handle) {
      pg_close($this->handle);
      $this->handle = false;
    }
  }

  // get last error message from connection
  function last_error() {
    return pg_errormessage($this->handle);
  }

  // return database name
  function name() {
    return pg_dbname($this->handle);
  }

  // execute a database query
  // returns a db_result_set object
  function query($query,$return_error=false) {
    if (is_array($this->queries)) {
      $a["query"] = $query;
      list($usec,$sec)=explode(" ",microtime()); 
      $start = $sec + ((float)$usec);
    }
    $result = pg_exec($this->handle,$query);
    if (is_array($this->queries)) {
      list($usec,$sec)=explode(" ",microtime()); 
      $a["time"] = $sec + ((float)$usec) - $start;
      $this->queries[] = $a;
    }
    if ($result)
      return new pg_resultset($result);
    // an error occured (call error_function if set and requested)
    if ($this->error_function&&!$return_error) {
      $func=$this->error_function;
      $func($this,$query);
    }
    return false;
  }


  /**** transactions ****/

  function transaction_begin() {
    return true;
  }

  function transaction_commit() {
    return true;
  }

  function transaction_rollback() {
    return false;
  }


  /**** SQL feature set ****/

  // returns an associative array of features
  //function sql_features() {
  //  return parent::sql_features();
  // }


  /**** methods to help create queries ****/

  // sql representing current date/time
  //function date_now() {
  // return "now()";
  //}

  // $bit should be a number with only one bit set
  //function bit_set($field,$bit) {
  //  return "(".$field."%".($bit<<1).">=".$bit.")";
  //}
  
  // $bit should be a number with only one bit set
  //function bit_unset($field,$bit) {
  //  return "(".$field."%".($bit<<1)."<".$bit.")";
  //}

}

// backwards compatibility
function db_connect($name,$host,$login,$password) {
  $conn_string = "dbname=".$name.
    ($host?" host=".$host:"").
    ($login?" user=".$login:"").
    ($password?" password=".$password:"");
  $conn = new pg_connection($conn_string);
  if ($conn->handle)
    register_shutdown_function(array($conn,"close"));
  else
    $conn=false;
  return $conn;
}


// connect to database
$conn_string = "dbname=".$db_name.
  ($db_host?" host=".$db_host:"").
  ($db_login?" user=".$db_login:"").
  ($db_password?" password=".$db_password:"");
$conn = new pg_connection($conn_string,$db_persist);
if (!$conn->handle) {
  if (!$db_conn_error)
    die("Cannot connect to postgres database '".$db_name."'"); //STRING
  $conn=false;
}
else {
  register_shutdown_function(array($conn,"close"));
  $conn->set_error_function("db_error_function");
  if ($DEBUG_LEVEL>0)
    $conn->track_queries();
}

?>

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us