home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / database / pgsql.class.inc < prev    next >
Text File  |  2004-03-08  |  6KB  |  233 lines

  1. <?php
  2. /*
  3.  * Session Management for PHP3
  4.  *
  5.  * Copyright (c) 1998-2000 NetUSE AG
  6.  *                    Boris Erdmann, Kristian Koehntopp
  7.  *
  8.  * $Id: pgsql.class.inc,v 1.1.1.1 2003/07/03 15:04:02 mschering Exp $
  9.  *
  10.  */
  11.  
  12. class db {
  13.   var $Host     = "";
  14.   var $Database = "";
  15.   var $User     = "";
  16.   var $Password = "";
  17.  
  18.   var $Link_ID  = 0;
  19.   var $Query_ID = 0;
  20.   var $Record   = array();
  21.   var $Row      = 0;
  22.  
  23.   var $Seq_Table     = "db_sequence";
  24.  
  25.   var $Errno    = 0;
  26.   var $Error    = "";
  27.   var $Debug    = 0;
  28.  
  29.   var $Auto_Free = 0; # Set this to 1 for automatic pg_freeresult on
  30.                       # last record.
  31.  
  32.   function ifadd($add, $me) {
  33.           if("" != $add) return " ".$me.$add;
  34.   }
  35.  
  36.   /* public: constructor */
  37.   function db($query = "") {
  38.       global $GO_CONFIG;
  39.       $this->Host = $GO_CONFIG->db_host;
  40.       $this->Database = $GO_CONFIG->db_name;
  41.       $this->User = $GO_CONFIG->db_user;
  42.         $this->Password = $GO_CONFIG->db_pass;
  43.       $this->query($query);
  44.   }
  45.  
  46.   function connect() {
  47.           if ( 0 == $this->Link_ID ) {
  48.                   $cstr = "dbname=".$this->Database.
  49.                   $this->ifadd($this->Host, "host=").
  50.                   $this->ifadd($this->Port, "port=").
  51.                   $this->ifadd($this->User, "user=").
  52.                   $this->ifadd($this->Password, "password=");
  53.                   $this->Link_ID=pg_pconnect($cstr);
  54.                   if (!$this->Link_ID) {
  55.                           $this->halt("Link-ID == false, pconnect failed");
  56.                   }
  57.           }
  58.   }
  59.  
  60.   function query($Query_String) {
  61.     /* No empty queries, please, since PHP4 chokes on them. */
  62.     if ($Query_String == "")
  63.       /* The empty query string is passed on from the constructor,
  64.        * when calling the class without a query, e.g. in situations
  65.        * like these: '$db = new DB_Sql_Subclass;'
  66.        */
  67.       return 0;
  68.  
  69.     $this->connect();
  70.  
  71.     if ($this->Debug)
  72.       printf("<br>Debug: query = %s<br>\n", $Query_String);
  73.  
  74.     $this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
  75.     $this->Row   = 0;
  76.  
  77.     $this->Error = pg_ErrorMessage($this->Link_ID);
  78.     $this->Errno = ($this->Error == "")?0:1;
  79.     if (!$this->Query_ID) {
  80.       $this->halt("Invalid SQL: ".$Query_String);
  81.     }
  82.  
  83.     return $this->Query_ID;
  84.   }
  85.  
  86.   function next_record() {
  87.     $this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
  88.  
  89.     $this->Error = pg_ErrorMessage($this->Link_ID);
  90.     $this->Errno = ($this->Error == "")?0:1;
  91.  
  92.     $stat = is_array($this->Record);
  93.     if (!$stat && $this->Auto_Free) {
  94.       pg_freeresult($this->Query_ID);
  95.       $this->Query_ID = 0;
  96.     }
  97.     return $stat;
  98.   }
  99.  
  100.   function seek($pos) {
  101.     $this->Row = $pos;
  102.   }
  103.  
  104.   function lock($table, $mode = "write") {
  105.     if ($mode == "write") {
  106.       $result = pg_Exec($this->Link_ID, "lock table $table");
  107.     } else {
  108.       $result = 1;
  109.     }
  110.     return $result;
  111.   }
  112.  
  113.   function unlock() {
  114.     return pg_Exec($this->Link_ID, "commit");
  115.   }
  116.  
  117.  
  118.   /* public: sequence numbers */
  119.   function nextid($seq_name) {
  120.     $this->connect();
  121.  
  122.     if ($this->lock($this->Seq_Table)) {
  123.       /* get sequence number (locked) and increment */
  124.       $q  = sprintf("select nextid from %s where seq_name = '%s'",
  125.                 $this->Seq_Table,
  126.                 $seq_name);
  127.       $id  = @pg_Exec($this->Link_ID, $q);
  128.       $res = @pg_Fetch_Array($id, 0);
  129.  
  130.       /* No current value, make one */
  131.       if (!is_array($res)) {
  132.         $currentid = 0;
  133.         $q = sprintf("insert into %s values('%s', %s)",
  134.                  $this->Seq_Table,
  135.                  $seq_name,
  136.                  $currentid);
  137.         $id = @pg_Exec($this->Link_ID, $q);
  138.       } else {
  139.         $currentid = $res["nextid"];
  140.       }
  141.       $nextid = $currentid + 1;
  142.       $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  143.                $this->Seq_Table,
  144.                $nextid,
  145.                $seq_name);
  146.       $id = @pg_Exec($this->Link_ID, $q);
  147.       $this->unlock();
  148.     } else {
  149.       $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
  150.       return 0;
  151.     }
  152.     return $nextid;
  153.   }
  154.  
  155.  
  156.  
  157.   function metadata($table) {
  158.     $count = 0;
  159.     $id    = 0;
  160.     $res   = array();
  161.  
  162.     $this->connect();
  163.     $id = pg_exec($this->Link_ID, "select * from $table");
  164.     if ($id < 0) {
  165.       $this->Error = pg_ErrorMessage($id);
  166.       $this->Errno = 1;
  167.       $this->halt("Metadata query failed.");
  168.     }
  169.     $count = pg_NumFields($id);
  170.  
  171.     for ($i=0; $i<$count; $i++) {
  172.       $res[$i]["table"] = $table;
  173.       $res[$i]["name"]  = pg_FieldName  ($id, $i);
  174.       $res[$i]["type"]  = pg_FieldType  ($id, $i);
  175.       $res[$i]["len"]   = pg_FieldSize  ($id, $i);
  176.       $res[$i]["flags"] = "";
  177.     }
  178.  
  179.     pg_FreeResult($id);
  180.     return $res;
  181.   }
  182.  
  183.   function affected_rows() {
  184.     return pg_cmdtuples($this->Query_ID);
  185.   }
  186.  
  187.   function num_rows() {
  188.     return pg_numrows($this->Query_ID);
  189.   }
  190.  
  191.   function num_fields() {
  192.     return pg_numfields($this->Query_ID);
  193.   }
  194.  
  195.   function nf() {
  196.     return $this->num_rows();
  197.   }
  198.  
  199.   function np() {
  200.     print $this->num_rows();
  201.   }
  202.  
  203.   function f($Name) {
  204.     return $this->Record[$Name];
  205.   }
  206.  
  207.   function p($Name) {
  208.     print $this->Record[$Name];
  209.   }
  210.  
  211.   function halt($msg) {
  212.     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  213.     printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
  214.       $this->Errno,
  215.       $this->Error);
  216.     die("Session halted.");
  217.   }
  218.  
  219.   function table_names() {
  220.     $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
  221.     $i=0;
  222.     while ($this->next_record())
  223.      {
  224.       $return[$i]["table_name"]= $this->f(0);
  225.       $return[$i]["tablespace_name"]=$this->Database;
  226.       $return[$i]["database"]=$this->Database;
  227.       $i++;
  228.      }
  229.     return $return;
  230.   }
  231. }
  232. ?>
  233.