"Fossies" - the Fresh Open Source Software Archive

Member "mythreads/lib/phplib/db_pgsql.inc" (29 Mar 2001, 5400 Bytes) of package /linux/privat/mythreads-links_1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "db_pgsql.inc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.0_vs_1.2.1.

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