download this file: class.pgsql.dbs.php view text/plain: class.pgsql.dbs.php file encoding: UTF-8 [goback]
<?php
##
## this file name is 'class.pgsql.dbs.php'
##
## pgsql object
##
## author : Chilbong Kim<san2(at)linuxchannel.net>
##
## changes :
##    - 2003.07.11 : check is_array()
##    - 2003.06.12 : new build
##
## references :
##
## usage(example) :
##    require_once 'class.pgsql.dbs.php';
##    $pgsql = new pgsql; // none debug mode
##    $pgsql->connect('localhost','user','passwd','dbname');
##    $result = $pgsql->query('....');
##    ...
##    $pgsql->close();
##

class pgsql
{
  var 
$debug 0;    // boolean, 1(true), 0(false), debug mode
  
var $link;        // resource link_identifier
  
var $dbname '';    // string, selected database name

  ## set debug mode
  ##
  
function pgsql($debug=0)
  {
    
$this->debug $debug;
  }

  
## pgsql connect
  ##
  ## return :
  ##    $resource    resource id
  ##
  
function connect($host$user$passwd$dbname='')
  {
    
define('__SQLSTART__',microtime());

    
$connect $this->connect_string($host,$user,$passwd,$dbname);

    if(
$this->debug)
    { 
$this->link pg_connect($connect); }
    else
    { 
$this->link = @pg_connect($connect); }

    return 
$this->link;
  }

  
## pgsql persistent connection to pgsql server
  ##
  
function pconnect($host$user$passwd$dbname='')
  {
    
define('__SQLSTART__',microtime());

    
$connect $this->connect_string($host,$user,$passwd,$dbname);

    if(
$this->debug)
    { 
$this->link pg_pconnect($host,$user,$passwd); }
    else
    { 
$this->link = @pg_pconnect($host,$user,$passwd); }

    return 
$this->link;
  }

  function 
connect_string($host$user$passwd$dbname='')
  {
    if(
$host$connect "host=$host";
    if(
$user$connect .= " user=$user";
    if(
$passwd$connect .= " password=$passwd";
    if(
$dbname$connect .= " dbname=$dbname";

    return 
$connect;
  }

  
## pgsql close
  ##
  
function close($link='')
  {
    
$result = @pg_close($link $link $this->link);

    
define('__SQLEND__',microtime());

    return 
$result;
  }

  
## pgsql result
  ##
  
function result($result$row$field)
  {
    return 
pg_result($result,$row,$field);
  }

  
## this NULL
  ##
  
function select_db($dbname='')
  {
    return 
0//always false
  
}

  
## pgsql query
  ##
  
function query($sql$link='')
  {
    if(
$link$result pg_query($link,$sql);
    else 
$result pg_query($sql);

    return 
$result;
  }

  
## pgsql query
  ##
  
function db_query($dbname$sql)
  {
    return 
0// always false
  
}

  
## pgsql free result
  ##
  
function free_result($result)
  {
    return @
pg_free_result($result);
  }

  
## pgsql fetch array
  ##
  
function fetch_array($result)
  {
    return 
pg_fetch_array($result);
  }

  
## pgsql fetch row
  ##
  
function fetch_row($result)
  {
    return 
pg_fetch_row($result);
  }

  
## pgsql fetch assoc
  ##
  
function fetch_assoc($result)
  {
    return 
pg_fetch_assoc($result);
  }

  
## pgsql fetch object
  ##
  
function fetch_object($result)
  {
    return 
pg_fetch_object($result);
  }

  
## pgsql list dbs
  ##
  
function list_dbs($link='')
  {
    
$result = @pg_list_dbs($link $link $this->link);
    
$this->error(pg_result_error(),'list_dbs()');

    return 
$result;
  }

  
## pgsql data seek
  ##
  
function data_seek($result$number)
  {
    
$boolean = @pg_data_seek($result,$number);
    
$this->error(pg_result_error(),'data_seek()');

    return 
$boolean;
  }

  
## pgsql list tables
  ##
  
function list_tables($dbname='')
  {
    
$result = @pg_list_tables($dbname $dbname $this->dbname);
    
$this->error(pg_result_error(),'list_tables()');

    return 
$result;
  }

  
## pgsql get list tables
  ##
  
function get_list_tables($dbname='')
  {
    
$result $this->list_tables($dbname);
    
$this->error(pg_result_error(),'get_list_tables()');

    if(
$result) {
        while(
$row $this->fetch_row($result))
        { 
$tables[] = $row[0]; }
    }

    return 
$tables// array
  
}

  
## pgsql check table exist
  ##
  
function table_exists($table$dbname='')
  {
    
$tables $this->get_list_tables($dbname);
    if(
$tables && is_array($tables)) $result = @in_array($table,$tables);
    unset(
$tables);

    return 
$result// boolean
  
}

  
## pgsql num rows
  ##
  
function num_rows($result)
  {
    
$rows = @pg_num_rows($result);
    
$this->error(pg_result_error(),'num_rows()');

    return 
$rows// int
  
}

  
/***
  ## pgsql error report
  ##
  function error($error, $func)
  {
    if($this->debug && $errno)
    { echo 'ERRNO : '.$error.' : '.$func.' in '.__FILE__."<BR>\n"; }
  }
  ***/
// end of class

?>