[phplib] database wrapper PHPLIB to PEAR From: mailling <email protected>
Date: 07/29/01

I realized a wrapper from PHPLIB to PEAR. That means it is possible, using
the same application that before (phplib database layer) to work with PEAR
driver. - in main cases; it worked for me at least
That migth interests people, for example to test PEAR driver, or get a
smooth moving....

<?php
/**
  * Database Management for PHP
  * Emulator of PHPLib, Port to Pear
  *
  * based of the mysql driver of PHPLIB
  * * Copyright (c) 1998-2000 NetUSE AG
  * Boris Erdmann, Kristian Koehntopp
  *
  * Copyright (c) 2001 Stephane Huther shuther <email protected>
  * LGPL license
  *
  * $Id: db_common.inc,v 1.1 2001/07/15 22:57:36 shuther Exp $
  *
  *  <email protected> the seek method is not implemented
  * improve the error handling $Error is never filled
  *
  */

/** Must be changed, to the database that you want to use*/

/** specify where your PEAR directory is, you can let it like that if PEAR
is in your include path in php.ini*/
        define('PEAR_DIRECTORY','');
        require_once PEAR_DIRECTORY . 'DB.php';

class DB_Sql {

/** PEAR driver: mysql, ....*/
   var $type = "mysql";

   /* public: connection parameters */
   var $Host = "";
   var $Database = "";
   var $User = "";
   var $Password = "";

   /* public: configuration parameters */
   var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result()
   var $Debug = 0; ## Set to 1 for debugging messages.
   var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore
errors quietly), "report" (ignore errror, but spit a warning)
   var $Seq_Table = "db_sequence";

   /* public: result array and current row number */
   var $Record = array();
   var $Row;

   /* public: current error number and error text */
   var $Errno = 0;
   var $Error = "Not filed !";

   /* public: this is an api revision, not a CVS revision.
   You must precize here the type!!!*/

   var $revision = "1.2"; //We emulate mysql 1.2 API

   /* private: link and query handles */
   var $Link_ID = 0;
   var $Query_ID = 0;

     /**
      * Handle for the current database connection.
      *  <email protected> resource $db
      */
     var $db;

     /**
      * Boolean indicating whether or not we're connected to the SQL server.
      *  <email protected> boolean $connected
      */
     var $connected = false;

   /* public: constructor */
   function DB_Sql($query = '') {

             /* Connect to the SQL server using the supplied parameters. */
                return $this->query($query);

             }

   /* public: some trivial reporting */
   function link_id() {
     return $this->Link_ID;
   }

   function query_id() {
     return $this->Query_ID;
   }

   /* public: connection management */
   function connect($Database = "", $Host = "", $User = "", $Password = "") {

        if ($this->connected)
                return $this->Link_ID;

     /* Handle defaults */
     if ("" == $Database)
       $Database = $this->Database;
     if ("" == $Host)
       $Host = $this->Host;
     if ("" == $User)
       $User = $this->User;
     if ("" == $Password)
       $Password = $this->Password;

        $this->params=array('phptype' => $this->type, 'hostspec' => $Host,
'username' => $User, 'password'=> $Password, 'database' => $Database);

             $this->db = &DB::connect($this->params, true);
// var_dumpr($this->params);
// var_dumpr($this->db);

             if (DB::isError($this->db) || DB::isWarning($this->db)) {
                        $this->halt("pconnect($Host, $User, \$Password) failed.");
                        return 0;
                }

     $this->Link_ID = $this->db->connection;
     $this->connected=true;
     return $this->Link_ID;
   }

   /* public: discard the query result */
   function free() {
          $this->Query_ID=false;
   }

   /* public: perform a query */
   function query($Query_String) {
     /* No empty queries, please, since PHP4 chokes on them. */
     if ($Query_String == "")
       /* The empty query string is passed on from the constructor,
        * when calling the class without a query, e.g. in situations
        * like these: '$db = new DB_Sql_Subclass;'
        */
       return 0;

     if (!$this->connect()) {
       return 0; /* we already complained in connect() about that. */
     };

     # New query, discard previous result.
     if ($this->Query_ID) {
       $this->free();
     }

     if ($this->Debug)
       printf("Debug: query = %s<br>\n", $Query_String);

     $this->Query_ID = $this->db->simpleQuery($Query_String);
     $this->Row = 0;
     $this->Errno = $this->db->errorNative();
     if (!$this->Query_ID) {
       $this->halt("Invalid SQL: ".$Query_String);
     }

     # Will return nada if it fails. That's fine.
     return $this->Query_ID;
   }

   /* public: walk result set */
   function next_record() {
     if (!$this->Query_ID) {
       $this->halt("next_record called with no query pending.");
       return 0;
     }

     $this->Record = $this->db->fetchRow($this->Query_ID, DB_FETCHMODE_ASSOC);;
     $this->Row += 1;
     $this->Errno = $this->db->errorNative();

     $stat = is_array($this->Record);
     if (!$stat && $this->Auto_Free) {
       $this->free();
     }
     return $stat;
   }

   /* public: position in result set
   NOT SUPPORTED BY PEAR!, but possible to use $result_object->result to
do it*/
   function seek($pos = 0) {
          return false;
   }

   /* public: table locking */
   function lock($table, $mode="write") {
     $this->connect();

     $query="lock tables ";
     if (is_array($table)) {
       while (list($key,$value)=each($table)) {
         if ($key=="read" && $key!=0) {
           $query.="$value read, ";
         } else {
           $query.="$value $mode, ";
         }
       }
       $query=substr($query,0,-2);
     } else {
       $query.="$table $mode";
     }
     $res = $this->query($query);
     if (!$res) {
       $this->halt("lock($table, $mode) failed.");
       return 0;
     }
     return $res;
   }

   function unlock() {
     $this->connect();

     $res = $this->query("unlock tables");
     if (!$res) {
       $this->halt("unlock() failed.");
       return 0;
     }
     return $res;
   }

   /* public: evaluate the result (size, width) */
   function affected_rows() {
     return $this->db->affectedRows();
   }

   function num_rows() {
     return $this->db->numRows();
   }

   function num_fields() {
     return $this->db->numCols();
   }

   /* public: shorthand notation */
   function nf() {
     return $this->num_rows();
   }

   function np() {
     print $this->num_rows();
   }

   function f($Name) {
     return $this->Record[$Name];
   }

   function p($Name) {
     print $this->Record[$Name];
   }

   /* public: return table metadata */
   function metadata($table='',$full=false) {
          if ($full)
                  $mode=DB_TABLEINFO_ORDER;
          else
                  $mode=null;

        return $this->db->tableInfo($table ,$mode);
   }

   /* private: error handling */
   function halt($msg) {
     $this->Errno = $this->db->errorNative();
     if ($this->Halt_On_Error == "no")
       return;

     $this->haltmsg($msg);

     if ($this->Halt_On_Error != "report")
       die("Session halted.");
   }

   function haltmsg($msg) {
     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
     printf("<b>SQL Error</b>: %s (%s)<br>\n",
       $this->Errno,
       $this->Error);
   }

/** Not supported by PEAR*/
   function table_names() {
          return false;
}
}

?>

-- 
Abbestellen mit Mail an:   phplib-unsubscribe <email protected>
Kommandoliste mit Mail an: phplib-help <email protected>