db_pgsql.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:5k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. /*
  3.  * Session Management for PHP3
  4.  *
  5.  * Copyright (c) 1998,1999 SH Online Dienst GmbH
  6.  *                    Boris Erdmann, Kristian Koehntopp
  7.  *
  8.  * $Id: db_pgsql.inc,v 1.2 2000/04/11 14:46:24 prenagha Exp $
  9.  *
  10.  */ 
  11. class DB_Sql {
  12.   var $Host     = "";
  13.   var $Database = "";
  14.   var $User     = "";
  15.   var $Password = "";
  16.   var $Link_ID  = 0;
  17.   var $Query_ID = 0;
  18.   var $Record   = array();
  19.   var $Row      = 0;
  20.   var $Seq_Table     = "db_sequence";
  21.   var $Errno    = 0;
  22.   var $Error    = "";
  23.   var $Auto_Free = 0; # Set this to 1 for automatic pg_freeresult on 
  24.                       # last record.
  25.   function ifadd($add, $me) {
  26.   if("" != $add) return " ".$me.$add;
  27.   }
  28.   
  29.   function connect() {
  30.   if ( 0 == $this->Link_ID ) {
  31.   $cstr = "dbname=".$this->Database.
  32.   $this->ifadd($this->Host, "host=").
  33.   $this->ifadd($this->Port, "port=").
  34.   $this->ifadd($this->User, "user=").
  35.   $this->ifadd($this->Password, "password=");
  36.   $this->Link_ID=pg_pconnect($cstr);
  37.   if (!$this->Link_ID) {
  38.   $this->halt("Link-ID == false, pconnect failed");
  39.   }
  40.   }
  41.   }
  42.   function query($Query_String) {
  43.     $this->connect();
  44. #   printf("<br>Debug: query = %s<br>n", $Query_String);
  45.     $this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
  46.     $this->Row   = 0;
  47.     $this->Error = pg_ErrorMessage($this->Link_ID);
  48.     $this->Errno = ($this->Error == "")?0:1;
  49.     if (!$this->Query_ID) {
  50.       $this->halt("Invalid SQL: ".$Query_String);
  51.     }
  52.     return $this->Query_ID;
  53.   }
  54.   
  55.   function next_record() {
  56.     $this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
  57.     
  58.     $this->Error = pg_ErrorMessage($this->Link_ID);
  59.     $this->Errno = ($this->Error == "")?0:1;
  60.     $stat = is_array($this->Record);
  61.     if (!$stat && $this->Auto_Free) {
  62.       pg_freeresult($this->Query_ID);
  63.       $this->Query_ID = 0;
  64.     }
  65.     return $stat;
  66.   }
  67.   function seek($pos) {
  68.     $this->Row = $pos;
  69.   }
  70.   function lock($table, $mode = "write") {
  71.     if ($mode == "write") {
  72.       $result = pg_Exec($this->Link_ID, "lock table $table");
  73.     } else {
  74.       $result = 1;
  75.     }
  76.     return $result;
  77.   }
  78.   
  79.   function unlock() {
  80.     return pg_Exec($this->Link_ID, "commit");
  81.   }
  82.   /* public: sequence numbers */
  83.   function nextid($seq_name) {
  84.     $this->connect();
  85.     if ($this->lock($this->Seq_Table)) {
  86.       /* get sequence number (locked) and increment */
  87.       $q  = sprintf("select nextid from %s where seq_name = '%s'",
  88.                 $this->Seq_Table,
  89.                 $seq_name);
  90.       $id  = @pg_Exec($this->Link_ID, $q);
  91.       $res = @pg_Fetch_Array($id, 0);
  92.       
  93.       /* No current value, make one */
  94.       if (!is_array($res)) {
  95.         $currentid = 0;
  96.         $q = sprintf("insert into %s values('%s', %s)",
  97.                  $this->Seq_Table,
  98.                  $seq_name,
  99.                  $currentid);
  100.         $id = @pg_Exec($this->Link_ID, $q);
  101.       } else {
  102.         $currentid = $res["nextid"];
  103.       }
  104.       $nextid = $currentid + 1;
  105.       $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  106.                $this->Seq_Table,
  107.                $nextid,
  108.                $seq_name);
  109.       $id = @pg_Exec($this->Link_ID, $q);
  110.       $this->unlock();
  111.     } else {
  112.       $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
  113.       return 0;
  114.     }
  115.     return $nextid;
  116.   }
  117.   function metadata($table) {
  118.     $count = 0;
  119.     $id    = 0;
  120.     $res   = array();
  121.     $this->connect();
  122.     $id = pg_exec($this->Link_ID, "select * from $table");
  123.     if ($id < 0) {
  124.       $this->Error = pg_ErrorMessage($id);
  125.       $this->Errno = 1;
  126.       $this->halt("Metadata query failed.");
  127.     }
  128.     $count = pg_NumFields($id);
  129.     
  130.     for ($i=0; $i<$count; $i++) {
  131.       $res[$i]["table"] = $table;
  132.       $res[$i]["name"]  = pg_FieldName  ($id, $i); 
  133.       $res[$i]["type"]  = pg_FieldType  ($id, $i);
  134.       $res[$i]["len"]   = pg_FieldSize  ($id, $i);
  135.       $res[$i]["flags"] = "";
  136.     }
  137.     
  138.     pg_FreeResult($id);
  139.     return $res;
  140.   }
  141.   function affected_rows() {
  142.     return pg_cmdtuples($this->Query_ID);
  143.   }
  144.   function num_rows() {
  145.     return pg_numrows($this->Query_ID);
  146.   }
  147.   function num_fields() {
  148.     return pg_numfields($this->Query_ID);
  149.   }
  150.   function nf() {
  151.     return $this->num_rows();
  152.   }
  153.   function np() {
  154.     print $this->num_rows();
  155.   }
  156.   function f($Name) {
  157.     return $this->Record[$Name];
  158.   }
  159.   function p($Name) {
  160.     print $this->Record[$Name];
  161.   }
  162.   
  163.   function halt($msg) {
  164.     printf("</td></tr></table><b>Database error:</b> %s<br>n", $msg);
  165.     printf("<b>PostgreSQL Error</b>: %s (%s)<br>n",
  166.       $this->Errno,
  167.       $this->Error);
  168.     die("Session halted.");
  169.   }
  170.   function table_names() {
  171.     $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
  172.     $i=0;
  173.     while ($this->next_record())
  174.      {
  175.       $return[$i]["table_name"]= $this->f(0);
  176.       $return[$i]["tablespace_name"]=$this->Database;
  177.       $return[$i]["database"]=$this->Database;
  178.       $i++;
  179.      }
  180.     return $return;
  181.   }
  182. }
  183. ?>