cox             Wed Mar 28 15:52:21 2001 EDT

  Modified files:              
    /php4/pear  DB.php 
    /php4/pear/DB       ifx.php mysql.php pgsql.php 
  Log:
  pgsql.php
  * better handling of $rownum param in fetchInto()
  * changed property numrows[] to num_rows[]
  
  DB.php
  * $rowmun in fetch* now defaults to NULL
  
  mysql.php
  * added default $fetchmode to ordered
  * removed extra checks in connect()
  * fetchrow() now uses fetchInto()
  * added the "fetch absolute row numbers" feature to fetchInto()
  (not tested)
  
  ifx.php
  * better handling of $rownum param in fetchInto()
  
  
  
Index: php4/pear/DB.php
diff -u php4/pear/DB.php:1.54 php4/pear/DB.php:1.55
--- php4/pear/DB.php:1.54       Mon Mar 26 15:31:49 2001
+++ php4/pear/DB.php    Wed Mar 28 15:52:21 2001
@@ -17,7 +17,7 @@
 // |          Tomas V.V.Cox <[EMAIL PROTECTED]>                             |
 // +----------------------------------------------------------------------+
 //
-// $Id: DB.php,v 1.54 2001/03/26 23:31:49 cox Exp $
+// $Id: DB.php,v 1.55 2001/03/28 23:52:21 cox Exp $
 //
 // Database independent query interface.
 //
@@ -585,7 +585,7 @@
      *
      * @return  array   a row of data, or false on error
      */
-    function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0)
+    function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
     {
         $res = $this->fetchInto ($arr, $fetchmode, $rownum);
         if ($res !== DB_OK) {
@@ -603,12 +603,12 @@
      *
      * @return  int     error code
      */
-    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0)
+    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
     {
         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
             $fetchmode = $this->dbh->fetchmode;
         }
-        return $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum=0);
+        return $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
     }
 
     /**
Index: php4/pear/DB/ifx.php
diff -u php4/pear/DB/ifx.php:1.4 php4/pear/DB/ifx.php:1.5
--- php4/pear/DB/ifx.php:1.4    Mon Mar 26 16:57:24 2001
+++ php4/pear/DB/ifx.php        Wed Mar 28 15:52:21 2001
@@ -30,8 +30,6 @@
 class DB_ifx extends DB_common
 {
     var $connection;
-    var $numrows;
-    var $row;
     var $affected = 0;
     var $dsn = array();
     var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
@@ -145,7 +143,7 @@
      *
      * @return  array   a row of data, or false on error
      */
-    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0)
+    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
     {
         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
             $fetchmode = $this->fetchmode;
@@ -168,9 +166,12 @@
      * @return int an array on success, a DB error code on failure, NULL
      *             if there is no more data
      */
-    function fetchInto($res, &$row, $fetchmode, $rownum=0)
+    function fetchInto($result, &$row, $fetchmode, $rownum=null)
     {
-        $rownum = ($rownum > 0) ? $rownum : null;
+        if (($rownum !== null) && ($rownum <= 0)) {
+            return $this->raiseError();
+        }
+        // if $rownum is null, fetch row will return the next row
         if (!$row = @ifx_fetch_row($result, $rownum)) {
             return NULL;
         }
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.50 php4/pear/DB/mysql.php:1.51
--- php4/pear/DB/mysql.php:1.50 Fri Mar 23 23:00:45 2001
+++ php4/pear/DB/mysql.php      Wed Mar 28 15:52:21 2001
@@ -25,7 +25,7 @@
 // XXX legend:
 //
 // XXX ERRORMSG: The error message from the mysql function should
-//                              be registered here.
+//               be registered here.
 //
 
 require_once "DB/common.php";
@@ -38,6 +38,8 @@
     var $phptype, $dbsyntax;
     var $prepare_tokens = array();
     var $prepare_types = array();
+    var $num_rows = array();
+    var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
 
     // }}}
     // {{{ constructor
@@ -89,28 +91,17 @@
      * @access public
      * @return int DB_OK on success, a DB error on failure
      */
-    
-    function connect($dsn, $persistent = false)
+
+    function connect($dsninfo, $persistent = false)
     {
-        if (is_array($dsn)) {
-            $dsninfo = &$dsn;
-        } else {
-            $dsninfo = DB::parseDSN($dsn);
-        }
-        
-        if (!$dsninfo || !$dsninfo["phptype"]) {
-            return $this->raiseError(); // XXX ERRORMSG
-        }
-       
         $this->dsn = $dsninfo;
-
         $dbhost = $dsninfo["hostspec"] ? $dsninfo["hostspec"] : "localhost";
         $user = $dsninfo["username"];
         $pw = $dsninfo["password"];
-        
+
         DB::assertExtension("mysql");
         $connect_function = $persistent ? "mysql_pconnect" : "mysql_connect";
-        
+
         if ($dbhost && $user && $pw) {
             $conn = @$connect_function($dbhost, $user, $pw);
         } elseif ($dbhost && $user) {
@@ -120,17 +111,17 @@
         } else {
             $conn = false;
         }
-        
+
         if ($conn == false) {
-            return $this->raiseError(); // XXX ERRORMSG
+            return $this->raiseError(DB_ERROR_CONNECT_FAILED);
         }
-        
+
         if ($dsninfo["database"]) {
             if (!mysql_select_db($dsninfo["database"], $conn)) {
                 return $this->raiseError(); // XXX ERRORMSG
             }
         }
-        
+
         $this->connection = $conn;
         return DB_OK;
     }
@@ -175,46 +166,38 @@
         }
         // Determine which queries that should return data, and which
         // should return an error code only.
-        return DB::isManip($query) ? DB_OK : $result;
+        if (DB::isManip($query)) {
+            return DB_OK;
+        }
+        $numrows = $this->numrows($result);
+        if (is_object($numrows)) {
+            return $numrows;
+        }
+        $this->num_rows[$result] = $numrows;
+        return $result;
     }
 
     // }}}
     // {{{ fetchRow()
 
     /**
-     * Fetch a row and return as array.
-     *
+     * Fetch and return a row of data (it uses fetchInto for that)
      * @param $result MySQL result identifier
-     * @param $fetchmode how the resulting array should be indexed
-     *
-     * @access public
+     * @param   $fetchmode  format of fetched row array
+     * @param   $rownum     the absolute row number to fetch
      *
-     * @return mixed an array on success, a DB error on failure, NULL
-     *               if there is no more data
+     * @return  array   a row of data, or false on error
      */
-    function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT)
+    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
     {
         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
             $fetchmode = $this->fetchmode;
         }
-
-        if ($fetchmode & DB_FETCHMODE_ASSOC) {
-            $row = mysql_fetch_array($result, MYSQL_ASSOC);
-        } else {
-            $row = mysql_fetch_row($result);
+        $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum);
+        if ($res !== DB_OK) {
+            return $res;
         }
-       
-        if (!$row) {
-            $errno = mysql_errno($this->connection);
-
-            if (!$errno) {
-                return null;
-            }
-           
-            return $this->mysqlRaiseError($errno);
-        }
-       
-        return $row;
+        return $arr;
     }
 
     // }}}
@@ -226,33 +209,32 @@
      * @param $result MySQL result identifier
      * @param $arr (reference) array where data from the row is stored
      * @param $fetchmode how the array data should be indexed
-     *
+     * @param   $rownum the row number to fetch
      * @access public
      *
      * @return int DB_OK on success, a DB error on failure
      */
-    function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT)
+    function fetchInto($result, &$arr, $fetchmode, $rownum=null)
     {
-        if ($fetchmode == DB_FETCHMODE_DEFAULT) {
-            $fetchmode = $this->fetchmode;
+        if ($rownum !== null) {
+            if (($rownum > 0) && ($rownum <= $this->num_rows[$result])) {
+                mysql_data_seek ($result, $rownum);
+            } else {
+                return null;
+            }
         }
-
         if ($fetchmode & DB_FETCHMODE_ASSOC) {
             $arr = mysql_fetch_array($result, MYSQL_ASSOC);
         } else {
             $arr = mysql_fetch_row($result);
         }
-
         if (!$arr) {
             $errno = mysql_errno($this->connection);
-
             if (!$errno) {
                 return NULL;
             }
-
             return $this->mysqlRaiseError($errno);
         }
-
         return DB_OK;
     }
 
@@ -281,7 +263,7 @@
         unset($this->prepare_tokens[$result]);
         unset($this->prepare_types[$result]);
 
-        return true; 
+        return true;
     }
 
     // }}}
@@ -325,7 +307,6 @@
         if ($rows === null) {
             return $this->mysqlRaiseError();
         }
-
         return $rows;
     }
 
@@ -376,7 +357,7 @@
      * @access public
      *
      * @param $seq_name the name of the sequence
-     * 
+     *
      * @param $ondemand whether to create the sequence table on demand
      * (default is true)
      *
@@ -404,7 +385,7 @@
         }
         return mysql_insert_id($this->connection);
     }
-               
+
     // }}}
     // {{{ createSequence()
 
@@ -454,7 +435,7 @@
 
     // }}}
     // {{{ tableInfo()
-    
+
     function tableInfo($result, $mode = null) {
         $count = 0;
         $id    = 0;
Index: php4/pear/DB/pgsql.php
diff -u php4/pear/DB/pgsql.php:1.33 php4/pear/DB/pgsql.php:1.34
--- php4/pear/DB/pgsql.php:1.33 Mon Mar 26 16:57:24 2001
+++ php4/pear/DB/pgsql.php      Wed Mar 28 15:52:21 2001
@@ -41,7 +41,7 @@
     var $transaction_opcount = 0;
     var $dsn = array();
     var $row = array();
-    var $numrows = array();
+    var $num_rows = array();
     var $affected = 0;
     var $autocommit = true;
     var $fetchmode = DB_FETCHMODE_ORDERED;
@@ -169,7 +169,7 @@
             if (is_object($numrows)) {
                 return $numrows;
             }
-            $this->numrows[$result] = $numrows;
+            $this->num_rows[$result] = $numrows;
             $this->affected = 0;
             return $result;
         } else {
@@ -224,7 +224,7 @@
      *
      * @return  array   a row of data, or false on error
      */
-    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0)
+    function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
     {
         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
             $fetchmode = $this->fetchmode;
@@ -248,11 +248,11 @@
      *
      * @return int DB_OK on success, a DB error code on failure
      */
-    function fetchInto($result, &$row, $fetchmode, $rownum=0)
+    function fetchInto($result, &$row, $fetchmode, $rownum=null)
     {
-        $rownum = ($rownum > 0) ? $rownum : $this->row[$result];
-        if ($rownum >= $this->numrows[$result]) {
-            return NULL;
+        $rownum = ($rownum !== null) ? $rownum : $this->row[$result];
+        if ($rownum > $this->num_rows[$result]) {
+            return null;
         }
         if ($fetchmode & DB_FETCHMODE_ASSOC) {
             $row = @pg_fetch_array($result, $rownum, PGSQL_ASSOC);
@@ -260,10 +260,11 @@
             $row = @pg_fetch_row($result, $rownum);
         }
         if (!$row) {
-            $err = $this->pgsqlRaiseError();
-            if ($err) {
-                return $err;
+            $err = pg_errormessage($this->connection);
+            if (!$err) {
+                return null;
             }
+            return $this->pgsqlRaiseError();
         }
         $this->row[$result] = ++$rownum;
         return DB_OK;
@@ -290,7 +291,7 @@
         unset($this->prepare_tokens[$result]);
         unset($this->prepare_types[$result]);
         unset($this->row[$result]);
-        unset($this->numrows[$result]);
+        unset($this->num_rows[$result]);
         $this->affected = 0;
         return true;
     }

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to