home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / database / mysql.class.inc < prev    next >
Text File  |  2004-03-08  |  10KB  |  384 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: mysql.class.inc,v 1.1.1.1 2003/07/03 15:03:59 mschering Exp $
  9.  *
  10.  */
  11.  
  12. class db {
  13.  
  14.   /* public: connection parameters */
  15.   var $Host     = "";
  16.   var $Database = "";
  17.   var $User     = "";
  18.   var $Password = "";
  19.  
  20.   /* public: configuration parameters */
  21.   var $Auto_Free     = 0;     ## Set to 1 for automatic mysql_free_result()
  22.   var $Debug         = 0;     ## Set to 1 for debugging messages.
  23.   var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  24.   var $Seq_Table     = "db_sequence";
  25.  
  26.   /* public: result array and current row number */
  27.   var $Record   = array();
  28.   var $Row;
  29.  
  30.   /* public: current error number and error text */
  31.   var $Errno    = 0;
  32.   var $Error    = "";
  33.  
  34.   /* public: this is an api revision, not a CVS revision. */
  35.   var $type     = "mysql";
  36.   var $revision = "1.2";
  37.  
  38.   /* private: link and query handles */
  39.   var $Link_ID  = 0;
  40.   var $Query_ID = 0;
  41.  
  42.  
  43.  
  44.   /* public: constructor */
  45.   function db($query = "") {
  46.       global $GO_CONFIG;
  47.       $this->Host = $GO_CONFIG->db_host;
  48.       $this->Database = $GO_CONFIG->db_name;
  49.       $this->User = $GO_CONFIG->db_user;
  50.         $this->Password = $GO_CONFIG->db_pass;
  51.       $this->query($query);
  52.   }
  53.  
  54.   /* public: some trivial reporting */
  55.   function link_id() {
  56.     return $this->Link_ID;
  57.   }
  58.  
  59.   function query_id() {
  60.     return $this->Query_ID;
  61.   }
  62.  
  63.   /* public: connection management */
  64.   function connect($Database = "", $Host = "", $User = "", $Password = "") {
  65.     /* Handle defaults */
  66.     if ("" == $Database)
  67.       $Database = $this->Database;
  68.     if ("" == $Host)
  69.       $Host     = $this->Host;
  70.     if ("" == $User)
  71.       $User     = $this->User;
  72.     if ("" == $Password)
  73.       $Password = $this->Password;
  74.  
  75.     /* establish connection, select database */
  76.     if ( 0 == $this->Link_ID ) {
  77.  
  78.       $this->Link_ID=mysql_pconnect($Host, $User, $Password);
  79.       if (!$this->Link_ID) {
  80.         $this->halt("pconnect($Host, $User, \$Password) failed.");
  81.         return 0;
  82.       }
  83.  
  84.       if (!@mysql_select_db($Database,$this->Link_ID)) {
  85.         $this->halt("cannot use database ".$this->Database);
  86.         return 0;
  87.       }
  88.     }
  89.  
  90.     return $this->Link_ID;
  91.   }
  92.  
  93.   /* public: discard the query result */
  94.   function free() {
  95.       @mysql_free_result($this->Query_ID);
  96.       $this->Query_ID = 0;
  97.   }
  98.  
  99.   /* public: perform a query */
  100.   function query($Query_String) {
  101.     /* No empty queries, please, since PHP4 chokes on them. */
  102.     if ($Query_String == "")
  103.       /* The empty query string is passed on from the constructor,
  104.        * when calling the class without a query, e.g. in situations
  105.        * like these: '$db = new DB_Sql_Subclass;'
  106.        */
  107.       return 0;
  108.  
  109.     if (!$this->connect()) {
  110.       return 0; /* we already complained in connect() about that. */
  111.     };
  112.  
  113.     # New query, discard previous result.
  114.     if ($this->Query_ID) {
  115.       $this->free();
  116.     }
  117.  
  118.     if ($this->Debug)
  119.       printf("Debug: query = %s<br>\n", $Query_String);
  120.  
  121.     $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
  122.     $this->Row   = 0;
  123.     $this->Errno = mysql_errno();
  124.     $this->Error = mysql_error();
  125.     if (!$this->Query_ID) {
  126.       $this->halt("Invalid SQL: ".$Query_String);
  127.     }
  128.  
  129.     # Will return nada if it fails. That's fine.
  130.     return $this->Query_ID;
  131.   }
  132.  
  133.   /* public: walk result set */
  134.   function next_record() {
  135.     if (!$this->Query_ID) {
  136.       $this->halt("next_record called with no query pending.");
  137.       return 0;
  138.     }
  139.  
  140.     $this->Record = @mysql_fetch_array($this->Query_ID);
  141.     $this->Row   += 1;
  142.     $this->Errno  = mysql_errno();
  143.     $this->Error  = mysql_error();
  144.  
  145.     $stat = is_array($this->Record);
  146.     if (!$stat && $this->Auto_Free) {
  147.       $this->free();
  148.     }
  149.     return $stat;
  150.   }
  151.  
  152.   /* public: position in result set */
  153.   function seek($pos = 0) {
  154.     $status = @mysql_data_seek($this->Query_ID, $pos);
  155.     if ($status)
  156.       $this->Row = $pos;
  157.     else {
  158.       $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
  159.  
  160.       /* half assed attempt to save the day,
  161.        * but do not consider this documented or even
  162.        * desireable behaviour.
  163.        */
  164.       @mysql_data_seek($this->Query_ID, $this->num_rows());
  165.       $this->Row = $this->num_rows;
  166.       return 0;
  167.     }
  168.  
  169.     return 1;
  170.   }
  171.  
  172.   /* public: table locking */
  173.   function lock($table, $mode="write") {
  174.     $this->connect();
  175.  
  176.     $query="lock tables ";
  177.     if (is_array($table)) {
  178.       while (list($key,$value)=each($table)) {
  179.         if ($key=="read" && $key!=0) {
  180.           $query.="$value read, ";
  181.         } else {
  182.           $query.="$value $mode, ";
  183.         }
  184.       }
  185.       $query=substr($query,0,-2);
  186.     } else {
  187.       $query.="$table $mode";
  188.     }
  189.     $res = @mysql_query($query, $this->Link_ID);
  190.     if (!$res) {
  191.       $this->halt("lock($table, $mode) failed.");
  192.       return 0;
  193.     }
  194.     return $res;
  195.   }
  196.  
  197.   function unlock() {
  198.     $this->connect();
  199.  
  200.     $res = @mysql_query("unlock tables", $this->Link_ID);
  201.     if (!$res) {
  202.       $this->halt("unlock() failed.");
  203.       return 0;
  204.     }
  205.     return $res;
  206.   }
  207.  
  208.  
  209.   /* public: evaluate the result (size, width) */
  210.   function affected_rows() {
  211.     return @mysql_affected_rows($this->Link_ID);
  212.   }
  213.  
  214.   function num_rows() {
  215.     return @mysql_num_rows($this->Query_ID);
  216.   }
  217.  
  218.   function num_fields() {
  219.     return @mysql_num_fields($this->Query_ID);
  220.   }
  221.  
  222.   /* public: shorthand notation */
  223.   function nf() {
  224.     return $this->num_rows();
  225.   }
  226.  
  227.   function np() {
  228.     print $this->num_rows();
  229.   }
  230.  
  231.   function f($Name) {
  232.     return $this->Record[$Name];
  233.   }
  234.  
  235.   function p($Name) {
  236.     print $this->Record[$Name];
  237.   }
  238.  
  239.   /* public: sequence numbers */
  240.   function nextid($seq_name) {
  241.     $this->connect();
  242.  
  243.     if ($this->lock($this->Seq_Table)) {
  244.       /* get sequence number (locked) and increment */
  245.       $q  = sprintf("select nextid from %s where seq_name = '%s'",
  246.                 $this->Seq_Table,
  247.                 $seq_name);
  248.       $id  = @mysql_query($q, $this->Link_ID);
  249.       $res = @mysql_fetch_array($id);
  250.  
  251.       /* No current value, make one */
  252.       if (!is_array($res)) {
  253.         $currentid = 0;
  254.         $q = sprintf("insert into %s values('%s', %s)",
  255.                  $this->Seq_Table,
  256.                  $seq_name,
  257.                  $currentid);
  258.         $id = @mysql_query($q, $this->Link_ID);
  259.       } else {
  260.         $currentid = $res["nextid"];
  261.       }
  262.       $nextid = $currentid + 1;
  263.       $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  264.                $this->Seq_Table,
  265.                $nextid,
  266.                $seq_name);
  267.       $id = @mysql_query($q, $this->Link_ID);
  268.       $this->unlock();
  269.     } else {
  270.       $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
  271.       return 0;
  272.     }
  273.     return $nextid;
  274.   }
  275.  
  276.   /* public: return table metadata */
  277.   function metadata($table='',$full=false) {
  278.     $count = 0;
  279.     $id    = 0;
  280.     $res   = array();
  281.  
  282.     /*
  283.      * Due to compatibility problems with Table we changed the behavior
  284.      * of metadata();
  285.      * depending on $full, metadata returns the following values:
  286.      *
  287.      * - full is false (default):
  288.      * $result[]:
  289.      *   [0]["table"]  table name
  290.      *   [0]["name"]   field name
  291.      *   [0]["type"]   field type
  292.      *   [0]["len"]    field length
  293.      *   [0]["flags"]  field flags
  294.      *
  295.      * - full is true
  296.      * $result[]:
  297.      *   ["num_fields"] number of metadata records
  298.      *   [0]["table"]  table name
  299.      *   [0]["name"]   field name
  300.      *   [0]["type"]   field type
  301.      *   [0]["len"]    field length
  302.      *   [0]["flags"]  field flags
  303.      *   ["meta"][field name]  index of field named "field name"
  304.      *   The last one is used, if you have a field name, but no index.
  305.      *   Test:  if (isset($result['meta']['myfield'])) { ...
  306.      */
  307.  
  308.     // if no $table specified, assume that we are working with a query
  309.     // result
  310.     if ($table) {
  311.       $this->connect();
  312.       $id = @mysql_list_fields($this->Database, $table);
  313.       if (!$id)
  314.         $this->halt("Metadata query failed.");
  315.     } else {
  316.       $id = $this->Query_ID;
  317.       if (!$id)
  318.         $this->halt("No query specified.");
  319.     }
  320.  
  321.     $count = @mysql_num_fields($id);
  322.  
  323.     // made this IF due to performance (one if is faster than $count if's)
  324.     if (!$full) {
  325.       for ($i=0; $i<$count; $i++) {
  326.         $res[$i]["table"] = @mysql_field_table ($id, $i);
  327.         $res[$i]["name"]  = @mysql_field_name  ($id, $i);
  328.         $res[$i]["type"]  = @mysql_field_type  ($id, $i);
  329.         $res[$i]["len"]   = @mysql_field_len   ($id, $i);
  330.         $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  331.       }
  332.     } else { // full
  333.       $res["num_fields"]= $count;
  334.  
  335.       for ($i=0; $i<$count; $i++) {
  336.         $res[$i]["table"] = @mysql_field_table ($id, $i);
  337.         $res[$i]["name"]  = @mysql_field_name  ($id, $i);
  338.         $res[$i]["type"]  = @mysql_field_type  ($id, $i);
  339.         $res[$i]["len"]   = @mysql_field_len   ($id, $i);
  340.         $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  341.         $res["meta"][$res[$i]["name"]] = $i;
  342.       }
  343.     }
  344.  
  345.     // free the result only if we were called on a table
  346.     if ($table) @mysql_free_result($id);
  347.     return $res;
  348.   }
  349.  
  350.   /* private: error handling */
  351.   function halt($msg) {
  352.     $this->Error = @mysql_error($this->Link_ID);
  353.     $this->Errno = @mysql_errno($this->Link_ID);
  354.     if ($this->Halt_On_Error == "no")
  355.       return;
  356.  
  357.     $this->haltmsg($msg);
  358.  
  359.     if ($this->Halt_On_Error != "report")
  360.       die("Session halted.");
  361.   }
  362.  
  363.   function haltmsg($msg) {
  364.     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  365.     printf("<b>MySQL Error</b>: %s (%s)<br>\n",
  366.       $this->Errno,
  367.       $this->Error);
  368.   }
  369.  
  370.   function table_names() {
  371.     $this->query("SHOW TABLES");
  372.     $i=0;
  373.     while ($info=mysql_fetch_row($this->Query_ID))
  374.      {
  375.       $return[$i]["table_name"]= $info[0];
  376.       $return[$i]["tablespace_name"]=$this->Database;
  377.       $return[$i]["database"]=$this->Database;
  378.       $i++;
  379.      }
  380.    return $return;
  381.   }
  382. }
  383. ?>
  384.